Skip to content

Commit 0454666

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 24d769b commit 0454666

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

src/backend/access/nbtree/nbtsort.c

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,7 +1245,6 @@ _bt_begin_parallel(BTBuildState *buildstate, bool isconcurrent, int request)
12451245
BTLeader *btleader = (BTLeader *) palloc0(sizeof(BTLeader));
12461246
BufferUsage *bufferusage;
12471247
bool leaderparticipates = true;
1248-
char *sharedquery;
12491248
int querylen;
12501249

12511250
#ifdef DISABLE_LEADER_PARTICIPATION
@@ -1308,9 +1307,14 @@ _bt_begin_parallel(BTBuildState *buildstate, bool isconcurrent, int request)
13081307
shm_toc_estimate_keys(&pcxt->estimator, 1);
13091308

13101309
/* Finally, estimate PARALLEL_KEY_QUERY_TEXT space */
1311-
querylen = strlen(debug_query_string);
1312-
shm_toc_estimate_chunk(&pcxt->estimator, querylen + 1);
1313-
shm_toc_estimate_keys(&pcxt->estimator, 1);
1310+
if (debug_query_string)
1311+
{
1312+
querylen = strlen(debug_query_string);
1313+
shm_toc_estimate_chunk(&pcxt->estimator, querylen + 1);
1314+
shm_toc_estimate_keys(&pcxt->estimator, 1);
1315+
}
1316+
else
1317+
querylen = 0; /* keep compiler quiet */
13141318

13151319
/* Everyone's had a chance to ask for space, so now create the DSM */
13161320
InitializeParallelDSM(pcxt);
@@ -1372,9 +1376,14 @@ _bt_begin_parallel(BTBuildState *buildstate, bool isconcurrent, int request)
13721376
}
13731377

13741378
/* Store query string for workers */
1375-
sharedquery = (char *) shm_toc_allocate(pcxt->toc, querylen + 1);
1376-
memcpy(sharedquery, debug_query_string, querylen + 1);
1377-
shm_toc_insert(pcxt->toc, PARALLEL_KEY_QUERY_TEXT, sharedquery);
1379+
if (debug_query_string)
1380+
{
1381+
char *sharedquery;
1382+
1383+
sharedquery = (char *) shm_toc_allocate(pcxt->toc, querylen + 1);
1384+
memcpy(sharedquery, debug_query_string, querylen + 1);
1385+
shm_toc_insert(pcxt->toc, PARALLEL_KEY_QUERY_TEXT, sharedquery);
1386+
}
13781387

13791388
/* Allocate space for each worker's BufferUsage; no need to initialize */
13801389
bufferusage = shm_toc_allocate(pcxt->toc,
@@ -1577,7 +1586,7 @@ _bt_parallel_build_main(dsm_segment *seg, shm_toc *toc)
15771586
#endif /* BTREE_BUILD_STATS */
15781587

15791588
/* Set debug_query_string for individual workers first */
1580-
sharedquery = shm_toc_lookup(toc, PARALLEL_KEY_QUERY_TEXT, false);
1589+
sharedquery = shm_toc_lookup(toc, PARALLEL_KEY_QUERY_TEXT, true);
15811590
debug_query_string = sharedquery;
15821591

15831592
/* Report the query string from leader */

0 commit comments

Comments
 (0)