Giter Club home page Giter Club logo

Comments (4)

laurenz avatar laurenz commented on September 17, 2024

Since you say that you are using a procedure, and the problem happened at the sixth execution of the query, I strongly suspect that the problem is that PostgreSQL starts using a generic plan. If that is the cause of the problem, you can fix the procedure by setting

ALTER PROCEDURE name_of_procedure() SET plan_cache_mode = force_custom_plan;

Then start a new database session and try again.

If that does not fix the problem, please show me the definition of the procedure.

from oracle_fdw.

SteveDirschelTR avatar SteveDirschelTR commented on September 17, 2024

Hi. I altered the procedure but now when I execute it an error is thrown after trying to process one day:

shudslong01a=> alter procedure uds_long.copy_co_session() set plan_cache_mode = force_custom_plan;
ALTER PROCEDURE
shudslong01a=>  call uds_long.copy_co_session();
ERROR:  invalid transaction termination
CONTEXT:  PL/pgSQL function uds_long.copy_co_session() line 91 at COMMIT

Here is that procedure:

create or replace procedure uds_long.copy_co_session()
 LANGUAGE plpgsql
 AS $$
 DECLARE

   v_run_date		TIMESTAMP;
   v_exit		BOOLEAN := FALSE;
   v_start_time		TIMESTAMP;
   v_end_time		TIMESTAMP;
   v_rows_processed	INTEGER;

   v_state                     TEXT;
   v_msg                       TEXT;
   v_detail                    TEXT;
   v_hint                      TEXT;
   v_context                   TEXT;

BEGIN


  WHILE v_exit = FALSE
  LOOP

     SELECT last_run_date
       INTO v_run_date
       FROM uds_long.copy_tracker
      WHERE table_name = 'co_session';

     /* Stop copying when caught up to current day - 2 */

     IF date_trunc('day', v_run_date) = date_trunc('day', now()) OR date_trunc('day', v_run_date) = date_trunc('day', now() - interval '1 day') THEN

        v_exit = TRUE;

     ELSE

        /* copy 1 day of data.  Putting inside a BEGIN/END block to allow committing between days if there are multiple days to copy */
        /* if there are multiple days to copy and 1 day fails the procedure will stop processing. */

        BEGIN
           v_start_time := clock_timestamp();


            INSERT into uds_long.co_session
   		   (SESSION_ID, ONE_PASS_USER_NAME, FIRST_NAME, LAST_NAME, EMAIL_ADDRESS, EXPIRES_TIME,SITE,
		   CREATED_TIME, SESSION_EXPIRES_TIME, PRISM_GUID, SATOKEN, SESSION_ENDED_DATE, SESSION_STATUS,
		   LAST_UPDATED_TIME, USER_CLASSIFICATION, ORPHAN_EXPIRES_TIME, EXPIRES_TIME_REASON,
		   SESSION_ENDED_REASON, PRODUCT_NAME, EMULATEE_PRISM_GUID, EMULATEE_PRISM_REN_AUTH_TOKEN,
		   PRISM_RENEWED_AUTH_TOKEN, TIER, SESSION_BASED_PREFS, SESSION_SOURCE,SERVICE_TYPE,
		   PMD_DATA_VERSION, CREATED_TIME_AND_TZ, PAYMENT_TYPE, PRISM_REGISTRATION_KEY, CREATED_TIME_LONG,
		   ONE_PASS_PRODUCT, SESSION_STORAGE_PROFILE, PRODUCT_VIEW, IP_ADDRESS,USER_CATEGORY,
		   BILLING_METHOD, ROAMING_STATUS, ONE_PASS_USER_ID)
            SELECT SESSION_ID, ONE_PASS_USER_NAME, FIRST_NAME, LAST_NAME, EMAIL_ADDRESS, EXPIRES_TIME,SITE,
		   CREATED_TIME, SESSION_EXPIRES_TIME, PRISM_GUID, SATOKEN, SESSION_ENDED_DATE, SESSION_STATUS,
		   LAST_UPDATED_TIME, USER_CLASSIFICATION, ORPHAN_EXPIRES_TIME, EXPIRES_TIME_REASON,
		   SESSION_ENDED_REASON, PRODUCT_NAME, EMULATEE_PRISM_GUID, EMULATEE_PRISM_REN_AUTH_TOKEN,
		   PRISM_RENEWED_AUTH_TOKEN, TIER, SESSION_BASED_PREFS, SESSION_SOURCE,SERVICE_TYPE,
		   PMD_DATA_VERSION, CREATED_TIME_AND_TZ, PAYMENT_TYPE, PRISM_REGISTRATION_KEY, CREATED_TIME_LONG,
		   ONE_PASS_PRODUCT, SESSION_STORAGE_PROFILE, PRODUCT_VIEW, IP_ADDRESS,USER_CATEGORY,
		   BILLING_METHOD, ROAMING_STATUS, ONE_PASS_USER_ID
              FROM uds_long_link.co_session
             WHERE created_time >= date_trunc('day', v_run_date)
               AND created_time < date_trunc('day', v_run_date + interval '1 day');


            GET DIAGNOSTICS v_rows_processed  = row_count;

            v_end_time := clock_timestamp();

            UPDATE uds_long.copy_tracker
               SET last_run_date = v_run_date + interval '1 day'
             WHERE table_name = 'co_session';

            INSERT into uds_long.copy_tracker_log
            VALUES ('co_session','SUCCESS',v_start_time, v_end_time, v_run_date, v_rows_processed);

            

        EXCEPTION WHEN OTHERS THEN

           v_exit = TRUE;

           get stacked diagnostics
             v_state   = returned_sqlstate,
             v_msg     = message_text,
             v_detail  = pg_exception_detail,
             v_hint    = pg_exception_hint,
             v_context = pg_exception_context;

           raise exception 'state: %, message: %, detail: %, hint: %, context: %', v_state, v_msg, v_detail, v_hint, v_context;
        END;

        COMMIT;

     END IF;

  END LOOP;

END $$;

from oracle_fdw.

laurenz avatar laurenz commented on September 17, 2024

I wasn't aware that setting a parameter on a procedure would be a problem with transaction control statements...

You will have to undo the change:

ALTER PROCEDURE uds_long.copy_co_session() RESET plan_cache_mode;

Then you have two alternatives:

  1. Run the following before you execute the INSERT statement:

    SET plan_cache_mode = force_custom_plan;

    You can do that directly in the procedure. Don't forget to RESET the parameter after the INSERT. or you won't get any generic plans at all.

  2. Run your INSERT as a dynamic statement:

    EXECUTE format(
               'INSERT into uds_long.co_session ...
                SELECT ...
                FROM uds_long_link.co_session
                WHERE created_time >= %L  AND created_time < %L',
               date_trunc('day', v_run_date),
               date_trunc('day', v_run_date + interval '1 day')
            );

from oracle_fdw.

SteveDirschelTR avatar SteveDirschelTR commented on September 17, 2024

I did the SET plan_cache_mode = force_custom_plan in the procedure before the insert and then did a reset on it after the commit and now the query is always pushing the CREATED_TIME filter into the query on the Oracle side. So that fixed the issue.

Thanks for your help on this!

from oracle_fdw.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.