PG-261: Fix regression tests on distribution packages.

The code added in pgss_store() to handle an assertion failure when
GetUserId() was being called introduced a problem with regression tests
in some builds, specifically our PG11 and PG12 distributions for Ubuntu.

The problem was detected when calling some json functions that would
trigger parallel workers, to solve the problem now we check if our hooks
are being called by parallel workers, in this case we can avoid doing
work, it also fixes the issue mentioned above as we won't call
GetUserId() anymore in an invalid context.
pull/121/head
Diego Fronza 2021-10-15 11:56:28 -03:00
parent 52ea543275
commit bf8c93b185
1 changed files with 18 additions and 8 deletions

View File

@ -14,7 +14,9 @@
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
#include "postgres.h" #include "postgres.h"
#include "access/parallel.h"
#include <regex.h> #include <regex.h>
#ifdef BENCHMARK #ifdef BENCHMARK
#include <time.h> /* clock() */ #include <time.h> /* clock() */
@ -367,6 +369,9 @@ pgss_post_parse_analyze(ParseState *pstate, Query *query, JumbleState *jstate)
if (!IsSystemInitialized()) if (!IsSystemInitialized())
return; return;
if (IsParallelWorker())
return;
/* /*
* Clear queryId for prepared statements related utility, as those will * Clear queryId for prepared statements related utility, as those will
* inherit from the underlying statement's one (except DEALLOCATE which is * inherit from the underlying statement's one (except DEALLOCATE which is
@ -423,6 +428,9 @@ pgss_post_parse_analyze(ParseState *pstate, Query *query)
if (!IsSystemInitialized()) if (!IsSystemInitialized())
return; return;
if (IsParallelWorker())
return;
/* /*
* Utility statements get queryId zero. We do this even in cases where * Utility statements get queryId zero. We do this even in cases where
* the statement contains an optimizable statement for which a queryId * the statement contains an optimizable statement for which a queryId
@ -485,6 +493,9 @@ pgss_ExecutorStart(QueryDesc *queryDesc, int eflags)
else else
standard_ExecutorStart(queryDesc, eflags); standard_ExecutorStart(queryDesc, eflags);
if (IsParallelWorker())
return;
/* /*
* If query has queryId zero, don't track it. This prevents double * If query has queryId zero, don't track it. This prevents double
* counting of optimizable statements that are directly contained in * counting of optimizable statements that are directly contained in
@ -655,7 +666,7 @@ pgss_ExecutorEnd(QueryDesc *queryDesc)
MemoryContextSwitchTo(mct); MemoryContextSwitchTo(mct);
} }
if (queryId != UINT64CONST(0) && queryDesc->totaltime) if (queryId != UINT64CONST(0) && queryDesc->totaltime && !IsParallelWorker())
{ {
/* /*
* Make sure stats accumulation is done. (Note: it's okay if several * Make sure stats accumulation is done. (Note: it's okay if several
@ -770,7 +781,7 @@ pgss_planner_hook(Query *parse, const char *query_string, int cursorOptions, Par
{ {
PlannedStmt *result; PlannedStmt *result;
if (PGSM_TRACK_PLANNING && query_string && parse->queryId != UINT64CONST(0)) if (PGSM_TRACK_PLANNING && query_string && parse->queryId != UINT64CONST(0) && !IsParallelWorker())
{ {
PlanInfo plan_info; PlanInfo plan_info;
instr_time start; instr_time start;
@ -942,7 +953,7 @@ static void pgss_ProcessUtility(PlannedStmt *pstmt, const char *queryString,
if (PGSM_TRACK_UTILITY && if (PGSM_TRACK_UTILITY &&
!IsA(parsetree, ExecuteStmt) && !IsA(parsetree, ExecuteStmt) &&
!IsA(parsetree, PrepareStmt) && !IsA(parsetree, PrepareStmt) &&
!IsA(parsetree, DeallocateStmt)) !IsA(parsetree, DeallocateStmt) && !IsParallelWorker())
{ {
instr_time start; instr_time start;
instr_time duration; instr_time duration;
@ -1453,7 +1464,6 @@ pgss_store(uint64 queryid,
uint64 bucketid; uint64 bucketid;
uint64 prev_bucket_id; uint64 prev_bucket_id;
uint64 userid; uint64 userid;
int con;
uint64 planid; uint64 planid;
uint64 appid; uint64 appid;
char comments[512] = ""; char comments[512] = "";
@ -1468,10 +1478,7 @@ pgss_store(uint64 queryid,
return; return;
Assert(query != NULL); Assert(query != NULL);
if (kind == PGSS_ERROR) userid = GetUserId();
GetUserIdAndSecContext((unsigned int *)&userid, &con);
else
userid = GetUserId();
application_name_len = pg_get_application_name(application_name); application_name_len = pg_get_application_name(application_name);
planid = plan_info ? plan_info->planid: 0; planid = plan_info ? plan_info->planid: 0;
@ -3255,6 +3262,9 @@ pgsm_emit_log_hook(ErrorData *edata)
if (!IsSystemInitialized() || edata == NULL) if (!IsSystemInitialized() || edata == NULL)
goto exit; goto exit;
if (IsParallelWorker())
return;
if ((edata->elevel == ERROR || edata->elevel == WARNING || edata->elevel == INFO || edata->elevel == DEBUG1)) if ((edata->elevel == ERROR || edata->elevel == WARNING || edata->elevel == INFO || edata->elevel == DEBUG1))
{ {
uint64 queryid = 0; uint64 queryid = 0;