Skip to content

Commit f90e80b

Browse files
committed
Reproduce debug_query_string==NULL on parallel workers.
Certain background workers initiate parallel queries while debug_query_string==NULL, at which point they attempted strlen(NULL) and died to SIGSEGV. Older debug_query_string observers allow NULL, so do likewise in these newer ones. Back-patch to v11, where commit 7de4a1b introduced the first of these. Discussion: https://postgr.es/m/20201014022636.GA1962668@rfd.leadboat.com
1 parent 970c050 commit f90e80b

File tree

2 files changed

+36
-17
lines changed

2 files changed

+36
-17
lines changed

src/backend/access/heap/vacuumlazy.c

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3237,7 +3237,6 @@ begin_parallel_vacuum(Oid relid, Relation *Irel, LVRelStats *vacrelstats,
32373237
WalUsage *wal_usage;
32383238
bool *can_parallel_vacuum;
32393239
long maxtuples;
3240-
char *sharedquery;
32413240
Size est_shared;
32423241
Size est_deadtuples;
32433242
int nindexes_mwm = 0;
@@ -3334,9 +3333,14 @@ begin_parallel_vacuum(Oid relid, Relation *Irel, LVRelStats *vacrelstats,
33343333
shm_toc_estimate_keys(&pcxt->estimator, 1);
33353334

33363335
/* Finally, estimate PARALLEL_VACUUM_KEY_QUERY_TEXT space */
3337-
querylen = strlen(debug_query_string);
3338-
shm_toc_estimate_chunk(&pcxt->estimator, querylen + 1);
3339-
shm_toc_estimate_keys(&pcxt->estimator, 1);
3336+
if (debug_query_string)
3337+
{
3338+
querylen = strlen(debug_query_string);
3339+
shm_toc_estimate_chunk(&pcxt->estimator, querylen + 1);
3340+
shm_toc_estimate_keys(&pcxt->estimator, 1);
3341+
}
3342+
else
3343+
querylen = 0; /* keep compiler quiet */
33403344

33413345
InitializeParallelDSM(pcxt);
33423346

@@ -3381,10 +3385,16 @@ begin_parallel_vacuum(Oid relid, Relation *Irel, LVRelStats *vacrelstats,
33813385
lps->wal_usage = wal_usage;
33823386

33833387
/* Store query string for workers */
3384-
sharedquery = (char *) shm_toc_allocate(pcxt->toc, querylen + 1);
3385-
memcpy(sharedquery, debug_query_string, querylen + 1);
3386-
sharedquery[querylen] = '\0';
3387-
shm_toc_insert(pcxt->toc, PARALLEL_VACUUM_KEY_QUERY_TEXT, sharedquery);
3388+
if (debug_query_string)
3389+
{
3390+
char *sharedquery;
3391+
3392+
sharedquery = (char *) shm_toc_allocate(pcxt->toc, querylen + 1);
3393+
memcpy(sharedquery, debug_query_string, querylen + 1);
3394+
sharedquery[querylen] = '\0';
3395+
shm_toc_insert(pcxt->toc,
3396+
PARALLEL_VACUUM_KEY_QUERY_TEXT, sharedquery);
3397+
}
33883398

33893399
pfree(can_parallel_vacuum);
33903400
return lps;
@@ -3527,7 +3537,7 @@ parallel_vacuum_main(dsm_segment *seg, shm_toc *toc)
35273537
elog(DEBUG1, "starting parallel vacuum worker for bulk delete");
35283538

35293539
/* Set debug_query_string for individual workers */
3530-
sharedquery = shm_toc_lookup(toc, PARALLEL_VACUUM_KEY_QUERY_TEXT, false);
3540+
sharedquery = shm_toc_lookup(toc, PARALLEL_VACUUM_KEY_QUERY_TEXT, true);
35313541
debug_query_string = sharedquery;
35323542
pgstat_report_activity(STATE_RUNNING, debug_query_string);
35333543

src/backend/access/nbtree/nbtsort.c

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1466,7 +1466,6 @@ _bt_begin_parallel(BTBuildState *buildstate, bool isconcurrent, int request)
14661466
WalUsage *walusage;
14671467
BufferUsage *bufferusage;
14681468
bool leaderparticipates = true;
1469-
char *sharedquery;
14701469
int querylen;
14711470

14721471
#ifdef DISABLE_LEADER_PARTICIPATION
@@ -1533,9 +1532,14 @@ _bt_begin_parallel(BTBuildState *buildstate, bool isconcurrent, int request)
15331532
shm_toc_estimate_keys(&pcxt->estimator, 1);
15341533

15351534
/* Finally, estimate PARALLEL_KEY_QUERY_TEXT space */
1536-
querylen = strlen(debug_query_string);
1537-
shm_toc_estimate_chunk(&pcxt->estimator, querylen + 1);
1538-
shm_toc_estimate_keys(&pcxt->estimator, 1);
1535+
if (debug_query_string)
1536+
{
1537+
querylen = strlen(debug_query_string);
1538+
shm_toc_estimate_chunk(&pcxt->estimator, querylen + 1);
1539+
shm_toc_estimate_keys(&pcxt->estimator, 1);
1540+
}
1541+
else
1542+
querylen = 0; /* keep compiler quiet */
15391543

15401544
/* Everyone's had a chance to ask for space, so now create the DSM */
15411545
InitializeParallelDSM(pcxt);
@@ -1599,9 +1603,14 @@ _bt_begin_parallel(BTBuildState *buildstate, bool isconcurrent, int request)
15991603
}
16001604

16011605
/* Store query string for workers */
1602-
sharedquery = (char *) shm_toc_allocate(pcxt->toc, querylen + 1);
1603-
memcpy(sharedquery, debug_query_string, querylen + 1);
1604-
shm_toc_insert(pcxt->toc, PARALLEL_KEY_QUERY_TEXT, sharedquery);
1606+
if (debug_query_string)
1607+
{
1608+
char *sharedquery;
1609+
1610+
sharedquery = (char *) shm_toc_allocate(pcxt->toc, querylen + 1);
1611+
memcpy(sharedquery, debug_query_string, querylen + 1);
1612+
shm_toc_insert(pcxt->toc, PARALLEL_KEY_QUERY_TEXT, sharedquery);
1613+
}
16051614

16061615
/*
16071616
* Allocate space for each worker's WalUsage and BufferUsage; no need to
@@ -1806,7 +1815,7 @@ _bt_parallel_build_main(dsm_segment *seg, shm_toc *toc)
18061815
#endif /* BTREE_BUILD_STATS */
18071816

18081817
/* Set debug_query_string for individual workers first */
1809-
sharedquery = shm_toc_lookup(toc, PARALLEL_KEY_QUERY_TEXT, false);
1818+
sharedquery = shm_toc_lookup(toc, PARALLEL_KEY_QUERY_TEXT, true);
18101819
debug_query_string = sharedquery;
18111820

18121821
/* Report the query string from leader */

0 commit comments

Comments
 (0)