LCOV - code coverage report
Current view: top level - src/backend/postmaster - autovacuum.c (source / functions) Coverage Total Hit
Test: PostgreSQL 19devel Lines: 81.4 % 1058 861
Test Date: 2026-04-16 23:16:28 Functions: 94.1 % 34 32
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /*-------------------------------------------------------------------------
       2              :  *
       3              :  * autovacuum.c
       4              :  *
       5              :  * PostgreSQL Integrated Autovacuum Daemon
       6              :  *
       7              :  * The autovacuum system is structured in two different kinds of processes: the
       8              :  * autovacuum launcher and the autovacuum worker.  The launcher is an
       9              :  * always-running process, started by the postmaster when the autovacuum GUC
      10              :  * parameter is set.  The launcher schedules autovacuum workers to be started
      11              :  * when appropriate.  The workers are the processes which execute the actual
      12              :  * vacuuming; they connect to a database as determined in the launcher, and
      13              :  * once connected they examine the catalogs to select the tables to vacuum.
      14              :  *
      15              :  * The autovacuum launcher cannot start the worker processes by itself,
      16              :  * because doing so would cause robustness issues (namely, failure to shut
      17              :  * them down on exceptional conditions, and also, since the launcher is
      18              :  * connected to shared memory and is thus subject to corruption there, it is
      19              :  * not as robust as the postmaster).  So it leaves that task to the postmaster.
      20              :  *
      21              :  * There is an autovacuum shared memory area, where the launcher stores
      22              :  * information about the database it wants vacuumed.  When it wants a new
      23              :  * worker to start, it sets a flag in shared memory and sends a signal to the
      24              :  * postmaster.  Then postmaster knows nothing more than it must start a worker;
      25              :  * so it forks a new child, which turns into a worker.  This new process
      26              :  * connects to shared memory, and there it can inspect the information that the
      27              :  * launcher has set up.
      28              :  *
      29              :  * If the fork() call fails in the postmaster, it sets a flag in the shared
      30              :  * memory area, and sends a signal to the launcher.  The launcher, upon
      31              :  * noticing the flag, can try starting the worker again by resending the
      32              :  * signal.  Note that the failure can only be transient (fork failure due to
      33              :  * high load, memory pressure, too many processes, etc); more permanent
      34              :  * problems, like failure to connect to a database, are detected later in the
      35              :  * worker and dealt with just by having the worker exit normally.  The launcher
      36              :  * will launch a new worker again later, per schedule.
      37              :  *
      38              :  * When the worker is done vacuuming it sends SIGUSR2 to the launcher.  The
      39              :  * launcher then wakes up and is able to launch another worker, if the schedule
      40              :  * is so tight that a new worker is needed immediately.  At this time the
      41              :  * launcher can also balance the settings for the various remaining workers'
      42              :  * cost-based vacuum delay feature.
      43              :  *
      44              :  * Note that there can be more than one worker in a database concurrently.
      45              :  * They will store the table they are currently vacuuming in shared memory, so
      46              :  * that other workers avoid being blocked waiting for the vacuum lock for that
      47              :  * table.  They will also fetch the last time the table was vacuumed from
      48              :  * pgstats just before vacuuming each table, to avoid vacuuming a table that
      49              :  * was just finished being vacuumed by another worker and thus is no longer
      50              :  * noted in shared memory.  However, there is a small window (due to not yet
      51              :  * holding the relation lock) during which a worker may choose a table that was
      52              :  * already vacuumed; this is a bug in the current design.
      53              :  *
      54              :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
      55              :  * Portions Copyright (c) 1994, Regents of the University of California
      56              :  *
      57              :  *
      58              :  * IDENTIFICATION
      59              :  *    src/backend/postmaster/autovacuum.c
      60              :  *
      61              :  *-------------------------------------------------------------------------
      62              :  */
      63              : #include "postgres.h"
      64              : 
      65              : #include <math.h>
      66              : #include <signal.h>
      67              : #include <sys/time.h>
      68              : #include <unistd.h>
      69              : 
      70              : #include "access/heapam.h"
      71              : #include "access/htup_details.h"
      72              : #include "access/multixact.h"
      73              : #include "access/reloptions.h"
      74              : #include "access/tableam.h"
      75              : #include "access/transam.h"
      76              : #include "access/xact.h"
      77              : #include "catalog/dependency.h"
      78              : #include "catalog/namespace.h"
      79              : #include "catalog/pg_database.h"
      80              : #include "catalog/pg_namespace.h"
      81              : #include "commands/vacuum.h"
      82              : #include "common/int.h"
      83              : #include "funcapi.h"
      84              : #include "lib/ilist.h"
      85              : #include "libpq/pqsignal.h"
      86              : #include "miscadmin.h"
      87              : #include "nodes/makefuncs.h"
      88              : #include "pgstat.h"
      89              : #include "postmaster/autovacuum.h"
      90              : #include "postmaster/interrupt.h"
      91              : #include "postmaster/postmaster.h"
      92              : #include "storage/aio_subsys.h"
      93              : #include "storage/bufmgr.h"
      94              : #include "storage/ipc.h"
      95              : #include "storage/fd.h"
      96              : #include "storage/latch.h"
      97              : #include "storage/lmgr.h"
      98              : #include "storage/pmsignal.h"
      99              : #include "storage/proc.h"
     100              : #include "storage/procsignal.h"
     101              : #include "storage/smgr.h"
     102              : #include "storage/subsystems.h"
     103              : #include "tcop/tcopprot.h"
     104              : #include "utils/fmgroids.h"
     105              : #include "utils/fmgrprotos.h"
     106              : #include "utils/guc_hooks.h"
     107              : #include "utils/injection_point.h"
     108              : #include "utils/lsyscache.h"
     109              : #include "utils/memutils.h"
     110              : #include "utils/ps_status.h"
     111              : #include "utils/rel.h"
     112              : #include "utils/snapmgr.h"
     113              : #include "utils/syscache.h"
     114              : #include "utils/timeout.h"
     115              : #include "utils/timestamp.h"
     116              : #include "utils/tuplestore.h"
     117              : #include "utils/wait_event.h"
     118              : 
     119              : 
     120              : /*
     121              :  * GUC parameters
     122              :  */
     123              : bool        autovacuum_start_daemon = false;
     124              : int         autovacuum_worker_slots;
     125              : int         autovacuum_max_workers;
     126              : int         autovacuum_work_mem = -1;
     127              : int         autovacuum_naptime;
     128              : int         autovacuum_vac_thresh;
     129              : int         autovacuum_vac_max_thresh;
     130              : double      autovacuum_vac_scale;
     131              : int         autovacuum_vac_ins_thresh;
     132              : double      autovacuum_vac_ins_scale;
     133              : int         autovacuum_anl_thresh;
     134              : double      autovacuum_anl_scale;
     135              : int         autovacuum_freeze_max_age;
     136              : int         autovacuum_multixact_freeze_max_age;
     137              : double      autovacuum_freeze_score_weight = 1.0;
     138              : double      autovacuum_multixact_freeze_score_weight = 1.0;
     139              : double      autovacuum_vacuum_score_weight = 1.0;
     140              : double      autovacuum_vacuum_insert_score_weight = 1.0;
     141              : double      autovacuum_analyze_score_weight = 1.0;
     142              : double      autovacuum_vac_cost_delay;
     143              : int         autovacuum_vac_cost_limit;
     144              : 
     145              : int         Log_autovacuum_min_duration = 600000;
     146              : int         Log_autoanalyze_min_duration = 600000;
     147              : 
     148              : /* the minimum allowed time between two awakenings of the launcher */
     149              : #define MIN_AUTOVAC_SLEEPTIME 100.0 /* milliseconds */
     150              : #define MAX_AUTOVAC_SLEEPTIME 300   /* seconds */
     151              : 
     152              : /*
     153              :  * Variables to save the cost-related storage parameters for the current
     154              :  * relation being vacuumed by this autovacuum worker. Using these, we can
     155              :  * ensure we don't overwrite the values of vacuum_cost_delay and
     156              :  * vacuum_cost_limit after reloading the configuration file. They are
     157              :  * initialized to "invalid" values to indicate that no cost-related storage
     158              :  * parameters were specified and will be set in do_autovacuum() after checking
     159              :  * the storage parameters in table_recheck_autovac().
     160              :  */
     161              : static double av_storage_param_cost_delay = -1;
     162              : static int  av_storage_param_cost_limit = -1;
     163              : 
     164              : /* Flags set by signal handlers */
     165              : static volatile sig_atomic_t got_SIGUSR2 = false;
     166              : 
     167              : /* Comparison points for determining whether freeze_max_age is exceeded */
     168              : static TransactionId recentXid;
     169              : static MultiXactId recentMulti;
     170              : 
     171              : /* Default freeze ages to use for autovacuum (varies by database) */
     172              : static int  default_freeze_min_age;
     173              : static int  default_freeze_table_age;
     174              : static int  default_multixact_freeze_min_age;
     175              : static int  default_multixact_freeze_table_age;
     176              : 
     177              : /* Memory context for long-lived data */
     178              : static MemoryContext AutovacMemCxt;
     179              : 
     180              : /* struct to keep track of databases in launcher */
     181              : typedef struct avl_dbase
     182              : {
     183              :     Oid         adl_datid;      /* hash key -- must be first */
     184              :     TimestampTz adl_next_worker;
     185              :     int         adl_score;
     186              :     dlist_node  adl_node;
     187              : } avl_dbase;
     188              : 
     189              : /* struct to keep track of databases in worker */
     190              : typedef struct avw_dbase
     191              : {
     192              :     Oid         adw_datid;
     193              :     char       *adw_name;
     194              :     TransactionId adw_frozenxid;
     195              :     MultiXactId adw_minmulti;
     196              :     PgStat_StatDBEntry *adw_entry;
     197              : } avw_dbase;
     198              : 
     199              : /* struct to keep track of tables to vacuum and/or analyze, in 1st pass */
     200              : typedef struct av_relation
     201              : {
     202              :     Oid         ar_toastrelid;  /* hash key - must be first */
     203              :     Oid         ar_relid;
     204              :     bool        ar_hasrelopts;
     205              :     AutoVacOpts ar_reloptions;  /* copy of AutoVacOpts from the main table's
     206              :                                  * reloptions, or NULL if none */
     207              : } av_relation;
     208              : 
     209              : /* struct to keep track of tables to vacuum and/or analyze, after rechecking */
     210              : typedef struct autovac_table
     211              : {
     212              :     Oid         at_relid;
     213              :     VacuumParams at_params;
     214              :     double      at_storage_param_vac_cost_delay;
     215              :     int         at_storage_param_vac_cost_limit;
     216              :     bool        at_dobalance;
     217              :     char       *at_relname;
     218              :     char       *at_nspname;
     219              :     char       *at_datname;
     220              : } autovac_table;
     221              : 
     222              : /*-------------
     223              :  * This struct holds information about a single worker's whereabouts.  We keep
     224              :  * an array of these in shared memory, sized according to
     225              :  * autovacuum_worker_slots.
     226              :  *
     227              :  * wi_links     entry into free list or running list
     228              :  * wi_dboid     OID of the database this worker is supposed to work on
     229              :  * wi_tableoid  OID of the table currently being vacuumed, if any
     230              :  * wi_sharedrel flag indicating whether table is marked relisshared
     231              :  * wi_proc      pointer to PGPROC of the running worker, NULL if not started
     232              :  * wi_launchtime Time at which this worker was launched
     233              :  * wi_dobalance Whether this worker should be included in balance calculations
     234              :  *
     235              :  * All fields are protected by AutovacuumLock, except for wi_tableoid and
     236              :  * wi_sharedrel which are protected by AutovacuumScheduleLock (note these
     237              :  * two fields are read-only for everyone except that worker itself).
     238              :  *-------------
     239              :  */
     240              : typedef struct WorkerInfoData
     241              : {
     242              :     dlist_node  wi_links;
     243              :     Oid         wi_dboid;
     244              :     Oid         wi_tableoid;
     245              :     PGPROC     *wi_proc;
     246              :     TimestampTz wi_launchtime;
     247              :     pg_atomic_flag wi_dobalance;
     248              :     bool        wi_sharedrel;
     249              : } WorkerInfoData;
     250              : 
     251              : typedef struct WorkerInfoData *WorkerInfo;
     252              : 
     253              : /*
     254              :  * Possible signals received by the launcher from remote processes.  These are
     255              :  * stored atomically in shared memory so that other processes can set them
     256              :  * without locking.
     257              :  */
     258              : typedef enum
     259              : {
     260              :     AutoVacForkFailed,          /* failed trying to start a worker */
     261              :     AutoVacRebalance,           /* rebalance the cost limits */
     262              : }           AutoVacuumSignal;
     263              : 
     264              : #define AutoVacNumSignals (AutoVacRebalance + 1)
     265              : 
     266              : /*
     267              :  * Autovacuum workitem array, stored in AutoVacuumShmem->av_workItems.  This
     268              :  * list is mostly protected by AutovacuumLock, except that if an item is
     269              :  * marked 'active' other processes must not modify the work-identifying
     270              :  * members.
     271              :  */
     272              : typedef struct AutoVacuumWorkItem
     273              : {
     274              :     AutoVacuumWorkItemType avw_type;
     275              :     bool        avw_used;       /* below data is valid */
     276              :     bool        avw_active;     /* being processed */
     277              :     Oid         avw_database;
     278              :     Oid         avw_relation;
     279              :     BlockNumber avw_blockNumber;
     280              : } AutoVacuumWorkItem;
     281              : 
     282              : #define NUM_WORKITEMS   256
     283              : 
     284              : /*-------------
     285              :  * The main autovacuum shmem struct.  On shared memory we store this main
     286              :  * struct and the array of WorkerInfo structs.  This struct keeps:
     287              :  *
     288              :  * av_signal        set by other processes to indicate various conditions
     289              :  * av_launcherpid   the PID of the autovacuum launcher
     290              :  * av_freeWorkers   the WorkerInfo freelist
     291              :  * av_runningWorkers the WorkerInfo non-free queue
     292              :  * av_startingWorker pointer to WorkerInfo currently being started (cleared by
     293              :  *                  the worker itself as soon as it's up and running)
     294              :  * av_workItems     work item array
     295              :  * av_nworkersForBalance the number of autovacuum workers to use when
     296              :  *                  calculating the per worker cost limit
     297              :  *
     298              :  * This struct is protected by AutovacuumLock, except for av_signal and parts
     299              :  * of the worker list (see above).
     300              :  *-------------
     301              :  */
     302              : typedef struct
     303              : {
     304              :     sig_atomic_t av_signal[AutoVacNumSignals];
     305              :     pid_t       av_launcherpid;
     306              :     dclist_head av_freeWorkers;
     307              :     dlist_head  av_runningWorkers;
     308              :     WorkerInfo  av_startingWorker;
     309              :     AutoVacuumWorkItem av_workItems[NUM_WORKITEMS];
     310              :     pg_atomic_uint32 av_nworkersForBalance;
     311              : } AutoVacuumShmemStruct;
     312              : 
     313              : static AutoVacuumShmemStruct *AutoVacuumShmem;
     314              : 
     315              : static void AutoVacuumShmemRequest(void *arg);
     316              : static void AutoVacuumShmemInit(void *arg);
     317              : 
     318              : const ShmemCallbacks AutoVacuumShmemCallbacks = {
     319              :     .request_fn = AutoVacuumShmemRequest,
     320              :     .init_fn = AutoVacuumShmemInit,
     321              : };
     322              : 
     323              : /*
     324              :  * the database list (of avl_dbase elements) in the launcher, and the context
     325              :  * that contains it
     326              :  */
     327              : static dlist_head DatabaseList = DLIST_STATIC_INIT(DatabaseList);
     328              : static MemoryContext DatabaseListCxt = NULL;
     329              : 
     330              : /*
     331              :  * This struct is used by relation_needs_vacanalyze() to return the table's
     332              :  * score (i.e., the maximum of the component scores) as well as the component
     333              :  * scores themselves.
     334              :  */
     335              : typedef struct
     336              : {
     337              :     double      max;            /* maximum of all values below */
     338              :     double      xid;            /* transaction ID component */
     339              :     double      mxid;           /* multixact ID component */
     340              :     double      vac;            /* vacuum component */
     341              :     double      vac_ins;        /* vacuum insert component */
     342              :     double      anl;            /* analyze component */
     343              : } AutoVacuumScores;
     344              : 
     345              : /*
     346              :  * This struct is used to track and sort the list of tables to process.
     347              :  */
     348              : typedef struct
     349              : {
     350              :     Oid         oid;
     351              :     double      score;
     352              : } TableToProcess;
     353              : 
     354              : /*
     355              :  * Dummy pointer to persuade Valgrind that we've not leaked the array of
     356              :  * avl_dbase structs.  Make it global to ensure the compiler doesn't
     357              :  * optimize it away.
     358              :  */
     359              : #ifdef USE_VALGRIND
     360              : extern avl_dbase *avl_dbase_array;
     361              : avl_dbase  *avl_dbase_array;
     362              : #endif
     363              : 
     364              : /* Pointer to my own WorkerInfo, valid on each worker */
     365              : static WorkerInfo MyWorkerInfo = NULL;
     366              : 
     367              : static Oid  do_start_worker(void);
     368              : static void ProcessAutoVacLauncherInterrupts(void);
     369              : pg_noreturn static void AutoVacLauncherShutdown(void);
     370              : static void launcher_determine_sleep(bool canlaunch, bool recursing,
     371              :                                      struct timeval *nap);
     372              : static void launch_worker(TimestampTz now);
     373              : static List *get_database_list(void);
     374              : static void rebuild_database_list(Oid newdb);
     375              : static int  db_comparator(const void *a, const void *b);
     376              : static void autovac_recalculate_workers_for_balance(void);
     377              : 
     378              : static void do_autovacuum(void);
     379              : static void FreeWorkerInfo(int code, Datum arg);
     380              : 
     381              : static autovac_table *table_recheck_autovac(Oid relid, HTAB *table_toast_map,
     382              :                                             TupleDesc pg_class_desc,
     383              :                                             int effective_multixact_freeze_max_age);
     384              : static void relation_needs_vacanalyze(Oid relid, AutoVacOpts *relopts,
     385              :                                       Form_pg_class classForm,
     386              :                                       int effective_multixact_freeze_max_age,
     387              :                                       int elevel,
     388              :                                       bool *dovacuum, bool *doanalyze, bool *wraparound,
     389              :                                       AutoVacuumScores *scores);
     390              : 
     391              : static void autovacuum_do_vac_analyze(autovac_table *tab,
     392              :                                       BufferAccessStrategy bstrategy);
     393              : static AutoVacOpts *extract_autovac_opts(HeapTuple tup,
     394              :                                          TupleDesc pg_class_desc);
     395              : static void perform_work_item(AutoVacuumWorkItem *workitem);
     396              : static void autovac_report_activity(autovac_table *tab);
     397              : static void autovac_report_workitem(AutoVacuumWorkItem *workitem,
     398              :                                     const char *nspname, const char *relname);
     399              : static void avl_sigusr2_handler(SIGNAL_ARGS);
     400              : static bool av_worker_available(void);
     401              : static void check_av_worker_gucs(void);
     402              : 
     403              : 
     404              : 
     405              : /********************************************************************
     406              :  *                    AUTOVACUUM LAUNCHER CODE
     407              :  ********************************************************************/
     408              : 
     409              : /*
     410              :  * Main entry point for the autovacuum launcher process.
     411              :  */
     412              : void
     413          482 : AutoVacLauncherMain(const void *startup_data, size_t startup_data_len)
     414              : {
     415              :     sigjmp_buf  local_sigjmp_buf;
     416              : 
     417              :     Assert(startup_data_len == 0);
     418              : 
     419              :     /* Release postmaster's working memory context */
     420          482 :     if (PostmasterContext)
     421              :     {
     422          482 :         MemoryContextDelete(PostmasterContext);
     423          482 :         PostmasterContext = NULL;
     424              :     }
     425              : 
     426          482 :     init_ps_display(NULL);
     427              : 
     428          482 :     ereport(DEBUG1,
     429              :             (errmsg_internal("autovacuum launcher started")));
     430              : 
     431          482 :     if (PostAuthDelay)
     432            0 :         pg_usleep(PostAuthDelay * 1000000L);
     433              : 
     434              :     Assert(GetProcessingMode() == InitProcessing);
     435              : 
     436              :     /*
     437              :      * Set up signal handlers.  We operate on databases much like a regular
     438              :      * backend, so we use the same signal handling.  See equivalent code in
     439              :      * tcop/postgres.c.
     440              :      */
     441          482 :     pqsignal(SIGHUP, SignalHandlerForConfigReload);
     442          482 :     pqsignal(SIGINT, StatementCancelHandler);
     443          482 :     pqsignal(SIGTERM, SignalHandlerForShutdownRequest);
     444              :     /* SIGQUIT handler was already set up by InitPostmasterChild */
     445              : 
     446          482 :     InitializeTimeouts();       /* establishes SIGALRM handler */
     447              : 
     448          482 :     pqsignal(SIGPIPE, PG_SIG_IGN);
     449          482 :     pqsignal(SIGUSR1, procsignal_sigusr1_handler);
     450          482 :     pqsignal(SIGUSR2, avl_sigusr2_handler);
     451          482 :     pqsignal(SIGFPE, FloatExceptionHandler);
     452          482 :     pqsignal(SIGCHLD, PG_SIG_DFL);
     453              : 
     454              :     /*
     455              :      * Create a per-backend PGPROC struct in shared memory.  We must do this
     456              :      * before we can use LWLocks or access any shared memory.
     457              :      */
     458          482 :     InitProcess();
     459              : 
     460              :     /* Early initialization */
     461          482 :     BaseInit();
     462              : 
     463          482 :     InitPostgres(NULL, InvalidOid, NULL, InvalidOid, 0, NULL);
     464              : 
     465          482 :     SetProcessingMode(NormalProcessing);
     466              : 
     467              :     /*
     468              :      * Create a memory context that we will do all our work in.  We do this so
     469              :      * that we can reset the context during error recovery and thereby avoid
     470              :      * possible memory leaks.
     471              :      */
     472          482 :     AutovacMemCxt = AllocSetContextCreate(TopMemoryContext,
     473              :                                           "Autovacuum Launcher",
     474              :                                           ALLOCSET_DEFAULT_SIZES);
     475          482 :     MemoryContextSwitchTo(AutovacMemCxt);
     476              : 
     477              :     /*
     478              :      * If an exception is encountered, processing resumes here.
     479              :      *
     480              :      * This code is a stripped down version of PostgresMain error recovery.
     481              :      *
     482              :      * Note that we use sigsetjmp(..., 1), so that the prevailing signal mask
     483              :      * (to wit, BlockSig) will be restored when longjmp'ing to here.  Thus,
     484              :      * signals other than SIGQUIT will be blocked until we complete error
     485              :      * recovery.  It might seem that this policy makes the HOLD_INTERRUPTS()
     486              :      * call redundant, but it is not since InterruptPending might be set
     487              :      * already.
     488              :      */
     489          482 :     if (sigsetjmp(local_sigjmp_buf, 1) != 0)
     490              :     {
     491              :         /* since not using PG_TRY, must reset error stack by hand */
     492            0 :         error_context_stack = NULL;
     493              : 
     494              :         /* Prevents interrupts while cleaning up */
     495            0 :         HOLD_INTERRUPTS();
     496              : 
     497              :         /* Forget any pending QueryCancel or timeout request */
     498            0 :         disable_all_timeouts(false);
     499            0 :         QueryCancelPending = false; /* second to avoid race condition */
     500              : 
     501              :         /* Report the error to the server log */
     502            0 :         EmitErrorReport();
     503              : 
     504              :         /* Abort the current transaction in order to recover */
     505            0 :         AbortCurrentTransaction();
     506              : 
     507              :         /*
     508              :          * Release any other resources, for the case where we were not in a
     509              :          * transaction.
     510              :          */
     511            0 :         LWLockReleaseAll();
     512            0 :         pgstat_report_wait_end();
     513            0 :         pgaio_error_cleanup();
     514            0 :         UnlockBuffers();
     515              :         /* this is probably dead code, but let's be safe: */
     516            0 :         if (AuxProcessResourceOwner)
     517            0 :             ReleaseAuxProcessResources(false);
     518            0 :         AtEOXact_Buffers(false);
     519            0 :         AtEOXact_SMgr();
     520            0 :         AtEOXact_Files(false);
     521            0 :         AtEOXact_HashTables(false);
     522              : 
     523              :         /*
     524              :          * Now return to normal top-level context and clear ErrorContext for
     525              :          * next time.
     526              :          */
     527            0 :         MemoryContextSwitchTo(AutovacMemCxt);
     528            0 :         FlushErrorState();
     529              : 
     530              :         /* Flush any leaked data in the top-level context */
     531            0 :         MemoryContextReset(AutovacMemCxt);
     532              : 
     533              :         /* don't leave dangling pointers to freed memory */
     534            0 :         DatabaseListCxt = NULL;
     535            0 :         dlist_init(&DatabaseList);
     536              : 
     537              :         /* Now we can allow interrupts again */
     538            0 :         RESUME_INTERRUPTS();
     539              : 
     540              :         /* if in shutdown mode, no need for anything further; just go away */
     541            0 :         if (ShutdownRequestPending)
     542            0 :             AutoVacLauncherShutdown();
     543              : 
     544              :         /*
     545              :          * Sleep at least 1 second after any error.  We don't want to be
     546              :          * filling the error logs as fast as we can.
     547              :          */
     548            0 :         pg_usleep(1000000L);
     549              :     }
     550              : 
     551              :     /* We can now handle ereport(ERROR) */
     552          482 :     PG_exception_stack = &local_sigjmp_buf;
     553              : 
     554              :     /* must unblock signals before calling rebuild_database_list */
     555          482 :     sigprocmask(SIG_SETMASK, &UnBlockSig, NULL);
     556              : 
     557              :     /*
     558              :      * Set always-secure search path.  Launcher doesn't connect to a database,
     559              :      * so this has no effect.
     560              :      */
     561          482 :     SetConfigOption("search_path", "", PGC_SUSET, PGC_S_OVERRIDE);
     562              : 
     563              :     /*
     564              :      * Force zero_damaged_pages OFF in the autovac process, even if it is set
     565              :      * in postgresql.conf.  We don't really want such a dangerous option being
     566              :      * applied non-interactively.
     567              :      */
     568          482 :     SetConfigOption("zero_damaged_pages", "false", PGC_SUSET, PGC_S_OVERRIDE);
     569              : 
     570              :     /*
     571              :      * Force settable timeouts off to avoid letting these settings prevent
     572              :      * regular maintenance from being executed.
     573              :      */
     574          482 :     SetConfigOption("statement_timeout", "0", PGC_SUSET, PGC_S_OVERRIDE);
     575          482 :     SetConfigOption("transaction_timeout", "0", PGC_SUSET, PGC_S_OVERRIDE);
     576          482 :     SetConfigOption("lock_timeout", "0", PGC_SUSET, PGC_S_OVERRIDE);
     577          482 :     SetConfigOption("idle_in_transaction_session_timeout", "0",
     578              :                     PGC_SUSET, PGC_S_OVERRIDE);
     579              : 
     580              :     /*
     581              :      * Force default_transaction_isolation to READ COMMITTED.  We don't want
     582              :      * to pay the overhead of serializable mode, nor add any risk of causing
     583              :      * deadlocks or delaying other transactions.
     584              :      */
     585          482 :     SetConfigOption("default_transaction_isolation", "read committed",
     586              :                     PGC_SUSET, PGC_S_OVERRIDE);
     587              : 
     588              :     /*
     589              :      * Even when system is configured to use a different fetch consistency,
     590              :      * for autovac we always want fresh stats.
     591              :      */
     592          482 :     SetConfigOption("stats_fetch_consistency", "none", PGC_SUSET, PGC_S_OVERRIDE);
     593              : 
     594              :     /*
     595              :      * In emergency mode, just start a worker (unless shutdown was requested)
     596              :      * and go away.
     597              :      */
     598          482 :     if (!AutoVacuumingActive())
     599              :     {
     600            0 :         if (!ShutdownRequestPending)
     601            0 :             do_start_worker();
     602            0 :         proc_exit(0);           /* done */
     603              :     }
     604              : 
     605          482 :     AutoVacuumShmem->av_launcherpid = MyProcPid;
     606              : 
     607              :     /*
     608              :      * Create the initial database list.  The invariant we want this list to
     609              :      * keep is that it's ordered by decreasing next_worker.  As soon as an
     610              :      * entry is updated to a higher time, it will be moved to the front (which
     611              :      * is correct because the only operation is to add autovacuum_naptime to
     612              :      * the entry, and time always increases).
     613              :      */
     614          482 :     rebuild_database_list(InvalidOid);
     615              : 
     616              :     /* loop until shutdown request */
     617         5294 :     while (!ShutdownRequestPending)
     618              :     {
     619              :         struct timeval nap;
     620         5293 :         TimestampTz current_time = 0;
     621              :         bool        can_launch;
     622              : 
     623              :         /*
     624              :          * This loop is a bit different from the normal use of WaitLatch,
     625              :          * because we'd like to sleep before the first launch of a child
     626              :          * process.  So it's WaitLatch, then ResetLatch, then check for
     627              :          * wakening conditions.
     628              :          */
     629              : 
     630         5293 :         launcher_determine_sleep(av_worker_available(), false, &nap);
     631              : 
     632              :         /*
     633              :          * Wait until naptime expires or we get some type of signal (all the
     634              :          * signal handlers will wake us by calling SetLatch).
     635              :          */
     636         5293 :         (void) WaitLatch(MyLatch,
     637              :                          WL_LATCH_SET | WL_TIMEOUT | WL_EXIT_ON_PM_DEATH,
     638         5293 :                          (nap.tv_sec * 1000L) + (nap.tv_usec / 1000L),
     639              :                          WAIT_EVENT_AUTOVACUUM_MAIN);
     640              : 
     641         5290 :         ResetLatch(MyLatch);
     642              : 
     643         5290 :         ProcessAutoVacLauncherInterrupts();
     644              : 
     645              :         /*
     646              :          * a worker finished, or postmaster signaled failure to start a worker
     647              :          */
     648         4812 :         if (got_SIGUSR2)
     649              :         {
     650         2831 :             got_SIGUSR2 = false;
     651              : 
     652              :             /* rebalance cost limits, if needed */
     653         2831 :             if (AutoVacuumShmem->av_signal[AutoVacRebalance])
     654              :             {
     655         1380 :                 LWLockAcquire(AutovacuumLock, LW_EXCLUSIVE);
     656         1380 :                 AutoVacuumShmem->av_signal[AutoVacRebalance] = false;
     657         1380 :                 autovac_recalculate_workers_for_balance();
     658         1380 :                 LWLockRelease(AutovacuumLock);
     659              :             }
     660              : 
     661         2831 :             if (AutoVacuumShmem->av_signal[AutoVacForkFailed])
     662              :             {
     663              :                 /*
     664              :                  * If the postmaster failed to start a new worker, we sleep
     665              :                  * for a little while and resend the signal.  The new worker's
     666              :                  * state is still in memory, so this is sufficient.  After
     667              :                  * that, we restart the main loop.
     668              :                  *
     669              :                  * XXX should we put a limit to the number of times we retry?
     670              :                  * I don't think it makes much sense, because a future start
     671              :                  * of a worker will continue to fail in the same way.
     672              :                  */
     673            0 :                 AutoVacuumShmem->av_signal[AutoVacForkFailed] = false;
     674            0 :                 pg_usleep(1000000L);    /* 1s */
     675            0 :                 SendPostmasterSignal(PMSIGNAL_START_AUTOVAC_WORKER);
     676         1512 :                 continue;
     677              :             }
     678              :         }
     679              : 
     680              :         /*
     681              :          * There are some conditions that we need to check before trying to
     682              :          * start a worker.  First, we need to make sure that there is a worker
     683              :          * slot available.  Second, we need to make sure that no other worker
     684              :          * failed while starting up.
     685              :          */
     686              : 
     687         4812 :         current_time = GetCurrentTimestamp();
     688         4812 :         LWLockAcquire(AutovacuumLock, LW_SHARED);
     689              : 
     690         4812 :         can_launch = av_worker_available();
     691              : 
     692         4812 :         if (AutoVacuumShmem->av_startingWorker != NULL)
     693              :         {
     694              :             int         waittime;
     695           19 :             WorkerInfo  worker = AutoVacuumShmem->av_startingWorker;
     696              : 
     697              :             /*
     698              :              * We can't launch another worker when another one is still
     699              :              * starting up (or failed while doing so), so just sleep for a bit
     700              :              * more; that worker will wake us up again as soon as it's ready.
     701              :              * We will only wait autovacuum_naptime seconds (up to a maximum
     702              :              * of 60 seconds) for this to happen however.  Note that failure
     703              :              * to connect to a particular database is not a problem here,
     704              :              * because the worker removes itself from the startingWorker
     705              :              * pointer before trying to connect.  Problems detected by the
     706              :              * postmaster (like fork() failure) are also reported and handled
     707              :              * differently.  The only problems that may cause this code to
     708              :              * fire are errors in the earlier sections of AutoVacWorkerMain,
     709              :              * before the worker removes the WorkerInfo from the
     710              :              * startingWorker pointer.
     711              :              */
     712           19 :             waittime = Min(autovacuum_naptime, 60) * 1000;
     713           19 :             if (TimestampDifferenceExceeds(worker->wi_launchtime, current_time,
     714              :                                            waittime))
     715              :             {
     716            0 :                 LWLockRelease(AutovacuumLock);
     717            0 :                 LWLockAcquire(AutovacuumLock, LW_EXCLUSIVE);
     718              : 
     719              :                 /*
     720              :                  * No other process can put a worker in starting mode, so if
     721              :                  * startingWorker is still INVALID after exchanging our lock,
     722              :                  * we assume it's the same one we saw above (so we don't
     723              :                  * recheck the launch time).
     724              :                  */
     725            0 :                 if (AutoVacuumShmem->av_startingWorker != NULL)
     726              :                 {
     727            0 :                     worker = AutoVacuumShmem->av_startingWorker;
     728            0 :                     worker->wi_dboid = InvalidOid;
     729            0 :                     worker->wi_tableoid = InvalidOid;
     730            0 :                     worker->wi_sharedrel = false;
     731            0 :                     worker->wi_proc = NULL;
     732            0 :                     worker->wi_launchtime = 0;
     733            0 :                     dclist_push_head(&AutoVacuumShmem->av_freeWorkers,
     734              :                                      &worker->wi_links);
     735            0 :                     AutoVacuumShmem->av_startingWorker = NULL;
     736            0 :                     ereport(WARNING,
     737              :                             errmsg("autovacuum worker took too long to start; canceled"));
     738              :                 }
     739              :             }
     740              :             else
     741           19 :                 can_launch = false;
     742              :         }
     743         4812 :         LWLockRelease(AutovacuumLock);  /* either shared or exclusive */
     744              : 
     745              :         /* if we can't do anything, just go back to sleep */
     746         4812 :         if (!can_launch)
     747         1512 :             continue;
     748              : 
     749              :         /* We're OK to start a new worker */
     750              : 
     751         3300 :         if (dlist_is_empty(&DatabaseList))
     752              :         {
     753              :             /*
     754              :              * Special case when the list is empty: start a worker right away.
     755              :              * This covers the initial case, when no database is in pgstats
     756              :              * (thus the list is empty).  Note that the constraints in
     757              :              * launcher_determine_sleep keep us from starting workers too
     758              :              * quickly (at most once every autovacuum_naptime when the list is
     759              :              * empty).
     760              :              */
     761            1 :             launch_worker(current_time);
     762              :         }
     763              :         else
     764              :         {
     765              :             /*
     766              :              * because rebuild_database_list constructs a list with most
     767              :              * distant adl_next_worker first, we obtain our database from the
     768              :              * tail of the list.
     769              :              */
     770              :             avl_dbase  *avdb;
     771              : 
     772         3299 :             avdb = dlist_tail_element(avl_dbase, adl_node, &DatabaseList);
     773              : 
     774              :             /*
     775              :              * launch a worker if next_worker is right now or it is in the
     776              :              * past
     777              :              */
     778         3299 :             if (TimestampDifferenceExceeds(avdb->adl_next_worker,
     779              :                                            current_time, 0))
     780         1418 :                 launch_worker(current_time);
     781              :         }
     782              :     }
     783              : 
     784            1 :     AutoVacLauncherShutdown();
     785              : }
     786              : 
     787              : /*
     788              :  * Process any new interrupts.
     789              :  */
     790              : static void
     791         5290 : ProcessAutoVacLauncherInterrupts(void)
     792              : {
     793              :     /* the normal shutdown case */
     794         5290 :     if (ShutdownRequestPending)
     795          477 :         AutoVacLauncherShutdown();
     796              : 
     797         4813 :     if (ConfigReloadPending)
     798              :     {
     799           52 :         int         autovacuum_max_workers_prev = autovacuum_max_workers;
     800              : 
     801           52 :         ConfigReloadPending = false;
     802           52 :         ProcessConfigFile(PGC_SIGHUP);
     803              : 
     804              :         /* shutdown requested in config file? */
     805           52 :         if (!AutoVacuumingActive())
     806            1 :             AutoVacLauncherShutdown();
     807              : 
     808              :         /*
     809              :          * If autovacuum_max_workers changed, emit a WARNING if
     810              :          * autovacuum_worker_slots < autovacuum_max_workers.  If it didn't
     811              :          * change, skip this to avoid too many repeated log messages.
     812              :          */
     813           51 :         if (autovacuum_max_workers_prev != autovacuum_max_workers)
     814            0 :             check_av_worker_gucs();
     815              : 
     816              :         /* rebuild the list in case the naptime changed */
     817           51 :         rebuild_database_list(InvalidOid);
     818              :     }
     819              : 
     820              :     /* Process barrier events */
     821         4812 :     if (ProcSignalBarrierPending)
     822           73 :         ProcessProcSignalBarrier();
     823              : 
     824              :     /* Perform logging of memory contexts of this process */
     825         4812 :     if (LogMemoryContextPending)
     826            0 :         ProcessLogMemoryContextInterrupt();
     827              : 
     828              :     /* Process sinval catchup interrupts that happened while sleeping */
     829         4812 :     ProcessCatchupInterrupt();
     830         4812 : }
     831              : 
     832              : /*
     833              :  * Perform a normal exit from the autovac launcher.
     834              :  */
     835              : static void
     836          479 : AutoVacLauncherShutdown(void)
     837              : {
     838          479 :     ereport(DEBUG1,
     839              :             (errmsg_internal("autovacuum launcher shutting down")));
     840          479 :     AutoVacuumShmem->av_launcherpid = 0;
     841              : 
     842          479 :     proc_exit(0);               /* done */
     843              : }
     844              : 
     845              : /*
     846              :  * Determine the time to sleep, based on the database list.
     847              :  *
     848              :  * The "canlaunch" parameter indicates whether we can start a worker right now,
     849              :  * for example due to the workers being all busy.  If this is false, we will
     850              :  * cause a long sleep, which will be interrupted when a worker exits.
     851              :  */
     852              : static void
     853         5365 : launcher_determine_sleep(bool canlaunch, bool recursing, struct timeval *nap)
     854              : {
     855              :     /*
     856              :      * We sleep until the next scheduled vacuum.  We trust that when the
     857              :      * database list was built, care was taken so that no entries have times
     858              :      * in the past; if the first entry has too close a next_worker value, or a
     859              :      * time in the past, we will sleep a small nominal time.
     860              :      */
     861         5365 :     if (!canlaunch)
     862              :     {
     863         2774 :         nap->tv_sec = autovacuum_naptime;
     864         2774 :         nap->tv_usec = 0;
     865              :     }
     866         2591 :     else if (!dlist_is_empty(&DatabaseList))
     867              :     {
     868         2569 :         TimestampTz current_time = GetCurrentTimestamp();
     869              :         TimestampTz next_wakeup;
     870              :         avl_dbase  *avdb;
     871              :         long        secs;
     872              :         int         usecs;
     873              : 
     874         2569 :         avdb = dlist_tail_element(avl_dbase, adl_node, &DatabaseList);
     875              : 
     876         2569 :         next_wakeup = avdb->adl_next_worker;
     877         2569 :         TimestampDifference(current_time, next_wakeup, &secs, &usecs);
     878              : 
     879         2569 :         nap->tv_sec = secs;
     880         2569 :         nap->tv_usec = usecs;
     881              :     }
     882              :     else
     883              :     {
     884              :         /* list is empty, sleep for whole autovacuum_naptime seconds  */
     885           22 :         nap->tv_sec = autovacuum_naptime;
     886           22 :         nap->tv_usec = 0;
     887              :     }
     888              : 
     889              :     /*
     890              :      * If the result is exactly zero, it means a database had an entry with
     891              :      * time in the past.  Rebuild the list so that the databases are evenly
     892              :      * distributed again, and recalculate the time to sleep.  This can happen
     893              :      * if there are more tables needing vacuum than workers, and they all take
     894              :      * longer to vacuum than autovacuum_naptime.
     895              :      *
     896              :      * We only recurse once.  rebuild_database_list should always return times
     897              :      * in the future, but it seems best not to trust too much on that.
     898              :      */
     899         5365 :     if (nap->tv_sec == 0 && nap->tv_usec == 0 && !recursing)
     900              :     {
     901           72 :         rebuild_database_list(InvalidOid);
     902           72 :         launcher_determine_sleep(canlaunch, true, nap);
     903           72 :         return;
     904              :     }
     905              : 
     906              :     /* The smallest time we'll allow the launcher to sleep. */
     907         5293 :     if (nap->tv_sec <= 0 && nap->tv_usec <= MIN_AUTOVAC_SLEEPTIME * 1000)
     908              :     {
     909          211 :         nap->tv_sec = 0;
     910          211 :         nap->tv_usec = MIN_AUTOVAC_SLEEPTIME * 1000;
     911              :     }
     912              : 
     913              :     /*
     914              :      * If the sleep time is too large, clamp it to an arbitrary maximum (plus
     915              :      * any fractional seconds, for simplicity).  This avoids an essentially
     916              :      * infinite sleep in strange cases like the system clock going backwards a
     917              :      * few years.
     918              :      */
     919         5293 :     if (nap->tv_sec > MAX_AUTOVAC_SLEEPTIME)
     920            9 :         nap->tv_sec = MAX_AUTOVAC_SLEEPTIME;
     921              : }
     922              : 
     923              : /*
     924              :  * Build an updated DatabaseList.  It must only contain databases that appear
     925              :  * in pgstats, and must be sorted by next_worker from highest to lowest,
     926              :  * distributed regularly across the next autovacuum_naptime interval.
     927              :  *
     928              :  * Receives the Oid of the database that made this list be generated (we call
     929              :  * this the "new" database, because when the database was already present on
     930              :  * the list, we expect that this function is not called at all).  The
     931              :  * preexisting list, if any, will be used to preserve the order of the
     932              :  * databases in the autovacuum_naptime period.  The new database is put at the
     933              :  * end of the interval.  The actual values are not saved, which should not be
     934              :  * much of a problem.
     935              :  */
     936              : static void
     937          622 : rebuild_database_list(Oid newdb)
     938              : {
     939              :     List       *dblist;
     940              :     ListCell   *cell;
     941              :     MemoryContext newcxt;
     942              :     MemoryContext oldcxt;
     943              :     MemoryContext tmpcxt;
     944              :     HASHCTL     hctl;
     945              :     int         score;
     946              :     int         nelems;
     947              :     HTAB       *dbhash;
     948              :     dlist_iter  iter;
     949              : 
     950          622 :     newcxt = AllocSetContextCreate(AutovacMemCxt,
     951              :                                    "Autovacuum database list",
     952              :                                    ALLOCSET_DEFAULT_SIZES);
     953          622 :     tmpcxt = AllocSetContextCreate(newcxt,
     954              :                                    "Autovacuum database list (tmp)",
     955              :                                    ALLOCSET_DEFAULT_SIZES);
     956          622 :     oldcxt = MemoryContextSwitchTo(tmpcxt);
     957              : 
     958              :     /*
     959              :      * Implementing this is not as simple as it sounds, because we need to put
     960              :      * the new database at the end of the list; next the databases that were
     961              :      * already on the list, and finally (at the tail of the list) all the
     962              :      * other databases that are not on the existing list.
     963              :      *
     964              :      * To do this, we build an empty hash table of scored databases.  We will
     965              :      * start with the lowest score (zero) for the new database, then
     966              :      * increasing scores for the databases in the existing list, in order, and
     967              :      * lastly increasing scores for all databases gotten via
     968              :      * get_database_list() that are not already on the hash.
     969              :      *
     970              :      * Then we will put all the hash elements into an array, sort the array by
     971              :      * score, and finally put the array elements into the new doubly linked
     972              :      * list.
     973              :      */
     974          622 :     hctl.keysize = sizeof(Oid);
     975          622 :     hctl.entrysize = sizeof(avl_dbase);
     976          622 :     hctl.hcxt = tmpcxt;
     977          622 :     dbhash = hash_create("autovacuum db hash", 20, &hctl, /* magic number here
     978              :                                                              * FIXME */
     979              :                          HASH_ELEM | HASH_BLOBS | HASH_CONTEXT);
     980              : 
     981              :     /* start by inserting the new database */
     982          622 :     score = 0;
     983          622 :     if (OidIsValid(newdb))
     984              :     {
     985              :         avl_dbase  *db;
     986              :         PgStat_StatDBEntry *entry;
     987              : 
     988              :         /* only consider this database if it has a pgstat entry */
     989           17 :         entry = pgstat_fetch_stat_dbentry(newdb);
     990           17 :         if (entry != NULL)
     991              :         {
     992              :             /* we assume it isn't found because the hash was just created */
     993           14 :             db = hash_search(dbhash, &newdb, HASH_ENTER, NULL);
     994              : 
     995              :             /* hash_search already filled in the key */
     996           14 :             db->adl_score = score++;
     997              :             /* next_worker is filled in later */
     998              :         }
     999              :     }
    1000              : 
    1001              :     /* Now insert the databases from the existing list */
    1002          955 :     dlist_foreach(iter, &DatabaseList)
    1003              :     {
    1004          333 :         avl_dbase  *avdb = dlist_container(avl_dbase, adl_node, iter.cur);
    1005              :         avl_dbase  *db;
    1006              :         bool        found;
    1007              :         PgStat_StatDBEntry *entry;
    1008              : 
    1009              :         /*
    1010              :          * skip databases with no stat entries -- in particular, this gets rid
    1011              :          * of dropped databases
    1012              :          */
    1013          333 :         entry = pgstat_fetch_stat_dbentry(avdb->adl_datid);
    1014          333 :         if (entry == NULL)
    1015            0 :             continue;
    1016              : 
    1017          333 :         db = hash_search(dbhash, &(avdb->adl_datid), HASH_ENTER, &found);
    1018              : 
    1019          333 :         if (!found)
    1020              :         {
    1021              :             /* hash_search already filled in the key */
    1022          333 :             db->adl_score = score++;
    1023              :             /* next_worker is filled in later */
    1024              :         }
    1025              :     }
    1026              : 
    1027              :     /* finally, insert all qualifying databases not previously inserted */
    1028          622 :     dblist = get_database_list();
    1029         2805 :     foreach(cell, dblist)
    1030              :     {
    1031         2183 :         avw_dbase  *avdb = lfirst(cell);
    1032              :         avl_dbase  *db;
    1033              :         bool        found;
    1034              :         PgStat_StatDBEntry *entry;
    1035              : 
    1036              :         /* only consider databases with a pgstat entry */
    1037         2183 :         entry = pgstat_fetch_stat_dbentry(avdb->adw_datid);
    1038         2183 :         if (entry == NULL)
    1039         1118 :             continue;
    1040              : 
    1041         1065 :         db = hash_search(dbhash, &(avdb->adw_datid), HASH_ENTER, &found);
    1042              :         /* only update the score if the database was not already on the hash */
    1043         1065 :         if (!found)
    1044              :         {
    1045              :             /* hash_search already filled in the key */
    1046          718 :             db->adl_score = score++;
    1047              :             /* next_worker is filled in later */
    1048              :         }
    1049              :     }
    1050          622 :     nelems = score;
    1051              : 
    1052              :     /* from here on, the allocated memory belongs to the new list */
    1053          622 :     MemoryContextSwitchTo(newcxt);
    1054          622 :     dlist_init(&DatabaseList);
    1055              : 
    1056          622 :     if (nelems > 0)
    1057              :     {
    1058              :         TimestampTz current_time;
    1059              :         int         millis_increment;
    1060              :         avl_dbase  *dbary;
    1061              :         avl_dbase  *db;
    1062              :         HASH_SEQ_STATUS seq;
    1063              :         int         i;
    1064              : 
    1065              :         /* put all the hash elements into an array */
    1066          601 :         dbary = palloc(nelems * sizeof(avl_dbase));
    1067              :         /* keep Valgrind quiet */
    1068              : #ifdef USE_VALGRIND
    1069              :         avl_dbase_array = dbary;
    1070              : #endif
    1071              : 
    1072          601 :         i = 0;
    1073          601 :         hash_seq_init(&seq, dbhash);
    1074         1666 :         while ((db = hash_seq_search(&seq)) != NULL)
    1075         1065 :             memcpy(&(dbary[i++]), db, sizeof(avl_dbase));
    1076              : 
    1077              :         /* sort the array */
    1078          601 :         qsort(dbary, nelems, sizeof(avl_dbase), db_comparator);
    1079              : 
    1080              :         /*
    1081              :          * Determine the time interval between databases in the schedule. If
    1082              :          * we see that the configured naptime would take us to sleep times
    1083              :          * lower than our min sleep time (which launcher_determine_sleep is
    1084              :          * coded not to allow), silently use a larger naptime (but don't touch
    1085              :          * the GUC variable).
    1086              :          */
    1087          601 :         millis_increment = 1000.0 * autovacuum_naptime / nelems;
    1088          601 :         if (millis_increment <= MIN_AUTOVAC_SLEEPTIME)
    1089            0 :             millis_increment = MIN_AUTOVAC_SLEEPTIME * 1.1;
    1090              : 
    1091          601 :         current_time = GetCurrentTimestamp();
    1092              : 
    1093              :         /*
    1094              :          * move the elements from the array into the dlist, setting the
    1095              :          * next_worker while walking the array
    1096              :          */
    1097         1666 :         for (i = 0; i < nelems; i++)
    1098              :         {
    1099         1065 :             db = &(dbary[i]);
    1100              : 
    1101         1065 :             current_time = TimestampTzPlusMilliseconds(current_time,
    1102              :                                                        millis_increment);
    1103         1065 :             db->adl_next_worker = current_time;
    1104              : 
    1105              :             /* later elements should go closer to the head of the list */
    1106         1065 :             dlist_push_head(&DatabaseList, &db->adl_node);
    1107              :         }
    1108              :     }
    1109              : 
    1110              :     /* all done, clean up memory */
    1111          622 :     if (DatabaseListCxt != NULL)
    1112          140 :         MemoryContextDelete(DatabaseListCxt);
    1113          622 :     MemoryContextDelete(tmpcxt);
    1114          622 :     DatabaseListCxt = newcxt;
    1115          622 :     MemoryContextSwitchTo(oldcxt);
    1116          622 : }
    1117              : 
    1118              : /* qsort comparator for avl_dbase, using adl_score */
    1119              : static int
    1120          697 : db_comparator(const void *a, const void *b)
    1121              : {
    1122         1394 :     return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
    1123          697 :                       ((const avl_dbase *) b)->adl_score);
    1124              : }
    1125              : 
    1126              : /*
    1127              :  * do_start_worker
    1128              :  *
    1129              :  * Bare-bones procedure for starting an autovacuum worker from the launcher.
    1130              :  * It determines what database to work on, sets up shared memory stuff and
    1131              :  * signals postmaster to start the worker.  It fails gracefully if invoked when
    1132              :  * autovacuum_workers are already active.
    1133              :  *
    1134              :  * Return value is the OID of the database that the worker is going to process,
    1135              :  * or InvalidOid if no worker was actually started.
    1136              :  */
    1137              : static Oid
    1138         1419 : do_start_worker(void)
    1139              : {
    1140              :     List       *dblist;
    1141              :     ListCell   *cell;
    1142              :     TransactionId xidForceLimit;
    1143              :     MultiXactId multiForceLimit;
    1144              :     bool        for_xid_wrap;
    1145              :     bool        for_multi_wrap;
    1146              :     avw_dbase  *avdb;
    1147              :     TimestampTz current_time;
    1148         1419 :     bool        skipit = false;
    1149         1419 :     Oid         retval = InvalidOid;
    1150              :     MemoryContext tmpcxt,
    1151              :                 oldcxt;
    1152              : 
    1153              :     /* return quickly when there are no free workers */
    1154         1419 :     LWLockAcquire(AutovacuumLock, LW_SHARED);
    1155         1419 :     if (!av_worker_available())
    1156              :     {
    1157            0 :         LWLockRelease(AutovacuumLock);
    1158            0 :         return InvalidOid;
    1159              :     }
    1160         1419 :     LWLockRelease(AutovacuumLock);
    1161              : 
    1162              :     /*
    1163              :      * Create and switch to a temporary context to avoid leaking the memory
    1164              :      * allocated for the database list.
    1165              :      */
    1166         1419 :     tmpcxt = AllocSetContextCreate(CurrentMemoryContext,
    1167              :                                    "Autovacuum start worker (tmp)",
    1168              :                                    ALLOCSET_DEFAULT_SIZES);
    1169         1419 :     oldcxt = MemoryContextSwitchTo(tmpcxt);
    1170              : 
    1171              :     /* Get a list of databases */
    1172         1419 :     dblist = get_database_list();
    1173              : 
    1174              :     /*
    1175              :      * Determine the oldest datfrozenxid/relfrozenxid that we will allow to
    1176              :      * pass without forcing a vacuum.  (This limit can be tightened for
    1177              :      * particular tables, but not loosened.)
    1178              :      */
    1179         1419 :     recentXid = ReadNextTransactionId();
    1180         1419 :     xidForceLimit = recentXid - autovacuum_freeze_max_age;
    1181              :     /* ensure it's a "normal" XID, else TransactionIdPrecedes misbehaves */
    1182              :     /* this can cause the limit to go backwards by 3, but that's OK */
    1183         1419 :     if (xidForceLimit < FirstNormalTransactionId)
    1184            0 :         xidForceLimit -= FirstNormalTransactionId;
    1185              : 
    1186              :     /* Also determine the oldest datminmxid we will consider. */
    1187         1419 :     recentMulti = ReadNextMultiXactId();
    1188         1419 :     multiForceLimit = recentMulti - MultiXactMemberFreezeThreshold();
    1189         1419 :     if (multiForceLimit < FirstMultiXactId)
    1190            0 :         multiForceLimit -= FirstMultiXactId;
    1191              : 
    1192              :     /*
    1193              :      * Choose a database to connect to.  We pick the database that was least
    1194              :      * recently auto-vacuumed, or one that needs vacuuming to prevent Xid
    1195              :      * wraparound-related data loss.  If any db at risk of Xid wraparound is
    1196              :      * found, we pick the one with oldest datfrozenxid, independently of
    1197              :      * autovacuum times; similarly we pick the one with the oldest datminmxid
    1198              :      * if any is in MultiXactId wraparound.  Note that those in Xid wraparound
    1199              :      * danger are given more priority than those in multi wraparound danger.
    1200              :      *
    1201              :      * Note that a database with no stats entry is not considered, except for
    1202              :      * Xid wraparound purposes.  The theory is that if no one has ever
    1203              :      * connected to it since the stats were last initialized, it doesn't need
    1204              :      * vacuuming.
    1205              :      *
    1206              :      * XXX This could be improved if we had more info about whether it needs
    1207              :      * vacuuming before connecting to it.  Perhaps look through the pgstats
    1208              :      * data for the database's tables?  One idea is to keep track of the
    1209              :      * number of new and dead tuples per database in pgstats.  However it
    1210              :      * isn't clear how to construct a metric that measures that and not cause
    1211              :      * starvation for less busy databases.
    1212              :      */
    1213         1419 :     avdb = NULL;
    1214         1419 :     for_xid_wrap = false;
    1215         1419 :     for_multi_wrap = false;
    1216         1419 :     current_time = GetCurrentTimestamp();
    1217         5741 :     foreach(cell, dblist)
    1218              :     {
    1219         4322 :         avw_dbase  *tmp = lfirst(cell);
    1220              :         dlist_iter  iter;
    1221              : 
    1222              :         /* Check to see if this one is at risk of wraparound */
    1223         4322 :         if (TransactionIdPrecedes(tmp->adw_frozenxid, xidForceLimit))
    1224              :         {
    1225         3995 :             if (avdb == NULL ||
    1226         1589 :                 TransactionIdPrecedes(tmp->adw_frozenxid,
    1227              :                                       avdb->adw_frozenxid))
    1228          899 :                 avdb = tmp;
    1229         2406 :             for_xid_wrap = true;
    1230         3336 :             continue;
    1231              :         }
    1232         1916 :         else if (for_xid_wrap)
    1233           56 :             continue;           /* ignore not-at-risk DBs */
    1234         1860 :         else if (MultiXactIdPrecedes(tmp->adw_minmulti, multiForceLimit))
    1235              :         {
    1236            0 :             if (avdb == NULL ||
    1237            0 :                 MultiXactIdPrecedes(tmp->adw_minmulti, avdb->adw_minmulti))
    1238            0 :                 avdb = tmp;
    1239            0 :             for_multi_wrap = true;
    1240            0 :             continue;
    1241              :         }
    1242         1860 :         else if (for_multi_wrap)
    1243            0 :             continue;           /* ignore not-at-risk DBs */
    1244              : 
    1245              :         /* Find pgstat entry if any */
    1246         1860 :         tmp->adw_entry = pgstat_fetch_stat_dbentry(tmp->adw_datid);
    1247              : 
    1248              :         /*
    1249              :          * Skip a database with no pgstat entry; it means it hasn't seen any
    1250              :          * activity.
    1251              :          */
    1252         1860 :         if (!tmp->adw_entry)
    1253           65 :             continue;
    1254              : 
    1255              :         /*
    1256              :          * Also, skip a database that appears on the database list as having
    1257              :          * been processed recently (less than autovacuum_naptime seconds ago).
    1258              :          * We do this so that we don't select a database which we just
    1259              :          * selected, but that pgstat hasn't gotten around to updating the last
    1260              :          * autovacuum time yet.
    1261              :          */
    1262         1795 :         skipit = false;
    1263              : 
    1264         3733 :         dlist_reverse_foreach(iter, &DatabaseList)
    1265              :         {
    1266         3699 :             avl_dbase  *dbp = dlist_container(avl_dbase, adl_node, iter.cur);
    1267              : 
    1268         3699 :             if (dbp->adl_datid == tmp->adw_datid)
    1269              :             {
    1270              :                 /*
    1271              :                  * Skip this database if its next_worker value falls between
    1272              :                  * the current time and the current time plus naptime.
    1273              :                  */
    1274         1761 :                 if (!TimestampDifferenceExceeds(dbp->adl_next_worker,
    1275          809 :                                                 current_time, 0) &&
    1276          809 :                     !TimestampDifferenceExceeds(current_time,
    1277              :                                                 dbp->adl_next_worker,
    1278              :                                                 autovacuum_naptime * 1000))
    1279          809 :                     skipit = true;
    1280              : 
    1281         1761 :                 break;
    1282              :             }
    1283              :         }
    1284         1795 :         if (skipit)
    1285          809 :             continue;
    1286              : 
    1287              :         /*
    1288              :          * Remember the db with oldest autovac time.  (If we are here, both
    1289              :          * tmp->entry and db->entry must be non-null.)
    1290              :          */
    1291          986 :         if (avdb == NULL ||
    1292          385 :             tmp->adw_entry->last_autovac_time < avdb->adw_entry->last_autovac_time)
    1293          719 :             avdb = tmp;
    1294              :     }
    1295              : 
    1296              :     /* Found a database -- process it */
    1297         1419 :     if (avdb != NULL)
    1298              :     {
    1299              :         WorkerInfo  worker;
    1300              :         dlist_node *wptr;
    1301              : 
    1302         1418 :         LWLockAcquire(AutovacuumLock, LW_EXCLUSIVE);
    1303              : 
    1304              :         /*
    1305              :          * Get a worker entry from the freelist.  We checked above, so there
    1306              :          * really should be a free slot.
    1307              :          */
    1308         1418 :         wptr = dclist_pop_head_node(&AutoVacuumShmem->av_freeWorkers);
    1309              : 
    1310         1418 :         worker = dlist_container(WorkerInfoData, wi_links, wptr);
    1311         1418 :         worker->wi_dboid = avdb->adw_datid;
    1312         1418 :         worker->wi_proc = NULL;
    1313         1418 :         worker->wi_launchtime = GetCurrentTimestamp();
    1314              : 
    1315         1418 :         AutoVacuumShmem->av_startingWorker = worker;
    1316              : 
    1317         1418 :         LWLockRelease(AutovacuumLock);
    1318              : 
    1319         1418 :         SendPostmasterSignal(PMSIGNAL_START_AUTOVAC_WORKER);
    1320              : 
    1321         1418 :         retval = avdb->adw_datid;
    1322              :     }
    1323            1 :     else if (skipit)
    1324              :     {
    1325              :         /*
    1326              :          * If we skipped all databases on the list, rebuild it, because it
    1327              :          * probably contains a dropped database.
    1328              :          */
    1329            0 :         rebuild_database_list(InvalidOid);
    1330              :     }
    1331              : 
    1332         1419 :     MemoryContextSwitchTo(oldcxt);
    1333         1419 :     MemoryContextDelete(tmpcxt);
    1334              : 
    1335         1419 :     return retval;
    1336              : }
    1337              : 
    1338              : /*
    1339              :  * launch_worker
    1340              :  *
    1341              :  * Wrapper for starting a worker from the launcher.  Besides actually starting
    1342              :  * it, update the database list to reflect the next time that another one will
    1343              :  * need to be started on the selected database.  The actual database choice is
    1344              :  * left to do_start_worker.
    1345              :  *
    1346              :  * This routine is also expected to insert an entry into the database list if
    1347              :  * the selected database was previously absent from the list.
    1348              :  */
    1349              : static void
    1350         1419 : launch_worker(TimestampTz now)
    1351              : {
    1352              :     Oid         dbid;
    1353              :     dlist_iter  iter;
    1354              : 
    1355         1419 :     dbid = do_start_worker();
    1356         1419 :     if (OidIsValid(dbid))
    1357              :     {
    1358         1418 :         bool        found = false;
    1359              : 
    1360              :         /*
    1361              :          * Walk the database list and update the corresponding entry.  If the
    1362              :          * database is not on the list, we'll recreate the list.
    1363              :          */
    1364         2891 :         dlist_foreach(iter, &DatabaseList)
    1365              :         {
    1366         2874 :             avl_dbase  *avdb = dlist_container(avl_dbase, adl_node, iter.cur);
    1367              : 
    1368         2874 :             if (avdb->adl_datid == dbid)
    1369              :             {
    1370         1401 :                 found = true;
    1371              : 
    1372              :                 /*
    1373              :                  * add autovacuum_naptime seconds to the current time, and use
    1374              :                  * that as the new "next_worker" field for this database.
    1375              :                  */
    1376         1401 :                 avdb->adl_next_worker =
    1377         1401 :                     TimestampTzPlusMilliseconds(now, autovacuum_naptime * 1000);
    1378              : 
    1379         1401 :                 dlist_move_head(&DatabaseList, iter.cur);
    1380         1401 :                 break;
    1381              :             }
    1382              :         }
    1383              : 
    1384              :         /*
    1385              :          * If the database was not present in the database list, we rebuild
    1386              :          * the list.  It's possible that the database does not get into the
    1387              :          * list anyway, for example if it's a database that doesn't have a
    1388              :          * pgstat entry, but this is not a problem because we don't want to
    1389              :          * schedule workers regularly into those in any case.
    1390              :          */
    1391         1418 :         if (!found)
    1392           17 :             rebuild_database_list(dbid);
    1393              :     }
    1394         1419 : }
    1395              : 
    1396              : /*
    1397              :  * Called from postmaster to signal a failure to fork a process to become
    1398              :  * worker.  The postmaster should kill(SIGUSR2) the launcher shortly
    1399              :  * after calling this function.
    1400              :  */
    1401              : void
    1402            0 : AutoVacWorkerFailed(void)
    1403              : {
    1404            0 :     AutoVacuumShmem->av_signal[AutoVacForkFailed] = true;
    1405            0 : }
    1406              : 
    1407              : /* SIGUSR2: a worker is up and running, or just finished, or failed to fork */
    1408              : static void
    1409         2832 : avl_sigusr2_handler(SIGNAL_ARGS)
    1410              : {
    1411         2832 :     got_SIGUSR2 = true;
    1412         2832 :     SetLatch(MyLatch);
    1413         2832 : }
    1414              : 
    1415              : 
    1416              : /********************************************************************
    1417              :  *                    AUTOVACUUM WORKER CODE
    1418              :  ********************************************************************/
    1419              : 
    1420              : /*
    1421              :  * Main entry point for autovacuum worker processes.
    1422              :  */
    1423              : void
    1424         1425 : AutoVacWorkerMain(const void *startup_data, size_t startup_data_len)
    1425              : {
    1426              :     sigjmp_buf  local_sigjmp_buf;
    1427              :     Oid         dbid;
    1428              : 
    1429              :     Assert(startup_data_len == 0);
    1430              : 
    1431              :     /* Release postmaster's working memory context */
    1432         1425 :     if (PostmasterContext)
    1433              :     {
    1434         1425 :         MemoryContextDelete(PostmasterContext);
    1435         1425 :         PostmasterContext = NULL;
    1436              :     }
    1437              : 
    1438         1425 :     init_ps_display(NULL);
    1439              : 
    1440              :     Assert(GetProcessingMode() == InitProcessing);
    1441              : 
    1442              :     /*
    1443              :      * Set up signal handlers.  We operate on databases much like a regular
    1444              :      * backend, so we use the same signal handling.  See equivalent code in
    1445              :      * tcop/postgres.c.
    1446              :      */
    1447         1425 :     pqsignal(SIGHUP, SignalHandlerForConfigReload);
    1448              : 
    1449              :     /*
    1450              :      * SIGINT is used to signal canceling the current table's vacuum; SIGTERM
    1451              :      * means abort and exit cleanly, and SIGQUIT means abandon ship.
    1452              :      */
    1453         1425 :     pqsignal(SIGINT, StatementCancelHandler);
    1454         1425 :     pqsignal(SIGTERM, die);
    1455              :     /* SIGQUIT handler was already set up by InitPostmasterChild */
    1456              : 
    1457         1425 :     InitializeTimeouts();       /* establishes SIGALRM handler */
    1458              : 
    1459         1425 :     pqsignal(SIGPIPE, PG_SIG_IGN);
    1460         1425 :     pqsignal(SIGUSR1, procsignal_sigusr1_handler);
    1461         1425 :     pqsignal(SIGUSR2, PG_SIG_IGN);
    1462         1425 :     pqsignal(SIGFPE, FloatExceptionHandler);
    1463         1425 :     pqsignal(SIGCHLD, PG_SIG_DFL);
    1464              : 
    1465              :     /*
    1466              :      * Create a per-backend PGPROC struct in shared memory.  We must do this
    1467              :      * before we can use LWLocks or access any shared memory.
    1468              :      */
    1469         1425 :     InitProcess();
    1470              : 
    1471              :     /* Early initialization */
    1472         1425 :     BaseInit();
    1473              : 
    1474              :     /*
    1475              :      * If an exception is encountered, processing resumes here.
    1476              :      *
    1477              :      * Unlike most auxiliary processes, we don't attempt to continue
    1478              :      * processing after an error; we just clean up and exit.  The autovac
    1479              :      * launcher is responsible for spawning another worker later.
    1480              :      *
    1481              :      * Note that we use sigsetjmp(..., 1), so that the prevailing signal mask
    1482              :      * (to wit, BlockSig) will be restored when longjmp'ing to here.  Thus,
    1483              :      * signals other than SIGQUIT will be blocked until we exit.  It might
    1484              :      * seem that this policy makes the HOLD_INTERRUPTS() call redundant, but
    1485              :      * it is not since InterruptPending might be set already.
    1486              :      */
    1487         1425 :     if (sigsetjmp(local_sigjmp_buf, 1) != 0)
    1488              :     {
    1489              :         /* since not using PG_TRY, must reset error stack by hand */
    1490            0 :         error_context_stack = NULL;
    1491              : 
    1492              :         /* Prevents interrupts while cleaning up */
    1493            0 :         HOLD_INTERRUPTS();
    1494              : 
    1495              :         /* Report the error to the server log */
    1496            0 :         EmitErrorReport();
    1497              : 
    1498              :         /*
    1499              :          * We can now go away.  Note that because we called InitProcess, a
    1500              :          * callback was registered to do ProcKill, which will clean up
    1501              :          * necessary state.
    1502              :          */
    1503            0 :         proc_exit(0);
    1504              :     }
    1505              : 
    1506              :     /* We can now handle ereport(ERROR) */
    1507         1425 :     PG_exception_stack = &local_sigjmp_buf;
    1508              : 
    1509         1425 :     sigprocmask(SIG_SETMASK, &UnBlockSig, NULL);
    1510              : 
    1511              :     /*
    1512              :      * Set always-secure search path, so malicious users can't redirect user
    1513              :      * code (e.g. pg_index.indexprs).  (That code runs in a
    1514              :      * SECURITY_RESTRICTED_OPERATION sandbox, so malicious users could not
    1515              :      * take control of the entire autovacuum worker in any case.)
    1516              :      */
    1517         1425 :     SetConfigOption("search_path", "", PGC_SUSET, PGC_S_OVERRIDE);
    1518              : 
    1519              :     /*
    1520              :      * Force zero_damaged_pages OFF in the autovac process, even if it is set
    1521              :      * in postgresql.conf.  We don't really want such a dangerous option being
    1522              :      * applied non-interactively.
    1523              :      */
    1524         1425 :     SetConfigOption("zero_damaged_pages", "false", PGC_SUSET, PGC_S_OVERRIDE);
    1525              : 
    1526              :     /*
    1527              :      * Force settable timeouts off to avoid letting these settings prevent
    1528              :      * regular maintenance from being executed.
    1529              :      */
    1530         1425 :     SetConfigOption("statement_timeout", "0", PGC_SUSET, PGC_S_OVERRIDE);
    1531         1425 :     SetConfigOption("transaction_timeout", "0", PGC_SUSET, PGC_S_OVERRIDE);
    1532         1425 :     SetConfigOption("lock_timeout", "0", PGC_SUSET, PGC_S_OVERRIDE);
    1533         1425 :     SetConfigOption("idle_in_transaction_session_timeout", "0",
    1534              :                     PGC_SUSET, PGC_S_OVERRIDE);
    1535              : 
    1536              :     /*
    1537              :      * Force default_transaction_isolation to READ COMMITTED.  We don't want
    1538              :      * to pay the overhead of serializable mode, nor add any risk of causing
    1539              :      * deadlocks or delaying other transactions.
    1540              :      */
    1541         1425 :     SetConfigOption("default_transaction_isolation", "read committed",
    1542              :                     PGC_SUSET, PGC_S_OVERRIDE);
    1543              : 
    1544              :     /*
    1545              :      * Force synchronous replication off to allow regular maintenance even if
    1546              :      * we are waiting for standbys to connect. This is important to ensure we
    1547              :      * aren't blocked from performing anti-wraparound tasks.
    1548              :      */
    1549         1425 :     if (synchronous_commit > SYNCHRONOUS_COMMIT_LOCAL_FLUSH)
    1550         1425 :         SetConfigOption("synchronous_commit", "local",
    1551              :                         PGC_SUSET, PGC_S_OVERRIDE);
    1552              : 
    1553              :     /*
    1554              :      * Even when system is configured to use a different fetch consistency,
    1555              :      * for autovac we always want fresh stats.
    1556              :      */
    1557         1425 :     SetConfigOption("stats_fetch_consistency", "none", PGC_SUSET, PGC_S_OVERRIDE);
    1558              : 
    1559              :     /*
    1560              :      * Get the info about the database we're going to work on.
    1561              :      */
    1562         1425 :     LWLockAcquire(AutovacuumLock, LW_EXCLUSIVE);
    1563              : 
    1564              :     /*
    1565              :      * beware of startingWorker being INVALID; this should normally not
    1566              :      * happen, but if a worker fails after forking and before this, the
    1567              :      * launcher might have decided to remove it from the queue and start
    1568              :      * again.
    1569              :      */
    1570         1425 :     if (AutoVacuumShmem->av_startingWorker != NULL)
    1571              :     {
    1572         1425 :         MyWorkerInfo = AutoVacuumShmem->av_startingWorker;
    1573         1425 :         dbid = MyWorkerInfo->wi_dboid;
    1574         1425 :         MyWorkerInfo->wi_proc = MyProc;
    1575              : 
    1576              :         /* insert into the running list */
    1577         1425 :         dlist_push_head(&AutoVacuumShmem->av_runningWorkers,
    1578         1425 :                         &MyWorkerInfo->wi_links);
    1579              : 
    1580              :         /*
    1581              :          * remove from the "starting" pointer, so that the launcher can start
    1582              :          * a new worker if required
    1583              :          */
    1584         1425 :         AutoVacuumShmem->av_startingWorker = NULL;
    1585         1425 :         LWLockRelease(AutovacuumLock);
    1586              : 
    1587         1425 :         on_shmem_exit(FreeWorkerInfo, 0);
    1588              : 
    1589              :         /* wake up the launcher */
    1590         1425 :         if (AutoVacuumShmem->av_launcherpid != 0)
    1591         1425 :             kill(AutoVacuumShmem->av_launcherpid, SIGUSR2);
    1592              :     }
    1593              :     else
    1594              :     {
    1595              :         /* no worker entry for me, go away */
    1596            0 :         elog(WARNING, "autovacuum worker started without a worker entry");
    1597            0 :         dbid = InvalidOid;
    1598            0 :         LWLockRelease(AutovacuumLock);
    1599              :     }
    1600              : 
    1601         1425 :     if (OidIsValid(dbid))
    1602              :     {
    1603              :         char        dbname[NAMEDATALEN];
    1604              : 
    1605              :         /*
    1606              :          * Report autovac startup to the cumulative stats system.  We
    1607              :          * deliberately do this before InitPostgres, so that the
    1608              :          * last_autovac_time will get updated even if the connection attempt
    1609              :          * fails.  This is to prevent autovac from getting "stuck" repeatedly
    1610              :          * selecting an unopenable database, rather than making any progress
    1611              :          * on stuff it can connect to.
    1612              :          */
    1613         1425 :         pgstat_report_autovac(dbid);
    1614              : 
    1615              :         /*
    1616              :          * Connect to the selected database, specifying no particular user,
    1617              :          * and ignoring datallowconn.  Collect the database's name for
    1618              :          * display.
    1619              :          *
    1620              :          * Note: if we have selected a just-deleted database (due to using
    1621              :          * stale stats info), we'll fail and exit here.
    1622              :          */
    1623         1425 :         InitPostgres(NULL, dbid, NULL, InvalidOid,
    1624              :                      INIT_PG_OVERRIDE_ALLOW_CONNS,
    1625              :                      dbname);
    1626         1425 :         SetProcessingMode(NormalProcessing);
    1627         1425 :         set_ps_display(dbname);
    1628         1425 :         ereport(DEBUG1,
    1629              :                 (errmsg_internal("autovacuum: processing database \"%s\"", dbname)));
    1630              : 
    1631         1425 :         if (PostAuthDelay)
    1632            0 :             pg_usleep(PostAuthDelay * 1000000L);
    1633              : 
    1634              :         /* And do an appropriate amount of work */
    1635         1425 :         recentXid = ReadNextTransactionId();
    1636         1425 :         recentMulti = ReadNextMultiXactId();
    1637         1425 :         do_autovacuum();
    1638              :     }
    1639              : 
    1640              :     /* All done, go away */
    1641         1423 :     proc_exit(0);
    1642              : }
    1643              : 
    1644              : /*
    1645              :  * Return a WorkerInfo to the free list
    1646              :  */
    1647              : static void
    1648         1425 : FreeWorkerInfo(int code, Datum arg)
    1649              : {
    1650         1425 :     if (MyWorkerInfo != NULL)
    1651              :     {
    1652         1425 :         LWLockAcquire(AutovacuumLock, LW_EXCLUSIVE);
    1653              : 
    1654         1425 :         dlist_delete(&MyWorkerInfo->wi_links);
    1655         1425 :         MyWorkerInfo->wi_dboid = InvalidOid;
    1656         1425 :         MyWorkerInfo->wi_tableoid = InvalidOid;
    1657         1425 :         MyWorkerInfo->wi_sharedrel = false;
    1658         1425 :         MyWorkerInfo->wi_proc = NULL;
    1659         1425 :         MyWorkerInfo->wi_launchtime = 0;
    1660         1425 :         pg_atomic_clear_flag(&MyWorkerInfo->wi_dobalance);
    1661         1425 :         dclist_push_head(&AutoVacuumShmem->av_freeWorkers,
    1662         1425 :                          &MyWorkerInfo->wi_links);
    1663              :         /* not mine anymore */
    1664         1425 :         MyWorkerInfo = NULL;
    1665              : 
    1666              :         /*
    1667              :          * now that we're inactive, cause a rebalancing of the surviving
    1668              :          * workers
    1669              :          */
    1670         1425 :         AutoVacuumShmem->av_signal[AutoVacRebalance] = true;
    1671         1425 :         LWLockRelease(AutovacuumLock);
    1672              :     }
    1673         1425 : }
    1674              : 
    1675              : /*
    1676              :  * Update vacuum cost-based delay-related parameters for autovacuum workers and
    1677              :  * backends executing VACUUM or ANALYZE using the value of relevant GUCs and
    1678              :  * global state. This must be called during setup for vacuum and after every
    1679              :  * config reload to ensure up-to-date values.
    1680              :  */
    1681              : void
    1682       205670 : VacuumUpdateCosts(void)
    1683              : {
    1684       205670 :     if (MyWorkerInfo)
    1685              :     {
    1686       196989 :         if (av_storage_param_cost_delay >= 0)
    1687            0 :             vacuum_cost_delay = av_storage_param_cost_delay;
    1688       196989 :         else if (autovacuum_vac_cost_delay >= 0)
    1689       196989 :             vacuum_cost_delay = autovacuum_vac_cost_delay;
    1690              :         else
    1691              :             /* fall back to VacuumCostDelay */
    1692            0 :             vacuum_cost_delay = VacuumCostDelay;
    1693              : 
    1694       196989 :         AutoVacuumUpdateCostLimit();
    1695              :     }
    1696              :     else
    1697              :     {
    1698              :         /* Must be explicit VACUUM or ANALYZE or parallel autovacuum worker */
    1699         8681 :         vacuum_cost_delay = VacuumCostDelay;
    1700         8681 :         vacuum_cost_limit = VacuumCostLimit;
    1701              :     }
    1702              : 
    1703              :     /*
    1704              :      * If configuration changes are allowed to impact VacuumCostActive, make
    1705              :      * sure it is updated.
    1706              :      */
    1707       205670 :     if (VacuumFailsafeActive)
    1708              :         Assert(!VacuumCostActive);
    1709       205670 :     else if (vacuum_cost_delay > 0)
    1710       196992 :         VacuumCostActive = true;
    1711              :     else
    1712              :     {
    1713         8678 :         VacuumCostActive = false;
    1714         8678 :         VacuumCostBalance = 0;
    1715              :     }
    1716              : 
    1717              :     /*
    1718              :      * Since the cost logging requires a lock, avoid rendering the log message
    1719              :      * in case we are using a message level where the log wouldn't be emitted.
    1720              :      */
    1721       205670 :     if (MyWorkerInfo && message_level_is_interesting(DEBUG2))
    1722              :     {
    1723              :         Oid         dboid,
    1724              :                     tableoid;
    1725              : 
    1726              :         Assert(!LWLockHeldByMe(AutovacuumLock));
    1727              : 
    1728            5 :         LWLockAcquire(AutovacuumLock, LW_SHARED);
    1729            5 :         dboid = MyWorkerInfo->wi_dboid;
    1730            5 :         tableoid = MyWorkerInfo->wi_tableoid;
    1731            5 :         LWLockRelease(AutovacuumLock);
    1732              : 
    1733            5 :         elog(DEBUG2,
    1734              :              "Autovacuum VacuumUpdateCosts(db=%u, rel=%u, dobalance=%s, cost_limit=%d, cost_delay=%g active=%s failsafe=%s)",
    1735              :              dboid, tableoid, pg_atomic_unlocked_test_flag(&MyWorkerInfo->wi_dobalance) ? "no" : "yes",
    1736              :              vacuum_cost_limit, vacuum_cost_delay,
    1737              :              vacuum_cost_delay > 0 ? "yes" : "no",
    1738              :              VacuumFailsafeActive ? "yes" : "no");
    1739              :     }
    1740       205670 : }
    1741              : 
    1742              : /*
    1743              :  * Update vacuum_cost_limit with the correct value for an autovacuum worker,
    1744              :  * given the value of other relevant cost limit parameters and the number of
    1745              :  * workers across which the limit must be balanced. Autovacuum workers must
    1746              :  * call this regularly in case av_nworkersForBalance has been updated by
    1747              :  * another worker or by the autovacuum launcher. They must also call it after a
    1748              :  * config reload.
    1749              :  */
    1750              : void
    1751       200124 : AutoVacuumUpdateCostLimit(void)
    1752              : {
    1753       200124 :     if (!MyWorkerInfo)
    1754            5 :         return;
    1755              : 
    1756              :     /*
    1757              :      * note: in cost_limit, zero also means use value from elsewhere, because
    1758              :      * zero is not a valid value.
    1759              :      */
    1760              : 
    1761       200119 :     if (av_storage_param_cost_limit > 0)
    1762            0 :         vacuum_cost_limit = av_storage_param_cost_limit;
    1763              :     else
    1764              :     {
    1765              :         int         nworkers_for_balance;
    1766              : 
    1767       200119 :         if (autovacuum_vac_cost_limit > 0)
    1768            1 :             vacuum_cost_limit = autovacuum_vac_cost_limit;
    1769              :         else
    1770       200118 :             vacuum_cost_limit = VacuumCostLimit;
    1771              : 
    1772              :         /* Only balance limit if no cost-related storage parameters specified */
    1773       200119 :         if (pg_atomic_unlocked_test_flag(&MyWorkerInfo->wi_dobalance))
    1774            0 :             return;
    1775              : 
    1776              :         Assert(vacuum_cost_limit > 0);
    1777              : 
    1778       200119 :         nworkers_for_balance = pg_atomic_read_u32(&AutoVacuumShmem->av_nworkersForBalance);
    1779              : 
    1780              :         /* There is at least 1 autovac worker (this worker) */
    1781       200119 :         if (nworkers_for_balance <= 0)
    1782            0 :             elog(ERROR, "nworkers_for_balance must be > 0");
    1783              : 
    1784       200119 :         vacuum_cost_limit = Max(vacuum_cost_limit / nworkers_for_balance, 1);
    1785              :     }
    1786              : }
    1787              : 
    1788              : /*
    1789              :  * autovac_recalculate_workers_for_balance
    1790              :  *      Recalculate the number of workers to consider, given cost-related
    1791              :  *      storage parameters and the current number of active workers.
    1792              :  *
    1793              :  * Caller must hold the AutovacuumLock in at least shared mode to access
    1794              :  * worker->wi_proc.
    1795              :  */
    1796              : static void
    1797        99874 : autovac_recalculate_workers_for_balance(void)
    1798              : {
    1799              :     dlist_iter  iter;
    1800              :     int         orig_nworkers_for_balance;
    1801        99874 :     int         nworkers_for_balance = 0;
    1802              : 
    1803              :     Assert(LWLockHeldByMe(AutovacuumLock));
    1804              : 
    1805        99874 :     orig_nworkers_for_balance =
    1806        99874 :         pg_atomic_read_u32(&AutoVacuumShmem->av_nworkersForBalance);
    1807              : 
    1808       317575 :     dlist_foreach(iter, &AutoVacuumShmem->av_runningWorkers)
    1809              :     {
    1810       217701 :         WorkerInfo  worker = dlist_container(WorkerInfoData, wi_links, iter.cur);
    1811              : 
    1812       435402 :         if (worker->wi_proc == NULL ||
    1813       217701 :             pg_atomic_unlocked_test_flag(&worker->wi_dobalance))
    1814         2911 :             continue;
    1815              : 
    1816       214790 :         nworkers_for_balance++;
    1817              :     }
    1818              : 
    1819        99874 :     if (nworkers_for_balance != orig_nworkers_for_balance)
    1820         1862 :         pg_atomic_write_u32(&AutoVacuumShmem->av_nworkersForBalance,
    1821              :                             nworkers_for_balance);
    1822        99874 : }
    1823              : 
    1824              : /*
    1825              :  * get_database_list
    1826              :  *      Return a list of all databases found in pg_database.
    1827              :  *
    1828              :  * The list and associated data is allocated in the caller's memory context,
    1829              :  * which is in charge of ensuring that it's properly cleaned up afterwards.
    1830              :  *
    1831              :  * Note: this is the only function in which the autovacuum launcher uses a
    1832              :  * transaction.  Although we aren't attached to any particular database and
    1833              :  * therefore can't access most catalogs, we do have enough infrastructure
    1834              :  * to do a seqscan on pg_database.
    1835              :  */
    1836              : static List *
    1837         2041 : get_database_list(void)
    1838              : {
    1839         2041 :     List       *dblist = NIL;
    1840              :     Relation    rel;
    1841              :     TableScanDesc scan;
    1842              :     HeapTuple   tup;
    1843              :     MemoryContext resultcxt;
    1844              : 
    1845              :     /* This is the context that we will allocate our output data in */
    1846         2041 :     resultcxt = CurrentMemoryContext;
    1847              : 
    1848              :     /*
    1849              :      * Start a transaction so we can access pg_database.
    1850              :      */
    1851         2041 :     StartTransactionCommand();
    1852              : 
    1853         2041 :     rel = table_open(DatabaseRelationId, AccessShareLock);
    1854         2041 :     scan = table_beginscan_catalog(rel, 0, NULL);
    1855              : 
    1856         8551 :     while (HeapTupleIsValid(tup = heap_getnext(scan, ForwardScanDirection)))
    1857              :     {
    1858         6510 :         Form_pg_database pgdatabase = (Form_pg_database) GETSTRUCT(tup);
    1859              :         avw_dbase  *avdb;
    1860              :         MemoryContext oldcxt;
    1861              : 
    1862              :         /*
    1863              :          * If database has partially been dropped, we can't, nor need to,
    1864              :          * vacuum it.
    1865              :          */
    1866         6510 :         if (database_is_invalid_form(pgdatabase))
    1867              :         {
    1868            5 :             elog(DEBUG2,
    1869              :                  "autovacuum: skipping invalid database \"%s\"",
    1870              :                  NameStr(pgdatabase->datname));
    1871            5 :             continue;
    1872              :         }
    1873              : 
    1874              :         /*
    1875              :          * Allocate our results in the caller's context, not the
    1876              :          * transaction's. We do this inside the loop, and restore the original
    1877              :          * context at the end, so that leaky things like heap_getnext() are
    1878              :          * not called in a potentially long-lived context.
    1879              :          */
    1880         6505 :         oldcxt = MemoryContextSwitchTo(resultcxt);
    1881              : 
    1882         6505 :         avdb = palloc_object(avw_dbase);
    1883              : 
    1884         6505 :         avdb->adw_datid = pgdatabase->oid;
    1885         6505 :         avdb->adw_name = pstrdup(NameStr(pgdatabase->datname));
    1886         6505 :         avdb->adw_frozenxid = pgdatabase->datfrozenxid;
    1887         6505 :         avdb->adw_minmulti = pgdatabase->datminmxid;
    1888              :         /* this gets set later: */
    1889         6505 :         avdb->adw_entry = NULL;
    1890              : 
    1891         6505 :         dblist = lappend(dblist, avdb);
    1892         6505 :         MemoryContextSwitchTo(oldcxt);
    1893              :     }
    1894              : 
    1895         2041 :     table_endscan(scan);
    1896         2041 :     table_close(rel, AccessShareLock);
    1897              : 
    1898         2041 :     CommitTransactionCommand();
    1899              : 
    1900              :     /* Be sure to restore caller's memory context */
    1901         2041 :     MemoryContextSwitchTo(resultcxt);
    1902              : 
    1903         2041 :     return dblist;
    1904              : }
    1905              : 
    1906              : /*
    1907              :  * List comparator for TableToProcess.  Note that this sorts the tables based
    1908              :  * on their scores in descending order.
    1909              :  */
    1910              : static int
    1911       101797 : TableToProcessComparator(const ListCell *a, const ListCell *b)
    1912              : {
    1913       101797 :     TableToProcess *t1 = (TableToProcess *) lfirst(a);
    1914       101797 :     TableToProcess *t2 = (TableToProcess *) lfirst(b);
    1915              : 
    1916       101797 :     return (t2->score < t1->score) ? -1 : (t2->score > t1->score) ? 1 : 0;
    1917              : }
    1918              : 
    1919              : /*
    1920              :  * Process a database table-by-table
    1921              :  *
    1922              :  * Note that CHECK_FOR_INTERRUPTS is supposed to be used in certain spots in
    1923              :  * order not to ignore shutdown commands for too long.
    1924              :  */
    1925              : static void
    1926         1425 : do_autovacuum(void)
    1927              : {
    1928              :     Relation    classRel;
    1929              :     HeapTuple   tuple;
    1930              :     TableScanDesc relScan;
    1931              :     Form_pg_database dbForm;
    1932         1425 :     List       *tables_to_process = NIL;
    1933         1425 :     List       *orphan_oids = NIL;
    1934              :     HASHCTL     ctl;
    1935              :     HTAB       *table_toast_map;
    1936              :     ListCell   *volatile cell;
    1937              :     BufferAccessStrategy bstrategy;
    1938              :     ScanKeyData key;
    1939              :     TupleDesc   pg_class_desc;
    1940              :     int         effective_multixact_freeze_max_age;
    1941         1425 :     bool        did_vacuum = false;
    1942         1425 :     bool        found_concurrent_worker = false;
    1943              :     int         i;
    1944              : 
    1945              :     /*
    1946              :      * StartTransactionCommand and CommitTransactionCommand will automatically
    1947              :      * switch to other contexts.  We need this one to keep the list of
    1948              :      * relations to vacuum/analyze across transactions.
    1949              :      */
    1950         1425 :     AutovacMemCxt = AllocSetContextCreate(TopMemoryContext,
    1951              :                                           "Autovacuum worker",
    1952              :                                           ALLOCSET_DEFAULT_SIZES);
    1953         1425 :     MemoryContextSwitchTo(AutovacMemCxt);
    1954              : 
    1955              :     /* Start a transaction so our commands have one to play into. */
    1956         1425 :     StartTransactionCommand();
    1957              : 
    1958              :     /*
    1959              :      * This injection point is put in a transaction block to work with a wait
    1960              :      * that uses a condition variable.
    1961              :      */
    1962         1425 :     INJECTION_POINT("autovacuum-worker-start", NULL);
    1963              : 
    1964              :     /*
    1965              :      * Compute the multixact age for which freezing is urgent.  This is
    1966              :      * normally autovacuum_multixact_freeze_max_age, but may be less if
    1967              :      * multixact members are bloated.
    1968              :      */
    1969         1424 :     effective_multixact_freeze_max_age = MultiXactMemberFreezeThreshold();
    1970              : 
    1971              :     /*
    1972              :      * Find the pg_database entry and select the default freeze ages. We use
    1973              :      * zero in template and nonconnectable databases, else the system-wide
    1974              :      * default.
    1975              :      */
    1976         1424 :     tuple = SearchSysCache1(DATABASEOID, ObjectIdGetDatum(MyDatabaseId));
    1977         1424 :     if (!HeapTupleIsValid(tuple))
    1978            0 :         elog(ERROR, "cache lookup failed for database %u", MyDatabaseId);
    1979         1424 :     dbForm = (Form_pg_database) GETSTRUCT(tuple);
    1980              : 
    1981         1424 :     if (dbForm->datistemplate || !dbForm->datallowconn)
    1982              :     {
    1983          462 :         default_freeze_min_age = 0;
    1984          462 :         default_freeze_table_age = 0;
    1985          462 :         default_multixact_freeze_min_age = 0;
    1986          462 :         default_multixact_freeze_table_age = 0;
    1987              :     }
    1988              :     else
    1989              :     {
    1990          962 :         default_freeze_min_age = vacuum_freeze_min_age;
    1991          962 :         default_freeze_table_age = vacuum_freeze_table_age;
    1992          962 :         default_multixact_freeze_min_age = vacuum_multixact_freeze_min_age;
    1993          962 :         default_multixact_freeze_table_age = vacuum_multixact_freeze_table_age;
    1994              :     }
    1995              : 
    1996         1424 :     ReleaseSysCache(tuple);
    1997              : 
    1998              :     /* StartTransactionCommand changed elsewhere */
    1999         1424 :     MemoryContextSwitchTo(AutovacMemCxt);
    2000              : 
    2001         1424 :     classRel = table_open(RelationRelationId, AccessShareLock);
    2002              : 
    2003              :     /* create a copy so we can use it after closing pg_class */
    2004         1424 :     pg_class_desc = CreateTupleDescCopy(RelationGetDescr(classRel));
    2005              : 
    2006              :     /* create hash table for toast <-> main relid mapping */
    2007         1424 :     ctl.keysize = sizeof(Oid);
    2008         1424 :     ctl.entrysize = sizeof(av_relation);
    2009              : 
    2010         1424 :     table_toast_map = hash_create("TOAST to main relid map",
    2011              :                                   100,
    2012              :                                   &ctl,
    2013              :                                   HASH_ELEM | HASH_BLOBS);
    2014              : 
    2015              :     /*
    2016              :      * Scan pg_class to determine which tables to vacuum.
    2017              :      *
    2018              :      * We do this in two passes: on the first one we collect the list of plain
    2019              :      * relations and materialized views, and on the second one we collect
    2020              :      * TOAST tables. The reason for doing the second pass is that during it we
    2021              :      * want to use the main relation's pg_class.reloptions entry if the TOAST
    2022              :      * table does not have any, and we cannot obtain it unless we know
    2023              :      * beforehand what's the main table OID.
    2024              :      *
    2025              :      * We need to check TOAST tables separately because in cases with short,
    2026              :      * wide tables there might be proportionally much more activity in the
    2027              :      * TOAST table than in its parent.
    2028              :      */
    2029         1424 :     relScan = table_beginscan_catalog(classRel, 0, NULL);
    2030              : 
    2031              :     /*
    2032              :      * On the first pass, we collect main tables to vacuum, and also the main
    2033              :      * table relid to TOAST relid mapping.
    2034              :      */
    2035       655501 :     while ((tuple = heap_getnext(relScan, ForwardScanDirection)) != NULL)
    2036              :     {
    2037       654077 :         Form_pg_class classForm = (Form_pg_class) GETSTRUCT(tuple);
    2038              :         AutoVacOpts *relopts;
    2039              :         Oid         relid;
    2040              :         bool        dovacuum;
    2041              :         bool        doanalyze;
    2042              :         bool        wraparound;
    2043              :         AutoVacuumScores scores;
    2044              : 
    2045       654077 :         if (classForm->relkind != RELKIND_RELATION &&
    2046       546553 :             classForm->relkind != RELKIND_MATVIEW)
    2047       546512 :             continue;
    2048              : 
    2049       107571 :         relid = classForm->oid;
    2050              : 
    2051              :         /*
    2052              :          * Check if it is a temp table (presumably, of some other backend's).
    2053              :          * We cannot safely process other backends' temp tables.
    2054              :          */
    2055       107571 :         if (classForm->relpersistence == RELPERSISTENCE_TEMP)
    2056              :         {
    2057              :             /*
    2058              :              * We just ignore it if the owning backend is still active and
    2059              :              * using the temporary schema.  Also, for safety, ignore it if the
    2060              :              * namespace doesn't exist or isn't a temp namespace after all.
    2061              :              */
    2062            6 :             if (checkTempNamespaceStatus(classForm->relnamespace) == TEMP_NAMESPACE_IDLE)
    2063              :             {
    2064              :                 /*
    2065              :                  * The table seems to be orphaned -- although it might be that
    2066              :                  * the owning backend has already deleted it and exited; our
    2067              :                  * pg_class scan snapshot is not necessarily up-to-date
    2068              :                  * anymore, so we could be looking at a committed-dead entry.
    2069              :                  * Remember it so we can try to delete it later.
    2070              :                  */
    2071            0 :                 orphan_oids = lappend_oid(orphan_oids, relid);
    2072              :             }
    2073            6 :             continue;
    2074              :         }
    2075              : 
    2076              :         /* Fetch reloptions and the pgstat entry for this table */
    2077       107565 :         relopts = extract_autovac_opts(tuple, pg_class_desc);
    2078              : 
    2079              :         /* Check if it needs vacuum or analyze */
    2080       107565 :         relation_needs_vacanalyze(relid, relopts, classForm,
    2081              :                                   effective_multixact_freeze_max_age,
    2082              :                                   DEBUG3,
    2083              :                                   &dovacuum, &doanalyze, &wraparound,
    2084              :                                   &scores);
    2085              : 
    2086              :         /* Relations that need work are added to tables_to_process */
    2087       107565 :         if (dovacuum || doanalyze)
    2088              :         {
    2089        63356 :             TableToProcess *table = palloc_object(TableToProcess);
    2090              : 
    2091        63356 :             table->oid = relid;
    2092        63356 :             table->score = scores.max;
    2093        63356 :             tables_to_process = lappend(tables_to_process, table);
    2094              :         }
    2095              : 
    2096              :         /*
    2097              :          * Remember TOAST associations for the second pass.  Note: we must do
    2098              :          * this whether or not the table is going to be vacuumed, because we
    2099              :          * don't automatically vacuum toast tables along the parent table.
    2100              :          */
    2101       107565 :         if (OidIsValid(classForm->reltoastrelid))
    2102              :         {
    2103              :             av_relation *hentry;
    2104              :             bool        found;
    2105              : 
    2106       121010 :             hentry = hash_search(table_toast_map,
    2107        60505 :                                  &classForm->reltoastrelid,
    2108              :                                  HASH_ENTER, &found);
    2109              : 
    2110        60505 :             if (!found)
    2111              :             {
    2112              :                 /* hash_search already filled in the key */
    2113        60505 :                 hentry->ar_relid = relid;
    2114        60505 :                 hentry->ar_hasrelopts = false;
    2115        60505 :                 if (relopts != NULL)
    2116              :                 {
    2117         1244 :                     hentry->ar_hasrelopts = true;
    2118         1244 :                     memcpy(&hentry->ar_reloptions, relopts,
    2119              :                            sizeof(AutoVacOpts));
    2120              :                 }
    2121              :             }
    2122              :         }
    2123              : 
    2124              :         /* Release stuff to avoid per-relation leakage */
    2125       107565 :         if (relopts)
    2126         1289 :             pfree(relopts);
    2127              :     }
    2128              : 
    2129         1424 :     table_endscan(relScan);
    2130              : 
    2131              :     /* second pass: check TOAST tables */
    2132         1424 :     ScanKeyInit(&key,
    2133              :                 Anum_pg_class_relkind,
    2134              :                 BTEqualStrategyNumber, F_CHAREQ,
    2135              :                 CharGetDatum(RELKIND_TOASTVALUE));
    2136              : 
    2137         1424 :     relScan = table_beginscan_catalog(classRel, 1, &key);
    2138        61930 :     while ((tuple = heap_getnext(relScan, ForwardScanDirection)) != NULL)
    2139              :     {
    2140        60506 :         Form_pg_class classForm = (Form_pg_class) GETSTRUCT(tuple);
    2141              :         Oid         relid;
    2142              :         AutoVacOpts *relopts;
    2143        60506 :         bool        free_relopts = false;
    2144              :         bool        dovacuum;
    2145              :         bool        doanalyze;
    2146              :         bool        wraparound;
    2147              :         AutoVacuumScores scores;
    2148              : 
    2149              :         /*
    2150              :          * We cannot safely process other backends' temp tables, so skip 'em.
    2151              :          */
    2152        60506 :         if (classForm->relpersistence == RELPERSISTENCE_TEMP)
    2153            1 :             continue;
    2154              : 
    2155        60505 :         relid = classForm->oid;
    2156              : 
    2157              :         /*
    2158              :          * fetch reloptions -- if this toast table does not have them, try the
    2159              :          * main rel
    2160              :          */
    2161        60505 :         relopts = extract_autovac_opts(tuple, pg_class_desc);
    2162        60505 :         if (relopts)
    2163            2 :             free_relopts = true;
    2164              :         else
    2165              :         {
    2166              :             av_relation *hentry;
    2167              :             bool        found;
    2168              : 
    2169        60503 :             hentry = hash_search(table_toast_map, &relid, HASH_FIND, &found);
    2170        60503 :             if (found && hentry->ar_hasrelopts)
    2171         1242 :                 relopts = &hentry->ar_reloptions;
    2172              :         }
    2173              : 
    2174        60505 :         relation_needs_vacanalyze(relid, relopts, classForm,
    2175              :                                   effective_multixact_freeze_max_age,
    2176              :                                   DEBUG3,
    2177              :                                   &dovacuum, &doanalyze, &wraparound,
    2178              :                                   &scores);
    2179              : 
    2180              :         /* ignore analyze for toast tables */
    2181        60505 :         if (dovacuum)
    2182              :         {
    2183        35674 :             TableToProcess *table = palloc_object(TableToProcess);
    2184              : 
    2185        35674 :             table->oid = relid;
    2186        35674 :             table->score = scores.max;
    2187        35674 :             tables_to_process = lappend(tables_to_process, table);
    2188              :         }
    2189              : 
    2190              :         /* Release stuff to avoid leakage */
    2191        60505 :         if (free_relopts)
    2192            2 :             pfree(relopts);
    2193              :     }
    2194              : 
    2195         1424 :     table_endscan(relScan);
    2196         1424 :     table_close(classRel, AccessShareLock);
    2197              : 
    2198              :     /*
    2199              :      * Recheck orphan temporary tables, and if they still seem orphaned, drop
    2200              :      * them.  We'll eat a transaction per dropped table, which might seem
    2201              :      * excessive, but we should only need to do anything as a result of a
    2202              :      * previous backend crash, so this should not happen often enough to
    2203              :      * justify "optimizing".  Using separate transactions ensures that we
    2204              :      * don't bloat the lock table if there are many temp tables to be dropped,
    2205              :      * and it ensures that we don't lose work if a deletion attempt fails.
    2206              :      */
    2207         1424 :     foreach(cell, orphan_oids)
    2208              :     {
    2209            0 :         Oid         relid = lfirst_oid(cell);
    2210              :         Form_pg_class classForm;
    2211              :         ObjectAddress object;
    2212              : 
    2213              :         /*
    2214              :          * Check for user-requested abort.
    2215              :          */
    2216            0 :         CHECK_FOR_INTERRUPTS();
    2217              : 
    2218              :         /*
    2219              :          * Try to lock the table.  If we can't get the lock immediately,
    2220              :          * somebody else is using (or dropping) the table, so it's not our
    2221              :          * concern anymore.  Having the lock prevents race conditions below.
    2222              :          */
    2223            0 :         if (!ConditionalLockRelationOid(relid, AccessExclusiveLock))
    2224            0 :             continue;
    2225              : 
    2226              :         /*
    2227              :          * Re-fetch the pg_class tuple and re-check whether it still seems to
    2228              :          * be an orphaned temp table.  If it's not there or no longer the same
    2229              :          * relation, ignore it.
    2230              :          */
    2231            0 :         tuple = SearchSysCacheCopy1(RELOID, ObjectIdGetDatum(relid));
    2232            0 :         if (!HeapTupleIsValid(tuple))
    2233              :         {
    2234              :             /* be sure to drop useless lock so we don't bloat lock table */
    2235            0 :             UnlockRelationOid(relid, AccessExclusiveLock);
    2236            0 :             continue;
    2237              :         }
    2238            0 :         classForm = (Form_pg_class) GETSTRUCT(tuple);
    2239              : 
    2240              :         /*
    2241              :          * Make all the same tests made in the loop above.  In event of OID
    2242              :          * counter wraparound, the pg_class entry we have now might be
    2243              :          * completely unrelated to the one we saw before.
    2244              :          */
    2245            0 :         if (!((classForm->relkind == RELKIND_RELATION ||
    2246            0 :                classForm->relkind == RELKIND_MATVIEW) &&
    2247            0 :               classForm->relpersistence == RELPERSISTENCE_TEMP))
    2248              :         {
    2249            0 :             UnlockRelationOid(relid, AccessExclusiveLock);
    2250            0 :             continue;
    2251              :         }
    2252              : 
    2253            0 :         if (checkTempNamespaceStatus(classForm->relnamespace) != TEMP_NAMESPACE_IDLE)
    2254              :         {
    2255            0 :             UnlockRelationOid(relid, AccessExclusiveLock);
    2256            0 :             continue;
    2257              :         }
    2258              : 
    2259              :         /*
    2260              :          * Try to lock the temp namespace, too.  Even though we have lock on
    2261              :          * the table itself, there's a risk of deadlock against an incoming
    2262              :          * backend trying to clean out the temp namespace, in case this table
    2263              :          * has dependencies (such as sequences) that the backend's
    2264              :          * performDeletion call might visit in a different order.  If we can
    2265              :          * get AccessShareLock on the namespace, that's sufficient to ensure
    2266              :          * we're not running concurrently with RemoveTempRelations.  If we
    2267              :          * can't, back off and let RemoveTempRelations do its thing.
    2268              :          */
    2269            0 :         if (!ConditionalLockDatabaseObject(NamespaceRelationId,
    2270              :                                            classForm->relnamespace, 0,
    2271              :                                            AccessShareLock))
    2272              :         {
    2273            0 :             UnlockRelationOid(relid, AccessExclusiveLock);
    2274            0 :             continue;
    2275              :         }
    2276              : 
    2277              :         /* OK, let's delete it */
    2278            0 :         ereport(LOG,
    2279              :                 (errmsg("autovacuum: dropping orphan temp table \"%s.%s.%s\"",
    2280              :                         get_database_name(MyDatabaseId),
    2281              :                         get_namespace_name(classForm->relnamespace),
    2282              :                         NameStr(classForm->relname))));
    2283              : 
    2284              :         /*
    2285              :          * Deletion might involve TOAST table access, so ensure we have a
    2286              :          * valid snapshot.
    2287              :          */
    2288            0 :         PushActiveSnapshot(GetTransactionSnapshot());
    2289              : 
    2290            0 :         object.classId = RelationRelationId;
    2291            0 :         object.objectId = relid;
    2292            0 :         object.objectSubId = 0;
    2293            0 :         performDeletion(&object, DROP_CASCADE,
    2294              :                         PERFORM_DELETION_INTERNAL |
    2295              :                         PERFORM_DELETION_QUIETLY |
    2296              :                         PERFORM_DELETION_SKIP_EXTENSIONS);
    2297              : 
    2298              :         /*
    2299              :          * To commit the deletion, end current transaction and start a new
    2300              :          * one.  Note this also releases the locks we took.
    2301              :          */
    2302            0 :         PopActiveSnapshot();
    2303            0 :         CommitTransactionCommand();
    2304            0 :         StartTransactionCommand();
    2305              : 
    2306              :         /* StartTransactionCommand changed current memory context */
    2307            0 :         MemoryContextSwitchTo(AutovacMemCxt);
    2308              :     }
    2309              : 
    2310              :     /*
    2311              :      * In case list_sort() would modify the list even when all the scores are
    2312              :      * 0.0, skip sorting if all the weight parameters are set to 0.0.  This is
    2313              :      * probably not necessary, but we want to ensure folks have a guaranteed
    2314              :      * escape hatch from the scoring system.
    2315              :      */
    2316         1424 :     if (autovacuum_freeze_score_weight != 0.0 ||
    2317            0 :         autovacuum_multixact_freeze_score_weight != 0.0 ||
    2318            0 :         autovacuum_vacuum_score_weight != 0.0 ||
    2319            0 :         autovacuum_vacuum_insert_score_weight != 0.0 ||
    2320            0 :         autovacuum_analyze_score_weight != 0.0)
    2321         1424 :         list_sort(tables_to_process, TableToProcessComparator);
    2322              : 
    2323              :     /*
    2324              :      * Optionally, create a buffer access strategy object for VACUUM to use.
    2325              :      * We use the same BufferAccessStrategy object for all tables VACUUMed by
    2326              :      * this worker to prevent autovacuum from blowing out shared buffers.
    2327              :      *
    2328              :      * VacuumBufferUsageLimit being set to 0 results in
    2329              :      * GetAccessStrategyWithSize returning NULL, effectively meaning we can
    2330              :      * use up to all of shared buffers.
    2331              :      *
    2332              :      * If we later enter failsafe mode on any of the tables being vacuumed, we
    2333              :      * will cease use of the BufferAccessStrategy only for that table.
    2334              :      *
    2335              :      * XXX should we consider adding code to adjust the size of this if
    2336              :      * VacuumBufferUsageLimit changes?
    2337              :      */
    2338         1424 :     bstrategy = GetAccessStrategyWithSize(BAS_VACUUM, VacuumBufferUsageLimit);
    2339              : 
    2340              :     /*
    2341              :      * create a memory context to act as fake PortalContext, so that the
    2342              :      * contexts created in the vacuum code are cleaned up for each table.
    2343              :      */
    2344         1424 :     PortalContext = AllocSetContextCreate(AutovacMemCxt,
    2345              :                                           "Autovacuum Portal",
    2346              :                                           ALLOCSET_DEFAULT_SIZES);
    2347              : 
    2348              :     /*
    2349              :      * Perform operations on collected tables.
    2350              :      */
    2351       101865 :     foreach_ptr(TableToProcess, table, tables_to_process)
    2352              :     {
    2353        99019 :         Oid         relid = table->oid;
    2354              :         HeapTuple   classTup;
    2355              :         autovac_table *tab;
    2356              :         bool        isshared;
    2357              :         bool        skipit;
    2358              :         dlist_iter  iter;
    2359              : 
    2360        99019 :         CHECK_FOR_INTERRUPTS();
    2361              : 
    2362              :         /*
    2363              :          * Check for config changes before processing each collected table.
    2364              :          */
    2365        99019 :         if (ConfigReloadPending)
    2366              :         {
    2367            0 :             ConfigReloadPending = false;
    2368            0 :             ProcessConfigFile(PGC_SIGHUP);
    2369              : 
    2370              :             /*
    2371              :              * You might be tempted to bail out if we see autovacuum is now
    2372              :              * disabled.  Must resist that temptation -- this might be a
    2373              :              * for-wraparound emergency worker, in which case that would be
    2374              :              * entirely inappropriate.
    2375              :              */
    2376              :         }
    2377              : 
    2378              :         /*
    2379              :          * Find out whether the table is shared or not.  (It's slightly
    2380              :          * annoying to fetch the syscache entry just for this, but in typical
    2381              :          * cases it adds little cost because table_recheck_autovac would
    2382              :          * refetch the entry anyway.  We could buy that back by copying the
    2383              :          * tuple here and passing it to table_recheck_autovac, but that
    2384              :          * increases the odds of that function working with stale data.)
    2385              :          */
    2386        99019 :         classTup = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
    2387        99019 :         if (!HeapTupleIsValid(classTup))
    2388          525 :             continue;           /* somebody deleted the rel, forget it */
    2389        99019 :         isshared = ((Form_pg_class) GETSTRUCT(classTup))->relisshared;
    2390        99019 :         ReleaseSysCache(classTup);
    2391              : 
    2392              :         /*
    2393              :          * Hold schedule lock from here until we've claimed the table.  We
    2394              :          * also need the AutovacuumLock to walk the worker array, but that one
    2395              :          * can just be a shared lock.
    2396              :          */
    2397        99019 :         LWLockAcquire(AutovacuumScheduleLock, LW_EXCLUSIVE);
    2398        99019 :         LWLockAcquire(AutovacuumLock, LW_SHARED);
    2399              : 
    2400              :         /*
    2401              :          * Check whether the table is being vacuumed concurrently by another
    2402              :          * worker.
    2403              :          */
    2404        99019 :         skipit = false;
    2405       316279 :         dlist_foreach(iter, &AutoVacuumShmem->av_runningWorkers)
    2406              :         {
    2407       217782 :             WorkerInfo  worker = dlist_container(WorkerInfoData, wi_links, iter.cur);
    2408              : 
    2409              :             /* ignore myself */
    2410       217782 :             if (worker == MyWorkerInfo)
    2411        98790 :                 continue;
    2412              : 
    2413              :             /* ignore workers in other databases (unless table is shared) */
    2414       118992 :             if (!worker->wi_sharedrel && worker->wi_dboid != MyDatabaseId)
    2415            0 :                 continue;
    2416              : 
    2417       118992 :             if (worker->wi_tableoid == relid)
    2418              :             {
    2419          522 :                 skipit = true;
    2420          522 :                 found_concurrent_worker = true;
    2421          522 :                 break;
    2422              :             }
    2423              :         }
    2424        99019 :         LWLockRelease(AutovacuumLock);
    2425        99019 :         if (skipit)
    2426              :         {
    2427          522 :             LWLockRelease(AutovacuumScheduleLock);
    2428          522 :             continue;
    2429              :         }
    2430              : 
    2431              :         /*
    2432              :          * Store the table's OID in shared memory before releasing the
    2433              :          * schedule lock, so that other workers don't try to vacuum it
    2434              :          * concurrently.  (We claim it here so as not to hold
    2435              :          * AutovacuumScheduleLock while rechecking the stats.)
    2436              :          */
    2437        98497 :         MyWorkerInfo->wi_tableoid = relid;
    2438        98497 :         MyWorkerInfo->wi_sharedrel = isshared;
    2439        98497 :         LWLockRelease(AutovacuumScheduleLock);
    2440              : 
    2441              :         /*
    2442              :          * Check whether pgstat data still says we need to vacuum this table.
    2443              :          * It could have changed if something else processed the table while
    2444              :          * we weren't looking. This doesn't entirely close the race condition,
    2445              :          * but it is very small.
    2446              :          */
    2447        98497 :         MemoryContextSwitchTo(AutovacMemCxt);
    2448        98497 :         tab = table_recheck_autovac(relid, table_toast_map, pg_class_desc,
    2449              :                                     effective_multixact_freeze_max_age);
    2450        98497 :         if (tab == NULL)
    2451              :         {
    2452              :             /* someone else vacuumed the table, or it went away */
    2453            3 :             LWLockAcquire(AutovacuumScheduleLock, LW_EXCLUSIVE);
    2454            3 :             MyWorkerInfo->wi_tableoid = InvalidOid;
    2455            3 :             MyWorkerInfo->wi_sharedrel = false;
    2456            3 :             LWLockRelease(AutovacuumScheduleLock);
    2457            3 :             continue;
    2458              :         }
    2459              : 
    2460              :         /*
    2461              :          * Save the cost-related storage parameter values in global variables
    2462              :          * for reference when updating vacuum_cost_delay and vacuum_cost_limit
    2463              :          * during vacuuming this table.
    2464              :          */
    2465        98494 :         av_storage_param_cost_delay = tab->at_storage_param_vac_cost_delay;
    2466        98494 :         av_storage_param_cost_limit = tab->at_storage_param_vac_cost_limit;
    2467              : 
    2468              :         /*
    2469              :          * We only expect this worker to ever set the flag, so don't bother
    2470              :          * checking the return value. We shouldn't have to retry.
    2471              :          */
    2472        98494 :         if (tab->at_dobalance)
    2473        98494 :             pg_atomic_test_set_flag(&MyWorkerInfo->wi_dobalance);
    2474              :         else
    2475            0 :             pg_atomic_clear_flag(&MyWorkerInfo->wi_dobalance);
    2476              : 
    2477        98494 :         LWLockAcquire(AutovacuumLock, LW_SHARED);
    2478        98494 :         autovac_recalculate_workers_for_balance();
    2479        98494 :         LWLockRelease(AutovacuumLock);
    2480              : 
    2481              :         /*
    2482              :          * We wait until this point to update cost delay and cost limit
    2483              :          * values, even though we reloaded the configuration file above, so
    2484              :          * that we can take into account the cost-related storage parameters.
    2485              :          */
    2486        98494 :         VacuumUpdateCosts();
    2487              : 
    2488              : 
    2489              :         /* clean up memory before each iteration */
    2490        98494 :         MemoryContextReset(PortalContext);
    2491              : 
    2492              :         /*
    2493              :          * Save the relation name for a possible error message, to avoid a
    2494              :          * catalog lookup in case of an error.  If any of these return NULL,
    2495              :          * then the relation has been dropped since last we checked; skip it.
    2496              :          * Note: they must live in a long-lived memory context because we call
    2497              :          * vacuum and analyze in different transactions.
    2498              :          */
    2499              : 
    2500        98494 :         tab->at_relname = get_rel_name(tab->at_relid);
    2501        98494 :         tab->at_nspname = get_namespace_name(get_rel_namespace(tab->at_relid));
    2502        98494 :         tab->at_datname = get_database_name(MyDatabaseId);
    2503        98494 :         if (!tab->at_relname || !tab->at_nspname || !tab->at_datname)
    2504            0 :             goto deleted;
    2505              : 
    2506              :         /*
    2507              :          * We will abort vacuuming the current table if something errors out,
    2508              :          * and continue with the next one in schedule; in particular, this
    2509              :          * happens if we are interrupted with SIGINT.
    2510              :          */
    2511        98494 :         PG_TRY();
    2512              :         {
    2513              :             /* Use PortalContext for any per-table allocations */
    2514        98494 :             MemoryContextSwitchTo(PortalContext);
    2515              : 
    2516              :             /* have at it */
    2517        98494 :             autovacuum_do_vac_analyze(tab, bstrategy);
    2518              : 
    2519              :             /*
    2520              :              * Clear a possible query-cancel signal, to avoid a late reaction
    2521              :              * to an automatically-sent signal because of vacuuming the
    2522              :              * current table (we're done with it, so it would make no sense to
    2523              :              * cancel at this point.)
    2524              :              */
    2525        98493 :             QueryCancelPending = false;
    2526              :         }
    2527            0 :         PG_CATCH();
    2528              :         {
    2529              :             /*
    2530              :              * Abort the transaction, start a new one, and proceed with the
    2531              :              * next table in our list.
    2532              :              */
    2533            0 :             HOLD_INTERRUPTS();
    2534            0 :             if (tab->at_params.options & VACOPT_VACUUM)
    2535            0 :                 errcontext("automatic vacuum of table \"%s.%s.%s\"",
    2536              :                            tab->at_datname, tab->at_nspname, tab->at_relname);
    2537              :             else
    2538            0 :                 errcontext("automatic analyze of table \"%s.%s.%s\"",
    2539              :                            tab->at_datname, tab->at_nspname, tab->at_relname);
    2540            0 :             EmitErrorReport();
    2541              : 
    2542              :             /* this resets ProcGlobal->statusFlags[i] too */
    2543            0 :             AbortOutOfAnyTransaction();
    2544            0 :             FlushErrorState();
    2545            0 :             MemoryContextReset(PortalContext);
    2546              : 
    2547              :             /* restart our transaction for the following operations */
    2548            0 :             StartTransactionCommand();
    2549            0 :             RESUME_INTERRUPTS();
    2550              :         }
    2551        98493 :         PG_END_TRY();
    2552              : 
    2553              :         /* Make sure we're back in AutovacMemCxt */
    2554        98493 :         MemoryContextSwitchTo(AutovacMemCxt);
    2555              : 
    2556        98493 :         did_vacuum = true;
    2557              : 
    2558              :         /* ProcGlobal->statusFlags[i] are reset at the next end of xact */
    2559              : 
    2560              :         /* be tidy */
    2561        98493 : deleted:
    2562        98493 :         if (tab->at_datname != NULL)
    2563        98493 :             pfree(tab->at_datname);
    2564        98493 :         if (tab->at_nspname != NULL)
    2565        98493 :             pfree(tab->at_nspname);
    2566        98493 :         if (tab->at_relname != NULL)
    2567        98493 :             pfree(tab->at_relname);
    2568        98493 :         pfree(tab);
    2569              : 
    2570              :         /*
    2571              :          * Remove my info from shared memory.  We set wi_dobalance on the
    2572              :          * assumption that we are more likely than not to vacuum a table with
    2573              :          * no cost-related storage parameters next, so we want to claim our
    2574              :          * share of I/O as soon as possible to avoid thrashing the global
    2575              :          * balance.
    2576              :          */
    2577        98493 :         LWLockAcquire(AutovacuumScheduleLock, LW_EXCLUSIVE);
    2578        98493 :         MyWorkerInfo->wi_tableoid = InvalidOid;
    2579        98493 :         MyWorkerInfo->wi_sharedrel = false;
    2580        98493 :         LWLockRelease(AutovacuumScheduleLock);
    2581        98493 :         pg_atomic_test_set_flag(&MyWorkerInfo->wi_dobalance);
    2582              :     }
    2583              : 
    2584         1423 :     list_free_deep(tables_to_process);
    2585              : 
    2586              :     /*
    2587              :      * Perform additional work items, as requested by backends.
    2588              :      */
    2589         1423 :     LWLockAcquire(AutovacuumLock, LW_EXCLUSIVE);
    2590       365711 :     for (i = 0; i < NUM_WORKITEMS; i++)
    2591              :     {
    2592       364288 :         AutoVacuumWorkItem *workitem = &AutoVacuumShmem->av_workItems[i];
    2593              : 
    2594       364288 :         if (!workitem->avw_used)
    2595       364282 :             continue;
    2596            6 :         if (workitem->avw_active)
    2597            0 :             continue;
    2598            6 :         if (workitem->avw_database != MyDatabaseId)
    2599            0 :             continue;
    2600              : 
    2601              :         /* claim this one, and release lock while performing it */
    2602            6 :         workitem->avw_active = true;
    2603            6 :         LWLockRelease(AutovacuumLock);
    2604              : 
    2605            6 :         PushActiveSnapshot(GetTransactionSnapshot());
    2606            6 :         perform_work_item(workitem);
    2607            6 :         if (ActiveSnapshotSet())    /* transaction could have aborted */
    2608            6 :             PopActiveSnapshot();
    2609              : 
    2610              :         /*
    2611              :          * Check for config changes before acquiring lock for further jobs.
    2612              :          */
    2613            6 :         CHECK_FOR_INTERRUPTS();
    2614            6 :         if (ConfigReloadPending)
    2615              :         {
    2616            0 :             ConfigReloadPending = false;
    2617            0 :             ProcessConfigFile(PGC_SIGHUP);
    2618            0 :             VacuumUpdateCosts();
    2619              :         }
    2620              : 
    2621            6 :         LWLockAcquire(AutovacuumLock, LW_EXCLUSIVE);
    2622              : 
    2623              :         /* and mark it done */
    2624            6 :         workitem->avw_active = false;
    2625            6 :         workitem->avw_used = false;
    2626              :     }
    2627         1423 :     LWLockRelease(AutovacuumLock);
    2628              : 
    2629              :     /*
    2630              :      * We leak table_toast_map here (among other things), but since we're
    2631              :      * going away soon, it's not a problem normally.  But when using Valgrind,
    2632              :      * release some stuff to reduce complaints about leaked storage.
    2633              :      */
    2634              : #ifdef USE_VALGRIND
    2635              :     hash_destroy(table_toast_map);
    2636              :     FreeTupleDesc(pg_class_desc);
    2637              :     if (bstrategy)
    2638              :         pfree(bstrategy);
    2639              : #endif
    2640              : 
    2641              :     /* Run the rest in xact context, mainly to avoid Valgrind leak warnings */
    2642         1423 :     MemoryContextSwitchTo(TopTransactionContext);
    2643              : 
    2644              :     /*
    2645              :      * Update pg_database.datfrozenxid, and truncate pg_xact if possible. We
    2646              :      * only need to do this once, not after each table.
    2647              :      *
    2648              :      * Even if we didn't vacuum anything, it may still be important to do
    2649              :      * this, because one indirect effect of vac_update_datfrozenxid() is to
    2650              :      * update TransamVariables->xidVacLimit.  That might need to be done even
    2651              :      * if we haven't vacuumed anything, because relations with older
    2652              :      * relfrozenxid values or other databases with older datfrozenxid values
    2653              :      * might have been dropped, allowing xidVacLimit to advance.
    2654              :      *
    2655              :      * However, it's also important not to do this blindly in all cases,
    2656              :      * because when autovacuum=off this will restart the autovacuum launcher.
    2657              :      * If we're not careful, an infinite loop can result, where workers find
    2658              :      * no work to do and restart the launcher, which starts another worker in
    2659              :      * the same database that finds no work to do.  To prevent that, we skip
    2660              :      * this if (1) we found no work to do and (2) we skipped at least one
    2661              :      * table due to concurrent autovacuum activity.  In that case, the other
    2662              :      * worker has already done it, or will do so when it finishes.
    2663              :      */
    2664         1423 :     if (did_vacuum || !found_concurrent_worker)
    2665         1423 :         vac_update_datfrozenxid();
    2666              : 
    2667              :     /* Finally close out the last transaction. */
    2668         1423 :     CommitTransactionCommand();
    2669         1423 : }
    2670              : 
    2671              : /*
    2672              :  * Execute a previously registered work item.
    2673              :  */
    2674              : static void
    2675            6 : perform_work_item(AutoVacuumWorkItem *workitem)
    2676              : {
    2677            6 :     char       *cur_datname = NULL;
    2678            6 :     char       *cur_nspname = NULL;
    2679            6 :     char       *cur_relname = NULL;
    2680              : 
    2681              :     /*
    2682              :      * Note we do not store table info in MyWorkerInfo, since this is not
    2683              :      * vacuuming proper.
    2684              :      */
    2685              : 
    2686              :     /*
    2687              :      * Save the relation name for a possible error message, to avoid a catalog
    2688              :      * lookup in case of an error.  If any of these return NULL, then the
    2689              :      * relation has been dropped since last we checked; skip it.
    2690              :      */
    2691              :     Assert(CurrentMemoryContext == AutovacMemCxt);
    2692              : 
    2693            6 :     cur_relname = get_rel_name(workitem->avw_relation);
    2694            6 :     cur_nspname = get_namespace_name(get_rel_namespace(workitem->avw_relation));
    2695            6 :     cur_datname = get_database_name(MyDatabaseId);
    2696            6 :     if (!cur_relname || !cur_nspname || !cur_datname)
    2697            0 :         goto deleted2;
    2698              : 
    2699            6 :     autovac_report_workitem(workitem, cur_nspname, cur_relname);
    2700              : 
    2701              :     /* clean up memory before each work item */
    2702            6 :     MemoryContextReset(PortalContext);
    2703              : 
    2704              :     /*
    2705              :      * We will abort the current work item if something errors out, and
    2706              :      * continue with the next one; in particular, this happens if we are
    2707              :      * interrupted with SIGINT.  Note that this means that the work item list
    2708              :      * can be lossy.
    2709              :      */
    2710            6 :     PG_TRY();
    2711              :     {
    2712              :         /* Use PortalContext for any per-work-item allocations */
    2713            6 :         MemoryContextSwitchTo(PortalContext);
    2714              : 
    2715              :         /*
    2716              :          * Have at it.  Functions called here are responsible for any required
    2717              :          * user switch and sandbox.
    2718              :          */
    2719            6 :         switch (workitem->avw_type)
    2720              :         {
    2721            6 :             case AVW_BRINSummarizeRange:
    2722            6 :                 DirectFunctionCall2(brin_summarize_range,
    2723              :                                     ObjectIdGetDatum(workitem->avw_relation),
    2724              :                                     Int64GetDatum((int64) workitem->avw_blockNumber));
    2725            6 :                 break;
    2726            0 :             default:
    2727            0 :                 elog(WARNING, "unrecognized work item found: type %d",
    2728              :                      workitem->avw_type);
    2729            0 :                 break;
    2730              :         }
    2731              : 
    2732              :         /*
    2733              :          * Clear a possible query-cancel signal, to avoid a late reaction to
    2734              :          * an automatically-sent signal because of vacuuming the current table
    2735              :          * (we're done with it, so it would make no sense to cancel at this
    2736              :          * point.)
    2737              :          */
    2738            6 :         QueryCancelPending = false;
    2739              :     }
    2740            0 :     PG_CATCH();
    2741              :     {
    2742              :         /*
    2743              :          * Abort the transaction, start a new one, and proceed with the next
    2744              :          * table in our list.
    2745              :          */
    2746            0 :         HOLD_INTERRUPTS();
    2747            0 :         errcontext("processing work entry for relation \"%s.%s.%s\"",
    2748              :                    cur_datname, cur_nspname, cur_relname);
    2749            0 :         EmitErrorReport();
    2750              : 
    2751              :         /* this resets ProcGlobal->statusFlags[i] too */
    2752            0 :         AbortOutOfAnyTransaction();
    2753            0 :         FlushErrorState();
    2754            0 :         MemoryContextReset(PortalContext);
    2755              : 
    2756              :         /* restart our transaction for the following operations */
    2757            0 :         StartTransactionCommand();
    2758            0 :         RESUME_INTERRUPTS();
    2759              :     }
    2760            6 :     PG_END_TRY();
    2761              : 
    2762              :     /* Make sure we're back in AutovacMemCxt */
    2763            6 :     MemoryContextSwitchTo(AutovacMemCxt);
    2764              : 
    2765              :     /* We intentionally do not set did_vacuum here */
    2766              : 
    2767              :     /* be tidy */
    2768            6 : deleted2:
    2769            6 :     if (cur_datname)
    2770            6 :         pfree(cur_datname);
    2771            6 :     if (cur_nspname)
    2772            6 :         pfree(cur_nspname);
    2773            6 :     if (cur_relname)
    2774            6 :         pfree(cur_relname);
    2775            6 : }
    2776              : 
    2777              : /*
    2778              :  * extract_autovac_opts
    2779              :  *
    2780              :  * Given a relation's pg_class tuple, return a palloc'd copy of the
    2781              :  * AutoVacOpts portion of reloptions, if set; otherwise, return NULL.
    2782              :  *
    2783              :  * Note: callers do not have a relation lock on the table at this point,
    2784              :  * so the table could have been dropped, and its catalog rows gone, after
    2785              :  * we acquired the pg_class row.  If pg_class had a TOAST table, this would
    2786              :  * be a risk; fortunately, it doesn't.
    2787              :  */
    2788              : static AutoVacOpts *
    2789       266567 : extract_autovac_opts(HeapTuple tup, TupleDesc pg_class_desc)
    2790              : {
    2791              :     bytea      *relopts;
    2792              :     AutoVacOpts *av;
    2793              : 
    2794              :     Assert(((Form_pg_class) GETSTRUCT(tup))->relkind == RELKIND_RELATION ||
    2795              :            ((Form_pg_class) GETSTRUCT(tup))->relkind == RELKIND_MATVIEW ||
    2796              :            ((Form_pg_class) GETSTRUCT(tup))->relkind == RELKIND_TOASTVALUE);
    2797              : 
    2798       266567 :     relopts = extractRelOptions(tup, pg_class_desc, NULL);
    2799       266567 :     if (relopts == NULL)
    2800       264307 :         return NULL;
    2801              : 
    2802         2260 :     av = palloc_object(AutoVacOpts);
    2803         2260 :     memcpy(av, &(((StdRdOptions *) relopts)->autovacuum), sizeof(AutoVacOpts));
    2804         2260 :     pfree(relopts);
    2805              : 
    2806         2260 :     return av;
    2807              : }
    2808              : 
    2809              : 
    2810              : /*
    2811              :  * table_recheck_autovac
    2812              :  *
    2813              :  * Recheck whether a table still needs vacuum or analyze.  Return value is a
    2814              :  * valid autovac_table pointer if it does, NULL otherwise.
    2815              :  *
    2816              :  * Note that the returned autovac_table does not have the name fields set.
    2817              :  */
    2818              : static autovac_table *
    2819        98497 : table_recheck_autovac(Oid relid, HTAB *table_toast_map,
    2820              :                       TupleDesc pg_class_desc,
    2821              :                       int effective_multixact_freeze_max_age)
    2822              : {
    2823              :     Form_pg_class classForm;
    2824              :     HeapTuple   classTup;
    2825              :     bool        dovacuum;
    2826              :     bool        doanalyze;
    2827        98497 :     autovac_table *tab = NULL;
    2828              :     bool        wraparound;
    2829              :     AutoVacOpts *avopts;
    2830        98497 :     bool        free_avopts = false;
    2831              :     AutoVacuumScores scores;
    2832              : 
    2833              :     /* fetch the relation's relcache entry */
    2834        98497 :     classTup = SearchSysCacheCopy1(RELOID, ObjectIdGetDatum(relid));
    2835        98497 :     if (!HeapTupleIsValid(classTup))
    2836            0 :         return NULL;
    2837        98497 :     classForm = (Form_pg_class) GETSTRUCT(classTup);
    2838              : 
    2839              :     /*
    2840              :      * Get the applicable reloptions.  If it is a TOAST table, try to get the
    2841              :      * main table reloptions if the toast table itself doesn't have.
    2842              :      */
    2843        98497 :     avopts = extract_autovac_opts(classTup, pg_class_desc);
    2844        98497 :     if (avopts)
    2845          969 :         free_avopts = true;
    2846        97528 :     else if (classForm->relkind == RELKIND_TOASTVALUE &&
    2847              :              table_toast_map != NULL)
    2848              :     {
    2849              :         av_relation *hentry;
    2850              :         bool        found;
    2851              : 
    2852        35418 :         hentry = hash_search(table_toast_map, &relid, HASH_FIND, &found);
    2853        35418 :         if (found && hentry->ar_hasrelopts)
    2854          951 :             avopts = &hentry->ar_reloptions;
    2855              :     }
    2856              : 
    2857        98497 :     relation_needs_vacanalyze(relid, avopts, classForm,
    2858              :                               effective_multixact_freeze_max_age,
    2859              :                               DEBUG3,
    2860              :                               &dovacuum, &doanalyze, &wraparound,
    2861              :                               &scores);
    2862              : 
    2863              :     /* OK, it needs something done */
    2864        98497 :     if (doanalyze || dovacuum)
    2865              :     {
    2866              :         int         freeze_min_age;
    2867              :         int         freeze_table_age;
    2868              :         int         multixact_freeze_min_age;
    2869              :         int         multixact_freeze_table_age;
    2870              :         int         log_vacuum_min_duration;
    2871              :         int         log_analyze_min_duration;
    2872              : 
    2873              :         /*
    2874              :          * Calculate the vacuum cost parameters and the freeze ages.  If there
    2875              :          * are options set in pg_class.reloptions, use them; in the case of a
    2876              :          * toast table, try the main table too.  Otherwise use the GUC
    2877              :          * defaults, autovacuum's own first and plain vacuum second.
    2878              :          */
    2879              : 
    2880              :         /* -1 in autovac setting means use log_autovacuum_min_duration */
    2881         1920 :         log_vacuum_min_duration = (avopts && avopts->log_vacuum_min_duration >= 0)
    2882              :             ? avopts->log_vacuum_min_duration
    2883       100414 :             : Log_autovacuum_min_duration;
    2884              : 
    2885              :         /* -1 in autovac setting means use log_autoanalyze_min_duration */
    2886         1920 :         log_analyze_min_duration = (avopts && avopts->log_analyze_min_duration >= 0)
    2887              :             ? avopts->log_analyze_min_duration
    2888       100414 :             : Log_autoanalyze_min_duration;
    2889              : 
    2890              :         /* these do not have autovacuum-specific settings */
    2891         1920 :         freeze_min_age = (avopts && avopts->freeze_min_age >= 0)
    2892              :             ? avopts->freeze_min_age
    2893       100414 :             : default_freeze_min_age;
    2894              : 
    2895         1920 :         freeze_table_age = (avopts && avopts->freeze_table_age >= 0)
    2896              :             ? avopts->freeze_table_age
    2897       100414 :             : default_freeze_table_age;
    2898              : 
    2899       100414 :         multixact_freeze_min_age = (avopts &&
    2900         1920 :                                     avopts->multixact_freeze_min_age >= 0)
    2901              :             ? avopts->multixact_freeze_min_age
    2902       100414 :             : default_multixact_freeze_min_age;
    2903              : 
    2904       100414 :         multixact_freeze_table_age = (avopts &&
    2905         1920 :                                       avopts->multixact_freeze_table_age >= 0)
    2906              :             ? avopts->multixact_freeze_table_age
    2907       100414 :             : default_multixact_freeze_table_age;
    2908              : 
    2909        98494 :         tab = palloc_object(autovac_table);
    2910        98494 :         tab->at_relid = relid;
    2911              : 
    2912              :         /*
    2913              :          * Select VACUUM options.  Note we don't say VACOPT_PROCESS_TOAST, so
    2914              :          * that vacuum() skips toast relations.  Also note we tell vacuum() to
    2915              :          * skip vac_update_datfrozenxid(); we'll do that separately.
    2916              :          */
    2917        98494 :         tab->at_params.options =
    2918        98494 :             (dovacuum ? (VACOPT_VACUUM |
    2919              :                          VACOPT_PROCESS_MAIN |
    2920        98494 :                          VACOPT_SKIP_DATABASE_STATS) : 0) |
    2921        98494 :             (doanalyze ? VACOPT_ANALYZE : 0) |
    2922        98494 :             (!wraparound ? VACOPT_SKIP_LOCKED : 0);
    2923              : 
    2924              :         /*
    2925              :          * index_cleanup and truncate are unspecified at first in autovacuum.
    2926              :          * They will be filled in with usable values using their reloptions
    2927              :          * (or reloption defaults) later.
    2928              :          */
    2929        98494 :         tab->at_params.index_cleanup = VACOPTVALUE_UNSPECIFIED;
    2930        98494 :         tab->at_params.truncate = VACOPTVALUE_UNSPECIFIED;
    2931        98494 :         tab->at_params.freeze_min_age = freeze_min_age;
    2932        98494 :         tab->at_params.freeze_table_age = freeze_table_age;
    2933        98494 :         tab->at_params.multixact_freeze_min_age = multixact_freeze_min_age;
    2934        98494 :         tab->at_params.multixact_freeze_table_age = multixact_freeze_table_age;
    2935        98494 :         tab->at_params.is_wraparound = wraparound;
    2936        98494 :         tab->at_params.log_vacuum_min_duration = log_vacuum_min_duration;
    2937        98494 :         tab->at_params.log_analyze_min_duration = log_analyze_min_duration;
    2938        98494 :         tab->at_params.toast_parent = InvalidOid;
    2939              : 
    2940              :         /* Determine the number of parallel vacuum workers to use */
    2941        98494 :         tab->at_params.nworkers = 0;
    2942        98494 :         if (avopts)
    2943              :         {
    2944         1920 :             if (avopts->autovacuum_parallel_workers == 0)
    2945              :             {
    2946              :                 /*
    2947              :                  * Disable parallel vacuum, if the reloption sets the parallel
    2948              :                  * degree as zero.
    2949              :                  */
    2950            0 :                 tab->at_params.nworkers = -1;
    2951              :             }
    2952         1920 :             else if (avopts->autovacuum_parallel_workers > 0)
    2953            1 :                 tab->at_params.nworkers = avopts->autovacuum_parallel_workers;
    2954              : 
    2955              :             /*
    2956              :              * autovacuum_parallel_workers == -1 falls through, keep
    2957              :              * nworkers=0
    2958              :              */
    2959              :         }
    2960              : 
    2961              :         /*
    2962              :          * Later, in vacuum_rel(), we check reloptions for any
    2963              :          * vacuum_max_eager_freeze_failure_rate override.
    2964              :          */
    2965        98494 :         tab->at_params.max_eager_freeze_failure_rate = vacuum_max_eager_freeze_failure_rate;
    2966        98494 :         tab->at_storage_param_vac_cost_limit = avopts ?
    2967        98494 :             avopts->vacuum_cost_limit : 0;
    2968        98494 :         tab->at_storage_param_vac_cost_delay = avopts ?
    2969        98494 :             avopts->vacuum_cost_delay : -1;
    2970        98494 :         tab->at_relname = NULL;
    2971        98494 :         tab->at_nspname = NULL;
    2972        98494 :         tab->at_datname = NULL;
    2973              : 
    2974              :         /*
    2975              :          * If any of the cost delay parameters has been set individually for
    2976              :          * this table, disable the balancing algorithm.
    2977              :          */
    2978        98494 :         tab->at_dobalance =
    2979       100414 :             !(avopts && (avopts->vacuum_cost_limit > 0 ||
    2980       100414 :                          avopts->vacuum_cost_delay >= 0));
    2981              :     }
    2982              : 
    2983        98497 :     if (free_avopts)
    2984          969 :         pfree(avopts);
    2985        98497 :     heap_freetuple(classTup);
    2986        98497 :     return tab;
    2987              : }
    2988              : 
    2989              : /*
    2990              :  * relation_needs_vacanalyze
    2991              :  *
    2992              :  * Check whether a relation needs to be vacuumed or analyzed; return each into
    2993              :  * "dovacuum" and "doanalyze", respectively.  Also return whether the vacuum is
    2994              :  * being forced because of Xid or multixact wraparound.
    2995              :  *
    2996              :  * relopts is a pointer to the AutoVacOpts options (either for itself in the
    2997              :  * case of a plain table, or for either itself or its parent table in the case
    2998              :  * of a TOAST table), NULL if none.
    2999              :  *
    3000              :  * A table needs to be vacuumed if the number of dead tuples exceeds a
    3001              :  * threshold.  This threshold is calculated as
    3002              :  *
    3003              :  * threshold = vac_base_thresh + vac_scale_factor * reltuples
    3004              :  * if (threshold > vac_max_thresh)
    3005              :  *     threshold = vac_max_thresh;
    3006              :  *
    3007              :  * For analyze, the analysis done is that the number of tuples inserted,
    3008              :  * deleted and updated since the last analyze exceeds a threshold calculated
    3009              :  * in the same fashion as above.  Note that the cumulative stats system stores
    3010              :  * the number of tuples (both live and dead) that there were as of the last
    3011              :  * analyze.  This is asymmetric to the VACUUM case.
    3012              :  *
    3013              :  * We also force vacuum if the table's relfrozenxid is more than freeze_max_age
    3014              :  * transactions back, and if its relminmxid is more than
    3015              :  * multixact_freeze_max_age multixacts back.
    3016              :  *
    3017              :  * A table whose autovacuum_enabled option is false is
    3018              :  * automatically skipped (unless we have to vacuum it due to freeze_max_age).
    3019              :  * Thus autovacuum can be disabled for specific tables. Also, when the cumulative
    3020              :  * stats system does not have data about a table, it will be skipped.
    3021              :  *
    3022              :  * A table whose vac_base_thresh value is < 0 takes the base value from the
    3023              :  * autovacuum_vacuum_threshold GUC variable.  Similarly, a vac_scale_factor
    3024              :  * value < 0 is substituted with the value of
    3025              :  * autovacuum_vacuum_scale_factor GUC variable.  Ditto for analyze.
    3026              :  *
    3027              :  * This function also returns scores that can be used to sort the list of
    3028              :  * tables to process.  The idea is to have autovacuum prioritize tables that
    3029              :  * are furthest beyond their thresholds (e.g., a table nearing transaction ID
    3030              :  * wraparound should be vacuumed first).  This prioritization scheme is
    3031              :  * certainly far from perfect; there are simply too many possibilities for any
    3032              :  * scoring technique to work across all workloads, and the situation might
    3033              :  * change significantly between the time we calculate the score and the time
    3034              :  * that autovacuum processes it.  However, we have attempted to develop
    3035              :  * something that is expected to work for a large portion of workloads with
    3036              :  * reasonable parameter settings.
    3037              :  *
    3038              :  * The autovacuum table score is calculated as the maximum of the ratios of
    3039              :  * each of the table's relevant values to its threshold.  For example, if the
    3040              :  * number of inserted tuples is 100, and the insert threshold for the table is
    3041              :  * 80, the insert score is 1.25.  If all other scores are below that value, the
    3042              :  * returned score will be 1.25.  The other criteria considered for the score
    3043              :  * are the table ages (both relfrozenxid and relminmxid) compared to the
    3044              :  * corresponding freeze-max-age setting, the number of updated/deleted tuples
    3045              :  * compared to the vacuum threshold, and the number of inserted/updated/deleted
    3046              :  * tuples compared to the analyze threshold.
    3047              :  *
    3048              :  * One exception to the previous paragraph is for tables nearing wraparound,
    3049              :  * i.e., those that have surpassed the effective failsafe ages.  In that case,
    3050              :  * the relfrozen/relminmxid-based score is scaled aggressively so that the
    3051              :  * table has a decent chance of sorting to the front of the list.
    3052              :  *
    3053              :  * To adjust how strongly each component contributes to the score, the
    3054              :  * following parameters can be adjusted from their default of 1.0 to anywhere
    3055              :  * between 0.0 and 10.0 (inclusive).  Setting all of these to 0.0 restores
    3056              :  * pre-v19 prioritization behavior:
    3057              :  *
    3058              :  *     autovacuum_freeze_score_weight
    3059              :  *     autovacuum_multixact_freeze_score_weight
    3060              :  *     autovacuum_vacuum_score_weight
    3061              :  *     autovacuum_vacuum_insert_score_weight
    3062              :  *     autovacuum_analyze_score_weight
    3063              :  *
    3064              :  * The autovacuum table score is returned in scores->max.  The component scores
    3065              :  * are also returned in the "scores" argument via the other members of the
    3066              :  * AutoVacuumScores struct.
    3067              :  */
    3068              : static void
    3069       266567 : relation_needs_vacanalyze(Oid relid,
    3070              :                           AutoVacOpts *relopts,
    3071              :                           Form_pg_class classForm,
    3072              :                           int effective_multixact_freeze_max_age,
    3073              :                           int elevel,
    3074              :  /* output params below */
    3075              :                           bool *dovacuum,
    3076              :                           bool *doanalyze,
    3077              :                           bool *wraparound,
    3078              :                           AutoVacuumScores *scores)
    3079              : {
    3080              :     PgStat_StatTabEntry *tabentry;
    3081              :     bool        force_vacuum;
    3082              :     bool        av_enabled;
    3083       266567 :     bool        may_free = false;
    3084              : 
    3085              :     /* constants from reloptions or GUC variables */
    3086              :     int         vac_base_thresh,
    3087              :                 vac_max_thresh,
    3088              :                 vac_ins_base_thresh,
    3089              :                 anl_base_thresh;
    3090              :     float4      vac_scale_factor,
    3091              :                 vac_ins_scale_factor,
    3092              :                 anl_scale_factor;
    3093              : 
    3094              :     /* thresholds calculated from above constants */
    3095              :     float4      vacthresh,
    3096              :                 vacinsthresh,
    3097              :                 anlthresh;
    3098              : 
    3099              :     /* number of vacuum (resp. analyze) tuples at this time */
    3100              :     float4      vactuples,
    3101              :                 instuples,
    3102              :                 anltuples;
    3103              : 
    3104              :     /* freeze parameters */
    3105              :     int         freeze_max_age;
    3106              :     int         multixact_freeze_max_age;
    3107              :     TransactionId xidForceLimit;
    3108              :     TransactionId relfrozenxid;
    3109              :     MultiXactId relminmxid;
    3110              :     MultiXactId multiForceLimit;
    3111              :     uint32      xid_age;
    3112              :     uint32      mxid_age;
    3113              :     int         effective_xid_failsafe_age;
    3114              :     int         effective_mxid_failsafe_age;
    3115              : 
    3116       266567 :     float4      pcnt_unfrozen = 1;
    3117       266567 :     float4      reltuples = classForm->reltuples;
    3118       266567 :     int32       relpages = classForm->relpages;
    3119       266567 :     int32       relallfrozen = classForm->relallfrozen;
    3120              : 
    3121              :     Assert(classForm != NULL);
    3122              :     Assert(OidIsValid(relid));
    3123              : 
    3124       266567 :     memset(scores, 0, sizeof(AutoVacuumScores));
    3125       266567 :     *dovacuum = false;
    3126       266567 :     *doanalyze = false;
    3127              : 
    3128              :     /*
    3129              :      * Determine vacuum/analyze equation parameters.  We have two possible
    3130              :      * sources: the passed reloptions (which could be a main table or a toast
    3131              :      * table), or the autovacuum GUC variables.
    3132              :      */
    3133              : 
    3134              :     /* -1 in autovac setting means use plain vacuum_scale_factor */
    3135         4453 :     vac_scale_factor = (relopts && relopts->vacuum_scale_factor >= 0)
    3136            0 :         ? relopts->vacuum_scale_factor
    3137       271020 :         : autovacuum_vac_scale;
    3138              : 
    3139         4453 :     vac_base_thresh = (relopts && relopts->vacuum_threshold >= 0)
    3140              :         ? relopts->vacuum_threshold
    3141       271020 :         : autovacuum_vac_thresh;
    3142              : 
    3143              :     /* -1 is used to disable max threshold */
    3144         4453 :     vac_max_thresh = (relopts && relopts->vacuum_max_threshold >= -1)
    3145              :         ? relopts->vacuum_max_threshold
    3146       271020 :         : autovacuum_vac_max_thresh;
    3147              : 
    3148         4453 :     vac_ins_scale_factor = (relopts && relopts->vacuum_ins_scale_factor >= 0)
    3149            0 :         ? relopts->vacuum_ins_scale_factor
    3150       271020 :         : autovacuum_vac_ins_scale;
    3151              : 
    3152              :     /* -1 is used to disable insert vacuums */
    3153         4453 :     vac_ins_base_thresh = (relopts && relopts->vacuum_ins_threshold >= -1)
    3154              :         ? relopts->vacuum_ins_threshold
    3155       271020 :         : autovacuum_vac_ins_thresh;
    3156              : 
    3157         4453 :     anl_scale_factor = (relopts && relopts->analyze_scale_factor >= 0)
    3158            0 :         ? relopts->analyze_scale_factor
    3159       271020 :         : autovacuum_anl_scale;
    3160              : 
    3161         4453 :     anl_base_thresh = (relopts && relopts->analyze_threshold >= 0)
    3162              :         ? relopts->analyze_threshold
    3163       271020 :         : autovacuum_anl_thresh;
    3164              : 
    3165         4453 :     freeze_max_age = (relopts && relopts->freeze_max_age >= 0)
    3166            0 :         ? Min(relopts->freeze_max_age, autovacuum_freeze_max_age)
    3167       271020 :         : autovacuum_freeze_max_age;
    3168              : 
    3169         4453 :     multixact_freeze_max_age = (relopts && relopts->multixact_freeze_max_age >= 0)
    3170            0 :         ? Min(relopts->multixact_freeze_max_age, effective_multixact_freeze_max_age)
    3171       271020 :         : effective_multixact_freeze_max_age;
    3172              : 
    3173       266567 :     av_enabled = (relopts ? relopts->enabled : true);
    3174       266567 :     av_enabled &= AutoVacuumingActive();
    3175              : 
    3176       266567 :     relfrozenxid = classForm->relfrozenxid;
    3177       266567 :     relminmxid = classForm->relminmxid;
    3178              : 
    3179              :     /* Force vacuum if table is at risk of wraparound */
    3180       266567 :     xidForceLimit = recentXid - freeze_max_age;
    3181       266567 :     if (xidForceLimit < FirstNormalTransactionId)
    3182            0 :         xidForceLimit -= FirstNormalTransactionId;
    3183       533134 :     force_vacuum = (TransactionIdIsNormal(relfrozenxid) &&
    3184       266567 :                     TransactionIdPrecedes(relfrozenxid, xidForceLimit));
    3185       266567 :     if (!force_vacuum)
    3186              :     {
    3187        69862 :         multiForceLimit = recentMulti - multixact_freeze_max_age;
    3188        69862 :         if (multiForceLimit < FirstMultiXactId)
    3189            0 :             multiForceLimit -= FirstMultiXactId;
    3190       139724 :         force_vacuum = MultiXactIdIsValid(relminmxid) &&
    3191        69862 :             MultiXactIdPrecedes(relminmxid, multiForceLimit);
    3192              :     }
    3193       266567 :     *wraparound = force_vacuum;
    3194              : 
    3195              :     /*
    3196              :      * To calculate the (M)XID age portion of the score, divide the age by its
    3197              :      * respective *_freeze_max_age parameter.
    3198              :      */
    3199       266567 :     xid_age = TransactionIdIsNormal(relfrozenxid) ? recentXid - relfrozenxid : 0;
    3200       266567 :     mxid_age = MultiXactIdIsValid(relminmxid) ? recentMulti - relminmxid : 0;
    3201              : 
    3202       266567 :     scores->xid = (double) xid_age / freeze_max_age;
    3203       266567 :     scores->mxid = (double) mxid_age / multixact_freeze_max_age;
    3204              : 
    3205              :     /*
    3206              :      * To ensure tables are given increased priority once they begin
    3207              :      * approaching wraparound, we scale the score aggressively if the ages
    3208              :      * surpass vacuum_failsafe_age or vacuum_multixact_failsafe_age.
    3209              :      *
    3210              :      * As in vacuum_xid_failsafe_check(), the effective failsafe age is no
    3211              :      * less than 105% the value of the respective *_freeze_max_age parameter.
    3212              :      * Note that per-table settings could result in a low score even if the
    3213              :      * table surpasses the failsafe settings.  However, this is a strange
    3214              :      * enough corner case that we don't bother trying to handle it.
    3215              :      *
    3216              :      * We further adjust the effective failsafe ages with the weight
    3217              :      * parameters so that increasing them lowers the ages at which we begin
    3218              :      * scaling aggressively.
    3219              :      */
    3220       266567 :     effective_xid_failsafe_age = Max(vacuum_failsafe_age,
    3221              :                                      autovacuum_freeze_max_age * 1.05);
    3222       266567 :     effective_mxid_failsafe_age = Max(vacuum_multixact_failsafe_age,
    3223              :                                       autovacuum_multixact_freeze_max_age * 1.05);
    3224              : 
    3225       266567 :     if (autovacuum_freeze_score_weight > 1.0)
    3226            0 :         effective_xid_failsafe_age /= autovacuum_freeze_score_weight;
    3227       266567 :     if (autovacuum_multixact_freeze_score_weight > 1.0)
    3228            0 :         effective_mxid_failsafe_age /= autovacuum_multixact_freeze_score_weight;
    3229              : 
    3230       266567 :     if (xid_age >= effective_xid_failsafe_age)
    3231        47420 :         scores->xid = pow(scores->xid, Max(1.0, (double) xid_age / 100000000));
    3232       266567 :     if (mxid_age >= effective_mxid_failsafe_age)
    3233            0 :         scores->mxid = pow(scores->mxid, Max(1.0, (double) mxid_age / 100000000));
    3234              : 
    3235       266567 :     scores->xid *= autovacuum_freeze_score_weight;
    3236       266567 :     scores->mxid *= autovacuum_multixact_freeze_score_weight;
    3237              : 
    3238       266567 :     scores->max = Max(scores->xid, scores->mxid);
    3239       266567 :     if (force_vacuum)
    3240       196705 :         *dovacuum = true;
    3241              : 
    3242              :     /*
    3243              :      * If we found stats for the table, and autovacuum is currently enabled,
    3244              :      * make a threshold-based decision whether to vacuum and/or analyze.  If
    3245              :      * autovacuum is currently disabled, we must be here for anti-wraparound
    3246              :      * vacuuming only, so don't vacuum (or analyze) anything that's not being
    3247              :      * forced.
    3248              :      */
    3249       266567 :     tabentry = pgstat_fetch_stat_tabentry_ext(classForm->relisshared,
    3250              :                                               relid, &may_free);
    3251       266567 :     if (!tabentry)
    3252         4804 :         return;
    3253              : 
    3254       261763 :     vactuples = tabentry->dead_tuples;
    3255       261763 :     instuples = tabentry->ins_since_vacuum;
    3256       261763 :     anltuples = tabentry->mod_since_analyze;
    3257              : 
    3258              :     /* If the table hasn't yet been vacuumed, take reltuples as zero */
    3259       261763 :     if (reltuples < 0)
    3260         1950 :         reltuples = 0;
    3261              : 
    3262              :     /*
    3263              :      * If we have data for relallfrozen, calculate the unfrozen percentage of
    3264              :      * the table to modify insert scale factor. This helps us decide whether
    3265              :      * or not to vacuum an insert-heavy table based on the number of inserts
    3266              :      * to the more "active" part of the table.
    3267              :      */
    3268       261763 :     if (relpages > 0 && relallfrozen > 0)
    3269              :     {
    3270              :         /*
    3271              :          * It could be the stats were updated manually and relallfrozen >
    3272              :          * relpages. Clamp relallfrozen to relpages to avoid nonsensical
    3273              :          * calculations.
    3274              :          */
    3275        95281 :         relallfrozen = Min(relallfrozen, relpages);
    3276        95281 :         pcnt_unfrozen = 1 - ((float4) relallfrozen / relpages);
    3277              :     }
    3278              : 
    3279       261763 :     vacthresh = (float4) vac_base_thresh + vac_scale_factor * reltuples;
    3280       261763 :     if (vac_max_thresh >= 0 && vacthresh > (float4) vac_max_thresh)
    3281            0 :         vacthresh = (float4) vac_max_thresh;
    3282              : 
    3283       261763 :     vacinsthresh = (float4) vac_ins_base_thresh +
    3284       261763 :         vac_ins_scale_factor * reltuples * pcnt_unfrozen;
    3285       261763 :     anlthresh = (float4) anl_base_thresh + anl_scale_factor * reltuples;
    3286              : 
    3287              :     /* Determine if this table needs vacuum, and update the score. */
    3288       261763 :     scores->vac = (double) vactuples / Max(vacthresh, 1);
    3289       261763 :     scores->vac *= autovacuum_vacuum_score_weight;
    3290       261763 :     scores->max = Max(scores->max, scores->vac);
    3291       261763 :     if (av_enabled && vactuples > vacthresh)
    3292          219 :         *dovacuum = true;
    3293              : 
    3294       261763 :     if (vac_ins_base_thresh >= 0)
    3295              :     {
    3296       261763 :         scores->vac_ins = (double) instuples / Max(vacinsthresh, 1);
    3297       261763 :         scores->vac_ins *= autovacuum_vacuum_insert_score_weight;
    3298       261763 :         scores->max = Max(scores->max, scores->vac_ins);
    3299       261763 :         if (av_enabled && instuples > vacinsthresh)
    3300          172 :             *dovacuum = true;
    3301              :     }
    3302              : 
    3303              :     /*
    3304              :      * Determine if this table needs analyze, and update the score.  Note that
    3305              :      * we don't analyze TOAST tables and pg_statistic.
    3306              :      */
    3307       261763 :     if (relid != StatisticRelationId &&
    3308       259526 :         classForm->relkind != RELKIND_TOASTVALUE)
    3309              :     {
    3310       165590 :         scores->anl = (double) anltuples / Max(anlthresh, 1);
    3311       165590 :         scores->anl *= autovacuum_analyze_score_weight;
    3312       165590 :         scores->max = Max(scores->max, scores->anl);
    3313       165590 :         if (av_enabled && anltuples > anlthresh)
    3314          769 :             *doanalyze = true;
    3315              :     }
    3316              : 
    3317       261763 :     if (vac_ins_base_thresh >= 0)
    3318       261763 :         elog(elevel, "%s: vac: %.0f (thresh %.0f, score %.2f), ins: %.0f (thresh %.0f, score %.2f), anl: %.0f (thresh %.0f, score %.2f), xid score: %.2f, mxid score: %.2f",
    3319              :              NameStr(classForm->relname),
    3320              :              vactuples, vacthresh, scores->vac,
    3321              :              instuples, vacinsthresh, scores->vac_ins,
    3322              :              anltuples, anlthresh, scores->anl,
    3323              :              scores->xid, scores->mxid);
    3324              :     else
    3325            0 :         elog(elevel, "%s: vac: %.0f (thresh %.0f, score %.2f), ins: (disabled), anl: %.0f (thresh %.0f, score %.2f), xid score: %.2f, mxid score: %.2f",
    3326              :              NameStr(classForm->relname),
    3327              :              vactuples, vacthresh, scores->vac,
    3328              :              anltuples, anlthresh, scores->anl,
    3329              :              scores->xid, scores->mxid);
    3330              : 
    3331              :     /* Avoid leaking pgstat entries until the end of autovacuum. */
    3332       261763 :     if (may_free)
    3333       261763 :         pfree(tabentry);
    3334              : }
    3335              : 
    3336              : /*
    3337              :  * autovacuum_do_vac_analyze
    3338              :  *      Vacuum and/or analyze the specified table
    3339              :  *
    3340              :  * We expect the caller to have switched into a memory context that won't
    3341              :  * disappear at transaction commit.
    3342              :  */
    3343              : static void
    3344        98494 : autovacuum_do_vac_analyze(autovac_table *tab, BufferAccessStrategy bstrategy)
    3345              : {
    3346              :     RangeVar   *rangevar;
    3347              :     VacuumRelation *rel;
    3348              :     List       *rel_list;
    3349              :     MemoryContext vac_context;
    3350              :     MemoryContext old_context;
    3351              : 
    3352              :     /* Let pgstat know what we're doing */
    3353        98494 :     autovac_report_activity(tab);
    3354              : 
    3355              :     /* Create a context that vacuum() can use as cross-transaction storage */
    3356        98494 :     vac_context = AllocSetContextCreate(CurrentMemoryContext,
    3357              :                                         "Vacuum",
    3358              :                                         ALLOCSET_DEFAULT_SIZES);
    3359              : 
    3360              :     /* Set up one VacuumRelation target, identified by OID, for vacuum() */
    3361        98494 :     old_context = MemoryContextSwitchTo(vac_context);
    3362        98494 :     rangevar = makeRangeVar(tab->at_nspname, tab->at_relname, -1);
    3363        98494 :     rel = makeVacuumRelation(rangevar, tab->at_relid, NIL);
    3364        98494 :     rel_list = list_make1(rel);
    3365        98494 :     MemoryContextSwitchTo(old_context);
    3366              : 
    3367        98494 :     vacuum(rel_list, &tab->at_params, bstrategy, vac_context, true);
    3368              : 
    3369        98493 :     MemoryContextDelete(vac_context);
    3370        98493 : }
    3371              : 
    3372              : /*
    3373              :  * autovac_report_activity
    3374              :  *      Report to pgstat what autovacuum is doing
    3375              :  *
    3376              :  * We send a SQL string corresponding to what the user would see if the
    3377              :  * equivalent command was to be issued manually.
    3378              :  *
    3379              :  * Note we assume that we are going to report the next command as soon as we're
    3380              :  * done with the current one, and exit right after the last one, so we don't
    3381              :  * bother to report "<IDLE>" or some such.
    3382              :  */
    3383              : static void
    3384        98494 : autovac_report_activity(autovac_table *tab)
    3385              : {
    3386              : #define MAX_AUTOVAC_ACTIV_LEN (NAMEDATALEN * 2 + 56)
    3387              :     char        activity[MAX_AUTOVAC_ACTIV_LEN];
    3388              :     int         len;
    3389              : 
    3390              :     /* Report the command and possible options */
    3391        98494 :     if (tab->at_params.options & VACOPT_VACUUM)
    3392        98260 :         snprintf(activity, MAX_AUTOVAC_ACTIV_LEN,
    3393              :                  "autovacuum: VACUUM%s",
    3394        98260 :                  tab->at_params.options & VACOPT_ANALYZE ? " ANALYZE" : "");
    3395              :     else
    3396          234 :         snprintf(activity, MAX_AUTOVAC_ACTIV_LEN,
    3397              :                  "autovacuum: ANALYZE");
    3398              : 
    3399              :     /*
    3400              :      * Report the qualified name of the relation.
    3401              :      */
    3402        98494 :     len = strlen(activity);
    3403              : 
    3404        98494 :     snprintf(activity + len, MAX_AUTOVAC_ACTIV_LEN - len,
    3405              :              " %s.%s%s", tab->at_nspname, tab->at_relname,
    3406        98494 :              tab->at_params.is_wraparound ? " (to prevent wraparound)" : "");
    3407              : 
    3408              :     /* Set statement_timestamp() to current time for pg_stat_activity */
    3409        98494 :     SetCurrentStatementStartTimestamp();
    3410              : 
    3411        98494 :     pgstat_report_activity(STATE_RUNNING, activity);
    3412        98494 : }
    3413              : 
    3414              : /*
    3415              :  * autovac_report_workitem
    3416              :  *      Report to pgstat that autovacuum is processing a work item
    3417              :  */
    3418              : static void
    3419            6 : autovac_report_workitem(AutoVacuumWorkItem *workitem,
    3420              :                         const char *nspname, const char *relname)
    3421              : {
    3422              :     char        activity[MAX_AUTOVAC_ACTIV_LEN + 12 + 2];
    3423              :     char        blk[12 + 2];
    3424              :     int         len;
    3425              : 
    3426            6 :     switch (workitem->avw_type)
    3427              :     {
    3428            6 :         case AVW_BRINSummarizeRange:
    3429            6 :             snprintf(activity, MAX_AUTOVAC_ACTIV_LEN,
    3430              :                      "autovacuum: BRIN summarize");
    3431            6 :             break;
    3432              :     }
    3433              : 
    3434              :     /*
    3435              :      * Report the qualified name of the relation, and the block number if any
    3436              :      */
    3437            6 :     len = strlen(activity);
    3438              : 
    3439            6 :     if (BlockNumberIsValid(workitem->avw_blockNumber))
    3440            6 :         snprintf(blk, sizeof(blk), " %u", workitem->avw_blockNumber);
    3441              :     else
    3442            0 :         blk[0] = '\0';
    3443              : 
    3444            6 :     snprintf(activity + len, MAX_AUTOVAC_ACTIV_LEN - len,
    3445              :              " %s.%s%s", nspname, relname, blk);
    3446              : 
    3447              :     /* Set statement_timestamp() to current time for pg_stat_activity */
    3448            6 :     SetCurrentStatementStartTimestamp();
    3449              : 
    3450            6 :     pgstat_report_activity(STATE_RUNNING, activity);
    3451            6 : }
    3452              : 
    3453              : /*
    3454              :  * AutoVacuumingActive
    3455              :  *      Check GUC vars and report whether the autovacuum process should be
    3456              :  *      running.
    3457              :  */
    3458              : bool
    3459       312815 : AutoVacuumingActive(void)
    3460              : {
    3461       312815 :     if (!autovacuum_start_daemon || !pgstat_track_counts)
    3462         5209 :         return false;
    3463       307606 :     return true;
    3464              : }
    3465              : 
    3466              : /*
    3467              :  * Request one work item to the next autovacuum run processing our database.
    3468              :  * Return false if the request can't be recorded.
    3469              :  */
    3470              : bool
    3471            6 : AutoVacuumRequestWork(AutoVacuumWorkItemType type, Oid relationId,
    3472              :                       BlockNumber blkno)
    3473              : {
    3474              :     int         i;
    3475            6 :     bool        result = false;
    3476              : 
    3477            6 :     LWLockAcquire(AutovacuumLock, LW_EXCLUSIVE);
    3478              : 
    3479              :     /*
    3480              :      * Locate an unused work item and fill it with the given data.
    3481              :      */
    3482           21 :     for (i = 0; i < NUM_WORKITEMS; i++)
    3483              :     {
    3484           21 :         AutoVacuumWorkItem *workitem = &AutoVacuumShmem->av_workItems[i];
    3485              : 
    3486           21 :         if (workitem->avw_used)
    3487           15 :             continue;
    3488              : 
    3489            6 :         workitem->avw_used = true;
    3490            6 :         workitem->avw_active = false;
    3491            6 :         workitem->avw_type = type;
    3492            6 :         workitem->avw_database = MyDatabaseId;
    3493            6 :         workitem->avw_relation = relationId;
    3494            6 :         workitem->avw_blockNumber = blkno;
    3495            6 :         result = true;
    3496              : 
    3497              :         /* done */
    3498            6 :         break;
    3499              :     }
    3500              : 
    3501            6 :     LWLockRelease(AutovacuumLock);
    3502              : 
    3503            6 :     return result;
    3504              : }
    3505              : 
    3506              : /*
    3507              :  * autovac_init
    3508              :  *      This is called at postmaster initialization.
    3509              :  *
    3510              :  * All we do here is annoy the user if he got it wrong.
    3511              :  */
    3512              : void
    3513          983 : autovac_init(void)
    3514              : {
    3515          983 :     if (!autovacuum_start_daemon)
    3516          124 :         return;
    3517          859 :     else if (!pgstat_track_counts)
    3518            0 :         ereport(WARNING,
    3519              :                 (errmsg("autovacuum not started because of misconfiguration"),
    3520              :                  errhint("Enable the \"track_counts\" option.")));
    3521              :     else
    3522          859 :         check_av_worker_gucs();
    3523              : }
    3524              : 
    3525              : /*
    3526              :  * AutoVacuumShmemRequest
    3527              :  *      Register shared memory space needed for autovacuum
    3528              :  */
    3529              : static void
    3530         1238 : AutoVacuumShmemRequest(void *arg)
    3531              : {
    3532              :     Size        size;
    3533              : 
    3534              :     /*
    3535              :      * Need the fixed struct and the array of WorkerInfoData.
    3536              :      */
    3537         1238 :     size = sizeof(AutoVacuumShmemStruct);
    3538         1238 :     size = MAXALIGN(size);
    3539         1238 :     size = add_size(size, mul_size(autovacuum_worker_slots,
    3540              :                                    sizeof(WorkerInfoData)));
    3541              : 
    3542         1238 :     ShmemRequestStruct(.name = "AutoVacuum Data",
    3543              :                        .size = size,
    3544              :                        .ptr = (void **) &AutoVacuumShmem,
    3545              :         );
    3546         1238 : }
    3547              : 
    3548              : /*
    3549              :  * AutoVacuumShmemInit
    3550              :  *      Initialize autovacuum-related shared memory
    3551              :  */
    3552              : static void
    3553         1235 : AutoVacuumShmemInit(void *arg)
    3554              : {
    3555              :     WorkerInfo  worker;
    3556              : 
    3557         1235 :     AutoVacuumShmem->av_launcherpid = 0;
    3558         1235 :     dclist_init(&AutoVacuumShmem->av_freeWorkers);
    3559         1235 :     dlist_init(&AutoVacuumShmem->av_runningWorkers);
    3560         1235 :     AutoVacuumShmem->av_startingWorker = NULL;
    3561         1235 :     memset(AutoVacuumShmem->av_workItems, 0,
    3562              :            sizeof(AutoVacuumWorkItem) * NUM_WORKITEMS);
    3563              : 
    3564         1235 :     worker = (WorkerInfo) ((char *) AutoVacuumShmem +
    3565              :                            MAXALIGN(sizeof(AutoVacuumShmemStruct)));
    3566              : 
    3567              :     /* initialize the WorkerInfo free list */
    3568        15026 :     for (int i = 0; i < autovacuum_worker_slots; i++)
    3569              :     {
    3570        13791 :         dclist_push_head(&AutoVacuumShmem->av_freeWorkers,
    3571        13791 :                          &worker[i].wi_links);
    3572        13791 :         pg_atomic_init_flag(&worker[i].wi_dobalance);
    3573              :     }
    3574              : 
    3575         1235 :     pg_atomic_init_u32(&AutoVacuumShmem->av_nworkersForBalance, 0);
    3576         1235 : }
    3577              : 
    3578              : /*
    3579              :  * GUC check_hook for autovacuum_work_mem
    3580              :  */
    3581              : bool
    3582         1281 : check_autovacuum_work_mem(int *newval, void **extra, GucSource source)
    3583              : {
    3584              :     /*
    3585              :      * -1 indicates fallback.
    3586              :      *
    3587              :      * If we haven't yet changed the boot_val default of -1, just let it be.
    3588              :      * Autovacuum will look to maintenance_work_mem instead.
    3589              :      */
    3590         1281 :     if (*newval == -1)
    3591         1279 :         return true;
    3592              : 
    3593              :     /*
    3594              :      * We clamp manually-set values to at least 64kB.  Since
    3595              :      * maintenance_work_mem is always set to at least this value, do the same
    3596              :      * here.
    3597              :      */
    3598            2 :     if (*newval < 64)
    3599            2 :         *newval = 64;
    3600              : 
    3601            2 :     return true;
    3602              : }
    3603              : 
    3604              : /*
    3605              :  * Returns whether there is a free autovacuum worker slot available.
    3606              :  */
    3607              : static bool
    3608        11524 : av_worker_available(void)
    3609              : {
    3610              :     int         free_slots;
    3611              :     int         reserved_slots;
    3612              : 
    3613        11524 :     free_slots = dclist_count(&AutoVacuumShmem->av_freeWorkers);
    3614              : 
    3615        11524 :     reserved_slots = autovacuum_worker_slots - autovacuum_max_workers;
    3616        11524 :     reserved_slots = Max(0, reserved_slots);
    3617              : 
    3618        11524 :     return free_slots > reserved_slots;
    3619              : }
    3620              : 
    3621              : /*
    3622              :  * Emits a WARNING if autovacuum_worker_slots < autovacuum_max_workers.
    3623              :  */
    3624              : static void
    3625          859 : check_av_worker_gucs(void)
    3626              : {
    3627          859 :     if (autovacuum_worker_slots < autovacuum_max_workers)
    3628            0 :         ereport(WARNING,
    3629              :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    3630              :                  errmsg("\"autovacuum_max_workers\" (%d) should be less than or equal to \"autovacuum_worker_slots\" (%d)",
    3631              :                         autovacuum_max_workers, autovacuum_worker_slots),
    3632              :                  errdetail("The server will only start up to \"autovacuum_worker_slots\" (%d) autovacuum workers at a given time.",
    3633              :                            autovacuum_worker_slots)));
    3634          859 : }
    3635              : 
    3636              : /*
    3637              :  * pg_stat_get_autovacuum_scores
    3638              :  *
    3639              :  * Returns current autovacuum scores for all relevant tables in the current
    3640              :  * database.
    3641              :  */
    3642              : Datum
    3643            0 : pg_stat_get_autovacuum_scores(PG_FUNCTION_ARGS)
    3644              : {
    3645              :     int         effective_multixact_freeze_max_age;
    3646              :     Relation    rel;
    3647              :     TableScanDesc scan;
    3648              :     HeapTuple   tup;
    3649            0 :     ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
    3650              : 
    3651            0 :     InitMaterializedSRF(fcinfo, 0);
    3652              : 
    3653              :     /* some prerequisite initialization */
    3654            0 :     effective_multixact_freeze_max_age = MultiXactMemberFreezeThreshold();
    3655            0 :     recentXid = ReadNextTransactionId();
    3656            0 :     recentMulti = ReadNextMultiXactId();
    3657              : 
    3658              :     /* scan pg_class */
    3659            0 :     rel = table_open(RelationRelationId, AccessShareLock);
    3660            0 :     scan = table_beginscan_catalog(rel, 0, NULL);
    3661            0 :     while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL)
    3662              :     {
    3663            0 :         Form_pg_class form = (Form_pg_class) GETSTRUCT(tup);
    3664              :         AutoVacOpts *avopts;
    3665              :         bool        dovacuum;
    3666              :         bool        doanalyze;
    3667              :         bool        wraparound;
    3668              :         AutoVacuumScores scores;
    3669              :         Datum       vals[10];
    3670            0 :         bool        nulls[10] = {false};
    3671              : 
    3672              :         /* skip ineligible entries */
    3673            0 :         if (form->relkind != RELKIND_RELATION &&
    3674            0 :             form->relkind != RELKIND_MATVIEW &&
    3675            0 :             form->relkind != RELKIND_TOASTVALUE)
    3676            0 :             continue;
    3677            0 :         if (form->relpersistence == RELPERSISTENCE_TEMP)
    3678            0 :             continue;
    3679              : 
    3680            0 :         avopts = extract_autovac_opts(tup, RelationGetDescr(rel));
    3681            0 :         relation_needs_vacanalyze(form->oid, avopts, form,
    3682              :                                   effective_multixact_freeze_max_age,
    3683              :                                   LOG_NEVER,
    3684              :                                   &dovacuum, &doanalyze, &wraparound,
    3685              :                                   &scores);
    3686            0 :         if (avopts)
    3687            0 :             pfree(avopts);
    3688              : 
    3689            0 :         vals[0] = ObjectIdGetDatum(form->oid);
    3690            0 :         vals[1] = Float8GetDatum(scores.max);
    3691            0 :         vals[2] = Float8GetDatum(scores.xid);
    3692            0 :         vals[3] = Float8GetDatum(scores.mxid);
    3693            0 :         vals[4] = Float8GetDatum(scores.vac);
    3694            0 :         vals[5] = Float8GetDatum(scores.vac_ins);
    3695            0 :         vals[6] = Float8GetDatum(scores.anl);
    3696            0 :         vals[7] = BoolGetDatum(dovacuum);
    3697            0 :         vals[8] = BoolGetDatum(doanalyze);
    3698            0 :         vals[9] = BoolGetDatum(wraparound);
    3699              : 
    3700            0 :         tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, vals, nulls);
    3701              :     }
    3702            0 :     table_endscan(scan);
    3703            0 :     table_close(rel, AccessShareLock);
    3704              : 
    3705            0 :     return (Datum) 0;
    3706              : }
        

Generated by: LCOV version 2.0-1