@@ -93,8 +93,9 @@ spi_response_t *__copy_spi_data(MemoryContext ctx, int ret, int n)
9393int i ,j ;
9494Datum dat ;
9595bool is_null ;
96+ MemoryContext old ;
9697
97-
98+ old = MemoryContextSwitchTo ( ctx );
9899r = MemoryContextAlloc (ctx ,sizeof (spi_response_t ));
99100if (!r )return NULL ;
100101r -> retval = ret ;
@@ -147,6 +148,7 @@ spi_response_t *__copy_spi_data(MemoryContext ctx, int ret, int n)
147148}
148149}
149150}
151+ MemoryContextSwitchTo (old );
150152
151153return r ;
152154}
@@ -495,32 +497,32 @@ spi_response_t *execute_spi_sql_with_args(MemoryContext ctx, const char *sql, in
495497char other [100 ];
496498ResourceOwner oldowner = CurrentResourceOwner ;
497499spi_response_t * rv = NULL ;
498- MemoryContext old ;
500+ MemoryContext old = CurrentMemoryContext ;
499501
500502if (!ctx )ctx = SchedulerWorkerContext ;
501503
504+
505+
502506SetCurrentStatementStartTimestamp ();
503507BeginInternalSubTransaction (NULL );
504- old = MemoryContextSwitchTo (ctx );
508+ MemoryContextSwitchTo (old );
505509
506510PG_TRY ();
507511{
508512ret = SPI_execute_with_args (sql ,n ,argtypes ,values ,nulls , false,0 );
509- MemoryContextSwitchTo (ctx );
510513rv = __copy_spi_data (ctx ,ret ,SPI_processed );
511514if (!rv )
512515{
513516elog (LOG ,"ESSWA: Cannot allocate memory while copy resp data" );
514517return NULL ;
515518}
516519ReleaseCurrentSubTransaction ();
517- MemoryContextSwitchTo (ctx );
520+ MemoryContextSwitchTo (old );
518521CurrentResourceOwner = oldowner ;
519522SPI_restore_connection ();
520523}
521524PG_CATCH ();
522525{
523- MemoryContextSwitchTo (ctx );
524526edata = CopyErrorData ();
525527if (edata -> message )
526528{
@@ -541,7 +543,7 @@ spi_response_t *execute_spi_sql_with_args(MemoryContext ctx, const char *sql, in
541543}
542544RollbackAndReleaseCurrentSubTransaction ();
543545CurrentResourceOwner = oldowner ;
544- MemoryContextSwitchTo (ctx );
546+ MemoryContextSwitchTo (old );
545547SPI_restore_connection ();
546548FreeErrorData (edata );
547549FlushErrorState ();
@@ -574,12 +576,10 @@ spi_response_t *execute_spi_sql_with_args(MemoryContext ctx, const char *sql, in
574576if (!rv )
575577{
576578elog (LOG ,"ESSWA: Cannot allocate memory while reporting pg error" );
577- MemoryContextSwitchTo (old );
578579return NULL ;
579580}
580581}
581582
582- MemoryContextSwitchTo (old );
583583return rv ;
584584}
585585
@@ -599,6 +599,7 @@ spi_response_t *execute_spi_params_prepared(MemoryContext ctx, const char *sql,
599599int i ;
600600ResourceOwner oldowner = CurrentResourceOwner ;
601601spi_response_t * rv ;
602+ MemoryContext old = CurrentMemoryContext ;
602603
603604if (!ctx )ctx = SchedulerWorkerContext ;
604605
@@ -613,7 +614,7 @@ spi_response_t *execute_spi_params_prepared(MemoryContext ctx, const char *sql,
613614
614615SetCurrentStatementStartTimestamp ();
615616BeginInternalSubTransaction (NULL );
616- switch_to_worker_context ( );
617+ MemoryContextSwitchTo ( old );
617618
618619PG_TRY ();
619620{
@@ -630,14 +631,12 @@ spi_response_t *execute_spi_params_prepared(MemoryContext ctx, const char *sql,
630631}
631632}
632633ReleaseCurrentSubTransaction ();
633- switch_to_worker_context ( );
634+ MemoryContextSwitchTo ( old );
634635CurrentResourceOwner = oldowner ;
635636SPI_restore_connection ();
636637}
637638PG_CATCH ();
638639{
639- switch_to_worker_context ();
640-
641640edata = CopyErrorData ();
642641if (edata -> message )
643642{
@@ -660,7 +659,7 @@ spi_response_t *execute_spi_params_prepared(MemoryContext ctx, const char *sql,
660659FlushErrorState ();
661660RollbackAndReleaseCurrentSubTransaction ();
662661CurrentResourceOwner = oldowner ;
663- switch_to_worker_context ( );
662+ MemoryContextSwitchTo ( old );
664663SPI_restore_connection ();
665664}
666665PG_END_TRY ();