!452 fix bugs when cancel autonomous transaction

Merge pull request !452 from scarbor_fair/bug_in_AT
This commit is contained in:
opengauss-bot 2020-12-14 11:48:09 +08:00 committed by Gitee
commit d1e536e89b
7 changed files with 20 additions and 17 deletions

View File

@ -590,7 +590,7 @@ void errfinish(int dummy, ...)
if (t_thrd.msqueue_cxt.is_changed) {
pq_stop_redirect_to_shm_mq();
}
if (t_thrd.autonomous_cxt.handle) {
if (t_thrd.autonomous_cxt.handle.slot >= 0) {
StopBackgroundWorker();
}
}

View File

@ -94,11 +94,6 @@ typedef struct BackgroundWorkerArray {
BackgroundWorkerSlot slot[FLEXIBLE_ARRAY_MEMBER];
} BackgroundWorkerArray;
struct BackgroundWorkerHandle {
int slot;
uint64 generation;
};
/*
* List of internal background worker entry points. We need this for
* reasons explained in LookupBackgroundWorkerFunction(), below.
@ -1172,7 +1167,7 @@ void TerminateBackgroundWorker(const BackgroundWorkerHandle *handle)
{
bool signal_postmaster = false;
Assert(handle->slot < g_instance.attr.attr_storage.max_background_workers);
Assert( handle->slot >= 0 && handle->slot < g_instance.attr.attr_storage.max_background_workers);
BackgroundWorkerSlot* slot = &t_thrd.bgworker_cxt.background_worker_data->slot[handle->slot];
/* Set terminate flag in shared memory, unless slot has been reused. */
@ -1191,9 +1186,11 @@ void TerminateBackgroundWorker(const BackgroundWorkerHandle *handle)
void StopBackgroundWorker()
{
TerminateBackgroundWorker(t_thrd.autonomous_cxt.handle);
(void)WaitForBackgroundWorkerShutdown(t_thrd.autonomous_cxt.handle);
t_thrd.autonomous_cxt.handle = NULL;
TerminateBackgroundWorker(&(t_thrd.autonomous_cxt.handle));
(void)WaitForBackgroundWorkerShutdown(&(t_thrd.autonomous_cxt.handle));
// reset handle of autonomous_cxt
t_thrd.autonomous_cxt.handle.slot = -1;
t_thrd.autonomous_cxt.handle.generation = 0;
}
/*

View File

@ -137,7 +137,7 @@ AutonomousSession *AutonomousSessionStart(void)
shm_toc_estimate_chunk(&e, guc_len);
shm_toc_estimate_keys(&e, AUTONOMOUS_NKEYS);
segsize = shm_toc_estimate(&e);
seg = (char *)palloc(sizeof(char) * segsize);
seg = (char *)MemoryContextAlloc(t_thrd.top_mem_cxt, sizeof(char) * segsize);
session->seg = seg;
@ -191,7 +191,8 @@ AutonomousSession *AutonomousSessionStart(void)
shm_mq_set_handle(session->command_qh, session->worker_handle);
shm_mq_set_handle(session->response_qh, session->worker_handle);
t_thrd.autonomous_cxt.handle = session->worker_handle;
t_thrd.autonomous_cxt.handle.slot = session->worker_handle->slot;
t_thrd.autonomous_cxt.handle.generation = session->worker_handle->generation;
bgwstatus = WaitForBackgroundWorkerStartup(session->worker_handle, &pid);
if (bgwstatus != BGWH_STARTED)
@ -246,7 +247,8 @@ void AutonomousSessionEnd(AutonomousSession *session)
pfree(session->worker_handle);
pfree(session->seg);
pfree(session);
t_thrd.autonomous_cxt.handle = NULL;
t_thrd.autonomous_cxt.handle.slot = -1;
t_thrd.autonomous_cxt.handle.generation = 0;
}
AutonomousResult *AutonomousSessionExecute(AutonomousSession *session, const char *sql)

View File

@ -7436,7 +7436,7 @@ int PostgresMain(int argc, char* argv[], const char* dbname, const char* usernam
if (t_thrd.msqueue_cxt.is_changed) {
pq_stop_redirect_to_shm_mq();
}
if (t_thrd.autonomous_cxt.handle) {
if (t_thrd.autonomous_cxt.handle.slot >= 0) {
StopBackgroundWorker();
}
gstrace_tryblock_exit(true, oldTryCounter);

View File

@ -1390,7 +1390,8 @@ static void knl_t_heartbeat_init(knl_t_heartbeat_context* heartbeat_cxt)
static void knl_t_autonomous_init(knl_t_autonomous_context* autonomous_cxt)
{
autonomous_cxt->isnested = false;
autonomous_cxt->handle = NULL;
autonomous_cxt->handle.slot = -1;
autonomous_cxt->handle.generation = 0;
autonomous_cxt->sqlstmt = NULL;
autonomous_cxt->check_client_encoding_hook = NULL;
}

View File

@ -2690,7 +2690,7 @@ typedef void (*check_client_encoding_hook_type)(void);
typedef struct knl_t_autonomous_context {
PLpgSQL_expr* sqlstmt;
bool isnested;
BackgroundWorkerHandle* handle;
BackgroundWorkerHandle handle;
check_client_encoding_hook_type check_client_encoding_hook;
} knl_t_autonomous_context;

View File

@ -106,7 +106,10 @@ typedef enum BgwHandleStatus {
BGWH_POSTMASTER_DIED /* postmaster died; worker status unclear */
} BgwHandleStatus;
struct BackgroundWorkerHandle;
struct BackgroundWorkerHandle {
int slot;
uint64 generation;
};
typedef struct BackgroundWorkerHandle BackgroundWorkerHandle;
struct BackgroundWorkerArray;