Search code examples
postgresqldebuggingplsqlpsql

debugging psql - session procedure


I am trying to modify an existing sessions procedure to add cycle count.The error I am getting is SQL Error [42601]: ERROR: syntax error at or near "END" Position: 3587

--call transactions_packs.tep_session()

CREATE OR REPLACE PROCEDURE transactions_packs.tep_session()
 LANGUAGE plpgsql
AS $procedure$
DECLARE
    session "transactions_packs"."simple_sessions";
    "session_toSearch" TEXT;
    "end_timestamp" TIMESTAMP WITH TIME ZONE;
    "energy" NUMERIC;
    "charge" NUMERIC;
    "duration" NUMERIC;
    "cycle_count" numeric;
    "f" record ;
BEGIN
    cycle_count = '0';
    -- go to statement fore session reset 
    FOR session IN SELECT * FROM "transactions_packs"."simple_sessions" WHERE "sessionDuration" IS NULL
    LOOP
        BEGIN
            IF session."sessionType" = 0 THEN
                "session_toSearch" := 'Charging';
            ELSIF session."sessionType" = 1 THEN
                "session_toSearch" := 'Discharging';
            END IF;
            
            -- Session_count:Start
            EXECUTE FORMAT('
            FOR f IN select (current' || '%s' || '), "timestamp" 
            FROM "transactions_packs"."basic_measurements_packs" a order by a."timestamp" desc
            LOOP 
            BEGIN
            IF  AVG((current' || '%s' || '))  
            OVER (ORDER BY "f"."timestamp" ROWS BETWEEN 3 PRECEDING AND CURRENT ROW) > 0.01
            then cycle_count  = cycle_count  + 1;   
            END IF;
            END
            END LOOP;',"session_toSearch","session_toSearch")
            -- get value from If and else statement to fetch records from charging and discharging col
            --Session_count :End    
    END ;       
    END LOOP;
end;
$procedure$
;

where -

schema is transactions_packs tables are -

  1. simple_sessions
  2. basic_measurements_packs

Please let me know if there is any part of query is which cannot be understood.


Solution

  • The variable "hell" sounds like useless here, try something like this :

    create or replace procedure "public"."extract"(arg json)
    language plpgsql as $$
    begin
        raise notice 'test:%', arg->'global'->>'packetType';
        raise notice 'test1:%', arg-> 'transactional'->>'A';
    -- freeze the input
    end ;
    $$;