@@ -162,10 +162,7 @@ int do_one_job(schd_executor_share_t *shared, schd_executor_status_t *status)
162162int i ;
163163job_t * job ;
164164spi_response_t * r ;
165- MemoryContext old ;
166- MemoryContext mem = init_mem_ctx ("executor" );
167-
168- old = MemoryContextSwitchTo (mem );
165+ MemoryContext old ,mem ;
169166
170167EE .n = 0 ;
171168EE .errors = NULL ;
@@ -178,6 +175,9 @@ int do_one_job(schd_executor_share_t *shared, schd_executor_status_t *status)
178175return 0 ;
179176}
180177
178+ mem = init_mem_ctx ("executor" );
179+ old = MemoryContextSwitchTo (mem );
180+
181181* status = shared -> status = SchdExecutorWork ;
182182shared -> message [0 ]= 0 ;
183183
@@ -191,6 +191,9 @@ int do_one_job(schd_executor_share_t *shared, schd_executor_status_t *status)
191191shared -> worker_exit = true;
192192* status = shared -> status = SchdExecutorError ;
193193
194+ MemoryContextSwitchTo (old );
195+ MemoryContextDelete (mem );
196+
194197return -1 ;
195198}
196199current_job_id = job -> cron_id ;
@@ -211,6 +214,8 @@ int do_one_job(schd_executor_share_t *shared, schd_executor_status_t *status)
211214}
212215* status = shared -> worker_exit = true;
213216shared -> status = SchdExecutorError ;
217+ MemoryContextSwitchTo (old );
218+ MemoryContextDelete (mem );
214219return -2 ;
215220}
216221
@@ -257,7 +262,7 @@ int do_one_job(schd_executor_share_t *shared, schd_executor_status_t *status)
257262destroy_spi_data (r );
258263ABORT_SPI_SNAP ();
259264SetConfigOption ("schedule.transaction_state" ,"failure" ,PGC_INTERNAL ,PGC_S_SESSION );
260- executor_onrollback (job ,& EE );
265+ executor_onrollback (mem , job ,& EE );
261266
262267break ;
263268}
@@ -467,7 +472,7 @@ TimestampTz get_next_excution_time(char *sql, executor_error_t *ee)
467472return ts ;
468473}
469474
470- int executor_onrollback (job_t * job ,executor_error_t * ee )
475+ int executor_onrollback (MemoryContext mem , job_t * job ,executor_error_t * ee )
471476{
472477int rv ;
473478spi_response_t * r ;
@@ -476,7 +481,7 @@ int executor_onrollback(job_t *job, executor_error_t *ee)
476481pgstat_report_activity (STATE_RUNNING ,"execure onrollback" );
477482
478483START_SPI_SNAP ();
479- r = execute_spi (CurrentMemoryContext ,job -> onrollback );
484+ r = execute_spi (mem ,job -> onrollback );
480485if (r -> retval < 0 )
481486{
482487if (r -> error )