LCOV - differential code coverage report
Current view: top level - src/backend/commands - vacuumparallel.c (source / functions) Coverage Total Hit UBC GNC CBC DCB
Current: c38de3ae6098ccf1904109f2e550eb4777f66024 vs e352ac8c5c90ed6ce792f044c22145578542a330 Lines: 97.1 % 421 409 12 11 398 1
Current Date: 2026-11-05 14:14:01 +0100 Functions: 100.0 % 19 19 2 17
Baseline: lcov-20261105-baseline Branches: 78.9 % 228 180 48 2 178
Baseline Date: 2026-11-05 14:14:01 +0100 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(7,30] days: 100.0 % 1 1 1
(30,360] days: 95.9 % 98 94 4 11 83
(360..) days: 97.5 % 322 314 8 314
Function coverage date bins:
(30,360] days: 100.0 % 5 5 5
(360..) days: 100.0 % 14 14 2 12
Branch coverage date bins:
(30,360] days: 76.6 % 64 49 15 2 47
(360..) days: 79.9 % 164 131 33 131

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * vacuumparallel.c
                                  4                 :                :  *    Support routines for parallel vacuum and autovacuum execution. In the
                                  5                 :                :  *    comments below, the word "vacuum" will refer to both vacuum and
                                  6                 :                :  *    autovacuum.
                                  7                 :                :  *
                                  8                 :                :  * This file contains routines that are intended to support setting up, using,
                                  9                 :                :  * and tearing down a ParallelVacuumState.
                                 10                 :                :  *
                                 11                 :                :  * In a parallel vacuum, we perform both index bulk deletion and index cleanup
                                 12                 :                :  * with parallel worker processes.  Individual indexes are processed by one
                                 13                 :                :  * vacuum process.  ParallelVacuumState contains shared information as well as
                                 14                 :                :  * the memory space for storing dead items allocated in the DSA area.  We
                                 15                 :                :  * launch parallel worker processes at the start of parallel index
                                 16                 :                :  * bulk-deletion and index cleanup and once all indexes are processed, the
                                 17                 :                :  * parallel worker processes exit.  Each time we process indexes in parallel,
                                 18                 :                :  * the parallel context is re-initialized so that the same DSM can be used for
                                 19                 :                :  * multiple passes of index bulk-deletion and index cleanup.
                                 20                 :                :  *
                                 21                 :                :  * For parallel autovacuum, we need to propagate cost-based vacuum delay
                                 22                 :                :  * parameters from the leader to its workers, as the leader's parameters can
                                 23                 :                :  * change even while processing a table (e.g., due to a config reload).
                                 24                 :                :  * The PVSharedCostParams struct manages these parameters using a
                                 25                 :                :  * generation counter. Each parallel worker polls this shared state and
                                 26                 :                :  * refreshes its local delay parameters whenever a change is detected.
                                 27                 :                :  *
                                 28                 :                :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
                                 29                 :                :  * Portions Copyright (c) 1994, Regents of the University of California
                                 30                 :                :  *
                                 31                 :                :  * IDENTIFICATION
                                 32                 :                :  *    src/backend/commands/vacuumparallel.c
                                 33                 :                :  *
                                 34                 :                :  *-------------------------------------------------------------------------
                                 35                 :                :  */
                                 36                 :                : #include "postgres.h"
                                 37                 :                : 
                                 38                 :                : #include "access/amapi.h"
                                 39                 :                : #include "access/table.h"
                                 40                 :                : #include "access/xact.h"
                                 41                 :                : #include "commands/progress.h"
                                 42                 :                : #include "commands/vacuum.h"
                                 43                 :                : #include "executor/instrument.h"
                                 44                 :                : #include "optimizer/paths.h"
                                 45                 :                : #include "pgstat.h"
                                 46                 :                : #include "postmaster/interrupt.h"
                                 47                 :                : #include "storage/bufmgr.h"
                                 48                 :                : #include "storage/proc.h"
                                 49                 :                : #include "tcop/tcopprot.h"
                                 50                 :                : #include "utils/injection_point.h"
                                 51                 :                : #include "utils/lsyscache.h"
                                 52                 :                : #include "utils/rel.h"
                                 53                 :                : 
                                 54                 :                : /*
                                 55                 :                :  * DSM keys for parallel vacuum.  Unlike other parallel execution code, since
                                 56                 :                :  * we don't need to worry about DSM keys conflicting with plan_node_id we can
                                 57                 :                :  * use small integers.
                                 58                 :                :  */
                                 59                 :                : #define PARALLEL_VACUUM_KEY_SHARED          1
                                 60                 :                : #define PARALLEL_VACUUM_KEY_QUERY_TEXT      2
                                 61                 :                : #define PARALLEL_VACUUM_KEY_BUFFER_USAGE    3
                                 62                 :                : #define PARALLEL_VACUUM_KEY_WAL_USAGE       4
                                 63                 :                : #define PARALLEL_VACUUM_KEY_INDEX_STATS     5
                                 64                 :                : 
                                 65                 :                : /*
                                 66                 :                :  * Struct for cost-based vacuum delay related parameters to share among an
                                 67                 :                :  * autovacuum worker and its parallel vacuum workers.
                                 68                 :                :  */
                                 69                 :                : typedef struct PVSharedCostParams
                                 70                 :                : {
                                 71                 :                :     /*
                                 72                 :                :      * The generation counter is incremented by the leader process each time
                                 73                 :                :      * it updates the shared cost-based vacuum delay parameters. Parallel
                                 74                 :                :      * vacuum workers compare it with their local generation,
                                 75                 :                :      * shared_params_generation_local, to detect whether they need to refresh
                                 76                 :                :      * their local parameters. The generation starts from 1 so that a freshly
                                 77                 :                :      * started worker (whose local copy is 0) will always load the initial
                                 78                 :                :      * parameters on its first check.
                                 79                 :                :      */
                                 80                 :                :     pg_atomic_uint32 generation;
                                 81                 :                : 
                                 82                 :                :     slock_t     mutex;          /* protects all fields below */
                                 83                 :                : 
                                 84                 :                :     /* Parameters to share with parallel workers */
                                 85                 :                :     double      cost_delay;
                                 86                 :                :     int         cost_limit;
                                 87                 :                :     int         cost_page_dirty;
                                 88                 :                :     int         cost_page_hit;
                                 89                 :                :     int         cost_page_miss;
                                 90                 :                :     bool        track_cost_delay_timing;
                                 91                 :                : } PVSharedCostParams;
                                 92                 :                : 
                                 93                 :                : /*
                                 94                 :                :  * Shared information among parallel workers.  So this is allocated in the DSM
                                 95                 :                :  * segment.
                                 96                 :                :  */
                                 97                 :                : typedef struct PVShared
                                 98                 :                : {
                                 99                 :                :     /*
                                100                 :                :      * Target table relid, log level (for messages about parallel workers
                                101                 :                :      * launched during VACUUM VERBOSE) and query ID.  These fields are not
                                102                 :                :      * modified during the parallel vacuum.
                                103                 :                :      */
                                104                 :                :     Oid         relid;
                                105                 :                :     int         elevel;
                                106                 :                :     int64       queryid;
                                107                 :                : 
                                108                 :                :     /*
                                109                 :                :      * Fields for both index vacuum and cleanup.
                                110                 :                :      *
                                111                 :                :      * reltuples is the total number of input heap tuples.  We set either old
                                112                 :                :      * live tuples in the index vacuum case or the new live tuples in the
                                113                 :                :      * index cleanup case.
                                114                 :                :      *
                                115                 :                :      * estimated_count is true if reltuples is an estimated value.  (Note that
                                116                 :                :      * reltuples could be -1 in this case, indicating we have no idea.)
                                117                 :                :      */
                                118                 :                :     double      reltuples;
                                119                 :                :     bool        estimated_count;
                                120                 :                : 
                                121                 :                :     /*
                                122                 :                :      * In single process vacuum we could consume more memory during index
                                123                 :                :      * vacuuming or cleanup apart from the memory for heap scanning.  In
                                124                 :                :      * parallel vacuum, since individual vacuum workers can consume memory
                                125                 :                :      * equal to maintenance_work_mem, the new maintenance_work_mem for each
                                126                 :                :      * worker is set such that the parallel operation doesn't consume more
                                127                 :                :      * memory than single process vacuum.
                                128                 :                :      */
                                129                 :                :     int         maintenance_work_mem_worker;
                                130                 :                : 
                                131                 :                :     /*
                                132                 :                :      * The number of buffers each worker's Buffer Access Strategy ring should
                                133                 :                :      * contain.
                                134                 :                :      */
                                135                 :                :     int         ring_nbuffers;
                                136                 :                : 
                                137                 :                :     /*
                                138                 :                :      * Shared vacuum cost balance.  During parallel vacuum,
                                139                 :                :      * VacuumSharedCostBalance points to this value and it accumulates the
                                140                 :                :      * balance of each parallel vacuum worker.
                                141                 :                :      */
                                142                 :                :     pg_atomic_uint32 cost_balance;
                                143                 :                : 
                                144                 :                :     /*
                                145                 :                :      * Number of active parallel workers.  This is used for computing the
                                146                 :                :      * minimum threshold of the vacuum cost balance before a worker sleeps for
                                147                 :                :      * cost-based delay.
                                148                 :                :      */
                                149                 :                :     pg_atomic_uint32 active_nworkers;
                                150                 :                : 
                                151                 :                :     /* Counter for vacuuming and cleanup */
                                152                 :                :     pg_atomic_uint32 idx;
                                153                 :                : 
                                154                 :                :     /* DSA handle where the TidStore lives */
                                155                 :                :     dsa_handle  dead_items_dsa_handle;
                                156                 :                : 
                                157                 :                :     /* DSA pointer to the shared TidStore */
                                158                 :                :     dsa_pointer dead_items_handle;
                                159                 :                : 
                                160                 :                :     /* Statistics of shared dead items */
                                161                 :                :     VacDeadItemsInfo dead_items_info;
                                162                 :                : 
                                163                 :                :     /*
                                164                 :                :      * If 'true' then we are running parallel autovacuum. Otherwise, we are
                                165                 :                :      * running parallel maintenance VACUUM.
                                166                 :                :      */
                                167                 :                :     bool        is_autovacuum;
                                168                 :                : 
                                169                 :                :     /*
                                170                 :                :      * Cost-based vacuum delay parameters shared between the autovacuum leader
                                171                 :                :      * and its parallel workers.
                                172                 :                :      */
                                173                 :                :     PVSharedCostParams cost_params;
                                174                 :                : } PVShared;
                                175                 :                : 
                                176                 :                : /* Status used during parallel index vacuum or cleanup */
                                177                 :                : typedef enum PVIndVacStatus
                                178                 :                : {
                                179                 :                :     PARALLEL_INDVAC_STATUS_INITIAL = 0,
                                180                 :                :     PARALLEL_INDVAC_STATUS_NEED_BULKDELETE,
                                181                 :                :     PARALLEL_INDVAC_STATUS_NEED_CLEANUP,
                                182                 :                :     PARALLEL_INDVAC_STATUS_COMPLETED,
                                183                 :                : } PVIndVacStatus;
                                184                 :                : 
                                185                 :                : /*
                                186                 :                :  * Struct for index vacuum statistics of an index that is used for parallel vacuum.
                                187                 :                :  * This includes the status of parallel index vacuum as well as index statistics.
                                188                 :                :  */
                                189                 :                : typedef struct PVIndStats
                                190                 :                : {
                                191                 :                :     /*
                                192                 :                :      * The following two fields are set by leader process before executing
                                193                 :                :      * parallel index vacuum or parallel index cleanup.  These fields are not
                                194                 :                :      * fixed for the entire VACUUM operation.  They are only fixed for an
                                195                 :                :      * individual parallel index vacuum and cleanup.
                                196                 :                :      *
                                197                 :                :      * parallel_workers_can_process is true if both leader and worker can
                                198                 :                :      * process the index, otherwise only leader can process it.
                                199                 :                :      */
                                200                 :                :     PVIndVacStatus status;
                                201                 :                :     bool        parallel_workers_can_process;
                                202                 :                : 
                                203                 :                :     /*
                                204                 :                :      * Individual worker or leader stores the result of index vacuum or
                                205                 :                :      * cleanup.
                                206                 :                :      */
                                207                 :                :     bool        istat_updated;  /* are the stats updated? */
                                208                 :                :     IndexBulkDeleteResult istat;
                                209                 :                : } PVIndStats;
                                210                 :                : 
                                211                 :                : /*
                                212                 :                :  * Struct for maintaining a parallel vacuum state. typedef appears in vacuum.h.
                                213                 :                :  */
                                214                 :                : struct ParallelVacuumState
                                215                 :                : {
                                216                 :                :     /* NULL for worker processes */
                                217                 :                :     ParallelContext *pcxt;
                                218                 :                : 
                                219                 :                :     /* Parent Heap Relation */
                                220                 :                :     Relation    heaprel;
                                221                 :                : 
                                222                 :                :     /* Target indexes */
                                223                 :                :     Relation   *indrels;
                                224                 :                :     int         nindexes;
                                225                 :                : 
                                226                 :                :     /* Shared information among parallel vacuum workers */
                                227                 :                :     PVShared   *shared;
                                228                 :                : 
                                229                 :                :     /*
                                230                 :                :      * Shared index statistics among parallel vacuum workers. The array
                                231                 :                :      * element is allocated for every index, even those indexes where parallel
                                232                 :                :      * index vacuuming is unsafe or not worthwhile (e.g.,
                                233                 :                :      * will_parallel_vacuum[] is false).  During parallel vacuum,
                                234                 :                :      * IndexBulkDeleteResult of each index is kept in DSM and is copied into
                                235                 :                :      * local memory at the end of parallel vacuum.
                                236                 :                :      */
                                237                 :                :     PVIndStats *indstats;
                                238                 :                : 
                                239                 :                :     /* Shared dead items space among parallel vacuum workers */
                                240                 :                :     TidStore   *dead_items;
                                241                 :                : 
                                242                 :                :     /* Points to buffer usage area in DSM */
                                243                 :                :     BufferUsage *buffer_usage;
                                244                 :                : 
                                245                 :                :     /* Points to WAL usage area in DSM */
                                246                 :                :     WalUsage   *wal_usage;
                                247                 :                : 
                                248                 :                :     /*
                                249                 :                :      * False if the index is totally unsuitable target for all parallel
                                250                 :                :      * processing. For example, the index could be <
                                251                 :                :      * min_parallel_index_scan_size cutoff.
                                252                 :                :      */
                                253                 :                :     bool       *will_parallel_vacuum;
                                254                 :                : 
                                255                 :                :     /*
                                256                 :                :      * The number of indexes that support parallel index bulk-deletion and
                                257                 :                :      * parallel index cleanup respectively.
                                258                 :                :      */
                                259                 :                :     int         nindexes_parallel_bulkdel;
                                260                 :                :     int         nindexes_parallel_cleanup;
                                261                 :                :     int         nindexes_parallel_condcleanup;
                                262                 :                : 
                                263                 :                :     /* Buffer access strategy used by leader process */
                                264                 :                :     BufferAccessStrategy bstrategy;
                                265                 :                : 
                                266                 :                :     /*
                                267                 :                :      * Error reporting state.  The error callback is set only for workers
                                268                 :                :      * processes during parallel index vacuum.
                                269                 :                :      */
                                270                 :                :     char       *relnamespace;
                                271                 :                :     char       *relname;
                                272                 :                :     char       *indname;
                                273                 :                :     PVIndVacStatus status;
                                274                 :                : };
                                275                 :                : 
                                276                 :                : static PVSharedCostParams *pv_shared_cost_params = NULL;
                                277                 :                : 
                                278                 :                : /*
                                279                 :                :  * Worker-local copy of the last cost-parameter generation this worker has
                                280                 :                :  * applied.  Initialized to 0; since the leader initializes the shared
                                281                 :                :  * generation counter to 1, the first call to
                                282                 :                :  * parallel_vacuum_update_shared_delay_params() will always detect a
                                283                 :                :  * mismatch and read the initial parameters from shared memory.
                                284                 :                :  */
                                285                 :                : static uint32 shared_params_generation_local = 0;
                                286                 :                : 
                                287                 :                : static int  parallel_vacuum_compute_workers(Relation *indrels, int nindexes, int nrequested,
                                288                 :                :                                             bool *will_parallel_vacuum);
                                289                 :                : static void parallel_vacuum_process_all_indexes(ParallelVacuumState *pvs, int num_index_scans,
                                290                 :                :                                                 bool vacuum, PVWorkerStats *wstats);
                                291                 :                : static void parallel_vacuum_process_safe_indexes(ParallelVacuumState *pvs);
                                292                 :                : static void parallel_vacuum_process_unsafe_indexes(ParallelVacuumState *pvs);
                                293                 :                : static void parallel_vacuum_process_one_index(ParallelVacuumState *pvs, Relation indrel,
                                294                 :                :                                               PVIndStats *indstats);
                                295                 :                : static bool parallel_vacuum_index_is_parallel_safe(Relation indrel, int num_index_scans,
                                296                 :                :                                                    bool vacuum);
                                297                 :                : static void parallel_vacuum_error_callback(void *arg);
                                298                 :                : static inline void parallel_vacuum_set_cost_parameters(PVSharedCostParams *params);
                                299                 :                : static void parallel_vacuum_dsm_detach(dsm_segment *seg, Datum arg);
                                300                 :                : 
                                301                 :                : /*
                                302                 :                :  * Try to enter parallel mode and create a parallel context.  Then initialize
                                303                 :                :  * shared memory state.
                                304                 :                :  *
                                305                 :                :  * On success, return parallel vacuum state.  Otherwise return NULL.
                                306                 :                :  */
                                307                 :                : ParallelVacuumState *
 1778 akapila@postgresql.o      308                 :CBC        6344 : parallel_vacuum_init(Relation rel, Relation *indrels, int nindexes,
                                309                 :                :                      int nrequested_workers, int vac_work_mem,
                                310                 :                :                      int elevel, BufferAccessStrategy bstrategy)
                                311                 :                : {
                                312                 :                :     ParallelVacuumState *pvs;
                                313                 :                :     ParallelContext *pcxt;
                                314                 :                :     PVShared   *shared;
                                315                 :                :     TidStore   *dead_items;
                                316                 :                :     PVIndStats *indstats;
                                317                 :                :     BufferUsage *buffer_usage;
                                318                 :                :     WalUsage   *wal_usage;
                                319                 :                :     bool       *will_parallel_vacuum;
                                320                 :                :     Size        est_indstats_len;
                                321                 :                :     Size        est_shared_len;
                                322                 :           6344 :     int         nindexes_mwm = 0;
                                323                 :           6344 :     int         parallel_workers = 0;
                                324                 :                :     int         querylen;
                                325                 :                : 
                                326                 :                :     /*
                                327                 :                :      * A parallel vacuum must be requested and there must be indexes on the
                                328                 :                :      * relation
                                329                 :                :      */
                                330         [ -  + ]:           6344 :     Assert(nrequested_workers >= 0);
                                331         [ -  + ]:           6344 :     Assert(nindexes > 0);
                                332                 :                : 
                                333                 :                :     /*
                                334                 :                :      * Compute the number of parallel vacuum workers to launch
                                335                 :                :      */
  330 michael@paquier.xyz       336                 :           6344 :     will_parallel_vacuum = palloc0_array(bool, nindexes);
 1778 akapila@postgresql.o      337                 :           6344 :     parallel_workers = parallel_vacuum_compute_workers(indrels, nindexes,
                                338                 :                :                                                        nrequested_workers,
                                339                 :                :                                                        will_parallel_vacuum);
                                340         [ +  + ]:           6344 :     if (parallel_workers <= 0)
                                341                 :                :     {
                                342                 :                :         /* Can't perform vacuum in parallel -- return NULL */
                                343                 :           6317 :         pfree(will_parallel_vacuum);
                                344                 :           6317 :         return NULL;
                                345                 :                :     }
                                346                 :                : 
  330 michael@paquier.xyz       347                 :             27 :     pvs = palloc0_object(ParallelVacuumState);
 1778 akapila@postgresql.o      348                 :             27 :     pvs->indrels = indrels;
                                349                 :             27 :     pvs->nindexes = nindexes;
                                350                 :             27 :     pvs->will_parallel_vacuum = will_parallel_vacuum;
                                351                 :             27 :     pvs->bstrategy = bstrategy;
 1314 andres@anarazel.de        352                 :             27 :     pvs->heaprel = rel;
                                353                 :                : 
 1778 akapila@postgresql.o      354                 :             27 :     EnterParallelMode();
                                355                 :             27 :     pcxt = CreateParallelContext("postgres", "parallel_vacuum_main",
                                356                 :                :                                  parallel_workers);
                                357         [ -  + ]:             27 :     Assert(pcxt->nworkers > 0);
                                358                 :             27 :     pvs->pcxt = pcxt;
                                359                 :                : 
                                360                 :                :     /* Estimate size for index vacuum stats -- PARALLEL_VACUUM_KEY_INDEX_STATS */
                                361                 :             27 :     est_indstats_len = mul_size(sizeof(PVIndStats), nindexes);
                                362                 :             27 :     shm_toc_estimate_chunk(&pcxt->estimator, est_indstats_len);
                                363                 :             27 :     shm_toc_estimate_keys(&pcxt->estimator, 1);
                                364                 :                : 
                                365                 :                :     /* Estimate size for shared information -- PARALLEL_VACUUM_KEY_SHARED */
                                366                 :             27 :     est_shared_len = sizeof(PVShared);
                                367                 :             27 :     shm_toc_estimate_chunk(&pcxt->estimator, est_shared_len);
                                368                 :             27 :     shm_toc_estimate_keys(&pcxt->estimator, 1);
                                369                 :                : 
                                370                 :                :     /*
                                371                 :                :      * Estimate space for BufferUsage and WalUsage --
                                372                 :                :      * PARALLEL_VACUUM_KEY_BUFFER_USAGE and PARALLEL_VACUUM_KEY_WAL_USAGE.
                                373                 :                :      *
                                374                 :                :      * If there are no extensions loaded that care, we could skip this.  We
                                375                 :                :      * have no way of knowing whether anyone's looking at pgBufferUsage or
                                376                 :                :      * pgWalUsage, so do it unconditionally.
                                377                 :                :      */
                                378                 :             27 :     shm_toc_estimate_chunk(&pcxt->estimator,
                                379                 :                :                            mul_size(sizeof(BufferUsage), pcxt->nworkers));
                                380                 :             27 :     shm_toc_estimate_keys(&pcxt->estimator, 1);
                                381                 :             27 :     shm_toc_estimate_chunk(&pcxt->estimator,
                                382                 :                :                            mul_size(sizeof(WalUsage), pcxt->nworkers));
                                383                 :             27 :     shm_toc_estimate_keys(&pcxt->estimator, 1);
                                384                 :                : 
                                385                 :                :     /* Finally, estimate PARALLEL_VACUUM_KEY_QUERY_TEXT space */
                                386         [ +  + ]:             27 :     if (debug_query_string)
                                387                 :                :     {
                                388                 :             23 :         querylen = strlen(debug_query_string);
                                389                 :             23 :         shm_toc_estimate_chunk(&pcxt->estimator, querylen + 1);
                                390                 :             23 :         shm_toc_estimate_keys(&pcxt->estimator, 1);
                                391                 :                :     }
                                392                 :                :     else
                                393                 :              4 :         querylen = 0;           /* keep compiler quiet */
                                394                 :                : 
                                395                 :             27 :     InitializeParallelDSM(pcxt);
                                396                 :                : 
                                397                 :                :     /* Prepare index vacuum stats */
                                398                 :             27 :     indstats = (PVIndStats *) shm_toc_allocate(pcxt->toc, est_indstats_len);
 1540 pg@bowt.ie                399   [ +  -  +  -  :            603 :     MemSet(indstats, 0, est_indstats_len);
                                     +  -  +  -  +  
                                                 + ]
 1778 akapila@postgresql.o      400         [ +  + ]:            123 :     for (int i = 0; i < nindexes; i++)
                                401                 :                :     {
                                402                 :             96 :         Relation    indrel = indrels[i];
                                403                 :             96 :         uint8       vacoptions = indrel->rd_indam->amparallelvacuumoptions;
                                404                 :                : 
                                405                 :                :         /*
                                406                 :                :          * Cleanup option should be either disabled, always performing in
                                407                 :                :          * parallel or conditionally performing in parallel.
                                408                 :                :          */
                                409   [ +  +  -  + ]:             96 :         Assert(((vacoptions & VACUUM_OPTION_PARALLEL_CLEANUP) == 0) ||
                                410                 :                :                ((vacoptions & VACUUM_OPTION_PARALLEL_COND_CLEANUP) == 0));
                                411         [ -  + ]:             96 :         Assert(vacoptions <= VACUUM_OPTION_MAX_VALID_VALUE);
                                412                 :                : 
                                413         [ +  + ]:             96 :         if (!will_parallel_vacuum[i])
                                414                 :              4 :             continue;
                                415                 :                : 
                                416         [ +  + ]:             92 :         if (indrel->rd_indam->amusemaintenanceworkmem)
                                417                 :              8 :             nindexes_mwm++;
                                418                 :                : 
                                419                 :                :         /*
                                420                 :                :          * Remember the number of indexes that support parallel operation for
                                421                 :                :          * each phase.
                                422                 :                :          */
                                423         [ +  + ]:             92 :         if ((vacoptions & VACUUM_OPTION_PARALLEL_BULKDEL) != 0)
                                424                 :             84 :             pvs->nindexes_parallel_bulkdel++;
                                425         [ +  + ]:             92 :         if ((vacoptions & VACUUM_OPTION_PARALLEL_CLEANUP) != 0)
                                426                 :             16 :             pvs->nindexes_parallel_cleanup++;
                                427         [ +  + ]:             92 :         if ((vacoptions & VACUUM_OPTION_PARALLEL_COND_CLEANUP) != 0)
                                428                 :             68 :             pvs->nindexes_parallel_condcleanup++;
                                429                 :                :     }
                                430                 :             27 :     shm_toc_insert(pcxt->toc, PARALLEL_VACUUM_KEY_INDEX_STATS, indstats);
                                431                 :             27 :     pvs->indstats = indstats;
                                432                 :                : 
                                433                 :                :     /* Prepare shared information */
                                434                 :             27 :     shared = (PVShared *) shm_toc_allocate(pcxt->toc, est_shared_len);
                                435   [ +  -  +  -  :            459 :     MemSet(shared, 0, est_shared_len);
                                     +  -  +  -  +  
                                                 + ]
                                436                 :             27 :     shared->relid = RelationGetRelid(rel);
                                437                 :             27 :     shared->elevel = elevel;
  766 michael@paquier.xyz       438                 :             27 :     shared->queryid = pgstat_get_my_query_id();
 1778 akapila@postgresql.o      439                 :             27 :     shared->maintenance_work_mem_worker =
                                440                 :                :         (nindexes_mwm > 0) ?
  213 msawada@postgresql.o      441         [ +  + ]:             27 :         vac_work_mem / Min(parallel_workers, nindexes_mwm) :
                                442                 :                :         vac_work_mem;
                                443                 :                : 
  643 tgl@sss.pgh.pa.us         444                 :             27 :     shared->dead_items_info.max_bytes = vac_work_mem * (size_t) 1024;
                                445                 :                : 
                                446                 :                :     /* Prepare DSA space for dead items */
  947 msawada@postgresql.o      447                 :             27 :     dead_items = TidStoreCreateShared(shared->dead_items_info.max_bytes,
                                448                 :                :                                       LWTRANCHE_PARALLEL_VACUUM_DSA);
                                449                 :             27 :     pvs->dead_items = dead_items;
                                450                 :             27 :     shared->dead_items_handle = TidStoreGetHandle(dead_items);
                                451                 :             27 :     shared->dead_items_dsa_handle = dsa_get_handle(TidStoreGetDSA(dead_items));
                                452                 :                : 
                                453                 :                :     /* Use the same buffer size for all workers */
 1308 drowley@postgresql.o      454                 :             27 :     shared->ring_nbuffers = GetAccessStrategyBufferCount(bstrategy);
                                455                 :                : 
 1778 akapila@postgresql.o      456                 :             27 :     pg_atomic_init_u32(&(shared->cost_balance), 0);
                                457                 :             27 :     pg_atomic_init_u32(&(shared->active_nworkers), 0);
                                458                 :             27 :     pg_atomic_init_u32(&(shared->idx), 0);
                                459                 :                : 
  213 msawada@postgresql.o      460                 :             27 :     shared->is_autovacuum = AmAutoVacuumWorkerProcess();
                                461                 :                : 
                                462                 :                :     /*
                                463                 :                :      * Initialize shared cost-based vacuum delay parameters if it's for
                                464                 :                :      * autovacuum and the parallel context has workers. Note that the parallel
                                465                 :                :      * context falls back to the leader's private memory with no workers and
                                466                 :                :      * no segment when the maximum number of DSM segments has been reached.
                                467                 :                :      * There are then no workers to propagate the parameters to, and no
                                468                 :                :      * segment to register the detach callback on.
                                469                 :                :      */
   38                           470   [ +  +  +  - ]:             27 :     if (shared->is_autovacuum && pcxt->nworkers > 0)
                                471                 :                :     {
  213                           472                 :              4 :         parallel_vacuum_set_cost_parameters(&shared->cost_params);
                                473                 :              4 :         pg_atomic_init_u32(&shared->cost_params.generation, 1);
                                474                 :              4 :         SpinLockInit(&shared->cost_params.mutex);
                                475                 :                : 
                                476                 :              4 :         pv_shared_cost_params = &(shared->cost_params);
                                477                 :              4 :         on_dsm_detach(pcxt->seg, parallel_vacuum_dsm_detach, (Datum) 0);
                                478                 :                :     }
                                479                 :                : 
 1778 akapila@postgresql.o      480                 :             27 :     shm_toc_insert(pcxt->toc, PARALLEL_VACUUM_KEY_SHARED, shared);
                                481                 :             27 :     pvs->shared = shared;
                                482                 :                : 
                                483                 :                :     /*
                                484                 :                :      * Allocate space for each worker's BufferUsage and WalUsage; no need to
                                485                 :                :      * initialize
                                486                 :                :      */
                                487                 :             27 :     buffer_usage = shm_toc_allocate(pcxt->toc,
                                488                 :             27 :                                     mul_size(sizeof(BufferUsage), pcxt->nworkers));
                                489                 :             27 :     shm_toc_insert(pcxt->toc, PARALLEL_VACUUM_KEY_BUFFER_USAGE, buffer_usage);
                                490                 :             27 :     pvs->buffer_usage = buffer_usage;
                                491                 :             27 :     wal_usage = shm_toc_allocate(pcxt->toc,
                                492                 :             27 :                                  mul_size(sizeof(WalUsage), pcxt->nworkers));
                                493                 :             27 :     shm_toc_insert(pcxt->toc, PARALLEL_VACUUM_KEY_WAL_USAGE, wal_usage);
                                494                 :             27 :     pvs->wal_usage = wal_usage;
                                495                 :                : 
                                496                 :                :     /* Store query string for workers */
                                497         [ +  + ]:             27 :     if (debug_query_string)
                                498                 :                :     {
                                499                 :                :         char       *sharedquery;
                                500                 :                : 
                                501                 :             23 :         sharedquery = (char *) shm_toc_allocate(pcxt->toc, querylen + 1);
                                502                 :             23 :         memcpy(sharedquery, debug_query_string, querylen + 1);
                                503                 :             23 :         sharedquery[querylen] = '\0';
                                504                 :             23 :         shm_toc_insert(pcxt->toc,
                                505                 :                :                        PARALLEL_VACUUM_KEY_QUERY_TEXT, sharedquery);
                                506                 :                :     }
                                507                 :                : 
                                508                 :                :     /* Success -- return parallel vacuum state */
                                509                 :             27 :     return pvs;
                                510                 :                : }
                                511                 :                : 
                                512                 :                : /*
                                513                 :                :  * Destroy the parallel context, and end parallel mode.
                                514                 :                :  *
                                515                 :                :  * Since writes are not allowed during parallel mode, copy the
                                516                 :                :  * updated index statistics from DSM into local memory and then later use that
                                517                 :                :  * to update the index statistics.  One might think that we can exit from
                                518                 :                :  * parallel mode, update the index statistics and then destroy parallel
                                519                 :                :  * context, but that won't be safe (see ExitParallelMode).
                                520                 :                :  */
                                521                 :                : void
                                522                 :             26 : parallel_vacuum_end(ParallelVacuumState *pvs, IndexBulkDeleteResult **istats)
                                523                 :                : {
                                524         [ -  + ]:             26 :     Assert(!IsParallelWorker());
                                525                 :                : 
                                526                 :                :     /* Copy the updated statistics */
                                527         [ +  + ]:            120 :     for (int i = 0; i < pvs->nindexes; i++)
                                528                 :                :     {
                                529                 :             94 :         PVIndStats *indstats = &(pvs->indstats[i]);
                                530                 :                : 
                                531         [ +  + ]:             94 :         if (indstats->istat_updated)
                                532                 :                :         {
  330 michael@paquier.xyz       533                 :             68 :             istats[i] = palloc0_object(IndexBulkDeleteResult);
 1778 akapila@postgresql.o      534                 :             68 :             memcpy(istats[i], &indstats->istat, sizeof(IndexBulkDeleteResult));
                                535                 :                :         }
                                536                 :                :         else
                                537                 :             26 :             istats[i] = NULL;
                                538                 :                :     }
                                539                 :                : 
  947 msawada@postgresql.o      540                 :             26 :     TidStoreDestroy(pvs->dead_items);
                                541                 :                : 
 1778 akapila@postgresql.o      542                 :             26 :     DestroyParallelContext(pvs->pcxt);
                                543                 :             26 :     ExitParallelMode();
                                544                 :                : 
  213 msawada@postgresql.o      545         [ +  + ]:             26 :     if (AmAutoVacuumWorkerProcess())
                                546                 :              3 :         pv_shared_cost_params = NULL;
                                547                 :                : 
 1778 akapila@postgresql.o      548                 :             26 :     pfree(pvs->will_parallel_vacuum);
                                549                 :             26 :     pfree(pvs);
                                550                 :             26 : }
                                551                 :                : 
                                552                 :                : /*
                                553                 :                :  * DSM detach callback. This is invoked when an autovacuum worker detaches
                                554                 :                :  * from the DSM segment holding PVShared. It ensures to reset the local pointer
                                555                 :                :  * to the shared state even if parallel vacuum raises an error and doesn't
                                556                 :                :  * call parallel_vacuum_end().
                                557                 :                :  */
                                558                 :                : static void
  213 msawada@postgresql.o      559                 :              4 : parallel_vacuum_dsm_detach(dsm_segment *seg, Datum arg)
                                560                 :                : {
                                561         [ -  + ]:              4 :     Assert(AmAutoVacuumWorkerProcess());
                                562                 :              4 :     pv_shared_cost_params = NULL;
                                563                 :              4 : }
                                564                 :                : 
                                565                 :                : /*
                                566                 :                :  * Returns the dead items space and dead items information.
                                567                 :                :  */
                                568                 :                : TidStore *
  947                           569                 :             52 : parallel_vacuum_get_dead_items(ParallelVacuumState *pvs, VacDeadItemsInfo **dead_items_info_p)
                                570                 :                : {
                                571                 :             52 :     *dead_items_info_p = &(pvs->shared->dead_items_info);
 1778 akapila@postgresql.o      572                 :             52 :     return pvs->dead_items;
                                573                 :                : }
                                574                 :                : 
                                575                 :                : /* Forget all items in dead_items */
                                576                 :                : void
  947 msawada@postgresql.o      577                 :             25 : parallel_vacuum_reset_dead_items(ParallelVacuumState *pvs)
                                578                 :                : {
                                579                 :             25 :     VacDeadItemsInfo *dead_items_info = &(pvs->shared->dead_items_info);
                                580                 :                : 
                                581                 :                :     /*
                                582                 :                :      * Free the current tidstore and return allocated DSA segments to the
                                583                 :                :      * operating system. Then we recreate the tidstore with the same max_bytes
                                584                 :                :      * limitation we just used.
                                585                 :                :      */
  701 john.naylor@postgres      586                 :             25 :     TidStoreDestroy(pvs->dead_items);
  947 msawada@postgresql.o      587                 :             25 :     pvs->dead_items = TidStoreCreateShared(dead_items_info->max_bytes,
                                588                 :                :                                            LWTRANCHE_PARALLEL_VACUUM_DSA);
                                589                 :                : 
                                590                 :                :     /* Update the DSA pointer for dead_items to the new one */
  701 john.naylor@postgres      591                 :             25 :     pvs->shared->dead_items_dsa_handle = dsa_get_handle(TidStoreGetDSA(pvs->dead_items));
                                592                 :             25 :     pvs->shared->dead_items_handle = TidStoreGetHandle(pvs->dead_items);
                                593                 :                : 
                                594                 :                :     /* Reset the counter */
  947 msawada@postgresql.o      595                 :             25 :     dead_items_info->num_items = 0;
                                596                 :             25 : }
                                597                 :                : 
                                598                 :                : /*
                                599                 :                :  * Do parallel index bulk-deletion with parallel workers.
                                600                 :                :  */
                                601                 :                : void
 1778 akapila@postgresql.o      602                 :             26 : parallel_vacuum_bulkdel_all_indexes(ParallelVacuumState *pvs, long num_table_tuples,
                                603                 :                :                                     int num_index_scans, PVWorkerStats *wstats)
                                604                 :                : {
                                605         [ -  + ]:             26 :     Assert(!IsParallelWorker());
                                606                 :                : 
                                607                 :                :     /*
                                608                 :                :      * We can only provide an approximate value of num_heap_tuples, at least
                                609                 :                :      * for now.
                                610                 :                :      */
                                611                 :             26 :     pvs->shared->reltuples = num_table_tuples;
                                612                 :             26 :     pvs->shared->estimated_count = true;
                                613                 :                : 
  231 msawada@postgresql.o      614                 :             26 :     parallel_vacuum_process_all_indexes(pvs, num_index_scans, true, wstats);
 1778 akapila@postgresql.o      615                 :             25 : }
                                616                 :                : 
                                617                 :                : /*
                                618                 :                :  * Do parallel index cleanup with parallel workers.
                                619                 :                :  */
                                620                 :                : void
                                621                 :             26 : parallel_vacuum_cleanup_all_indexes(ParallelVacuumState *pvs, long num_table_tuples,
                                622                 :                :                                     int num_index_scans, bool estimated_count,
                                623                 :                :                                     PVWorkerStats *wstats)
                                624                 :                : {
                                625         [ -  + ]:             26 :     Assert(!IsParallelWorker());
                                626                 :                : 
                                627                 :                :     /*
                                628                 :                :      * We can provide a better estimate of total number of surviving tuples
                                629                 :                :      * (we assume indexes are more interested in that than in the number of
                                630                 :                :      * nominally live tuples).
                                631                 :                :      */
                                632                 :             26 :     pvs->shared->reltuples = num_table_tuples;
                                633                 :             26 :     pvs->shared->estimated_count = estimated_count;
                                634                 :                : 
  231 msawada@postgresql.o      635                 :             26 :     parallel_vacuum_process_all_indexes(pvs, num_index_scans, false, wstats);
 1778 akapila@postgresql.o      636                 :             26 : }
                                637                 :                : 
                                638                 :                : /*
                                639                 :                :  * Fill in the given structure with cost-based vacuum delay parameter values.
                                640                 :                :  */
                                641                 :                : static inline void
  213 msawada@postgresql.o      642                 :              7 : parallel_vacuum_set_cost_parameters(PVSharedCostParams *params)
                                643                 :                : {
                                644                 :              7 :     params->cost_delay = vacuum_cost_delay;
                                645                 :              7 :     params->cost_limit = vacuum_cost_limit;
                                646                 :              7 :     params->cost_page_dirty = VacuumCostPageDirty;
                                647                 :              7 :     params->cost_page_hit = VacuumCostPageHit;
                                648                 :              7 :     params->cost_page_miss = VacuumCostPageMiss;
   31                           649                 :              7 :     params->track_cost_delay_timing = track_cost_delay_timing;
  213                           650                 :              7 : }
                                651                 :                : 
                                652                 :                : /*
                                653                 :                :  * Updates the cost-based vacuum delay parameters for parallel autovacuum
                                654                 :                :  * workers.
                                655                 :                :  *
                                656                 :                :  * For non-autovacuum parallel workers, this function will have no effect.
                                657                 :                :  */
                                658                 :                : void
                                659                 :            374 : parallel_vacuum_update_shared_delay_params(void)
                                660                 :                : {
                                661                 :                :     uint32      params_generation;
                                662                 :                : 
                                663         [ -  + ]:            374 :     Assert(IsParallelWorker());
                                664                 :                : 
                                665                 :                :     /* Quick return if the worker is not running for the autovacuum */
                                666         [ +  + ]:            374 :     if (pv_shared_cost_params == NULL)
                                667                 :             11 :         return;
                                668                 :                : 
                                669                 :            363 :     params_generation = pg_atomic_read_u32(&pv_shared_cost_params->generation);
                                670         [ -  + ]:            363 :     Assert(shared_params_generation_local <= params_generation);
                                671                 :                : 
                                672                 :                :     /* Return if parameters had not changed in the leader */
                                673         [ +  + ]:            363 :     if (params_generation == shared_params_generation_local)
                                674                 :            359 :         return;
                                675                 :                : 
                                676                 :              4 :     SpinLockAcquire(&pv_shared_cost_params->mutex);
                                677                 :              4 :     VacuumCostDelay = pv_shared_cost_params->cost_delay;
                                678                 :              4 :     VacuumCostLimit = pv_shared_cost_params->cost_limit;
                                679                 :              4 :     VacuumCostPageDirty = pv_shared_cost_params->cost_page_dirty;
                                680                 :              4 :     VacuumCostPageHit = pv_shared_cost_params->cost_page_hit;
                                681                 :              4 :     VacuumCostPageMiss = pv_shared_cost_params->cost_page_miss;
   31                           682                 :              4 :     track_cost_delay_timing = pv_shared_cost_params->track_cost_delay_timing;
  213                           683                 :              4 :     SpinLockRelease(&pv_shared_cost_params->mutex);
                                684                 :                : 
                                685                 :              4 :     VacuumUpdateCosts();
                                686                 :                : 
                                687                 :              4 :     shared_params_generation_local = params_generation;
                                688                 :                : 
                                689   [ +  -  +  + ]:              4 :     elog(DEBUG2,
                                690                 :                :          "parallel autovacuum worker updated cost params: cost_limit=%d, cost_delay=%g, cost_page_miss=%d, cost_page_dirty=%d, cost_page_hit=%d, track_cost_delay_timing=%s",
                                691                 :                :          vacuum_cost_limit,
                                692                 :                :          vacuum_cost_delay,
                                693                 :                :          VacuumCostPageMiss,
                                694                 :                :          VacuumCostPageDirty,
                                695                 :                :          VacuumCostPageHit,
                                696                 :                :          track_cost_delay_timing ? "yes" : "no");
                                697                 :                : }
                                698                 :                : 
                                699                 :                : /*
                                700                 :                :  * Store the cost-based vacuum delay parameters in the shared memory so that
                                701                 :                :  * parallel vacuum workers can consume them (see
                                702                 :                :  * parallel_vacuum_update_shared_delay_params()).
                                703                 :                :  */
                                704                 :                : void
                                705                 :           2116 : parallel_vacuum_propagate_shared_delay_params(void)
                                706                 :                : {
                                707         [ -  + ]:           2116 :     Assert(AmAutoVacuumWorkerProcess());
                                708                 :                : 
                                709                 :                :     /*
                                710                 :                :      * Quick return if the leader process is not sharing the delay parameters.
                                711                 :                :      */
                                712         [ +  + ]:           2116 :     if (pv_shared_cost_params == NULL)
                                713                 :           2073 :         return;
                                714                 :                : 
                                715                 :                :     /*
                                716                 :                :      * Check if any delay parameters have changed. We can read them without
                                717                 :                :      * locks as only the leader can modify them.
                                718                 :                :      */
                                719         [ +  + ]:             43 :     if (vacuum_cost_delay == pv_shared_cost_params->cost_delay &&
                                720         [ +  + ]:             42 :         vacuum_cost_limit == pv_shared_cost_params->cost_limit &&
                                721         [ +  - ]:             40 :         VacuumCostPageDirty == pv_shared_cost_params->cost_page_dirty &&
                                722         [ +  - ]:             40 :         VacuumCostPageHit == pv_shared_cost_params->cost_page_hit &&
   31                           723         [ +  - ]:             40 :         VacuumCostPageMiss == pv_shared_cost_params->cost_page_miss &&
                                724         [ +  - ]:             40 :         track_cost_delay_timing == pv_shared_cost_params->track_cost_delay_timing)
  213                           725                 :             40 :         return;
                                726                 :                : 
                                727                 :                :     /* Update the shared delay parameters */
                                728                 :              3 :     SpinLockAcquire(&pv_shared_cost_params->mutex);
                                729                 :              3 :     parallel_vacuum_set_cost_parameters(pv_shared_cost_params);
                                730                 :              3 :     SpinLockRelease(&pv_shared_cost_params->mutex);
                                731                 :                : 
                                732                 :                :     /*
                                733                 :                :      * Increment the generation of the parameters, i.e. let parallel workers
                                734                 :                :      * know that they should re-read shared cost params.
                                735                 :                :      */
                                736                 :              3 :     pg_atomic_fetch_add_u32(&pv_shared_cost_params->generation, 1);
                                737                 :                : }
                                738                 :                : 
                                739                 :                : /*
                                740                 :                :  * Reload the configuration file if requested, and refresh the cost-based
                                741                 :                :  * delay parameters of an autovacuum worker running a parallel vacuum
                                742                 :                :  * (leader), propagating any change to its parallel workers.
                                743                 :                :  */
                                744                 :                : void
   35                           745                 :             10 : parallel_vacuum_refresh_cost_params(void)
                                746                 :                : {
                                747         [ -  + ]:             10 :     Assert(AmAutoVacuumWorkerProcess());
                                748                 :                : 
                                749                 :                :     /*
                                750                 :                :      * Quick return if the leader is not sharing the delay parameters with
                                751                 :                :      * parallel workers.
                                752                 :                :      */
                                753         [ -  + ]:             10 :     if (pv_shared_cost_params == NULL)
   35 msawada@postgresql.o      754                 :UBC           0 :         return;
                                755                 :                : 
   35 msawada@postgresql.o      756         [ -  + ]:CBC          10 :     if (ConfigReloadPending)
                                757                 :                :     {
   35 msawada@postgresql.o      758                 :UBC           0 :         ConfigReloadPending = false;
                                759                 :              0 :         ProcessConfigFile(PGC_SIGHUP);
                                760                 :                : 
                                761                 :                :         /* This re-divides the cost limit too */
                                762                 :              0 :         VacuumUpdateCosts();
                                763                 :                :     }
                                764                 :                :     else
                                765                 :                :     {
                                766                 :                :         /* The number of workers sharing the cost limit may have changed */
   35 msawada@postgresql.o      767                 :CBC          10 :         AutoVacuumUpdateCostLimit();
                                768                 :                :     }
                                769                 :                : 
                                770                 :             10 :     parallel_vacuum_propagate_shared_delay_params();
                                771                 :                : }
                                772                 :                : 
                                773                 :                : /*
                                774                 :                :  * Compute the number of parallel worker processes to request.  Both index
                                775                 :                :  * vacuum and index cleanup can be executed with parallel workers.
                                776                 :                :  * The index is eligible for parallel vacuum iff its size is greater than
                                777                 :                :  * min_parallel_index_scan_size as invoking workers for very small indexes
                                778                 :                :  * can hurt performance.
                                779                 :                :  *
                                780                 :                :  * nrequested is the number of parallel workers that user requested.  If
                                781                 :                :  * nrequested is 0, we compute the parallel degree based on nindexes, that is
                                782                 :                :  * the number of indexes that support parallel vacuum.  This function also
                                783                 :                :  * sets will_parallel_vacuum to remember indexes that participate in parallel
                                784                 :                :  * vacuum.
                                785                 :                :  */
                                786                 :                : static int
 1778 akapila@postgresql.o      787                 :           6344 : parallel_vacuum_compute_workers(Relation *indrels, int nindexes, int nrequested,
                                788                 :                :                                 bool *will_parallel_vacuum)
                                789                 :                : {
                                790                 :           6344 :     int         nindexes_parallel = 0;
                                791                 :           6344 :     int         nindexes_parallel_bulkdel = 0;
                                792                 :           6344 :     int         nindexes_parallel_cleanup = 0;
                                793                 :                :     int         parallel_workers;
                                794                 :                :     int         max_workers;
                                795                 :                : 
  213 msawada@postgresql.o      796                 :          12688 :     max_workers = AmAutoVacuumWorkerProcess() ?
                                797         [ +  + ]:           6344 :         autovacuum_max_parallel_workers :
                                798                 :                :         max_parallel_maintenance_workers;
                                799                 :                : 
                                800                 :                :     /*
                                801                 :                :      * We don't allow performing parallel operation in standalone backend or
                                802                 :                :      * when parallelism is disabled.
                                803                 :                :      */
                                804   [ +  +  +  + ]:           6344 :     if (!IsUnderPostmaster || max_workers == 0)
 1778 akapila@postgresql.o      805                 :           3081 :         return 0;
                                806                 :                : 
                                807                 :                :     /*
                                808                 :                :      * Compute the number of indexes that can participate in parallel vacuum.
                                809                 :                :      */
                                810         [ +  + ]:          10652 :     for (int i = 0; i < nindexes; i++)
                                811                 :                :     {
                                812                 :           7389 :         Relation    indrel = indrels[i];
                                813                 :           7389 :         uint8       vacoptions = indrel->rd_indam->amparallelvacuumoptions;
                                814                 :                : 
                                815                 :                :         /* Skip index that is not a suitable target for parallel index vacuum */
                                816         [ +  - ]:           7389 :         if (vacoptions == VACUUM_OPTION_NO_PARALLEL ||
                                817         [ +  + ]:           7389 :             RelationGetNumberOfBlocks(indrel) < min_parallel_index_scan_size)
                                818                 :           7286 :             continue;
                                819                 :                : 
                                820                 :            103 :         will_parallel_vacuum[i] = true;
                                821                 :                : 
                                822         [ +  + ]:            103 :         if ((vacoptions & VACUUM_OPTION_PARALLEL_BULKDEL) != 0)
                                823                 :             95 :             nindexes_parallel_bulkdel++;
                                824         [ +  + ]:            103 :         if (((vacoptions & VACUUM_OPTION_PARALLEL_CLEANUP) != 0) ||
                                825         [ +  + ]:             87 :             ((vacoptions & VACUUM_OPTION_PARALLEL_COND_CLEANUP) != 0))
                                826                 :             95 :             nindexes_parallel_cleanup++;
                                827                 :                :     }
                                828                 :                : 
                                829                 :           3263 :     nindexes_parallel = Max(nindexes_parallel_bulkdel,
                                830                 :                :                             nindexes_parallel_cleanup);
                                831                 :                : 
                                832                 :                :     /* The leader process takes one index */
                                833                 :           3263 :     nindexes_parallel--;
                                834                 :                : 
                                835                 :                :     /* No index supports parallel vacuum */
                                836         [ +  + ]:           3263 :     if (nindexes_parallel <= 0)
                                837                 :           3236 :         return 0;
                                838                 :                : 
                                839                 :                :     /* Compute the parallel degree */
                                840                 :             27 :     parallel_workers = (nrequested > 0) ?
                                841         [ +  + ]:             27 :         Min(nrequested, nindexes_parallel) : nindexes_parallel;
                                842                 :                : 
                                843                 :                :     /* Cap by GUC variable */
  213 msawada@postgresql.o      844                 :             27 :     parallel_workers = Min(parallel_workers, max_workers);
                                845                 :                : 
 1778 akapila@postgresql.o      846                 :             27 :     return parallel_workers;
                                847                 :                : }
                                848                 :                : 
                                849                 :                : /*
                                850                 :                :  * Perform index vacuum or index cleanup with parallel workers.  This function
                                851                 :                :  * must be used by the parallel vacuum leader process.
                                852                 :                :  *
                                853                 :                :  * If wstats is not NULL, the parallel worker statistics are updated.
                                854                 :                :  */
                                855                 :                : static void
                                856                 :             52 : parallel_vacuum_process_all_indexes(ParallelVacuumState *pvs, int num_index_scans,
                                857                 :                :                                     bool vacuum, PVWorkerStats *wstats)
                                858                 :                : {
                                859                 :                :     int         nworkers;
                                860                 :                :     PVIndVacStatus new_status;
                                861                 :                : 
                                862         [ -  + ]:             52 :     Assert(!IsParallelWorker());
                                863                 :                : 
                                864         [ +  + ]:             52 :     if (vacuum)
                                865                 :                :     {
                                866                 :             26 :         new_status = PARALLEL_INDVAC_STATUS_NEED_BULKDELETE;
                                867                 :                : 
                                868                 :                :         /* Determine the number of parallel workers to launch */
                                869                 :             26 :         nworkers = pvs->nindexes_parallel_bulkdel;
                                870                 :                :     }
                                871                 :                :     else
                                872                 :                :     {
                                873                 :             26 :         new_status = PARALLEL_INDVAC_STATUS_NEED_CLEANUP;
                                874                 :                : 
                                875                 :                :         /* Determine the number of parallel workers to launch */
                                876                 :             26 :         nworkers = pvs->nindexes_parallel_cleanup;
                                877                 :                : 
                                878                 :                :         /* Add conditionally parallel-aware indexes if in the first time call */
                                879         [ +  + ]:             26 :         if (num_index_scans == 0)
                                880                 :             13 :             nworkers += pvs->nindexes_parallel_condcleanup;
                                881                 :                :     }
                                882                 :                : 
                                883                 :                :     /* The leader process will participate */
                                884                 :             52 :     nworkers--;
                                885                 :                : 
                                886                 :                :     /*
                                887                 :                :      * It is possible that parallel context is initialized with fewer workers
                                888                 :                :      * than the number of indexes that need a separate worker in the current
                                889                 :                :      * phase, so we need to consider it.  See
                                890                 :                :      * parallel_vacuum_compute_workers().
                                891                 :                :      */
                                892                 :             52 :     nworkers = Min(nworkers, pvs->pcxt->nworkers);
                                893                 :                : 
                                894                 :                :     /* Update the statistics, if we asked to */
  231 msawada@postgresql.o      895   [ +  -  +  + ]:             52 :     if (wstats != NULL && nworkers > 0)
                                896                 :             40 :         wstats->nplanned += nworkers;
                                897                 :                : 
                                898                 :                :     /*
                                899                 :                :      * Set index vacuum status and mark whether parallel vacuum worker can
                                900                 :                :      * process it.
                                901                 :                :      */
 1778 akapila@postgresql.o      902         [ +  + ]:            212 :     for (int i = 0; i < pvs->nindexes; i++)
                                903                 :                :     {
                                904                 :            160 :         PVIndStats *indstats = &(pvs->indstats[i]);
                                905                 :                : 
                                906         [ -  + ]:            160 :         Assert(indstats->status == PARALLEL_INDVAC_STATUS_INITIAL);
                                907                 :            160 :         indstats->status = new_status;
                                908                 :            160 :         indstats->parallel_workers_can_process =
 1536                           909   [ +  +  +  + ]:            312 :             (pvs->will_parallel_vacuum[i] &&
 1778                           910                 :            152 :              parallel_vacuum_index_is_parallel_safe(pvs->indrels[i],
                                911                 :                :                                                     num_index_scans,
                                912                 :            160 :                                                     vacuum));
                                913                 :                :     }
                                914                 :                : 
                                915                 :                :     /* Reset the parallel index processing and progress counters */
                                916                 :             52 :     pg_atomic_write_u32(&(pvs->shared->idx), 0);
                                917                 :                : 
                                918                 :                :     /* Setup the shared cost-based vacuum delay and launch workers */
                                919         [ +  + ]:             52 :     if (nworkers > 0)
                                920                 :                :     {
                                921                 :                :         /* Reinitialize parallel context to relaunch parallel workers */
                                922         [ +  + ]:             40 :         if (num_index_scans > 0)
                                923                 :             13 :             ReinitializeParallelDSM(pvs->pcxt);
                                924                 :                : 
                                925                 :                :         /*
                                926                 :                :          * Set up shared cost balance and the number of active workers for
                                927                 :                :          * vacuum delay.  We need to do this before launching workers as
                                928                 :                :          * otherwise, they might not see the updated values for these
                                929                 :                :          * parameters.
                                930                 :                :          */
                                931                 :             40 :         pg_atomic_write_u32(&(pvs->shared->cost_balance), VacuumCostBalance);
                                932                 :             40 :         pg_atomic_write_u32(&(pvs->shared->active_nworkers), 0);
                                933                 :                : 
                                934                 :                :         /*
                                935                 :                :          * The number of workers can vary between bulkdelete and cleanup
                                936                 :                :          * phase.
                                937                 :                :          */
                                938                 :             40 :         ReinitializeParallelWorkers(pvs->pcxt, nworkers);
                                939                 :                : 
                                940                 :             40 :         LaunchParallelWorkers(pvs->pcxt);
                                941                 :                : 
                                942         [ +  - ]:             40 :         if (pvs->pcxt->nworkers_launched > 0)
                                943                 :                :         {
                                944                 :                :             /*
                                945                 :                :              * Reset the local cost values for leader backend as we have
                                946                 :                :              * already accumulated the remaining balance of heap.
                                947                 :                :              */
                                948                 :             40 :             VacuumCostBalance = 0;
                                949                 :             40 :             VacuumCostBalanceLocal = 0;
                                950                 :                : 
                                951                 :                :             /* Enable shared cost balance for leader backend */
                                952                 :             40 :             VacuumSharedCostBalance = &(pvs->shared->cost_balance);
                                953                 :             40 :             VacuumActiveNWorkers = &(pvs->shared->active_nworkers);
                                954                 :                : 
                                955                 :                :             /* Update the statistics, if we asked to */
  231 msawada@postgresql.o      956         [ +  - ]:             40 :             if (wstats != NULL)
                                957                 :             40 :                 wstats->nlaunched += pvs->pcxt->nworkers_launched;
                                958                 :                :         }
                                959                 :                : 
 1778 akapila@postgresql.o      960         [ +  + ]:             40 :         if (vacuum)
                                961         [ +  + ]:             26 :             ereport(pvs->shared->elevel,
                                962                 :                :                     (errmsg(ngettext("launched %d parallel vacuum worker for index vacuuming (planned: %d)",
                                963                 :                :                                      "launched %d parallel vacuum workers for index vacuuming (planned: %d)",
                                964                 :                :                                      pvs->pcxt->nworkers_launched),
                                965                 :                :                             pvs->pcxt->nworkers_launched, nworkers)));
                                966                 :                :         else
                                967         [ -  + ]:             14 :             ereport(pvs->shared->elevel,
                                968                 :                :                     (errmsg(ngettext("launched %d parallel vacuum worker for index cleanup (planned: %d)",
                                969                 :                :                                      "launched %d parallel vacuum workers for index cleanup (planned: %d)",
                                970                 :                :                                      pvs->pcxt->nworkers_launched),
                                971                 :                :                             pvs->pcxt->nworkers_launched, nworkers)));
                                972                 :                :     }
                                973                 :                : 
                                974                 :                :     /* Vacuum the indexes that can be processed by only leader process */
                                975                 :             52 :     parallel_vacuum_process_unsafe_indexes(pvs);
                                976                 :                : 
                                977                 :                :     /*
                                978                 :                :      * Join as a parallel worker.  The leader vacuums alone processes all
                                979                 :                :      * parallel-safe indexes in the case where no workers are launched.
                                980                 :                :      */
                                981                 :             52 :     parallel_vacuum_process_safe_indexes(pvs);
                                982                 :                : 
                                983                 :                :     /*
                                984                 :                :      * Next, accumulate buffer and WAL usage.  (This must wait for the workers
                                985                 :                :      * to finish, or we might get incomplete data.)
                                986                 :                :      */
                                987         [ +  + ]:             52 :     if (nworkers > 0)
                                988                 :                :     {
                                989                 :                :         /* Wait for all vacuum workers to finish */
                                990                 :             40 :         WaitForParallelWorkersToFinish(pvs->pcxt);
                                991                 :                : 
                                992         [ +  + ]:             87 :         for (int i = 0; i < pvs->pcxt->nworkers_launched; i++)
                                993                 :             48 :             InstrAccumParallelQuery(&pvs->buffer_usage[i], &pvs->wal_usage[i]);
                                994                 :                :     }
                                995                 :                : 
                                996                 :                :     /*
                                997                 :                :      * Reset all index status back to initial (while checking that we have
                                998                 :                :      * vacuumed all indexes).
                                999                 :                :      */
                               1000         [ +  + ]:            209 :     for (int i = 0; i < pvs->nindexes; i++)
                               1001                 :                :     {
                               1002                 :            158 :         PVIndStats *indstats = &(pvs->indstats[i]);
                               1003                 :                : 
                               1004         [ -  + ]:            158 :         if (indstats->status != PARALLEL_INDVAC_STATUS_COMPLETED)
 1778 akapila@postgresql.o     1005         [ #  # ]:UBC           0 :             elog(ERROR, "parallel index vacuum on index \"%s\" is not completed",
                               1006                 :                :                  RelationGetRelationName(pvs->indrels[i]));
                               1007                 :                : 
 1778 akapila@postgresql.o     1008                 :CBC         158 :         indstats->status = PARALLEL_INDVAC_STATUS_INITIAL;
                               1009                 :                :     }
                               1010                 :                : 
                               1011                 :                :     /*
                               1012                 :                :      * Carry the shared balance value to heap scan and disable shared costing
                               1013                 :                :      */
                               1014         [ +  + ]:             51 :     if (VacuumSharedCostBalance)
                               1015                 :                :     {
                               1016                 :             39 :         VacuumCostBalance = pg_atomic_read_u32(VacuumSharedCostBalance);
                               1017                 :             39 :         VacuumSharedCostBalance = NULL;
                               1018                 :             39 :         VacuumActiveNWorkers = NULL;
                               1019                 :                :     }
                               1020                 :             51 : }
                               1021                 :                : 
                               1022                 :                : /*
                               1023                 :                :  * Index vacuum/cleanup routine used by the leader process and parallel
                               1024                 :                :  * vacuum worker processes to vacuum the indexes in parallel.
                               1025                 :                :  */
                               1026                 :                : static void
                               1027                 :            100 : parallel_vacuum_process_safe_indexes(ParallelVacuumState *pvs)
                               1028                 :                : {
                               1029                 :                :     /*
                               1030                 :                :      * Increment the active worker count if we are able to launch any worker.
                               1031                 :                :      */
                               1032         [ +  + ]:            100 :     if (VacuumActiveNWorkers)
                               1033                 :             88 :         pg_atomic_add_fetch_u32(VacuumActiveNWorkers, 1);
                               1034                 :                : 
                               1035                 :                :     /* Loop until all indexes are vacuumed */
                               1036                 :                :     for (;;)
                               1037                 :            160 :     {
                               1038                 :                :         int         idx;
                               1039                 :                :         PVIndStats *indstats;
                               1040                 :                : 
                               1041                 :                :         /* Get an index number to process */
                               1042                 :            260 :         idx = pg_atomic_fetch_add_u32(&(pvs->shared->idx), 1);
                               1043                 :                : 
                               1044                 :                :         /* Done for all indexes? */
                               1045         [ +  + ]:            260 :         if (idx >= pvs->nindexes)
                               1046                 :            100 :             break;
                               1047                 :                : 
                               1048                 :            160 :         indstats = &(pvs->indstats[idx]);
                               1049                 :                : 
                               1050                 :                :         /*
                               1051                 :                :          * Skip vacuuming index that is unsafe for workers or has an
                               1052                 :                :          * unsuitable target for parallel index vacuum (this is vacuumed in
                               1053                 :                :          * parallel_vacuum_process_unsafe_indexes() by the leader).
                               1054                 :                :          */
                               1055         [ +  + ]:            160 :         if (!indstats->parallel_workers_can_process)
                               1056                 :             50 :             continue;
                               1057                 :                : 
                               1058                 :                :         /* Do vacuum or cleanup of the index */
                               1059                 :            110 :         parallel_vacuum_process_one_index(pvs, pvs->indrels[idx], indstats);
                               1060                 :                :     }
                               1061                 :                : 
                               1062                 :                :     /*
                               1063                 :                :      * We have completed the index vacuum so decrement the active worker
                               1064                 :                :      * count.
                               1065                 :                :      */
                               1066         [ +  + ]:            100 :     if (VacuumActiveNWorkers)
                               1067                 :             88 :         pg_atomic_sub_fetch_u32(VacuumActiveNWorkers, 1);
                               1068                 :            100 : }
                               1069                 :                : 
                               1070                 :                : /*
                               1071                 :                :  * Perform parallel vacuuming of indexes in leader process.
                               1072                 :                :  *
                               1073                 :                :  * Handles index vacuuming (or index cleanup) for indexes that are not
                               1074                 :                :  * parallel safe.  It's possible that this will vary for a given index, based
                               1075                 :                :  * on details like whether we're performing index cleanup right now.
                               1076                 :                :  *
                               1077                 :                :  * Also performs vacuuming of smaller indexes that fell under the size cutoff
                               1078                 :                :  * enforced by parallel_vacuum_compute_workers().
                               1079                 :                :  */
                               1080                 :                : static void
                               1081                 :             52 : parallel_vacuum_process_unsafe_indexes(ParallelVacuumState *pvs)
                               1082                 :                : {
                               1083         [ -  + ]:             52 :     Assert(!IsParallelWorker());
                               1084                 :                : 
                               1085                 :                :     /*
                               1086                 :                :      * Increment the active worker count if we are able to launch any worker.
                               1087                 :                :      */
                               1088         [ +  + ]:             52 :     if (VacuumActiveNWorkers)
                               1089                 :             40 :         pg_atomic_add_fetch_u32(VacuumActiveNWorkers, 1);
                               1090                 :                : 
                               1091         [ +  + ]:            212 :     for (int i = 0; i < pvs->nindexes; i++)
                               1092                 :                :     {
                               1093                 :            160 :         PVIndStats *indstats = &(pvs->indstats[i]);
                               1094                 :                : 
                               1095                 :                :         /* Skip, indexes that are safe for workers */
                               1096         [ +  + ]:            160 :         if (indstats->parallel_workers_can_process)
                               1097                 :            110 :             continue;
                               1098                 :                : 
                               1099                 :                :         /* Do vacuum or cleanup of the index */
                               1100                 :             50 :         parallel_vacuum_process_one_index(pvs, pvs->indrels[i], indstats);
                               1101                 :                :     }
                               1102                 :                : 
                               1103                 :                :     /*
                               1104                 :                :      * We have completed the index vacuum so decrement the active worker
                               1105                 :                :      * count.
                               1106                 :                :      */
                               1107         [ +  + ]:             52 :     if (VacuumActiveNWorkers)
                               1108                 :             40 :         pg_atomic_sub_fetch_u32(VacuumActiveNWorkers, 1);
                               1109                 :             52 : }
                               1110                 :                : 
                               1111                 :                : /*
                               1112                 :                :  * Vacuum or cleanup index either by leader process or by one of the worker
                               1113                 :                :  * process.  After vacuuming the index this function copies the index
                               1114                 :                :  * statistics returned from ambulkdelete and amvacuumcleanup to the DSM
                               1115                 :                :  * segment.
                               1116                 :                :  */
                               1117                 :                : static void
                               1118                 :            160 : parallel_vacuum_process_one_index(ParallelVacuumState *pvs, Relation indrel,
                               1119                 :                :                                   PVIndStats *indstats)
                               1120                 :                : {
                               1121                 :            160 :     IndexBulkDeleteResult *istat = NULL;
                               1122                 :                :     IndexBulkDeleteResult *istat_res;
                               1123                 :                :     IndexVacuumInfo ivinfo;
   34 michael@paquier.xyz      1124                 :GNC         160 :     const int   progress_index[] = {
                               1125                 :                :         PROGRESS_VACUUM_PHASE,
                               1126                 :                :         PROGRESS_VACUUM_CURRENT_INDEX_RELID
                               1127                 :                :     };
                               1128                 :                :     int64       progress_val[2];
                               1129                 :            160 :     const int   reset_index[] = {
                               1130                 :                :         PROGRESS_VACUUM_CURRENT_INDEX_RELID,
                               1131                 :                :         PROGRESS_SCAN_BLOCKS_TOTAL,
                               1132                 :                :         PROGRESS_SCAN_BLOCKS_DONE
                               1133                 :                :     };
                               1134                 :            160 :     const int64 reset_val[] = {(int64) InvalidOid, 0, 0};
                               1135                 :                : 
                               1136                 :                :     /*
                               1137                 :                :      * Update the pointer to the corresponding bulk-deletion result if someone
                               1138                 :                :      * has already updated it
                               1139                 :                :      */
 1778 akapila@postgresql.o     1140         [ +  + ]:CBC         160 :     if (indstats->istat_updated)
                               1141                 :             64 :         istat = &(indstats->istat);
                               1142                 :                : 
                               1143                 :            160 :     ivinfo.index = indrel;
 1312 pg@bowt.ie               1144                 :            160 :     ivinfo.heaprel = pvs->heaprel;
 1778 akapila@postgresql.o     1145                 :            160 :     ivinfo.analyze_only = false;
   42 msawada@postgresql.o     1146                 :            160 :     ivinfo.is_autovacuum = pvs->shared->is_autovacuum;
   34 michael@paquier.xyz      1147                 :GNC         160 :     ivinfo.report_progress = true;
 1756 pg@bowt.ie               1148                 :CBC         160 :     ivinfo.message_level = DEBUG2;
 1778 akapila@postgresql.o     1149                 :            160 :     ivinfo.estimated_count = pvs->shared->estimated_count;
                               1150                 :            160 :     ivinfo.num_heap_tuples = pvs->shared->reltuples;
                               1151                 :            160 :     ivinfo.strategy = pvs->bstrategy;
                               1152                 :                : 
                               1153                 :                :     /* Update error traceback information */
                               1154                 :            160 :     pvs->indname = pstrdup(RelationGetRelationName(indrel));
                               1155                 :            160 :     pvs->status = indstats->status;
                               1156                 :                : 
                               1157                 :                :     /* Report the phase and the index we're about to process */
   34 michael@paquier.xyz      1158                 :GNC         320 :     progress_val[0] = (indstats->status == PARALLEL_INDVAC_STATUS_NEED_BULKDELETE)
                               1159                 :                :         ? PROGRESS_VACUUM_PHASE_VACUUM_INDEX
                               1160         [ +  + ]:            160 :         : PROGRESS_VACUUM_PHASE_INDEX_CLEANUP;
                               1161                 :            160 :     progress_val[1] = (int64) RelationGetRelid(indrel);
                               1162                 :            160 :     pgstat_progress_update_multi_param(2, progress_index, progress_val);
                               1163                 :                : 
 1778 akapila@postgresql.o     1164      [ +  +  - ]:CBC         160 :     switch (indstats->status)
                               1165                 :                :     {
                               1166                 :             66 :         case PARALLEL_INDVAC_STATUS_NEED_BULKDELETE:
  947 msawada@postgresql.o     1167                 :             66 :             istat_res = vac_bulkdel_one_index(&ivinfo, istat, pvs->dead_items,
                               1168                 :             66 :                                               &pvs->shared->dead_items_info);
 1778 akapila@postgresql.o     1169                 :             66 :             break;
                               1170                 :             94 :         case PARALLEL_INDVAC_STATUS_NEED_CLEANUP:
                               1171                 :             94 :             istat_res = vac_cleanup_one_index(&ivinfo, istat);
                               1172                 :             94 :             break;
 1778 akapila@postgresql.o     1173                 :UBC           0 :         default:
                               1174         [ #  # ]:              0 :             elog(ERROR, "unexpected parallel vacuum index status %d for index \"%s\"",
                               1175                 :                :                  indstats->status,
                               1176                 :                :                  RelationGetRelationName(indrel));
                               1177                 :                :     }
                               1178                 :                : 
   34 michael@paquier.xyz      1179                 :GNC         160 :     pgstat_progress_update_multi_param(3, reset_index, reset_val);
                               1180                 :                : 
                               1181                 :                :     /*
                               1182                 :                :      * Copy the index bulk-deletion result returned from ambulkdelete and
                               1183                 :                :      * amvacuumcleanup to the DSM segment if it's the first cycle because they
                               1184                 :                :      * allocate locally and it's possible that an index will be vacuumed by a
                               1185                 :                :      * different vacuum process the next cycle.  Copying the result normally
                               1186                 :                :      * happens only the first time an index is vacuumed.  For any additional
                               1187                 :                :      * vacuum pass, we directly point to the result on the DSM segment and
                               1188                 :                :      * pass it to vacuum index APIs so that workers can update it directly.
                               1189                 :                :      *
                               1190                 :                :      * Since all vacuum workers write the bulk-deletion result at different
                               1191                 :                :      * slots we can write them without locking.
                               1192                 :                :      */
 1778 akapila@postgresql.o     1193   [ +  +  +  + ]:CBC         160 :     if (!indstats->istat_updated && istat_res != NULL)
                               1194                 :                :     {
                               1195                 :             70 :         memcpy(&(indstats->istat), istat_res, sizeof(IndexBulkDeleteResult));
                               1196                 :             70 :         indstats->istat_updated = true;
                               1197                 :                : 
                               1198                 :                :         /* Free the locally-allocated bulk-deletion result */
                               1199                 :             70 :         pfree(istat_res);
                               1200                 :                :     }
                               1201                 :                : 
                               1202                 :                :     /*
                               1203                 :                :      * Update the status to completed. No need to lock here since each worker
                               1204                 :                :      * touches different indexes.
                               1205                 :                :      */
                               1206                 :            160 :     indstats->status = PARALLEL_INDVAC_STATUS_COMPLETED;
                               1207                 :                : 
                               1208                 :                :     /* Reset error traceback information */
                               1209                 :            160 :     pvs->status = PARALLEL_INDVAC_STATUS_COMPLETED;
                               1210                 :            160 :     pfree(pvs->indname);
                               1211                 :            160 :     pvs->indname = NULL;
                               1212                 :                : 
                               1213                 :                :     /*
                               1214                 :                :      * Call the parallel variant of pgstat_progress_incr_param so workers can
                               1215                 :                :      * report progress of index vacuum to the leader.
                               1216                 :                :      */
 1213 msawada@postgresql.o     1217                 :            160 :     pgstat_progress_parallel_incr_param(PROGRESS_VACUUM_INDEXES_PROCESSED, 1);
 1778 akapila@postgresql.o     1218                 :            160 : }
                               1219                 :                : 
                               1220                 :                : /*
                               1221                 :                :  * Returns false, if the given index can't participate in the next execution of
                               1222                 :                :  * parallel index vacuum or parallel index cleanup.
                               1223                 :                :  */
                               1224                 :                : static bool
                               1225                 :            152 : parallel_vacuum_index_is_parallel_safe(Relation indrel, int num_index_scans,
                               1226                 :                :                                        bool vacuum)
                               1227                 :                : {
                               1228                 :                :     uint8       vacoptions;
                               1229                 :                : 
                               1230                 :            152 :     vacoptions = indrel->rd_indam->amparallelvacuumoptions;
                               1231                 :                : 
                               1232                 :                :     /* In parallel vacuum case, check if it supports parallel bulk-deletion */
                               1233         [ +  + ]:            152 :     if (vacuum)
                               1234                 :             62 :         return ((vacoptions & VACUUM_OPTION_PARALLEL_BULKDEL) != 0);
                               1235                 :                : 
                               1236                 :                :     /* Not safe, if the index does not support parallel cleanup */
                               1237         [ +  + ]:             90 :     if (((vacoptions & VACUUM_OPTION_PARALLEL_CLEANUP) == 0) &&
                               1238         [ +  + ]:             74 :         ((vacoptions & VACUUM_OPTION_PARALLEL_COND_CLEANUP) == 0))
                               1239                 :              8 :         return false;
                               1240                 :                : 
                               1241                 :                :     /*
                               1242                 :                :      * Not safe, if the index supports parallel cleanup conditionally, but we
                               1243                 :                :      * have already processed the index (for bulkdelete).  We do this to avoid
                               1244                 :                :      * the need to invoke workers when parallel index cleanup doesn't need to
                               1245                 :                :      * scan the index.  See the comments for option
                               1246                 :                :      * VACUUM_OPTION_PARALLEL_COND_CLEANUP to know when indexes support
                               1247                 :                :      * parallel cleanup conditionally.
                               1248                 :                :      */
                               1249         [ +  + ]:             82 :     if (num_index_scans > 0 &&
                               1250         [ +  + ]:             35 :         ((vacoptions & VACUUM_OPTION_PARALLEL_COND_CLEANUP) != 0))
                               1251                 :             33 :         return false;
                               1252                 :                : 
                               1253                 :             49 :     return true;
                               1254                 :                : }
                               1255                 :                : 
                               1256                 :                : /*
                               1257                 :                :  * Perform work within a launched parallel process.
                               1258                 :                :  *
                               1259                 :                :  * Parallel vacuum workers perform only index vacuum or index cleanup; they
                               1260                 :                :  * report progress for the index they are processing.
                               1261                 :                :  */
                               1262                 :                : void
                               1263                 :             49 : parallel_vacuum_main(dsm_segment *seg, shm_toc *toc)
                               1264                 :                : {
                               1265                 :                :     ParallelVacuumState pvs;
                               1266                 :                :     Relation    rel;
                               1267                 :                :     Relation   *indrels;
                               1268                 :                :     PVIndStats *indstats;
                               1269                 :                :     PVShared   *shared;
                               1270                 :                :     TidStore   *dead_items;
                               1271                 :                :     BufferUsage *buffer_usage;
                               1272                 :                :     WalUsage   *wal_usage;
                               1273                 :                :     int         nindexes;
                               1274                 :                :     char       *sharedquery;
                               1275                 :                :     ErrorContextCallback errcallback;
                               1276                 :                : 
                               1277                 :                :     /*
                               1278                 :                :      * A parallel vacuum worker carries only the PROC_IN_VACUUM flag. The
                               1279                 :                :      * leader, whether it's a backend running a VACUUM command or an
                               1280                 :                :      * autovacuum worker, sets PROC_IN_VACUUM when it starts vacuuming the
                               1281                 :                :      * table, and the worker inherits the flag by importing the leader's
                               1282                 :                :      * snapshot (see ProcArrayInstallRestoredXmin). The leader's other flags
                               1283                 :                :      * don't reach the worker: the snapshot import copies only the
                               1284                 :                :      * PROC_XMIN_FLAGS bits, so PROC_VACUUM_FOR_WRAPAROUND isn't carried over,
                               1285                 :                :      * and PROC_IS_AUTOVACUUM is never set on the worker in the first place
                               1286                 :                :      * since parallel workers run as regular background workers, not
                               1287                 :                :      * autovacuum workers.
                               1288                 :                :      */
                               1289         [ -  + ]:             49 :     Assert(MyProc->statusFlags == PROC_IN_VACUUM);
                               1290                 :                : 
                               1291         [ +  + ]:             49 :     elog(DEBUG1, "starting parallel vacuum worker");
                               1292                 :                : 
                               1293                 :                :     /* Used by tests to hold a worker while it is attached to its leader */
   21 msawada@postgresql.o     1294                 :             49 :     INJECTION_POINT("parallel-vacuum-worker-start", NULL);
                               1295                 :                : 
 1778 akapila@postgresql.o     1296                 :             48 :     shared = (PVShared *) shm_toc_lookup(toc, PARALLEL_VACUUM_KEY_SHARED, false);
                               1297                 :                : 
                               1298                 :                :     /* Set debug_query_string for individual workers */
                               1299                 :             48 :     sharedquery = shm_toc_lookup(toc, PARALLEL_VACUUM_KEY_QUERY_TEXT, true);
                               1300                 :             48 :     debug_query_string = sharedquery;
                               1301                 :             48 :     pgstat_report_activity(STATE_RUNNING, debug_query_string);
                               1302                 :                : 
                               1303                 :                :     /* Track query ID */
  766 michael@paquier.xyz      1304                 :             48 :     pgstat_report_query_id(shared->queryid, false);
                               1305                 :                : 
                               1306                 :                :     /*
                               1307                 :                :      * Open table.  The lock mode is the same as the leader process.  It's
                               1308                 :                :      * okay because the lock mode does not conflict among the parallel
                               1309                 :                :      * workers.
                               1310                 :                :      */
 1778 akapila@postgresql.o     1311                 :             48 :     rel = table_open(shared->relid, ShareUpdateExclusiveLock);
                               1312                 :                : 
                               1313                 :                :     /*
                               1314                 :                :      * Open all indexes. indrels are sorted in order by OID, which should be
                               1315                 :                :      * matched to the leader's one.
                               1316                 :                :      */
                               1317                 :             48 :     vac_open_indexes(rel, RowExclusiveLock, &nindexes, &indrels);
                               1318         [ -  + ]:             48 :     Assert(nindexes > 0);
                               1319                 :                : 
                               1320                 :                :     /*
                               1321                 :                :      * Apply the desired value of maintenance_work_mem within this process.
                               1322                 :                :      * Really we should use SetConfigOption() to change a GUC, but since we're
                               1323                 :                :      * already in parallel mode guc.c would complain about that.  Fortunately,
                               1324                 :                :      * by the same token guc.c will not let any user-defined code change it.
                               1325                 :                :      * So just avert your eyes while we do this:
                               1326                 :                :      */
                               1327         [ +  - ]:             48 :     if (shared->maintenance_work_mem_worker > 0)
                               1328                 :             48 :         maintenance_work_mem = shared->maintenance_work_mem_worker;
                               1329                 :                : 
                               1330                 :                :     /* Set index statistics */
                               1331                 :             48 :     indstats = (PVIndStats *) shm_toc_lookup(toc,
                               1332                 :                :                                              PARALLEL_VACUUM_KEY_INDEX_STATS,
                               1333                 :                :                                              false);
                               1334                 :                : 
                               1335                 :                :     /* Find dead_items in shared memory */
  947 msawada@postgresql.o     1336                 :             48 :     dead_items = TidStoreAttach(shared->dead_items_dsa_handle,
                               1337                 :                :                                 shared->dead_items_handle);
                               1338                 :                : 
                               1339                 :                :     /* Set cost-based vacuum delay */
  213                          1340         [ +  + ]:             48 :     if (shared->is_autovacuum)
                               1341                 :                :     {
                               1342                 :                :         /*
                               1343                 :                :          * Parallel autovacuum workers initialize cost-based delay parameters
                               1344                 :                :          * from the leader's shared state rather than GUC defaults, because
                               1345                 :                :          * the leader may have applied per-table or autovacuum-specific
                               1346                 :                :          * overrides. pv_shared_cost_params must be set before calling
                               1347                 :                :          * parallel_vacuum_update_shared_delay_params().
                               1348                 :                :          */
                               1349                 :              4 :         pv_shared_cost_params = &(shared->cost_params);
                               1350                 :              4 :         parallel_vacuum_update_shared_delay_params();
                               1351                 :                :     }
                               1352                 :                :     else
                               1353                 :             44 :         VacuumUpdateCosts();
                               1354                 :                : 
 1778 akapila@postgresql.o     1355                 :             48 :     VacuumCostBalance = 0;
                               1356                 :             48 :     VacuumCostBalanceLocal = 0;
                               1357                 :             48 :     VacuumSharedCostBalance = &(shared->cost_balance);
                               1358                 :             48 :     VacuumActiveNWorkers = &(shared->active_nworkers);
                               1359                 :                : 
                               1360                 :                :     /* Set parallel vacuum state */
                               1361                 :             48 :     pvs.indrels = indrels;
                               1362                 :             48 :     pvs.nindexes = nindexes;
                               1363                 :             48 :     pvs.indstats = indstats;
                               1364                 :             48 :     pvs.shared = shared;
                               1365                 :             48 :     pvs.dead_items = dead_items;
                               1366                 :             48 :     pvs.relnamespace = get_namespace_name(RelationGetNamespace(rel));
                               1367                 :             48 :     pvs.relname = pstrdup(RelationGetRelationName(rel));
 1314 andres@anarazel.de       1368                 :             48 :     pvs.heaprel = rel;
                               1369                 :                : 
                               1370                 :                :     /* These fields will be filled during index vacuum or cleanup */
 1778 akapila@postgresql.o     1371                 :             48 :     pvs.indname = NULL;
                               1372                 :             48 :     pvs.status = PARALLEL_INDVAC_STATUS_INITIAL;
                               1373                 :                : 
                               1374                 :                :     /* Each parallel VACUUM worker gets its own access strategy. */
 1308 drowley@postgresql.o     1375                 :             96 :     pvs.bstrategy = GetAccessStrategyWithSize(BAS_VACUUM,
                               1376                 :             48 :                                               shared->ring_nbuffers * (BLCKSZ / 1024));
                               1377                 :                : 
                               1378                 :                :     /* Setup error traceback support for ereport() */
 1778 akapila@postgresql.o     1379                 :             48 :     errcallback.callback = parallel_vacuum_error_callback;
                               1380                 :             48 :     errcallback.arg = &pvs;
                               1381                 :             48 :     errcallback.previous = error_context_stack;
                               1382                 :             48 :     error_context_stack = &errcallback;
                               1383                 :                : 
                               1384                 :                :     /* Prepare to track buffer usage during parallel execution */
                               1385                 :             48 :     InstrStartParallelQuery();
                               1386                 :                : 
                               1387                 :                :     /* Register this worker for vacuum progress reporting */
   34 michael@paquier.xyz      1388                 :GNC          48 :     pgstat_progress_start_command(PROGRESS_COMMAND_VACUUM, shared->relid);
                               1389                 :                : 
                               1390                 :                :     /* Process indexes to perform vacuum/cleanup */
 1778 akapila@postgresql.o     1391                 :CBC          48 :     parallel_vacuum_process_safe_indexes(&pvs);
                               1392                 :                : 
                               1393                 :                :     /* Report buffer/WAL usage during parallel execution */
                               1394                 :             48 :     buffer_usage = shm_toc_lookup(toc, PARALLEL_VACUUM_KEY_BUFFER_USAGE, false);
                               1395                 :             48 :     wal_usage = shm_toc_lookup(toc, PARALLEL_VACUUM_KEY_WAL_USAGE, false);
                               1396                 :             48 :     InstrEndParallelQuery(&buffer_usage[ParallelWorkerNumber],
                               1397                 :             48 :                           &wal_usage[ParallelWorkerNumber]);
                               1398                 :                : 
                               1399                 :                :     /* Report any remaining cost-based vacuum delay time */
   31 msawada@postgresql.o     1400         [ +  + ]:             48 :     if (parallel_vacuum_worker_delay_ns > 0)
  632 nathan@postgresql.or     1401                 :              2 :         pgstat_progress_parallel_incr_param(PROGRESS_VACUUM_DELAY_TIME,
                               1402                 :                :                                             parallel_vacuum_worker_delay_ns);
                               1403                 :                : 
  947 msawada@postgresql.o     1404                 :             48 :     TidStoreDetach(dead_items);
                               1405                 :                : 
                               1406                 :                :     /* Pop the error context stack */
 1778 akapila@postgresql.o     1407                 :             48 :     error_context_stack = errcallback.previous;
                               1408                 :                : 
   34 michael@paquier.xyz      1409                 :GNC          48 :     pgstat_progress_end_command();
                               1410                 :                : 
 1778 akapila@postgresql.o     1411                 :CBC          48 :     vac_close_indexes(nindexes, indrels, RowExclusiveLock);
                               1412                 :             48 :     table_close(rel, ShareUpdateExclusiveLock);
                               1413                 :             48 :     FreeAccessStrategy(pvs.bstrategy);
                               1414                 :                : 
  213 msawada@postgresql.o     1415         [ +  + ]:             48 :     if (shared->is_autovacuum)
                               1416                 :              4 :         pv_shared_cost_params = NULL;
 1778 akapila@postgresql.o     1417                 :             48 : }
                               1418                 :                : 
                               1419                 :                : /*
                               1420                 :                :  * Error context callback for errors occurring during parallel index vacuum.
                               1421                 :                :  * The error context messages should match the messages set in the lazy vacuum
                               1422                 :                :  * error context.  If you change this function, change vacuum_error_callback()
                               1423                 :                :  * as well.
                               1424                 :                :  */
                               1425                 :                : static void
                               1426                 :              7 : parallel_vacuum_error_callback(void *arg)
                               1427                 :                : {
                               1428                 :              7 :     ParallelVacuumState *errinfo = arg;
                               1429                 :                : 
                               1430      [ +  -  - ]:              7 :     switch (errinfo->status)
                               1431                 :                :     {
                               1432                 :              7 :         case PARALLEL_INDVAC_STATUS_NEED_BULKDELETE:
                               1433                 :              7 :             errcontext("while vacuuming index \"%s\" of relation \"%s.%s\"",
                               1434                 :                :                        errinfo->indname,
                               1435                 :                :                        errinfo->relnamespace,
                               1436                 :                :                        errinfo->relname);
                               1437                 :              7 :             break;
 1778 akapila@postgresql.o     1438                 :UBC           0 :         case PARALLEL_INDVAC_STATUS_NEED_CLEANUP:
                               1439                 :              0 :             errcontext("while cleaning up index \"%s\" of relation \"%s.%s\"",
                               1440                 :                :                        errinfo->indname,
                               1441                 :                :                        errinfo->relnamespace,
                               1442                 :                :                        errinfo->relname);
                               1443                 :              0 :             break;
                               1444                 :              0 :         case PARALLEL_INDVAC_STATUS_INITIAL:
                               1445                 :                :         case PARALLEL_INDVAC_STATUS_COMPLETED:
                               1446                 :                :         default:
                               1447                 :              0 :             return;
                               1448                 :                :     }
                               1449                 :                : }
        

Generated by: LCOV version 2.0-1