7171#define PARALLEL_KEY_TUPLESORT_SPOOL2 UINT64CONST(0xA000000000000003)
7272#define PARALLEL_KEY_QUERY_TEXT UINT64CONST(0xA000000000000004)
7373#define PARALLEL_KEY_WAL_USAGE UINT64CONST(0xA000000000000005)
74+ #define PARALLEL_KEY_BUFFER_USAGE UINT64CONST(0xA000000000000006)
7475
7576/*
7677 * DISABLE_LEADER_PARTICIPATION disables the leader's participation in
@@ -194,6 +195,7 @@ typedef struct BTLeader
194195 Sharedsort * sharedsort2 ;
195196 Snapshot snapshot ;
196197 WalUsage * walusage ;
198+ BufferUsage * bufferusage ;
197199} BTLeader ;
198200
199201/*
@@ -1457,6 +1459,7 @@ _bt_begin_parallel(BTBuildState *buildstate, bool isconcurrent, int request)
14571459 BTSpool * btspool = buildstate -> spool ;
14581460 BTLeader * btleader = (BTLeader * ) palloc0 (sizeof (BTLeader ));
14591461 WalUsage * walusage ;
1462+ BufferUsage * bufferusage ;
14601463 bool leaderparticipates = true;
14611464 char * sharedquery ;
14621465 int querylen ;
@@ -1510,16 +1513,19 @@ _bt_begin_parallel(BTBuildState *buildstate, bool isconcurrent, int request)
15101513 }
15111514
15121515 /*
1513- * Estimate space for WalUsage -- PARALLEL_KEY_WAL_USAGE
1516+ * Estimate space for WalUsage and BufferUsage -- PARALLEL_KEY_WAL_USAGE
1517+ * and PARALLEL_KEY_BUFFER_USAGE.
15141518 *
1515- * WalUsage during execution of maintenance command can be used by an
1516- * extension that reports the WAL usage, such as pg_stat_statements. We
1517- * have no way of knowing whether anyone's looking at pgWalUsage, so do it
1518- * unconditionally.
1519+ * If there are no extensions loaded that care, we could skip this. We
1520+ * have no way of knowing whether anyone's looking at pgWalUsage or
1521+ * pgBufferUsage, so do it unconditionally.
15191522 */
15201523 shm_toc_estimate_chunk (& pcxt -> estimator ,
15211524 mul_size (sizeof (WalUsage ), pcxt -> nworkers ));
15221525 shm_toc_estimate_keys (& pcxt -> estimator , 1 );
1526+ shm_toc_estimate_chunk (& pcxt -> estimator ,
1527+ mul_size (sizeof (BufferUsage ), pcxt -> nworkers ));
1528+ shm_toc_estimate_keys (& pcxt -> estimator , 1 );
15231529
15241530 /* Finally, estimate PARALLEL_KEY_QUERY_TEXT space */
15251531 querylen = strlen (debug_query_string );
@@ -1592,10 +1598,16 @@ _bt_begin_parallel(BTBuildState *buildstate, bool isconcurrent, int request)
15921598 memcpy (sharedquery , debug_query_string , querylen + 1 );
15931599 shm_toc_insert (pcxt -> toc , PARALLEL_KEY_QUERY_TEXT , sharedquery );
15941600
1595- /* Allocate space for each worker's WalUsage; no need to initialize */
1601+ /*
1602+ * Allocate space for each worker's WalUsage and BufferUsage; no need to
1603+ * initialize.
1604+ */
15961605 walusage = shm_toc_allocate (pcxt -> toc ,
15971606 mul_size (sizeof (WalUsage ), pcxt -> nworkers ));
15981607 shm_toc_insert (pcxt -> toc , PARALLEL_KEY_WAL_USAGE , walusage );
1608+ bufferusage = shm_toc_allocate (pcxt -> toc ,
1609+ mul_size (sizeof (BufferUsage ), pcxt -> nworkers ));
1610+ shm_toc_insert (pcxt -> toc , PARALLEL_KEY_BUFFER_USAGE , bufferusage );
15991611
16001612 /* Launch workers, saving status for leader/caller */
16011613 LaunchParallelWorkers (pcxt );
@@ -1608,6 +1620,7 @@ _bt_begin_parallel(BTBuildState *buildstate, bool isconcurrent, int request)
16081620 btleader -> sharedsort2 = sharedsort2 ;
16091621 btleader -> snapshot = snapshot ;
16101622 btleader -> walusage = walusage ;
1623+ btleader -> bufferusage = bufferusage ;
16111624
16121625 /* If no workers were successfully launched, back out (do serial build) */
16131626 if (pcxt -> nworkers_launched == 0 )
@@ -1646,7 +1659,7 @@ _bt_end_parallel(BTLeader *btleader)
16461659 * or we might get incomplete data.)
16471660 */
16481661 for (i = 0 ; i < btleader -> pcxt -> nworkers_launched ; i ++ )
1649- InstrAccumParallelQuery (NULL , & btleader -> walusage [i ]);
1662+ InstrAccumParallelQuery (& btleader -> bufferusage [ i ] , & btleader -> walusage [i ]);
16501663
16511664 /* Free last reference to MVCC snapshot, if one was used */
16521665 if (IsMVCCSnapshot (btleader -> snapshot ))
@@ -1779,6 +1792,7 @@ _bt_parallel_build_main(dsm_segment *seg, shm_toc *toc)
17791792 LOCKMODE heapLockmode ;
17801793 LOCKMODE indexLockmode ;
17811794 WalUsage * walusage ;
1795+ BufferUsage * bufferusage ;
17821796 int sortmem ;
17831797
17841798#ifdef BTREE_BUILD_STATS
@@ -1848,9 +1862,11 @@ _bt_parallel_build_main(dsm_segment *seg, shm_toc *toc)
18481862 _bt_parallel_scan_and_sort (btspool , btspool2 , btshared , sharedsort ,
18491863 sharedsort2 , sortmem , false);
18501864
1851- /* Report WAL usage during parallel execution */
1865+ /* Report WAL/buffer usage during parallel execution */
1866+ bufferusage = shm_toc_lookup (toc , PARALLEL_KEY_BUFFER_USAGE , false);
18521867 walusage = shm_toc_lookup (toc , PARALLEL_KEY_WAL_USAGE , false);
1853- InstrEndParallelQuery (NULL , & walusage [ParallelWorkerNumber ]);
1868+ InstrEndParallelQuery (& bufferusage [ParallelWorkerNumber ],
1869+ & walusage [ParallelWorkerNumber ]);
18541870
18551871#ifdef BTREE_BUILD_STATS
18561872 if (log_btree_build_stats )
0 commit comments