Age Owner Branch data TLA 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
494 peter@eisentraut.org 413 :CBC 452 : AutoVacLauncherMain(const void *startup_data, size_t startup_data_len)
414 : : {
415 : : sigjmp_buf local_sigjmp_buf;
416 : :
834 heikki.linnakangas@i 417 [ - + ]: 452 : Assert(startup_data_len == 0);
418 : :
419 : : /* Release postmaster's working memory context */
420 [ + - ]: 452 : if (PostmasterContext)
421 : : {
422 : 452 : MemoryContextDelete(PostmasterContext);
423 : 452 : PostmasterContext = NULL;
424 : : }
425 : :
2302 peter@eisentraut.org 426 : 452 : init_ps_display(NULL);
427 : :
3399 tgl@sss.pgh.pa.us 428 [ + + ]: 452 : ereport(DEBUG1,
429 : : (errmsg_internal("autovacuum launcher started")));
430 : :
6824 alvherre@alvh.no-ip. 431 [ - + ]: 452 : if (PostAuthDelay)
6824 alvherre@alvh.no-ip. 432 :UBC 0 : pg_usleep(PostAuthDelay * 1000000L);
433 : :
728 heikki.linnakangas@i 434 [ - + ]:CBC 452 : 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 : : */
2387 rhaas@postgresql.org 441 : 452 : pqsignal(SIGHUP, SignalHandlerForConfigReload);
6147 tgl@sss.pgh.pa.us 442 : 452 : pqsignal(SIGINT, StatementCancelHandler);
2387 rhaas@postgresql.org 443 : 452 : pqsignal(SIGTERM, SignalHandlerForShutdownRequest);
444 : : /* SIGQUIT handler was already set up by InitPostmasterChild */
445 : :
5097 alvherre@alvh.no-ip. 446 : 452 : InitializeTimeouts(); /* establishes SIGALRM handler */
447 : :
77 andrew@dunslane.net 448 :GNC 452 : pqsignal(SIGPIPE, PG_SIG_IGN);
6147 tgl@sss.pgh.pa.us 449 :CBC 452 : pqsignal(SIGUSR1, procsignal_sigusr1_handler);
450 : 452 : pqsignal(SIGUSR2, avl_sigusr2_handler);
7075 alvherre@alvh.no-ip. 451 : 452 : pqsignal(SIGFPE, FloatExceptionHandler);
77 andrew@dunslane.net 452 :GNC 452 : 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 : : */
6147 tgl@sss.pgh.pa.us 458 :CBC 452 : InitProcess();
459 : :
460 : : /* Early initialization */
1790 andres@anarazel.de 461 : 452 : BaseInit();
462 : :
993 michael@paquier.xyz 463 : 452 : InitPostgres(NULL, InvalidOid, NULL, InvalidOid, 0, NULL);
464 : :
6147 tgl@sss.pgh.pa.us 465 : 452 : 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 : : */
7015 alvherre@alvh.no-ip. 472 : 452 : AutovacMemCxt = AllocSetContextCreate(TopMemoryContext,
473 : : "Autovacuum Launcher",
474 : : ALLOCSET_DEFAULT_SIZES);
475 : 452 : 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 : : */
7075 489 [ - + ]: 452 : if (sigsetjmp(local_sigjmp_buf, 1) != 0)
490 : : {
491 : : /* since not using PG_TRY, must reset error stack by hand */
7075 alvherre@alvh.no-ip. 492 :UBC 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 */
5097 498 : 0 : disable_all_timeouts(false);
3296 tgl@sss.pgh.pa.us 499 : 0 : QueryCancelPending = false; /* second to avoid race condition */
500 : :
501 : : /* Report the error to the server log */
7075 alvherre@alvh.no-ip. 502 : 0 : EmitErrorReport();
503 : :
504 : : /* Abort the current transaction in order to recover */
6147 tgl@sss.pgh.pa.us 505 : 0 : AbortCurrentTransaction();
506 : :
507 : : /*
508 : : * Release any other resources, for the case where we were not in a
509 : : * transaction.
510 : : */
3241 alvherre@alvh.no-ip. 511 : 0 : LWLockReleaseAll();
512 : 0 : pgstat_report_wait_end();
470 andres@anarazel.de 513 : 0 : pgaio_error_cleanup();
3241 alvherre@alvh.no-ip. 514 : 0 : UnlockBuffers();
515 : : /* this is probably dead code, but let's be safe: */
2904 tgl@sss.pgh.pa.us 516 [ # # ]: 0 : if (AuxProcessResourceOwner)
517 : 0 : ReleaseAuxProcessResources(false);
3241 alvherre@alvh.no-ip. 518 : 0 : AtEOXact_Buffers(false);
519 : 0 : AtEOXact_SMgr();
2985 tgl@sss.pgh.pa.us 520 : 0 : AtEOXact_Files(false);
3241 alvherre@alvh.no-ip. 521 : 0 : AtEOXact_HashTables(false);
522 : :
523 : : /*
524 : : * Now return to normal top-level context and clear ErrorContext for
525 : : * next time.
526 : : */
7015 527 : 0 : MemoryContextSwitchTo(AutovacMemCxt);
7075 528 : 0 : FlushErrorState();
529 : :
530 : : /* Flush any leaked data in the top-level context */
958 nathan@postgresql.or 531 : 0 : MemoryContextReset(AutovacMemCxt);
532 : :
533 : : /* don't leave dangling pointers to freed memory */
7015 alvherre@alvh.no-ip. 534 : 0 : DatabaseListCxt = NULL;
5005 535 : 0 : dlist_init(&DatabaseList);
536 : :
537 : : /* Now we can allow interrupts again */
7075 538 [ # # ]: 0 : RESUME_INTERRUPTS();
539 : :
540 : : /* if in shutdown mode, no need for anything further; just go away */
2387 rhaas@postgresql.org 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 : : */
7075 alvherre@alvh.no-ip. 548 : 0 : pg_usleep(1000000L);
549 : : }
550 : :
551 : : /* We can now handle ereport(ERROR) */
7075 alvherre@alvh.no-ip. 552 :CBC 452 : PG_exception_stack = &local_sigjmp_buf;
553 : :
554 : : /* must unblock signals before calling rebuild_database_list */
1243 tmunro@postgresql.or 555 : 452 : 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 : : */
3046 noah@leadboat.com 561 : 452 : 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 : : */
5327 tgl@sss.pgh.pa.us 568 : 452 : 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 : 452 : SetConfigOption("statement_timeout", "0", PGC_SUSET, PGC_S_OVERRIDE);
866 akorotkov@postgresql 575 : 452 : SetConfigOption("transaction_timeout", "0", PGC_SUSET, PGC_S_OVERRIDE);
4854 tgl@sss.pgh.pa.us 576 : 452 : SetConfigOption("lock_timeout", "0", PGC_SUSET, PGC_S_OVERRIDE);
3667 577 : 452 : 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 : : */
5327 585 : 452 : 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 : : */
1546 andres@anarazel.de 592 : 452 : 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 : : */
6854 tgl@sss.pgh.pa.us 598 [ - + ]: 452 : if (!AutoVacuumingActive())
599 : : {
2387 rhaas@postgresql.org 600 [ # # ]:UBC 0 : if (!ShutdownRequestPending)
4101 alvherre@alvh.no-ip. 601 : 0 : do_start_worker();
6802 bruce@momjian.us 602 : 0 : proc_exit(0); /* done */
603 : : }
604 : :
7015 alvherre@alvh.no-ip. 605 :CBC 452 : 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 : 452 : rebuild_database_list(InvalidOid);
615 : :
616 : : /* loop until shutdown request */
2387 rhaas@postgresql.org 617 [ + + ]: 2256 : while (!ShutdownRequestPending)
618 : : {
619 : : struct timeval nap;
7015 alvherre@alvh.no-ip. 620 : 2254 : 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 : :
540 nathan@postgresql.or 630 : 2254 : 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 : : */
2776 tmunro@postgresql.or 636 : 2254 : (void) WaitLatch(MyLatch,
637 : : WL_LATCH_SET | WL_TIMEOUT | WL_EXIT_ON_PM_DEATH,
638 : 2254 : (nap.tv_sec * 1000L) + (nap.tv_usec / 1000L),
639 : : WAIT_EVENT_AUTOVACUUM_MAIN);
640 : :
4185 andres@anarazel.de 641 : 2251 : ResetLatch(MyLatch);
642 : :
482 heikki.linnakangas@i 643 : 2251 : ProcessAutoVacLauncherInterrupts();
644 : :
645 : : /*
646 : : * a worker finished, or postmaster signaled failure to start a worker
647 : : */
6147 tgl@sss.pgh.pa.us 648 [ + + ]: 1804 : if (got_SIGUSR2)
649 : : {
650 : 262 : got_SIGUSR2 = false;
651 : :
652 : : /* rebalance cost limits, if needed */
6945 alvherre@alvh.no-ip. 653 [ + + ]: 262 : if (AutoVacuumShmem->av_signal[AutoVacRebalance])
654 : : {
7015 655 : 130 : LWLockAcquire(AutovacuumLock, LW_EXCLUSIVE);
6945 656 : 130 : AutoVacuumShmem->av_signal[AutoVacRebalance] = false;
1180 dgustafsson@postgres 657 : 130 : autovac_recalculate_workers_for_balance();
7015 alvherre@alvh.no-ip. 658 : 130 : LWLockRelease(AutovacuumLock);
659 : : }
660 : :
6945 661 [ - + ]: 262 : 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 : : */
6945 alvherre@alvh.no-ip. 673 :UBC 0 : AutoVacuumShmem->av_signal[AutoVacForkFailed] = false;
5968 bruce@momjian.us 674 : 0 : pg_usleep(1000000L); /* 1s */
6945 alvherre@alvh.no-ip. 675 : 0 : SendPostmasterSignal(PMSIGNAL_START_AUTOVAC_WORKER);
6945 alvherre@alvh.no-ip. 676 :GBC 17 : 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 : :
6945 alvherre@alvh.no-ip. 687 :CBC 1804 : current_time = GetCurrentTimestamp();
7075 688 : 1804 : LWLockAcquire(AutovacuumLock, LW_SHARED);
689 : :
540 nathan@postgresql.or 690 : 1804 : can_launch = av_worker_available();
691 : :
6449 tgl@sss.pgh.pa.us 692 [ - + ]: 1804 : if (AutoVacuumShmem->av_startingWorker != NULL)
693 : : {
694 : : int waittime;
6449 tgl@sss.pgh.pa.us 695 :UBC 0 : 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 : : */
6945 alvherre@alvh.no-ip. 712 : 0 : waittime = Min(autovacuum_naptime, 60) * 1000;
6999 713 [ # # ]: 0 : if (TimestampDifferenceExceeds(worker->wi_launchtime, current_time,
714 : : waittime))
715 : : {
7015 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 : : */
6449 tgl@sss.pgh.pa.us 725 [ # # ]: 0 : if (AutoVacuumShmem->av_startingWorker != NULL)
726 : : {
727 : 0 : worker = AutoVacuumShmem->av_startingWorker;
7015 alvherre@alvh.no-ip. 728 : 0 : worker->wi_dboid = InvalidOid;
729 : 0 : worker->wi_tableoid = InvalidOid;
3703 730 : 0 : worker->wi_sharedrel = false;
6824 731 : 0 : worker->wi_proc = NULL;
7015 732 : 0 : worker->wi_launchtime = 0;
540 nathan@postgresql.or 733 : 0 : dclist_push_head(&AutoVacuumShmem->av_freeWorkers,
734 : : &worker->wi_links);
6449 tgl@sss.pgh.pa.us 735 : 0 : AutoVacuumShmem->av_startingWorker = NULL;
1681 alvherre@alvh.no-ip. 736 [ # # ]: 0 : ereport(WARNING,
737 : : errmsg("autovacuum worker took too long to start; canceled"));
738 : : }
739 : : }
740 : : else
7015 741 : 0 : can_launch = false;
742 : : }
6802 bruce@momjian.us 743 :CBC 1804 : LWLockRelease(AutovacuumLock); /* either shared or exclusive */
744 : :
745 : : /* if we can't do anything, just go back to sleep */
6945 alvherre@alvh.no-ip. 746 [ + + ]: 1804 : if (!can_launch)
6945 alvherre@alvh.no-ip. 747 :GBC 17 : continue;
748 : :
749 : : /* We're OK to start a new worker */
750 : :
5005 alvherre@alvh.no-ip. 751 [ + + ]:CBC 1787 : 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 : : */
6945 alvherre@alvh.no-ip. 761 :GBC 6 : 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 : :
5005 alvherre@alvh.no-ip. 772 :CBC 1781 : 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 [ + + ]: 1781 : if (TimestampDifferenceExceeds(avdb->adl_next_worker,
779 : : current_time, 0))
780 : 133 : launch_worker(current_time);
781 : : }
782 : : }
783 : :
2387 rhaas@postgresql.org 784 : 2 : AutoVacLauncherShutdown();
785 : : }
786 : :
787 : : /*
788 : : * Process any new interrupts.
789 : : */
790 : : static void
482 heikki.linnakangas@i 791 : 2251 : ProcessAutoVacLauncherInterrupts(void)
792 : : {
793 : : /* the normal shutdown case */
2387 rhaas@postgresql.org 794 [ + + ]: 2251 : if (ShutdownRequestPending)
795 : 446 : AutoVacLauncherShutdown();
796 : :
797 [ + + ]: 1805 : if (ConfigReloadPending)
798 : : {
540 nathan@postgresql.or 799 : 50 : int autovacuum_max_workers_prev = autovacuum_max_workers;
800 : :
2387 rhaas@postgresql.org 801 : 50 : ConfigReloadPending = false;
802 : 50 : ProcessConfigFile(PGC_SIGHUP);
803 : :
804 : : /* shutdown requested in config file? */
805 [ + + ]: 50 : 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 : : */
540 nathan@postgresql.or 813 [ - + ]: 49 : if (autovacuum_max_workers_prev != autovacuum_max_workers)
540 nathan@postgresql.or 814 :UBC 0 : check_av_worker_gucs();
815 : :
816 : : /* rebuild the list in case the naptime changed */
2387 rhaas@postgresql.org 817 :CBC 49 : rebuild_database_list(InvalidOid);
818 : : }
819 : :
820 : : /* Process barrier events */
2385 821 [ + + ]: 1804 : if (ProcSignalBarrierPending)
822 : 80 : ProcessProcSignalBarrier();
823 : :
824 : : /* Perform logging of memory contexts of this process */
1722 fujii@postgresql.org 825 [ - + ]: 1804 : if (LogMemoryContextPending)
1722 fujii@postgresql.org 826 :UBC 0 : ProcessLogMemoryContextInterrupt();
827 : :
828 : : /* Process sinval catchup interrupts that happened while sleeping */
2387 rhaas@postgresql.org 829 :CBC 1804 : ProcessCatchupInterrupt();
830 : 1804 : }
831 : :
832 : : /*
833 : : * Perform a normal exit from the autovac launcher.
834 : : */
835 : : static void
2231 noah@leadboat.com 836 : 449 : AutoVacLauncherShutdown(void)
837 : : {
3399 tgl@sss.pgh.pa.us 838 [ + + ]: 449 : ereport(DEBUG1,
839 : : (errmsg_internal("autovacuum launcher shutting down")));
7015 alvherre@alvh.no-ip. 840 : 449 : AutoVacuumShmem->av_launcherpid = 0;
841 : :
6802 bruce@momjian.us 842 : 449 : 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
3296 tgl@sss.pgh.pa.us 853 : 2257 : 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 : : */
7015 alvherre@alvh.no-ip. 861 [ + + ]: 2257 : if (!canlaunch)
862 : : {
6957 alvherre@alvh.no-ip. 863 :GBC 33 : nap->tv_sec = autovacuum_naptime;
864 : 33 : nap->tv_usec = 0;
865 : : }
5005 alvherre@alvh.no-ip. 866 [ + + ]:CBC 2224 : else if (!dlist_is_empty(&DatabaseList))
867 : : {
6802 bruce@momjian.us 868 : 2197 : TimestampTz current_time = GetCurrentTimestamp();
869 : : TimestampTz next_wakeup;
870 : : avl_dbase *avdb;
871 : : long secs;
872 : : int usecs;
873 : :
5005 alvherre@alvh.no-ip. 874 : 2197 : avdb = dlist_tail_element(avl_dbase, adl_node, &DatabaseList);
875 : :
7015 876 : 2197 : next_wakeup = avdb->adl_next_worker;
877 : 2197 : TimestampDifference(current_time, next_wakeup, &secs, &usecs);
878 : :
6957 879 : 2197 : nap->tv_sec = secs;
880 : 2197 : nap->tv_usec = usecs;
881 : : }
882 : : else
883 : : {
884 : : /* list is empty, sleep for whole autovacuum_naptime seconds */
885 : 27 : nap->tv_sec = autovacuum_naptime;
886 : 27 : 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 : : */
6939 tgl@sss.pgh.pa.us 899 [ + + + + : 2257 : if (nap->tv_sec == 0 && nap->tv_usec == 0 && !recursing)
+ - ]
900 : : {
7015 alvherre@alvh.no-ip. 901 : 3 : rebuild_database_list(InvalidOid);
6957 902 : 3 : launcher_determine_sleep(canlaunch, true, nap);
903 : 3 : return;
904 : : }
905 : :
906 : : /* The smallest time we'll allow the launcher to sleep. */
6230 907 [ + + + + ]: 2254 : if (nap->tv_sec <= 0 && nap->tv_usec <= MIN_AUTOVAC_SLEEPTIME * 1000)
908 : : {
6939 tgl@sss.pgh.pa.us 909 : 37 : nap->tv_sec = 0;
6230 alvherre@alvh.no-ip. 910 : 37 : 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 : : */
4029 919 [ + + ]: 2254 : if (nap->tv_sec > MAX_AUTOVAC_SLEEPTIME)
920 : 10 : 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
7015 937 : 513 : 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 : 513 : newcxt = AllocSetContextCreate(AutovacMemCxt,
951 : : "Autovacuum database list",
952 : : ALLOCSET_DEFAULT_SIZES);
953 : 513 : tmpcxt = AllocSetContextCreate(newcxt,
954 : : "Autovacuum database list (tmp)",
955 : : ALLOCSET_DEFAULT_SIZES);
956 : 513 : 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 : 513 : hctl.keysize = sizeof(Oid);
975 : 513 : hctl.entrysize = sizeof(avl_dbase);
976 : 513 : hctl.hcxt = tmpcxt;
1510 tgl@sss.pgh.pa.us 977 : 513 : 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 */
7015 alvherre@alvh.no-ip. 982 : 513 : score = 0;
983 [ + + ]: 513 : 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 : 9 : entry = pgstat_fetch_stat_dbentry(newdb);
990 [ + - ]: 9 : if (entry != NULL)
991 : : {
992 : : /* we assume it isn't found because the hash was just created */
993 : 9 : db = hash_search(dbhash, &newdb, HASH_ENTER, NULL);
994 : :
995 : : /* hash_search already filled in the key */
996 : 9 : db->adl_score = score++;
997 : : /* next_worker is filled in later */
998 : : }
999 : : }
1000 : :
1001 : : /* Now insert the databases from the existing list */
5005 1002 [ + - + + ]: 628 : dlist_foreach(iter, &DatabaseList)
1003 : : {
1004 : 115 : 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 : 115 : entry = pgstat_fetch_stat_dbentry(avdb->adl_datid);
1014 [ - + ]: 115 : if (entry == NULL)
5005 alvherre@alvh.no-ip. 1015 :UBC 0 : continue;
1016 : :
5005 alvherre@alvh.no-ip. 1017 :CBC 115 : db = hash_search(dbhash, &(avdb->adl_datid), HASH_ENTER, &found);
1018 : :
1019 [ + - ]: 115 : if (!found)
1020 : : {
1021 : : /* hash_search already filled in the key */
1022 : 115 : db->adl_score = score++;
1023 : : /* next_worker is filled in later */
1024 : : }
1025 : : }
1026 : :
1027 : : /* finally, insert all qualifying databases not previously inserted */
7015 1028 : 513 : dblist = get_database_list();
1029 [ + - + + : 2370 : foreach(cell, dblist)
+ + ]
1030 : : {
1031 : 1857 : 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 : 1857 : entry = pgstat_fetch_stat_dbentry(avdb->adw_datid);
1038 [ + + ]: 1857 : if (entry == NULL)
1039 : 1068 : continue;
1040 : :
1041 : 789 : 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 [ + + ]: 789 : if (!found)
1044 : : {
1045 : : /* hash_search already filled in the key */
1046 : 665 : db->adl_score = score++;
1047 : : /* next_worker is filled in later */
1048 : : }
1049 : : }
1050 : 513 : nelems = score;
1051 : :
1052 : : /* from here on, the allocated memory belongs to the new list */
1053 : 513 : MemoryContextSwitchTo(newcxt);
5005 1054 : 513 : dlist_init(&DatabaseList);
1055 : :
7015 1056 [ + + ]: 513 : 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 : 492 : dbary = palloc(nelems * sizeof(avl_dbase));
1067 : : /* keep Valgrind quiet */
1068 : : #ifdef USE_VALGRIND
1069 : : avl_dbase_array = dbary;
1070 : : #endif
1071 : :
1072 : 492 : i = 0;
1073 : 492 : hash_seq_init(&seq, dbhash);
1074 [ + + ]: 1281 : while ((db = hash_seq_search(&seq)) != NULL)
1075 : 789 : memcpy(&(dbary[i++]), db, sizeof(avl_dbase));
1076 : :
1077 : : /* sort the array */
1078 : 492 : 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 : 492 : millis_increment = 1000.0 * autovacuum_naptime / nelems;
6230 1088 [ - + ]: 492 : if (millis_increment <= MIN_AUTOVAC_SLEEPTIME)
6230 alvherre@alvh.no-ip. 1089 :UBC 0 : millis_increment = MIN_AUTOVAC_SLEEPTIME * 1.1;
1090 : :
7015 alvherre@alvh.no-ip. 1091 :CBC 492 : 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 [ + + ]: 1281 : for (i = 0; i < nelems; i++)
1098 : : {
1404 drowley@postgresql.o 1099 : 789 : db = &(dbary[i]);
1100 : :
7015 alvherre@alvh.no-ip. 1101 : 789 : current_time = TimestampTzPlusMilliseconds(current_time,
1102 : : millis_increment);
1103 : 789 : db->adl_next_worker = current_time;
1104 : :
1105 : : /* later elements should go closer to the head of the list */
5005 1106 : 789 : dlist_push_head(&DatabaseList, &db->adl_node);
1107 : : }
1108 : : }
1109 : :
1110 : : /* all done, clean up memory */
7015 1111 [ + + ]: 513 : if (DatabaseListCxt != NULL)
1112 : 61 : MemoryContextDelete(DatabaseListCxt);
1113 : 513 : MemoryContextDelete(tmpcxt);
1114 : 513 : DatabaseListCxt = newcxt;
1115 : 513 : MemoryContextSwitchTo(oldcxt);
1116 : 513 : }
1117 : :
1118 : : /* qsort comparator for avl_dbase, using adl_score */
1119 : : static int
1120 : 453 : db_comparator(const void *a, const void *b)
1121 : : {
865 nathan@postgresql.or 1122 : 906 : return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
1123 : 453 : ((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
7039 alvherre@alvh.no-ip. 1138 : 139 : 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;
7015 1148 : 139 : bool skipit = false;
6866 1149 : 139 : Oid retval = InvalidOid;
1150 : : MemoryContext tmpcxt,
1151 : : oldcxt;
1152 : :
1153 : : /* return quickly when there are no free workers */
7015 1154 : 139 : LWLockAcquire(AutovacuumLock, LW_SHARED);
540 nathan@postgresql.or 1155 [ - + ]: 139 : if (!av_worker_available())
1156 : : {
7015 alvherre@alvh.no-ip. 1157 :UBC 0 : LWLockRelease(AutovacuumLock);
1158 : 0 : return InvalidOid;
1159 : : }
7015 alvherre@alvh.no-ip. 1160 :CBC 139 : LWLockRelease(AutovacuumLock);
1161 : :
1162 : : /*
1163 : : * Create and switch to a temporary context to avoid leaking the memory
1164 : : * allocated for the database list.
1165 : : */
6866 1166 : 139 : tmpcxt = AllocSetContextCreate(CurrentMemoryContext,
1167 : : "Autovacuum start worker (tmp)",
1168 : : ALLOCSET_DEFAULT_SIZES);
1169 : 139 : oldcxt = MemoryContextSwitchTo(tmpcxt);
1170 : :
1171 : : /* Get a list of databases */
7015 1172 : 139 : 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 : : */
1961 tmunro@postgresql.or 1179 : 139 : recentXid = ReadNextTransactionId();
7039 alvherre@alvh.no-ip. 1180 : 139 : 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 [ - + ]: 139 : if (xidForceLimit < FirstNormalTransactionId)
7039 alvherre@alvh.no-ip. 1184 :UBC 0 : xidForceLimit -= FirstNormalTransactionId;
1185 : :
1186 : : /* Also determine the oldest datminmxid we will consider. */
4906 alvherre@alvh.no-ip. 1187 :CBC 139 : recentMulti = ReadNextMultiXactId();
4071 rhaas@postgresql.org 1188 : 139 : multiForceLimit = recentMulti - MultiXactMemberFreezeThreshold();
4906 alvherre@alvh.no-ip. 1189 [ - + ]: 139 : if (multiForceLimit < FirstMultiXactId)
4906 alvherre@alvh.no-ip. 1190 :UBC 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 : : */
7015 alvherre@alvh.no-ip. 1213 :CBC 139 : avdb = NULL;
7039 1214 : 139 : for_xid_wrap = false;
4906 1215 : 139 : for_multi_wrap = false;
7015 1216 : 139 : current_time = GetCurrentTimestamp();
7039 1217 [ + - + + : 775 : foreach(cell, dblist)
+ + ]
1218 : : {
7015 1219 : 636 : avw_dbase *tmp = lfirst(cell);
1220 : : dlist_iter iter;
1221 : :
1222 : : /* Check to see if this one is at risk of wraparound */
1223 [ - + ]: 636 : if (TransactionIdPrecedes(tmp->adw_frozenxid, xidForceLimit))
1224 : : {
7015 alvherre@alvh.no-ip. 1225 [ # # # # ]:UBC 0 : if (avdb == NULL ||
4906 1226 : 0 : TransactionIdPrecedes(tmp->adw_frozenxid,
1227 : : avdb->adw_frozenxid))
7015 1228 : 0 : avdb = tmp;
7039 1229 : 0 : for_xid_wrap = true;
7039 alvherre@alvh.no-ip. 1230 :CBC 474 : continue;
1231 : : }
1232 [ - + ]: 636 : else if (for_xid_wrap)
7039 alvherre@alvh.no-ip. 1233 :UBC 0 : continue; /* ignore not-at-risk DBs */
4670 alvherre@alvh.no-ip. 1234 [ - + ]:CBC 636 : else if (MultiXactIdPrecedes(tmp->adw_minmulti, multiForceLimit))
1235 : : {
4906 alvherre@alvh.no-ip. 1236 [ # # # # ]:UBC 0 : if (avdb == NULL ||
4670 1237 : 0 : MultiXactIdPrecedes(tmp->adw_minmulti, avdb->adw_minmulti))
4906 1238 : 0 : avdb = tmp;
1239 : 0 : for_multi_wrap = true;
1240 : 0 : continue;
1241 : : }
4906 alvherre@alvh.no-ip. 1242 [ - + ]:CBC 636 : else if (for_multi_wrap)
4906 alvherre@alvh.no-ip. 1243 :UBC 0 : continue; /* ignore not-at-risk DBs */
1244 : :
1245 : : /* Find pgstat entry if any */
6854 alvherre@alvh.no-ip. 1246 :CBC 636 : 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 : : */
7015 1252 [ + + ]: 636 : if (!tmp->adw_entry)
1253 : 107 : 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 : 529 : skipit = false;
1263 : :
5005 1264 [ + - + + ]: 1376 : dlist_reverse_foreach(iter, &DatabaseList)
1265 : : {
1266 : 1347 : avl_dbase *dbp = dlist_container(avl_dbase, adl_node, iter.cur);
1267 : :
7015 1268 [ + + ]: 1347 : 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 : : */
6994 1274 [ + + ]: 500 : if (!TimestampDifferenceExceeds(dbp->adl_next_worker,
6802 bruce@momjian.us 1275 : 367 : current_time, 0) &&
6999 alvherre@alvh.no-ip. 1276 [ + - ]: 367 : !TimestampDifferenceExceeds(current_time,
1277 : : dbp->adl_next_worker,
1278 : : autovacuum_naptime * 1000))
7015 1279 : 367 : skipit = true;
1280 : :
1281 : 500 : break;
1282 : : }
1283 : : }
1284 [ + + ]: 529 : if (skipit)
7039 1285 : 367 : 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 : : */
7015 1291 [ + + ]: 162 : if (avdb == NULL ||
1292 [ - + ]: 29 : tmp->adw_entry->last_autovac_time < avdb->adw_entry->last_autovac_time)
1293 : 133 : avdb = tmp;
1294 : : }
1295 : :
1296 : : /* Found a database -- process it */
1297 [ + + ]: 139 : if (avdb != NULL)
1298 : : {
1299 : : WorkerInfo worker;
1300 : : dlist_node *wptr;
1301 : :
7039 1302 : 133 : 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 : : */
540 nathan@postgresql.or 1308 : 133 : wptr = dclist_pop_head_node(&AutoVacuumShmem->av_freeWorkers);
1309 : :
5005 alvherre@alvh.no-ip. 1310 : 133 : worker = dlist_container(WorkerInfoData, wi_links, wptr);
7015 1311 : 133 : worker->wi_dboid = avdb->adw_datid;
6824 1312 : 133 : worker->wi_proc = NULL;
7015 1313 : 133 : worker->wi_launchtime = GetCurrentTimestamp();
1314 : :
6449 tgl@sss.pgh.pa.us 1315 : 133 : AutoVacuumShmem->av_startingWorker = worker;
1316 : :
7039 alvherre@alvh.no-ip. 1317 : 133 : LWLockRelease(AutovacuumLock);
1318 : :
1319 : 133 : SendPostmasterSignal(PMSIGNAL_START_AUTOVAC_WORKER);
1320 : :
6866 1321 : 133 : retval = avdb->adw_datid;
1322 : : }
7015 alvherre@alvh.no-ip. 1323 [ - + ]:GBC 6 : 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 : : */
7015 alvherre@alvh.no-ip. 1329 :UBC 0 : rebuild_database_list(InvalidOid);
1330 : : }
1331 : :
6866 alvherre@alvh.no-ip. 1332 :CBC 139 : MemoryContextSwitchTo(oldcxt);
1333 : 139 : MemoryContextDelete(tmpcxt);
1334 : :
1335 : 139 : 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
7015 1350 : 139 : launch_worker(TimestampTz now)
1351 : : {
1352 : : Oid dbid;
1353 : : dlist_iter iter;
1354 : :
1355 : 139 : dbid = do_start_worker();
1356 [ + + ]: 139 : if (OidIsValid(dbid))
1357 : : {
4780 bruce@momjian.us 1358 : 133 : 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 : : */
5005 alvherre@alvh.no-ip. 1364 [ + - + + ]: 509 : dlist_foreach(iter, &DatabaseList)
1365 : : {
1366 : 500 : avl_dbase *avdb = dlist_container(avl_dbase, adl_node, iter.cur);
1367 : :
7015 1368 [ + + ]: 500 : if (avdb->adl_datid == dbid)
1369 : : {
5005 1370 : 124 : 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 : : */
7015 1376 : 124 : avdb->adl_next_worker =
1377 : 124 : TimestampTzPlusMilliseconds(now, autovacuum_naptime * 1000);
1378 : :
5005 1379 : 124 : dlist_move_head(&DatabaseList, iter.cur);
7015 1380 : 124 : 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 : : */
5005 1391 [ + + ]: 133 : if (!found)
7015 1392 : 9 : rebuild_database_list(dbid);
1393 : : }
7039 1394 : 139 : }
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
6945 alvherre@alvh.no-ip. 1402 :UBC 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
6147 tgl@sss.pgh.pa.us 1409 :CBC 262 : avl_sigusr2_handler(SIGNAL_ARGS)
1410 : : {
1411 : 262 : got_SIGUSR2 = true;
4185 andres@anarazel.de 1412 : 262 : SetLatch(MyLatch);
7015 alvherre@alvh.no-ip. 1413 : 262 : }
1414 : :
1415 : :
1416 : : /********************************************************************
1417 : : * AUTOVACUUM WORKER CODE
1418 : : ********************************************************************/
1419 : :
1420 : : /*
1421 : : * Main entry point for autovacuum worker processes.
1422 : : */
1423 : : void
494 peter@eisentraut.org 1424 : 158 : AutoVacWorkerMain(const void *startup_data, size_t startup_data_len)
1425 : : {
1426 : : sigjmp_buf local_sigjmp_buf;
1427 : : Oid dbid;
1428 : :
834 heikki.linnakangas@i 1429 [ - + ]: 158 : Assert(startup_data_len == 0);
1430 : :
1431 : : /* Release postmaster's working memory context */
1432 [ + - ]: 158 : if (PostmasterContext)
1433 : : {
1434 : 158 : MemoryContextDelete(PostmasterContext);
1435 : 158 : PostmasterContext = NULL;
1436 : : }
1437 : :
2302 peter@eisentraut.org 1438 : 158 : init_ps_display(NULL);
1439 : :
728 heikki.linnakangas@i 1440 [ - + ]: 158 : 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 : : */
2387 rhaas@postgresql.org 1447 : 158 : 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 : : */
7656 tgl@sss.pgh.pa.us 1453 : 158 : pqsignal(SIGINT, StatementCancelHandler);
1454 : 158 : pqsignal(SIGTERM, die);
1455 : : /* SIGQUIT handler was already set up by InitPostmasterChild */
1456 : :
5097 alvherre@alvh.no-ip. 1457 : 158 : InitializeTimeouts(); /* establishes SIGALRM handler */
1458 : :
77 andrew@dunslane.net 1459 :GNC 158 : pqsignal(SIGPIPE, PG_SIG_IGN);
6178 tgl@sss.pgh.pa.us 1460 :CBC 158 : pqsignal(SIGUSR1, procsignal_sigusr1_handler);
77 andrew@dunslane.net 1461 :GNC 158 : pqsignal(SIGUSR2, PG_SIG_IGN);
7628 tgl@sss.pgh.pa.us 1462 :CBC 158 : pqsignal(SIGFPE, FloatExceptionHandler);
77 andrew@dunslane.net 1463 :GNC 158 : 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 : : */
7482 tgl@sss.pgh.pa.us 1469 :CBC 158 : InitProcess();
1470 : :
1471 : : /* Early initialization */
1790 andres@anarazel.de 1472 : 158 : 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 : : */
7656 tgl@sss.pgh.pa.us 1487 [ - + ]: 158 : if (sigsetjmp(local_sigjmp_buf, 1) != 0)
1488 : : {
1489 : : /* since not using PG_TRY, must reset error stack by hand */
2442 michael@paquier.xyz 1490 :UBC 0 : error_context_stack = NULL;
1491 : :
1492 : : /* Prevents interrupts while cleaning up */
7656 tgl@sss.pgh.pa.us 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) */
7656 tgl@sss.pgh.pa.us 1507 :CBC 158 : PG_exception_stack = &local_sigjmp_buf;
1508 : :
1243 tmunro@postgresql.or 1509 : 158 : 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 : : */
3046 noah@leadboat.com 1517 : 158 : 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 : : */
7420 tgl@sss.pgh.pa.us 1524 : 158 : 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 : : */
7015 alvherre@alvh.no-ip. 1530 : 158 : SetConfigOption("statement_timeout", "0", PGC_SUSET, PGC_S_OVERRIDE);
866 akorotkov@postgresql 1531 : 158 : SetConfigOption("transaction_timeout", "0", PGC_SUSET, PGC_S_OVERRIDE);
4854 tgl@sss.pgh.pa.us 1532 : 158 : SetConfigOption("lock_timeout", "0", PGC_SUSET, PGC_S_OVERRIDE);
3667 1533 : 158 : 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 : : */
5327 1541 : 158 : 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 : : */
5566 simon@2ndQuadrant.co 1549 [ + - ]: 158 : if (synchronous_commit > SYNCHRONOUS_COMMIT_LOCAL_FLUSH)
5327 tgl@sss.pgh.pa.us 1550 : 158 : 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 : : */
1546 andres@anarazel.de 1557 : 158 : 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 : : */
7015 alvherre@alvh.no-ip. 1562 : 158 : 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 : : */
6449 tgl@sss.pgh.pa.us 1570 [ + - ]: 158 : if (AutoVacuumShmem->av_startingWorker != NULL)
1571 : : {
1572 : 158 : MyWorkerInfo = AutoVacuumShmem->av_startingWorker;
6999 alvherre@alvh.no-ip. 1573 : 158 : dbid = MyWorkerInfo->wi_dboid;
6824 1574 : 158 : MyWorkerInfo->wi_proc = MyProc;
1575 : :
1576 : : /* insert into the running list */
5005 1577 : 158 : dlist_push_head(&AutoVacuumShmem->av_runningWorkers,
1578 : 158 : &MyWorkerInfo->wi_links);
1579 : :
1580 : : /*
1581 : : * remove from the "starting" pointer, so that the launcher can start
1582 : : * a new worker if required
1583 : : */
6449 tgl@sss.pgh.pa.us 1584 : 158 : AutoVacuumShmem->av_startingWorker = NULL;
6999 alvherre@alvh.no-ip. 1585 : 158 : LWLockRelease(AutovacuumLock);
1586 : :
1587 : 158 : on_shmem_exit(FreeWorkerInfo, 0);
1588 : :
1589 : : /* wake up the launcher */
1590 [ + + ]: 158 : if (AutoVacuumShmem->av_launcherpid != 0)
6147 tgl@sss.pgh.pa.us 1591 : 157 : kill(AutoVacuumShmem->av_launcherpid, SIGUSR2);
1592 : : }
1593 : : else
1594 : : {
1595 : : /* no worker entry for me, go away */
6945 alvherre@alvh.no-ip. 1596 [ # # ]:UBC 0 : elog(WARNING, "autovacuum worker started without a worker entry");
6997 tgl@sss.pgh.pa.us 1597 : 0 : dbid = InvalidOid;
6999 alvherre@alvh.no-ip. 1598 : 0 : LWLockRelease(AutovacuumLock);
1599 : : }
1600 : :
7075 alvherre@alvh.no-ip. 1601 [ + - ]:CBC 158 : 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 : 158 : 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 : : */
549 tgl@sss.pgh.pa.us 1623 : 158 : InitPostgres(NULL, dbid, NULL, InvalidOid,
1624 : : INIT_PG_OVERRIDE_ALLOW_CONNS,
1625 : : dbname);
7656 1626 : 157 : SetProcessingMode(NormalProcessing);
2302 peter@eisentraut.org 1627 : 157 : set_ps_display(dbname);
7369 bruce@momjian.us 1628 [ + + ]: 157 : ereport(DEBUG1,
1629 : : (errmsg_internal("autovacuum: processing database \"%s\"", dbname)));
1630 : :
6824 alvherre@alvh.no-ip. 1631 [ - + ]: 157 : if (PostAuthDelay)
6824 alvherre@alvh.no-ip. 1632 :UBC 0 : pg_usleep(PostAuthDelay * 1000000L);
1633 : :
1634 : : /* And do an appropriate amount of work */
1961 tmunro@postgresql.or 1635 :CBC 157 : recentXid = ReadNextTransactionId();
4906 alvherre@alvh.no-ip. 1636 : 157 : recentMulti = ReadNextMultiXactId();
7034 1637 : 157 : do_autovacuum();
1638 : : }
1639 : :
1640 : : /* All done, go away */
7656 tgl@sss.pgh.pa.us 1641 : 154 : proc_exit(0);
1642 : : }
1643 : :
1644 : : /*
1645 : : * Return a WorkerInfo to the free list
1646 : : */
1647 : : static void
7015 alvherre@alvh.no-ip. 1648 : 158 : FreeWorkerInfo(int code, Datum arg)
1649 : : {
1650 [ + - ]: 158 : if (MyWorkerInfo != NULL)
1651 : : {
1652 : 158 : LWLockAcquire(AutovacuumLock, LW_EXCLUSIVE);
1653 : :
5003 tgl@sss.pgh.pa.us 1654 : 158 : dlist_delete(&MyWorkerInfo->wi_links);
7015 alvherre@alvh.no-ip. 1655 : 158 : MyWorkerInfo->wi_dboid = InvalidOid;
1656 : 158 : MyWorkerInfo->wi_tableoid = InvalidOid;
3703 1657 : 158 : MyWorkerInfo->wi_sharedrel = false;
6824 1658 : 158 : MyWorkerInfo->wi_proc = NULL;
7015 1659 : 158 : MyWorkerInfo->wi_launchtime = 0;
1180 dgustafsson@postgres 1660 : 158 : pg_atomic_clear_flag(&MyWorkerInfo->wi_dobalance);
540 nathan@postgresql.or 1661 : 158 : dclist_push_head(&AutoVacuumShmem->av_freeWorkers,
1662 : 158 : &MyWorkerInfo->wi_links);
1663 : : /* not mine anymore */
7015 alvherre@alvh.no-ip. 1664 : 158 : MyWorkerInfo = NULL;
1665 : :
1666 : : /*
1667 : : * now that we're inactive, cause a rebalancing of the surviving
1668 : : * workers
1669 : : */
6945 1670 : 158 : AutoVacuumShmem->av_signal[AutoVacRebalance] = true;
7015 1671 : 158 : LWLockRelease(AutovacuumLock);
1672 : : }
1673 : 158 : }
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
1180 dgustafsson@postgres 1682 : 10506 : VacuumUpdateCosts(void)
1683 : : {
7015 alvherre@alvh.no-ip. 1684 [ + + ]: 10506 : if (MyWorkerInfo)
1685 : : {
1180 dgustafsson@postgres 1686 [ - + ]: 1757 : if (av_storage_param_cost_delay >= 0)
1180 dgustafsson@postgres 1687 :UBC 0 : vacuum_cost_delay = av_storage_param_cost_delay;
1180 dgustafsson@postgres 1688 [ + - ]:CBC 1757 : else if (autovacuum_vac_cost_delay >= 0)
1689 : 1757 : vacuum_cost_delay = autovacuum_vac_cost_delay;
1690 : : else
1691 : : /* fall back to VacuumCostDelay */
1180 dgustafsson@postgres 1692 :UBC 0 : vacuum_cost_delay = VacuumCostDelay;
1693 : :
1180 dgustafsson@postgres 1694 :CBC 1757 : AutoVacuumUpdateCostLimit();
1695 : : }
1696 : : else
1697 : : {
1698 : : /* Must be explicit VACUUM or ANALYZE or parallel autovacuum worker */
1699 : 8749 : vacuum_cost_delay = VacuumCostDelay;
1700 : 8749 : vacuum_cost_limit = VacuumCostLimit;
1701 : : }
1702 : :
1703 : : /*
1704 : : * If configuration changes are allowed to impact VacuumCostActive, make
1705 : : * sure it is updated.
1706 : : */
1707 [ - + ]: 10506 : if (VacuumFailsafeActive)
1180 dgustafsson@postgres 1708 [ # # ]:UBC 0 : Assert(!VacuumCostActive);
1180 dgustafsson@postgres 1709 [ + + ]:CBC 10506 : else if (vacuum_cost_delay > 0)
1710 : 1760 : VacuumCostActive = true;
1711 : : else
1712 : : {
1713 : 8746 : VacuumCostActive = false;
1714 : 8746 : 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 : : */
1167 1721 [ + + + + ]: 10506 : if (MyWorkerInfo && message_level_is_interesting(DEBUG2))
1722 : : {
1723 : : Oid dboid,
1724 : : tableoid;
1725 : :
1180 dgustafsson@postgres 1726 [ - + ]:GBC 11 : Assert(!LWLockHeldByMe(AutovacuumLock));
1727 : :
1728 : 11 : LWLockAcquire(AutovacuumLock, LW_SHARED);
1729 : 11 : dboid = MyWorkerInfo->wi_dboid;
1730 : 11 : tableoid = MyWorkerInfo->wi_tableoid;
1731 : 11 : LWLockRelease(AutovacuumLock);
1732 : :
1733 [ + - - + : 11 : 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 : : }
7015 alvherre@alvh.no-ip. 1740 :CBC 10506 : }
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
1180 dgustafsson@postgres 1751 : 5491 : AutoVacuumUpdateCostLimit(void)
1752 : : {
1753 [ + + ]: 5491 : if (!MyWorkerInfo)
1180 dgustafsson@postgres 1754 :GBC 19 : 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 : :
1180 dgustafsson@postgres 1761 [ - + ]:CBC 5472 : if (av_storage_param_cost_limit > 0)
1180 dgustafsson@postgres 1762 :UBC 0 : vacuum_cost_limit = av_storage_param_cost_limit;
1763 : : else
1764 : : {
1765 : : int nworkers_for_balance;
1766 : :
1180 dgustafsson@postgres 1767 [ + + ]:CBC 5472 : if (autovacuum_vac_cost_limit > 0)
1180 dgustafsson@postgres 1768 :GBC 8 : vacuum_cost_limit = autovacuum_vac_cost_limit;
1769 : : else
1180 dgustafsson@postgres 1770 :CBC 5464 : vacuum_cost_limit = VacuumCostLimit;
1771 : :
1772 : : /* Only balance limit if no cost-related storage parameters specified */
1773 [ - + ]: 5472 : if (pg_atomic_unlocked_test_flag(&MyWorkerInfo->wi_dobalance))
1180 dgustafsson@postgres 1774 :UBC 0 : return;
1775 : :
1180 dgustafsson@postgres 1776 [ - + ]:CBC 5472 : Assert(vacuum_cost_limit > 0);
1777 : :
1778 : 5472 : nworkers_for_balance = pg_atomic_read_u32(&AutoVacuumShmem->av_nworkersForBalance);
1779 : :
1780 : : /* There is at least 1 autovac worker (this worker) */
1781 [ - + ]: 5472 : if (nworkers_for_balance <= 0)
1180 dgustafsson@postgres 1782 [ # # ]:UBC 0 : elog(ERROR, "nworkers_for_balance must be > 0");
1783 : :
1180 dgustafsson@postgres 1784 :CBC 5472 : 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 : 1008 : autovac_recalculate_workers_for_balance(void)
1798 : : {
1799 : : dlist_iter iter;
1800 : : int orig_nworkers_for_balance;
1801 : 1008 : int nworkers_for_balance = 0;
1802 : :
1803 [ - + ]: 1008 : Assert(LWLockHeldByMe(AutovacuumLock));
1804 : :
1805 : 1008 : orig_nworkers_for_balance =
1806 : 1008 : pg_atomic_read_u32(&AutoVacuumShmem->av_nworkersForBalance);
1807 : :
5005 alvherre@alvh.no-ip. 1808 [ + - + + ]: 1886 : dlist_foreach(iter, &AutoVacuumShmem->av_runningWorkers)
1809 : : {
4780 bruce@momjian.us 1810 : 878 : WorkerInfo worker = dlist_container(WorkerInfoData, wi_links, iter.cur);
1811 : :
1180 dgustafsson@postgres 1812 [ + - - + ]: 1756 : if (worker->wi_proc == NULL ||
1813 : 878 : pg_atomic_unlocked_test_flag(&worker->wi_dobalance))
1180 dgustafsson@postgres 1814 :UBC 0 : continue;
1815 : :
1180 dgustafsson@postgres 1816 :CBC 878 : nworkers_for_balance++;
1817 : : }
1818 : :
1819 [ + + ]: 1008 : if (nworkers_for_balance != orig_nworkers_for_balance)
1820 : 152 : pg_atomic_write_u32(&AutoVacuumShmem->av_nworkersForBalance,
1821 : : nworkers_for_balance);
7015 alvherre@alvh.no-ip. 1822 : 1008 : }
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 : 652 : get_database_list(void)
1838 : : {
7563 bruce@momjian.us 1839 : 652 : 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 */
5713 alvherre@alvh.no-ip. 1846 : 652 : resultcxt = CurrentMemoryContext;
1847 : :
1848 : : /*
1849 : : * Start a transaction so we can access pg_database.
1850 : : */
6147 tgl@sss.pgh.pa.us 1851 : 652 : StartTransactionCommand();
1852 : :
2717 andres@anarazel.de 1853 : 652 : rel = table_open(DatabaseRelationId, AccessShareLock);
2668 1854 : 652 : scan = table_beginscan_catalog(rel, 0, NULL);
1855 : :
6147 tgl@sss.pgh.pa.us 1856 [ + + ]: 3158 : while (HeapTupleIsValid(tup = heap_getnext(scan, ForwardScanDirection)))
1857 : : {
1858 : 2506 : 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 : : */
1083 andres@anarazel.de 1866 [ + + ]: 2506 : if (database_is_invalid_form(pgdatabase))
1867 : : {
1868 [ - + ]: 13 : elog(DEBUG2,
1869 : : "autovacuum: skipping invalid database \"%s\"",
1870 : : NameStr(pgdatabase->datname));
1871 : 13 : 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 : : */
5713 alvherre@alvh.no-ip. 1880 : 2493 : oldcxt = MemoryContextSwitchTo(resultcxt);
1881 : :
202 michael@paquier.xyz 1882 :GNC 2493 : avdb = palloc_object(avw_dbase);
1883 : :
2779 andres@anarazel.de 1884 :CBC 2493 : avdb->adw_datid = pgdatabase->oid;
6147 tgl@sss.pgh.pa.us 1885 : 2493 : avdb->adw_name = pstrdup(NameStr(pgdatabase->datname));
1886 : 2493 : avdb->adw_frozenxid = pgdatabase->datfrozenxid;
4670 alvherre@alvh.no-ip. 1887 : 2493 : avdb->adw_minmulti = pgdatabase->datminmxid;
1888 : : /* this gets set later: */
7015 1889 : 2493 : avdb->adw_entry = NULL;
1890 : :
7035 1891 : 2493 : dblist = lappend(dblist, avdb);
5713 1892 : 2493 : MemoryContextSwitchTo(oldcxt);
1893 : : }
1894 : :
2668 andres@anarazel.de 1895 : 652 : table_endscan(scan);
2717 1896 : 652 : table_close(rel, AccessShareLock);
1897 : :
6147 tgl@sss.pgh.pa.us 1898 : 652 : CommitTransactionCommand();
1899 : :
1900 : : /* Be sure to restore caller's memory context */
1399 1901 : 652 : MemoryContextSwitchTo(resultcxt);
1902 : :
7656 1903 : 652 : 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
95 nathan@postgresql.or 1911 :GNC 3569 : TableToProcessComparator(const ListCell *a, const ListCell *b)
1912 : : {
1913 : 3569 : TableToProcess *t1 = (TableToProcess *) lfirst(a);
1914 : 3569 : TableToProcess *t2 = (TableToProcess *) lfirst(b);
1915 : :
1916 [ + + ]: 3569 : 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
7034 alvherre@alvh.no-ip. 1926 :CBC 157 : do_autovacuum(void)
1927 : : {
1928 : : Relation classRel;
1929 : : HeapTuple tuple;
1930 : : TableScanDesc relScan;
1931 : : Form_pg_database dbForm;
95 nathan@postgresql.or 1932 :GNC 157 : List *tables_to_process = NIL;
3508 rhaas@postgresql.org 1933 :CBC 157 : 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;
3448 1941 : 157 : bool did_vacuum = false;
1942 : 157 : 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 : : */
6940 alvherre@alvh.no-ip. 1950 : 157 : AutovacMemCxt = AllocSetContextCreate(TopMemoryContext,
1951 : : "Autovacuum worker",
1952 : : ALLOCSET_DEFAULT_SIZES);
1953 : 157 : MemoryContextSwitchTo(AutovacMemCxt);
1954 : :
1955 : : /* Start a transaction so our commands have one to play into. */
7656 tgl@sss.pgh.pa.us 1956 : 157 : 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 : : */
416 michael@paquier.xyz 1962 : 157 : 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 : : */
4071 rhaas@postgresql.org 1969 : 156 : 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 : : */
5980 1976 : 156 : tuple = SearchSysCache1(DATABASEOID, ObjectIdGetDatum(MyDatabaseId));
7177 tgl@sss.pgh.pa.us 1977 [ - + ]: 156 : if (!HeapTupleIsValid(tuple))
7177 tgl@sss.pgh.pa.us 1978 [ # # ]:UBC 0 : elog(ERROR, "cache lookup failed for database %u", MyDatabaseId);
7177 tgl@sss.pgh.pa.us 1979 :CBC 156 : dbForm = (Form_pg_database) GETSTRUCT(tuple);
1980 : :
1981 [ + + - + ]: 156 : if (dbForm->datistemplate || !dbForm->datallowconn)
1982 : : {
1983 : 68 : default_freeze_min_age = 0;
6374 heikki.linnakangas@i 1984 : 68 : default_freeze_table_age = 0;
4520 alvherre@alvh.no-ip. 1985 : 68 : default_multixact_freeze_min_age = 0;
1986 : 68 : default_multixact_freeze_table_age = 0;
1987 : : }
1988 : : else
1989 : : {
7177 tgl@sss.pgh.pa.us 1990 : 88 : default_freeze_min_age = vacuum_freeze_min_age;
6374 heikki.linnakangas@i 1991 : 88 : default_freeze_table_age = vacuum_freeze_table_age;
4520 alvherre@alvh.no-ip. 1992 : 88 : default_multixact_freeze_min_age = vacuum_multixact_freeze_min_age;
1993 : 88 : default_multixact_freeze_table_age = vacuum_multixact_freeze_table_age;
1994 : : }
1995 : :
7177 tgl@sss.pgh.pa.us 1996 : 156 : ReleaseSysCache(tuple);
1997 : :
1998 : : /* StartTransactionCommand changed elsewhere */
7656 1999 : 156 : MemoryContextSwitchTo(AutovacMemCxt);
2000 : :
2717 andres@anarazel.de 2001 : 156 : classRel = table_open(RelationRelationId, AccessShareLock);
2002 : :
2003 : : /* create a copy so we can use it after closing pg_class */
6350 alvherre@alvh.no-ip. 2004 : 156 : pg_class_desc = CreateTupleDescCopy(RelationGetDescr(classRel));
2005 : :
2006 : : /* create hash table for toast <-> main relid mapping */
6530 2007 : 156 : ctl.keysize = sizeof(Oid);
6350 2008 : 156 : ctl.entrysize = sizeof(av_relation);
2009 : :
6530 2010 : 156 : 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 : : */
2668 andres@anarazel.de 2029 : 156 : 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 : : */
7628 tgl@sss.pgh.pa.us 2035 [ + + ]: 94008 : while ((tuple = heap_getnext(relScan, ForwardScanDirection)) != NULL)
2036 : : {
2037 : 93852 : 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 : :
4867 kgrittn@postgresql.o 2045 [ + + ]: 93852 : if (classForm->relkind != RELKIND_RELATION &&
1779 alvherre@alvh.no-ip. 2046 [ + + ]: 73263 : classForm->relkind != RELKIND_MATVIEW)
4867 kgrittn@postgresql.o 2047 : 73192 : continue;
2048 : :
2779 andres@anarazel.de 2049 : 20747 : 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 : : */
5678 rhaas@postgresql.org 2055 [ + + ]: 20747 : 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 : : */
2314 tgl@sss.pgh.pa.us 2062 [ - + ]: 87 : 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 : : */
3508 rhaas@postgresql.org 2071 :UBC 0 : orphan_oids = lappend_oid(orphan_oids, relid);
2072 : : }
3502 tgl@sss.pgh.pa.us 2073 :CBC 87 : continue;
2074 : : }
2075 : :
2076 : : /* Fetch reloptions and the pgstat entry for this table */
2077 : 20660 : relopts = extract_autovac_opts(tuple, pg_class_desc);
2078 : :
2079 : : /* Check if it needs vacuum or analyze */
85 nathan@postgresql.or 2080 :GNC 20660 : 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 */
3502 tgl@sss.pgh.pa.us 2087 [ + + + + ]:CBC 20660 : if (dovacuum || doanalyze)
2088 : : {
95 nathan@postgresql.or 2089 :GNC 876 : TableToProcess *table = palloc_object(TableToProcess);
2090 : :
2091 : 876 : table->oid = relid;
2092 : 876 : table->score = scores.max;
2093 : 876 : 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 : : */
3502 tgl@sss.pgh.pa.us 2101 [ + + ]:CBC 20660 : if (OidIsValid(classForm->reltoastrelid))
2102 : : {
2103 : : av_relation *hentry;
2104 : : bool found;
2105 : :
2106 : 19830 : hentry = hash_search(table_toast_map,
2107 : 9915 : &classForm->reltoastrelid,
2108 : : HASH_ENTER, &found);
2109 : :
2110 [ + - ]: 9915 : if (!found)
2111 : : {
2112 : : /* hash_search already filled in the key */
2113 : 9915 : hentry->ar_relid = relid;
2114 : 9915 : hentry->ar_hasrelopts = false;
2115 [ + + ]: 9915 : if (relopts != NULL)
2116 : : {
2117 : 159 : hentry->ar_hasrelopts = true;
2118 : 159 : memcpy(&hentry->ar_reloptions, relopts,
2119 : : sizeof(AutoVacOpts));
2120 : : }
2121 : : }
2122 : : }
2123 : :
2124 : : /* Release stuff to avoid per-relation leakage */
403 2125 [ + + ]: 20660 : if (relopts)
2126 : 312 : pfree(relopts);
2127 : : }
2128 : :
2668 andres@anarazel.de 2129 : 156 : table_endscan(relScan);
2130 : :
2131 : : /* second pass: check TOAST tables */
6530 alvherre@alvh.no-ip. 2132 : 156 : ScanKeyInit(&key,
2133 : : Anum_pg_class_relkind,
2134 : : BTEqualStrategyNumber, F_CHAREQ,
2135 : : CharGetDatum(RELKIND_TOASTVALUE));
2136 : :
2668 andres@anarazel.de 2137 : 156 : relScan = table_beginscan_catalog(classRel, 1, &key);
6530 alvherre@alvh.no-ip. 2138 [ + + ]: 10095 : while ((tuple = heap_getnext(relScan, ForwardScanDirection)) != NULL)
2139 : : {
2140 : 9939 : Form_pg_class classForm = (Form_pg_class) GETSTRUCT(tuple);
2141 : : Oid relid;
2142 : : AutoVacOpts *relopts;
403 tgl@sss.pgh.pa.us 2143 : 9939 : 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 : : */
5678 rhaas@postgresql.org 2152 [ + + ]: 9939 : if (classForm->relpersistence == RELPERSISTENCE_TEMP)
6530 alvherre@alvh.no-ip. 2153 : 24 : continue;
2154 : :
2779 andres@anarazel.de 2155 : 9915 : relid = classForm->oid;
2156 : :
2157 : : /*
2158 : : * fetch reloptions -- if this toast table does not have them, try the
2159 : : * main rel
2160 : : */
6350 alvherre@alvh.no-ip. 2161 : 9915 : relopts = extract_autovac_opts(tuple, pg_class_desc);
403 tgl@sss.pgh.pa.us 2162 [ + + ]: 9915 : if (relopts)
2163 : 4 : free_relopts = true;
2164 : : else
2165 : : {
2166 : : av_relation *hentry;
2167 : : bool found;
2168 : :
6350 alvherre@alvh.no-ip. 2169 : 9911 : hentry = hash_search(table_toast_map, &relid, HASH_FIND, &found);
2170 [ + - + + ]: 9911 : if (found && hentry->ar_hasrelopts)
2171 : 155 : relopts = &hentry->ar_reloptions;
2172 : : }
2173 : :
85 nathan@postgresql.or 2174 :GNC 9915 : 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 */
6530 alvherre@alvh.no-ip. 2181 [ + + ]:CBC 9915 : if (dovacuum)
2182 : : {
95 nathan@postgresql.or 2183 :GNC 7 : TableToProcess *table = palloc_object(TableToProcess);
2184 : :
2185 : 7 : table->oid = relid;
2186 : 7 : table->score = scores.max;
2187 : 7 : tables_to_process = lappend(tables_to_process, table);
2188 : : }
2189 : :
2190 : : /* Release stuff to avoid leakage */
403 tgl@sss.pgh.pa.us 2191 [ + + ]:CBC 9915 : if (free_relopts)
2192 : 4 : pfree(relopts);
2193 : : }
2194 : :
2668 andres@anarazel.de 2195 : 156 : table_endscan(relScan);
2717 2196 : 156 : 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 : : */
3502 tgl@sss.pgh.pa.us 2207 [ - + - - : 156 : foreach(cell, orphan_oids)
- + ]
2208 : : {
3502 tgl@sss.pgh.pa.us 2209 :UBC 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 : :
2314 2253 [ # # ]: 0 : if (checkTempNamespaceStatus(classForm->relnamespace) != TEMP_NAMESPACE_IDLE)
2254 : : {
3502 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 : : */
819 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 */
3502 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 : : */
396 nathan@postgresql.or 2288 : 0 : PushActiveSnapshot(GetTransactionSnapshot());
2289 : :
3502 tgl@sss.pgh.pa.us 2290 : 0 : object.classId = RelationRelationId;
2291 : 0 : object.objectId = relid;
2292 : 0 : object.objectSubId = 0;
3497 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 : : */
396 nathan@postgresql.or 2302 : 0 : PopActiveSnapshot();
3508 rhaas@postgresql.org 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 : : */
95 nathan@postgresql.or 2316 [ - + ]:GNC 156 : if (autovacuum_freeze_score_weight != 0.0 ||
95 nathan@postgresql.or 2317 [ # # ]:UNC 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)
95 nathan@postgresql.or 2321 :GNC 156 : 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 : : */
1180 drowley@postgresql.o 2338 :CBC 156 : 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 : : */
6940 alvherre@alvh.no-ip. 2344 : 156 : PortalContext = AllocSetContextCreate(AutovacMemCxt,
2345 : : "Autovacuum Portal",
2346 : : ALLOCSET_DEFAULT_SIZES);
2347 : :
2348 : : /*
2349 : : * Perform operations on collected tables.
2350 : : */
95 nathan@postgresql.or 2351 [ + + + + :GNC 1191 : foreach_ptr(TableToProcess, table, tables_to_process)
+ + ]
2352 : : {
2353 : 883 : Oid relid = table->oid;
2354 : : HeapTuple classTup;
2355 : : autovac_table *tab;
2356 : : bool isshared;
2357 : : bool skipit;
2358 : : dlist_iter iter;
2359 : :
7641 tgl@sss.pgh.pa.us 2360 [ - + ]:CBC 883 : CHECK_FOR_INTERRUPTS();
2361 : :
2362 : : /*
2363 : : * Check for config changes before processing each collected table.
2364 : : */
2387 rhaas@postgresql.org 2365 [ - + ]: 883 : if (ConfigReloadPending)
2366 : : {
2387 rhaas@postgresql.org 2367 :UBC 0 : ConfigReloadPending = false;
4106 alvherre@alvh.no-ip. 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 : : */
3031 tgl@sss.pgh.pa.us 2386 :CBC 883 : classTup = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
2387 [ + + ]: 883 : if (!HeapTupleIsValid(classTup))
3031 tgl@sss.pgh.pa.us 2388 :GBC 5 : continue; /* somebody deleted the rel, forget it */
3031 tgl@sss.pgh.pa.us 2389 :CBC 878 : isshared = ((Form_pg_class) GETSTRUCT(classTup))->relisshared;
2390 : 878 : 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 : : */
7015 alvherre@alvh.no-ip. 2397 : 878 : LWLockAcquire(AutovacuumScheduleLock, LW_EXCLUSIVE);
2398 : 878 : LWLockAcquire(AutovacuumLock, LW_SHARED);
2399 : :
2400 : : /*
2401 : : * Check whether the table is being vacuumed concurrently by another
2402 : : * worker.
2403 : : */
2404 : 878 : skipit = false;
5005 2405 [ + - + + ]: 1756 : dlist_foreach(iter, &AutoVacuumShmem->av_runningWorkers)
2406 : : {
2407 : 878 : WorkerInfo worker = dlist_container(WorkerInfoData, wi_links, iter.cur);
2408 : :
2409 : : /* ignore myself */
7015 2410 [ + - ]: 878 : if (worker == MyWorkerInfo)
5005 2411 : 878 : continue;
2412 : :
2413 : : /* ignore workers in other databases (unless table is shared) */
3703 alvherre@alvh.no-ip. 2414 [ # # # # ]:UBC 0 : if (!worker->wi_sharedrel && worker->wi_dboid != MyDatabaseId)
5005 2415 : 0 : continue;
2416 : :
7015 2417 [ # # ]: 0 : if (worker->wi_tableoid == relid)
2418 : : {
2419 : 0 : skipit = true;
3448 rhaas@postgresql.org 2420 : 0 : found_concurrent_worker = true;
7015 alvherre@alvh.no-ip. 2421 : 0 : break;
2422 : : }
2423 : : }
7015 alvherre@alvh.no-ip. 2424 :CBC 878 : LWLockRelease(AutovacuumLock);
2425 [ - + ]: 878 : if (skipit)
2426 : : {
7015 alvherre@alvh.no-ip. 2427 :UBC 0 : LWLockRelease(AutovacuumScheduleLock);
2428 : 0 : 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 : : */
3031 tgl@sss.pgh.pa.us 2437 :CBC 878 : MyWorkerInfo->wi_tableoid = relid;
2438 : 878 : MyWorkerInfo->wi_sharedrel = isshared;
2439 : 878 : 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 : : */
6940 alvherre@alvh.no-ip. 2447 : 878 : MemoryContextSwitchTo(AutovacMemCxt);
4071 rhaas@postgresql.org 2448 : 878 : tab = table_recheck_autovac(relid, table_toast_map, pg_class_desc,
2449 : : effective_multixact_freeze_max_age);
7034 alvherre@alvh.no-ip. 2450 [ - + ]: 878 : if (tab == NULL)
2451 : : {
2452 : : /* someone else vacuumed the table, or it went away */
3031 tgl@sss.pgh.pa.us 2453 :UBC 0 : LWLockAcquire(AutovacuumScheduleLock, LW_EXCLUSIVE);
2454 : 0 : MyWorkerInfo->wi_tableoid = InvalidOid;
2455 : 0 : MyWorkerInfo->wi_sharedrel = false;
7015 alvherre@alvh.no-ip. 2456 : 0 : LWLockRelease(AutovacuumScheduleLock);
7624 tgl@sss.pgh.pa.us 2457 : 0 : 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 : : */
1180 dgustafsson@postgres 2465 :CBC 878 : av_storage_param_cost_delay = tab->at_storage_param_vac_cost_delay;
2466 : 878 : 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 [ + - ]: 878 : if (tab->at_dobalance)
2473 : 878 : pg_atomic_test_set_flag(&MyWorkerInfo->wi_dobalance);
2474 : : else
1180 dgustafsson@postgres 2475 :UBC 0 : pg_atomic_clear_flag(&MyWorkerInfo->wi_dobalance);
2476 : :
1180 dgustafsson@postgres 2477 :CBC 878 : LWLockAcquire(AutovacuumLock, LW_SHARED);
2478 : 878 : autovac_recalculate_workers_for_balance();
2479 : 878 : 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 : 878 : VacuumUpdateCosts();
2487 : :
2488 : :
2489 : : /* clean up memory before each iteration */
958 nathan@postgresql.or 2490 : 878 : 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 : :
6557 alvherre@alvh.no-ip. 2500 : 878 : tab->at_relname = get_rel_name(tab->at_relid);
2501 : 878 : tab->at_nspname = get_namespace_name(get_rel_namespace(tab->at_relid));
2502 : 878 : tab->at_datname = get_database_name(MyDatabaseId);
2503 [ + - + - : 878 : if (!tab->at_relname || !tab->at_nspname || !tab->at_datname)
- + ]
6557 alvherre@alvh.no-ip. 2504 :UBC 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 : : */
6941 alvherre@alvh.no-ip. 2511 [ + - ]:CBC 878 : PG_TRY();
2512 : : {
2513 : : /* Use PortalContext for any per-table allocations */
3202 tgl@sss.pgh.pa.us 2514 : 878 : MemoryContextSwitchTo(PortalContext);
2515 : :
2516 : : /* have at it */
6557 alvherre@alvh.no-ip. 2517 : 878 : 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 : : */
6822 2525 : 876 : QueryCancelPending = false;
2526 : : }
6941 alvherre@alvh.no-ip. 2527 :UBC 0 : PG_CATCH();
2528 : : {
2529 : : /*
2530 : : * Abort the transaction, start a new one, and proceed with the
2531 : : * next table in our list.
2532 : : */
6824 2533 : 0 : HOLD_INTERRUPTS();
2661 rhaas@postgresql.org 2534 [ # # ]: 0 : if (tab->at_params.options & VACOPT_VACUUM)
6824 alvherre@alvh.no-ip. 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();
958 nathan@postgresql.or 2545 : 0 : MemoryContextReset(PortalContext);
2546 : :
2547 : : /* restart our transaction for the following operations */
6824 alvherre@alvh.no-ip. 2548 : 0 : StartTransactionCommand();
2549 [ # # ]: 0 : RESUME_INTERRUPTS();
2550 : : }
6941 alvherre@alvh.no-ip. 2551 [ - + ]:CBC 876 : PG_END_TRY();
2552 : :
2553 : : /* Make sure we're back in AutovacMemCxt */
3202 tgl@sss.pgh.pa.us 2554 : 876 : MemoryContextSwitchTo(AutovacMemCxt);
2555 : :
3448 rhaas@postgresql.org 2556 : 876 : did_vacuum = true;
2557 : :
2558 : : /* ProcGlobal->statusFlags[i] are reset at the next end of xact */
2559 : :
2560 : : /* be tidy */
6557 alvherre@alvh.no-ip. 2561 : 876 : deleted:
2562 [ + - ]: 876 : if (tab->at_datname != NULL)
2563 : 876 : pfree(tab->at_datname);
2564 [ + - ]: 876 : if (tab->at_nspname != NULL)
2565 : 876 : pfree(tab->at_nspname);
2566 [ + - ]: 876 : if (tab->at_relname != NULL)
2567 : 876 : pfree(tab->at_relname);
7034 2568 : 876 : 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 : : */
3031 tgl@sss.pgh.pa.us 2577 : 876 : LWLockAcquire(AutovacuumScheduleLock, LW_EXCLUSIVE);
6824 alvherre@alvh.no-ip. 2578 : 876 : MyWorkerInfo->wi_tableoid = InvalidOid;
3703 2579 : 876 : MyWorkerInfo->wi_sharedrel = false;
3031 tgl@sss.pgh.pa.us 2580 : 876 : LWLockRelease(AutovacuumScheduleLock);
1180 dgustafsson@postgres 2581 : 876 : pg_atomic_test_set_flag(&MyWorkerInfo->wi_dobalance);
2582 : : }
2583 : :
95 nathan@postgresql.or 2584 :GNC 154 : list_free_deep(tables_to_process);
2585 : :
2586 : : /*
2587 : : * Perform additional work items, as requested by backends.
2588 : : */
3241 alvherre@alvh.no-ip. 2589 :CBC 154 : LWLockAcquire(AutovacuumLock, LW_EXCLUSIVE);
2590 [ + + ]: 39578 : for (i = 0; i < NUM_WORKITEMS; i++)
2591 : : {
2592 : 39424 : AutoVacuumWorkItem *workitem = &AutoVacuumShmem->av_workItems[i];
2593 : :
2594 [ + + ]: 39424 : if (!workitem->avw_used)
2595 : 39418 : continue;
2596 [ - + ]: 6 : if (workitem->avw_active)
3241 alvherre@alvh.no-ip. 2597 :UBC 0 : continue;
3165 alvherre@alvh.no-ip. 2598 [ - + ]:CBC 6 : if (workitem->avw_database != MyDatabaseId)
3165 alvherre@alvh.no-ip. 2599 :UBC 0 : continue;
2600 : :
2601 : : /* claim this one, and release lock while performing it */
3241 alvherre@alvh.no-ip. 2602 :CBC 6 : workitem->avw_active = true;
2603 : 6 : LWLockRelease(AutovacuumLock);
2604 : :
238 alvherre@kurilemu.de 2605 : 6 : PushActiveSnapshot(GetTransactionSnapshot());
3241 alvherre@alvh.no-ip. 2606 : 6 : perform_work_item(workitem);
238 alvherre@kurilemu.de 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 : : */
3241 alvherre@alvh.no-ip. 2613 [ - + ]: 6 : CHECK_FOR_INTERRUPTS();
2387 rhaas@postgresql.org 2614 [ - + ]: 6 : if (ConfigReloadPending)
2615 : : {
2387 rhaas@postgresql.org 2616 :UBC 0 : ConfigReloadPending = false;
3241 alvherre@alvh.no-ip. 2617 : 0 : ProcessConfigFile(PGC_SIGHUP);
1180 dgustafsson@postgres 2618 : 0 : VacuumUpdateCosts();
2619 : : }
2620 : :
3241 alvherre@alvh.no-ip. 2621 :CBC 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 : 154 : 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 */
332 tgl@sss.pgh.pa.us 2642 :GNC 154 : 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 : : */
3448 rhaas@postgresql.org 2664 [ + + + - ]:CBC 154 : if (did_vacuum || !found_concurrent_worker)
2665 : 154 : vac_update_datfrozenxid();
2666 : :
2667 : : /* Finally close out the last transaction. */
7656 tgl@sss.pgh.pa.us 2668 : 154 : CommitTransactionCommand();
2669 : 154 : }
2670 : :
2671 : : /*
2672 : : * Execute a previously registered work item.
2673 : : */
2674 : : static void
3377 alvherre@alvh.no-ip. 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 : : */
3202 tgl@sss.pgh.pa.us 2691 [ - + ]: 6 : Assert(CurrentMemoryContext == AutovacMemCxt);
2692 : :
3377 alvherre@alvh.no-ip. 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)
- + ]
3377 alvherre@alvh.no-ip. 2697 :UBC 0 : goto deleted2;
2698 : :
2685 alvherre@alvh.no-ip. 2699 :CBC 6 : autovac_report_workitem(workitem, cur_nspname, cur_relname);
2700 : :
2701 : : /* clean up memory before each work item */
958 nathan@postgresql.or 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 : : */
3377 alvherre@alvh.no-ip. 2710 [ + - ]: 6 : PG_TRY();
2711 : : {
2712 : : /* Use PortalContext for any per-work-item allocations */
3202 tgl@sss.pgh.pa.us 2713 : 6 : MemoryContextSwitchTo(PortalContext);
2714 : :
2715 : : /*
2716 : : * Have at it. Functions called here are responsible for any required
2717 : : * user switch and sandbox.
2718 : : */
3377 alvherre@alvh.no-ip. 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;
3377 alvherre@alvh.no-ip. 2726 :UBC 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 : : */
3377 alvherre@alvh.no-ip. 2738 :CBC 6 : QueryCancelPending = false;
2739 : : }
3377 alvherre@alvh.no-ip. 2740 :UBC 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();
958 nathan@postgresql.or 2754 : 0 : MemoryContextReset(PortalContext);
2755 : :
2756 : : /* restart our transaction for the following operations */
3377 alvherre@alvh.no-ip. 2757 : 0 : StartTransactionCommand();
2758 [ # # ]: 0 : RESUME_INTERRUPTS();
2759 : : }
3377 alvherre@alvh.no-ip. 2760 [ - + ]:CBC 6 : PG_END_TRY();
2761 : :
2762 : : /* Make sure we're back in AutovacMemCxt */
3202 tgl@sss.pgh.pa.us 2763 : 6 : MemoryContextSwitchTo(AutovacMemCxt);
2764 : :
2765 : : /* We intentionally do not set did_vacuum here */
2766 : :
2767 : : /* be tidy */
3377 alvherre@alvh.no-ip. 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 *
6350 2789 : 31453 : extract_autovac_opts(HeapTuple tup, TupleDesc pg_class_desc)
2790 : : {
2791 : : bytea *relopts;
2792 : : AutoVacOpts *av;
2793 : :
2794 [ + + + + : 31453 : 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 : :
810 akorotkov@postgresql 2798 : 31453 : relopts = extractRelOptions(tup, pg_class_desc, NULL);
2799 [ + + ]: 31453 : if (relopts == NULL)
2800 : 31109 : return NULL;
2801 : :
202 michael@paquier.xyz 2802 :GNC 344 : av = palloc_object(AutoVacOpts);
810 akorotkov@postgresql 2803 :CBC 344 : memcpy(av, &(((StdRdOptions *) relopts)->autovacuum), sizeof(AutoVacOpts));
2804 : 344 : pfree(relopts);
2805 : :
6350 alvherre@alvh.no-ip. 2806 : 344 : 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 : 878 : 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;
7034 2827 : 878 : autovac_table *tab = NULL;
2828 : : bool wraparound;
2829 : : AutoVacOpts *avopts;
403 tgl@sss.pgh.pa.us 2830 : 878 : bool free_avopts = false;
2831 : : AutoVacuumScores scores;
2832 : :
2833 : : /* fetch the relation's relcache entry */
5980 rhaas@postgresql.org 2834 : 878 : classTup = SearchSysCacheCopy1(RELOID, ObjectIdGetDatum(relid));
7034 alvherre@alvh.no-ip. 2835 [ - + ]: 878 : if (!HeapTupleIsValid(classTup))
7034 alvherre@alvh.no-ip. 2836 :UBC 0 : return NULL;
7034 alvherre@alvh.no-ip. 2837 :CBC 878 : 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 : : */
6350 2843 : 878 : avopts = extract_autovac_opts(classTup, pg_class_desc);
403 tgl@sss.pgh.pa.us 2844 [ + + ]: 878 : if (avopts)
2845 : 28 : free_avopts = true;
2846 [ + + + - ]: 850 : else if (classForm->relkind == RELKIND_TOASTVALUE &&
2847 : : table_toast_map != NULL)
2848 : : {
2849 : : av_relation *hentry;
2850 : : bool found;
2851 : :
6350 alvherre@alvh.no-ip. 2852 : 7 : hentry = hash_search(table_toast_map, &relid, HASH_FIND, &found);
2853 [ + - - + ]: 7 : if (found && hentry->ar_hasrelopts)
6350 alvherre@alvh.no-ip. 2854 :UBC 0 : avopts = &hentry->ar_reloptions;
2855 : : }
2856 : :
85 nathan@postgresql.or 2857 :GNC 878 : 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 */
6530 alvherre@alvh.no-ip. 2864 [ + + + - ]:CBC 878 : 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 */
258 peter@eisentraut.org 2881 [ + + ]:GNC 28 : log_vacuum_min_duration = (avopts && avopts->log_vacuum_min_duration >= 0)
2882 : : ? avopts->log_vacuum_min_duration
4106 alvherre@alvh.no-ip. 2883 [ + + ]:CBC 906 : : Log_autovacuum_min_duration;
2884 : :
2885 : : /* -1 in autovac setting means use log_autoanalyze_min_duration */
258 peter@eisentraut.org 2886 [ - + ]:GNC 28 : log_analyze_min_duration = (avopts && avopts->log_analyze_min_duration >= 0)
2887 : : ? avopts->log_analyze_min_duration
2888 [ + + ]: 906 : : Log_autoanalyze_min_duration;
2889 : :
2890 : : /* these do not have autovacuum-specific settings */
6151 alvherre@alvh.no-ip. 2891 [ - + ]:CBC 28 : freeze_min_age = (avopts && avopts->freeze_min_age >= 0)
2892 : : ? avopts->freeze_min_age
2893 [ + + ]: 906 : : default_freeze_min_age;
2894 : :
2895 [ - + ]: 28 : freeze_table_age = (avopts && avopts->freeze_table_age >= 0)
2896 : : ? avopts->freeze_table_age
2897 [ + + ]: 906 : : default_freeze_table_age;
2898 : :
4520 2899 : 906 : multixact_freeze_min_age = (avopts &&
2900 [ - + ]: 28 : avopts->multixact_freeze_min_age >= 0)
2901 : : ? avopts->multixact_freeze_min_age
2902 [ + + ]: 906 : : default_multixact_freeze_min_age;
2903 : :
2904 : 906 : multixact_freeze_table_age = (avopts &&
2905 [ - + ]: 28 : avopts->multixact_freeze_table_age >= 0)
2906 : : ? avopts->multixact_freeze_table_age
2907 [ + + ]: 906 : : default_multixact_freeze_table_age;
2908 : :
202 michael@paquier.xyz 2909 :GNC 878 : tab = palloc_object(autovac_table);
7034 alvherre@alvh.no-ip. 2910 :CBC 878 : 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 : : */
1271 tgl@sss.pgh.pa.us 2917 : 878 : tab->at_params.options =
1212 michael@paquier.xyz 2918 : 878 : (dovacuum ? (VACOPT_VACUUM |
2919 : : VACOPT_PROCESS_MAIN |
2920 [ + + ]: 878 : VACOPT_SKIP_DATABASE_STATS) : 0) |
4122 alvherre@alvh.no-ip. 2921 [ + + ]: 878 : (doanalyze ? VACOPT_ANALYZE : 0) |
2910 michael@paquier.xyz 2922 [ + - ]: 878 : (!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 : : */
1838 pg@bowt.ie 2929 : 878 : tab->at_params.index_cleanup = VACOPTVALUE_UNSPECIFIED;
2930 : 878 : tab->at_params.truncate = VACOPTVALUE_UNSPECIFIED;
4122 alvherre@alvh.no-ip. 2931 : 878 : tab->at_params.freeze_min_age = freeze_min_age;
2932 : 878 : tab->at_params.freeze_table_age = freeze_table_age;
2933 : 878 : tab->at_params.multixact_freeze_min_age = multixact_freeze_min_age;
2934 : 878 : tab->at_params.multixact_freeze_table_age = multixact_freeze_table_age;
2935 : 878 : tab->at_params.is_wraparound = wraparound;
258 peter@eisentraut.org 2936 :GNC 878 : tab->at_params.log_vacuum_min_duration = log_vacuum_min_duration;
2937 : 878 : tab->at_params.log_analyze_min_duration = log_analyze_min_duration;
839 nathan@postgresql.or 2938 :CBC 878 : tab->at_params.toast_parent = InvalidOid;
2939 : :
2940 : : /* Determine the number of parallel vacuum workers to use */
85 msawada@postgresql.o 2941 :GNC 878 : tab->at_params.nworkers = 0;
2942 [ + + ]: 878 : if (avopts)
2943 : : {
2944 [ - + ]: 28 : if (avopts->autovacuum_parallel_workers == 0)
2945 : : {
2946 : : /*
2947 : : * Disable parallel vacuum, if the reloption sets the parallel
2948 : : * degree as zero.
2949 : : */
85 msawada@postgresql.o 2950 :UNC 0 : tab->at_params.nworkers = -1;
2951 : : }
85 msawada@postgresql.o 2952 [ + + ]:GNC 28 : else if (avopts->autovacuum_parallel_workers > 0)
2953 : 3 : 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 : : */
504 melanieplageman@gmai 2965 :CBC 878 : tab->at_params.max_eager_freeze_failure_rate = vacuum_max_eager_freeze_failure_rate;
1180 dgustafsson@postgres 2966 : 878 : tab->at_storage_param_vac_cost_limit = avopts ?
2967 [ + + ]: 878 : avopts->vacuum_cost_limit : 0;
2968 : 878 : tab->at_storage_param_vac_cost_delay = avopts ?
2969 [ + + ]: 878 : avopts->vacuum_cost_delay : -1;
6557 alvherre@alvh.no-ip. 2970 : 878 : tab->at_relname = NULL;
2971 : 878 : tab->at_nspname = NULL;
2972 : 878 : 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 : : */
4288 2978 : 878 : tab->at_dobalance =
2979 [ + + + - ]: 906 : !(avopts && (avopts->vacuum_cost_limit > 0 ||
1162 dgustafsson@postgres 2980 [ + - ]: 906 : avopts->vacuum_cost_delay >= 0));
2981 : : }
2982 : :
403 tgl@sss.pgh.pa.us 2983 [ + + ]: 878 : if (free_avopts)
2984 : 28 : pfree(avopts);
7034 alvherre@alvh.no-ip. 2985 : 878 : heap_freetuple(classTup);
2986 : 878 : 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 relfrozenxid/relminmxid-based score is scaled aggressively so that the
3051 : : * table has a decent chance of sorting to the front of the list. Furthermore,
3052 : : * the relminmxid-based score is scaled aggressively as
3053 : : * effective_multixact_freeze_max_age is lowered due to high multixact member
3054 : : * space usage.
3055 : : *
3056 : : * To adjust how strongly each component contributes to the score, the
3057 : : * following parameters can be adjusted from their default of 1.0 to anywhere
3058 : : * between 0.0 and 10.0 (inclusive). Setting all of these to 0.0 restores
3059 : : * pre-v19 prioritization behavior:
3060 : : *
3061 : : * autovacuum_freeze_score_weight
3062 : : * autovacuum_multixact_freeze_score_weight
3063 : : * autovacuum_vacuum_score_weight
3064 : : * autovacuum_vacuum_insert_score_weight
3065 : : * autovacuum_analyze_score_weight
3066 : : *
3067 : : * The autovacuum table score is returned in scores->max. The component scores
3068 : : * are also returned in the "scores" argument via the other members of the
3069 : : * AutoVacuumScores struct.
3070 : : */
3071 : : static void
3072 : 31453 : relation_needs_vacanalyze(Oid relid,
3073 : : AutoVacOpts *relopts,
3074 : : Form_pg_class classForm,
3075 : : int effective_multixact_freeze_max_age,
3076 : : int elevel,
3077 : : /* output params below */
3078 : : bool *dovacuum,
3079 : : bool *doanalyze,
3080 : : bool *wraparound,
3081 : : AutoVacuumScores *scores)
3082 : : {
3083 : : PgStat_StatTabEntry *tabentry;
3084 : : bool force_vacuum;
3085 : : bool av_enabled;
82 nathan@postgresql.or 3086 :GNC 31453 : bool may_free = false;
3087 : :
3088 : : /* constants from reloptions or GUC variables */
3089 : : int vac_base_thresh,
3090 : : vac_max_thresh,
3091 : : vac_ins_base_thresh,
3092 : : anl_base_thresh;
3093 : : float4 vac_scale_factor,
3094 : : vac_ins_scale_factor,
3095 : : anl_scale_factor;
3096 : :
3097 : : /* thresholds calculated from above constants */
3098 : : float4 vacthresh,
3099 : : vacinsthresh,
3100 : : anlthresh;
3101 : :
3102 : : /* number of vacuum (resp. analyze) tuples at this time */
3103 : : float4 vactuples,
3104 : : instuples,
3105 : : anltuples;
3106 : :
3107 : : /* freeze parameters */
3108 : : int freeze_max_age;
3109 : : int multixact_freeze_max_age;
3110 : : TransactionId xidForceLimit;
3111 : : TransactionId relfrozenxid;
3112 : : MultiXactId relminmxid;
3113 : : MultiXactId multiForceLimit;
3114 : : uint32 xid_age;
3115 : : uint32 mxid_age;
3116 : : int effective_xid_failsafe_age;
3117 : : int effective_mxid_failsafe_age;
3118 : :
88 3119 : 31453 : float4 pcnt_unfrozen = 1;
3120 : 31453 : float4 reltuples = classForm->reltuples;
3121 : 31453 : int32 relpages = classForm->relpages;
3122 : 31453 : int32 relallfrozen = classForm->relallfrozen;
3123 : :
1341 peter@eisentraut.org 3124 [ - + ]:CBC 31453 : Assert(classForm != NULL);
3125 [ - + ]: 31453 : Assert(OidIsValid(relid));
3126 : :
95 nathan@postgresql.or 3127 :GNC 31453 : memset(scores, 0, sizeof(AutoVacuumScores));
3128 : 31453 : *dovacuum = false;
3129 : 31453 : *doanalyze = false;
3130 : :
3131 : : /*
3132 : : * Determine vacuum/analyze equation parameters. We have two possible
3133 : : * sources: the passed reloptions (which could be a main table or a toast
3134 : : * table), or the autovacuum GUC variables.
3135 : : */
3136 : :
3137 : : /* -1 in autovac setting means use plain vacuum_scale_factor */
6151 alvherre@alvh.no-ip. 3138 [ - + ]:CBC 499 : vac_scale_factor = (relopts && relopts->vacuum_scale_factor >= 0)
6151 alvherre@alvh.no-ip. 3139 :UBC 0 : ? relopts->vacuum_scale_factor
6151 alvherre@alvh.no-ip. 3140 [ + + ]:CBC 31952 : : autovacuum_vac_scale;
3141 : :
3142 [ - + ]: 499 : vac_base_thresh = (relopts && relopts->vacuum_threshold >= 0)
3143 : : ? relopts->vacuum_threshold
3144 [ + + ]: 31952 : : autovacuum_vac_thresh;
3145 : :
3146 : : /* -1 is used to disable max threshold */
510 nathan@postgresql.or 3147 [ - + ]: 499 : vac_max_thresh = (relopts && relopts->vacuum_max_threshold >= -1)
3148 : : ? relopts->vacuum_max_threshold
3149 [ + + ]: 31952 : : autovacuum_vac_max_thresh;
3150 : :
2285 drowley@postgresql.o 3151 [ - + ]: 499 : vac_ins_scale_factor = (relopts && relopts->vacuum_ins_scale_factor >= 0)
2285 drowley@postgresql.o 3152 :UBC 0 : ? relopts->vacuum_ins_scale_factor
2285 drowley@postgresql.o 3153 [ + + ]:CBC 31952 : : autovacuum_vac_ins_scale;
3154 : :
3155 : : /* -1 is used to disable insert vacuums */
3156 [ - + ]: 499 : vac_ins_base_thresh = (relopts && relopts->vacuum_ins_threshold >= -1)
3157 : : ? relopts->vacuum_ins_threshold
3158 [ + + ]: 31952 : : autovacuum_vac_ins_thresh;
3159 : :
6151 alvherre@alvh.no-ip. 3160 [ - + ]: 499 : anl_scale_factor = (relopts && relopts->analyze_scale_factor >= 0)
6151 alvherre@alvh.no-ip. 3161 :UBC 0 : ? relopts->analyze_scale_factor
6151 alvherre@alvh.no-ip. 3162 [ + + ]:CBC 31952 : : autovacuum_anl_scale;
3163 : :
3164 [ - + ]: 499 : anl_base_thresh = (relopts && relopts->analyze_threshold >= 0)
3165 : : ? relopts->analyze_threshold
3166 [ + + ]: 31952 : : autovacuum_anl_thresh;
3167 : :
3168 [ - + ]: 499 : freeze_max_age = (relopts && relopts->freeze_max_age >= 0)
6151 alvherre@alvh.no-ip. 3169 :UBC 0 : ? Min(relopts->freeze_max_age, autovacuum_freeze_max_age)
6151 alvherre@alvh.no-ip. 3170 [ + + ]:CBC 31952 : : autovacuum_freeze_max_age;
3171 : :
4520 3172 [ - + ]: 499 : multixact_freeze_max_age = (relopts && relopts->multixact_freeze_max_age >= 0)
4071 rhaas@postgresql.org 3173 :UBC 0 : ? Min(relopts->multixact_freeze_max_age, effective_multixact_freeze_max_age)
4071 rhaas@postgresql.org 3174 [ + + ]:CBC 31952 : : effective_multixact_freeze_max_age;
3175 : :
6151 alvherre@alvh.no-ip. 3176 [ + + ]: 31453 : av_enabled = (relopts ? relopts->enabled : true);
88 nathan@postgresql.or 3177 :GNC 31453 : av_enabled &= AutoVacuumingActive();
3178 : :
95 3179 : 31453 : relfrozenxid = classForm->relfrozenxid;
3180 : 31453 : relminmxid = classForm->relminmxid;
3181 : :
3182 : : /* Force vacuum if table is at risk of wraparound */
7177 tgl@sss.pgh.pa.us 3183 :CBC 31453 : xidForceLimit = recentXid - freeze_max_age;
3184 [ - + ]: 31453 : if (xidForceLimit < FirstNormalTransactionId)
7177 tgl@sss.pgh.pa.us 3185 :UBC 0 : xidForceLimit -= FirstNormalTransactionId;
792 noah@leadboat.com 3186 [ + - - + ]:CBC 62906 : force_vacuum = (TransactionIdIsNormal(relfrozenxid) &&
3187 : 31453 : TransactionIdPrecedes(relfrozenxid, xidForceLimit));
4906 alvherre@alvh.no-ip. 3188 [ + - ]: 31453 : if (!force_vacuum)
3189 : : {
4520 3190 : 31453 : multiForceLimit = recentMulti - multixact_freeze_max_age;
4906 3191 [ - + ]: 31453 : if (multiForceLimit < FirstMultiXactId)
4906 alvherre@alvh.no-ip. 3192 :UBC 0 : multiForceLimit -= FirstMultiXactId;
792 noah@leadboat.com 3193 [ + - - + ]:CBC 62906 : force_vacuum = MultiXactIdIsValid(relminmxid) &&
3194 : 31453 : MultiXactIdPrecedes(relminmxid, multiForceLimit);
3195 : : }
6824 alvherre@alvh.no-ip. 3196 : 31453 : *wraparound = force_vacuum;
3197 : :
3198 : : /*
3199 : : * To calculate the (M)XID age portion of the score, divide the age by its
3200 : : * respective *_freeze_max_age parameter. The multixact_freeze_max_age
3201 : : * variable might be 0 here (i.e., a division-by-zero hazard), so in that
3202 : : * case we use the mxid_age as the MXID score.
3203 : : */
88 nathan@postgresql.or 3204 [ + - ]:GNC 31453 : xid_age = TransactionIdIsNormal(relfrozenxid) ? recentXid - relfrozenxid : 0;
3205 [ + - ]: 31453 : mxid_age = MultiXactIdIsValid(relminmxid) ? recentMulti - relminmxid : 0;
3206 : :
3207 : 31453 : scores->xid = (double) xid_age / freeze_max_age;
12 3208 [ + - ]: 31453 : scores->mxid = (double) mxid_age / Max(1, multixact_freeze_max_age);
3209 : :
3210 : : /*
3211 : : * To ensure tables are given increased priority once they begin
3212 : : * approaching wraparound, we scale the score aggressively if the ages
3213 : : * surpass vacuum_failsafe_age or vacuum_multixact_failsafe_age.
3214 : : *
3215 : : * As in vacuum_xid_failsafe_check(), the effective failsafe age is no
3216 : : * less than 105% the value of the respective *_freeze_max_age parameter.
3217 : : * Note that per-table settings could result in a low score even if the
3218 : : * table surpasses the failsafe settings. However, this is a strange
3219 : : * enough corner case that we don't bother trying to handle it.
3220 : : *
3221 : : * We further adjust the effective failsafe ages with the weight
3222 : : * parameters so that increasing them lowers the ages at which we begin
3223 : : * scaling aggressively.
3224 : : */
88 3225 [ + - ]: 31453 : effective_xid_failsafe_age = Max(vacuum_failsafe_age,
3226 : : autovacuum_freeze_max_age * 1.05);
3227 [ + - ]: 31453 : effective_mxid_failsafe_age = Max(vacuum_multixact_failsafe_age,
3228 : : autovacuum_multixact_freeze_max_age * 1.05);
3229 : :
3230 [ - + ]: 31453 : if (autovacuum_freeze_score_weight > 1.0)
88 nathan@postgresql.or 3231 :UNC 0 : effective_xid_failsafe_age /= autovacuum_freeze_score_weight;
88 nathan@postgresql.or 3232 [ - + ]:GNC 31453 : if (autovacuum_multixact_freeze_score_weight > 1.0)
88 nathan@postgresql.or 3233 :UNC 0 : effective_mxid_failsafe_age /= autovacuum_multixact_freeze_score_weight;
3234 : :
88 nathan@postgresql.or 3235 [ - + ]:GNC 31453 : if (xid_age >= effective_xid_failsafe_age)
88 nathan@postgresql.or 3236 [ # # ]:UNC 0 : scores->xid = pow(scores->xid, Max(1.0, (double) xid_age / 100000000));
88 nathan@postgresql.or 3237 [ - + ]:GNC 31453 : if (mxid_age >= effective_mxid_failsafe_age)
88 nathan@postgresql.or 3238 [ # # ]:UNC 0 : scores->mxid = pow(scores->mxid, Max(1.0, (double) mxid_age / 100000000));
3239 : :
88 nathan@postgresql.or 3240 :GNC 31453 : scores->xid *= autovacuum_freeze_score_weight;
3241 : 31453 : scores->mxid *= autovacuum_multixact_freeze_score_weight;
3242 : :
3243 [ + - ]: 31453 : scores->max = Max(scores->xid, scores->mxid);
3244 [ - + ]: 31453 : if (force_vacuum)
95 nathan@postgresql.or 3245 :UNC 0 : *dovacuum = true;
3246 : :
3247 : : /*
3248 : : * If we found stats for the table, and autovacuum is currently enabled,
3249 : : * make a threshold-based decision whether to vacuum and/or analyze. If
3250 : : * autovacuum is currently disabled, we must be here for anti-wraparound
3251 : : * vacuuming only, so don't vacuum (or analyze) anything that's not being
3252 : : * forced.
3253 : : */
85 nathan@postgresql.or 3254 :GNC 31453 : tabentry = pgstat_fetch_stat_tabentry_ext(classForm->relisshared,
3255 : : relid, &may_free);
88 3256 [ + + ]: 31453 : if (!tabentry)
3257 : 8027 : return;
3258 : :
3259 : 23426 : vactuples = tabentry->dead_tuples;
3260 : 23426 : instuples = tabentry->ins_since_vacuum;
3261 : 23426 : anltuples = tabentry->mod_since_analyze;
3262 : :
3263 : : /* If the table hasn't yet been vacuumed, take reltuples as zero */
3264 [ + + ]: 23426 : if (reltuples < 0)
3265 : 5564 : reltuples = 0;
3266 : :
3267 : : /*
3268 : : * If we have data for relallfrozen, calculate the unfrozen percentage of
3269 : : * the table to modify insert scale factor. This helps us decide whether
3270 : : * or not to vacuum an insert-heavy table based on the number of inserts
3271 : : * to the more "active" part of the table.
3272 : : */
3273 [ + + + + ]: 23426 : if (relpages > 0 && relallfrozen > 0)
3274 : : {
3275 : : /*
3276 : : * It could be the stats were updated manually and relallfrozen >
3277 : : * relpages. Clamp relallfrozen to relpages to avoid nonsensical
3278 : : * calculations.
3279 : : */
3280 : 6237 : relallfrozen = Min(relallfrozen, relpages);
3281 : 6237 : pcnt_unfrozen = 1 - ((float4) relallfrozen / relpages);
3282 : : }
3283 : :
3284 : 23426 : vacthresh = (float4) vac_base_thresh + vac_scale_factor * reltuples;
3285 [ + - - + ]: 23426 : if (vac_max_thresh >= 0 && vacthresh > (float4) vac_max_thresh)
88 nathan@postgresql.or 3286 :UNC 0 : vacthresh = (float4) vac_max_thresh;
3287 : :
88 nathan@postgresql.or 3288 :GNC 23426 : vacinsthresh = (float4) vac_ins_base_thresh +
3289 : 23426 : vac_ins_scale_factor * reltuples * pcnt_unfrozen;
3290 : 23426 : anlthresh = (float4) anl_base_thresh + anl_scale_factor * reltuples;
3291 : :
3292 : : /* Determine if this table needs vacuum, and update the score. */
3293 [ + - ]: 23426 : scores->vac = (double) vactuples / Max(vacthresh, 1);
3294 : 23426 : scores->vac *= autovacuum_vacuum_score_weight;
3295 [ + + ]: 23426 : scores->max = Max(scores->max, scores->vac);
3296 [ + + + + ]: 23426 : if (av_enabled && vactuples > vacthresh)
3297 : 739 : *dovacuum = true;
3298 : :
3299 [ + - ]: 23426 : if (vac_ins_base_thresh >= 0)
3300 : : {
3301 [ + - ]: 23426 : scores->vac_ins = (double) instuples / Max(vacinsthresh, 1);
3302 : 23426 : scores->vac_ins *= autovacuum_vacuum_insert_score_weight;
3303 [ + + ]: 23426 : scores->max = Max(scores->max, scores->vac_ins);
3304 [ + + + + ]: 23426 : if (av_enabled && instuples > vacinsthresh)
3305 : 381 : *dovacuum = true;
3306 : : }
3307 : :
3308 : : /*
3309 : : * Determine if this table needs analyze, and update the score. Note that
3310 : : * we don't analyze TOAST tables and pg_statistic.
3311 : : */
3312 [ + + ]: 23426 : if (relid != StatisticRelationId &&
3313 [ + + ]: 23289 : classForm->relkind != RELKIND_TOASTVALUE)
3314 : : {
3315 [ + - ]: 16484 : scores->anl = (double) anltuples / Max(anlthresh, 1);
3316 : 16484 : scores->anl *= autovacuum_analyze_score_weight;
3317 [ + + ]: 16484 : scores->max = Max(scores->max, scores->anl);
3318 [ + + + + ]: 16484 : if (av_enabled && anltuples > anlthresh)
3319 : 1556 : *doanalyze = true;
3320 : : }
3321 : :
3322 [ + - ]: 23426 : if (vac_ins_base_thresh >= 0)
3323 [ - + ]: 23426 : 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",
3324 : : NameStr(classForm->relname),
3325 : : vactuples, vacthresh, scores->vac,
3326 : : instuples, vacinsthresh, scores->vac_ins,
3327 : : anltuples, anlthresh, scores->anl,
3328 : : scores->xid, scores->mxid);
3329 : : else
88 nathan@postgresql.or 3330 [ # # ]:UNC 0 : elog(elevel, "%s: vac: %.0f (thresh %.0f, score %.2f), ins: (disabled), anl: %.0f (thresh %.0f, score %.2f), xid score: %.2f, mxid score: %.2f",
3331 : : NameStr(classForm->relname),
3332 : : vactuples, vacthresh, scores->vac,
3333 : : anltuples, anlthresh, scores->anl,
3334 : : scores->xid, scores->mxid);
3335 : :
3336 : : /* Avoid leaking pgstat entries until the end of autovacuum. */
82 nathan@postgresql.or 3337 [ + - ]:GNC 23426 : if (may_free)
3338 : 23426 : pfree(tabentry);
3339 : : }
3340 : :
3341 : : /*
3342 : : * autovacuum_do_vac_analyze
3343 : : * Vacuum and/or analyze the specified table
3344 : : *
3345 : : * We expect the caller to have switched into a memory context that won't
3346 : : * disappear at transaction commit.
3347 : : */
3348 : : static void
4122 alvherre@alvh.no-ip. 3349 :CBC 878 : autovacuum_do_vac_analyze(autovac_table *tab, BufferAccessStrategy bstrategy)
3350 : : {
3351 : : RangeVar *rangevar;
3352 : : VacuumRelation *rel;
3353 : : List *rel_list;
3354 : : MemoryContext vac_context;
3355 : : MemoryContext old_context;
3356 : :
3357 : : /* Let pgstat know what we're doing */
6557 3358 : 878 : autovac_report_activity(tab);
3359 : :
3360 : : /* Create a context that vacuum() can use as cross-transaction storage */
403 tgl@sss.pgh.pa.us 3361 : 878 : vac_context = AllocSetContextCreate(CurrentMemoryContext,
3362 : : "Vacuum",
3363 : : ALLOCSET_DEFAULT_SIZES);
3364 : :
3365 : : /* Set up one VacuumRelation target, identified by OID, for vacuum() */
3366 : 878 : old_context = MemoryContextSwitchTo(vac_context);
3192 3367 : 878 : rangevar = makeRangeVar(tab->at_nspname, tab->at_relname, -1);
3368 : 878 : rel = makeVacuumRelation(rangevar, tab->at_relid, NIL);
3369 : 878 : rel_list = list_make1(rel);
403 3370 : 878 : MemoryContextSwitchTo(old_context);
3371 : :
91 nathan@postgresql.or 3372 : 878 : vacuum(rel_list, &tab->at_params, bstrategy, vac_context, true);
3373 : :
1181 drowley@postgresql.o 3374 : 876 : MemoryContextDelete(vac_context);
7656 tgl@sss.pgh.pa.us 3375 : 876 : }
3376 : :
3377 : : /*
3378 : : * autovac_report_activity
3379 : : * Report to pgstat what autovacuum is doing
3380 : : *
3381 : : * We send a SQL string corresponding to what the user would see if the
3382 : : * equivalent command was to be issued manually.
3383 : : *
3384 : : * Note we assume that we are going to report the next command as soon as we're
3385 : : * done with the current one, and exit right after the last one, so we don't
3386 : : * bother to report "<IDLE>" or some such.
3387 : : */
3388 : : static void
6557 alvherre@alvh.no-ip. 3389 : 878 : autovac_report_activity(autovac_table *tab)
3390 : : {
3391 : : #define MAX_AUTOVAC_ACTIV_LEN (NAMEDATALEN * 2 + 56)
3392 : : char activity[MAX_AUTOVAC_ACTIV_LEN];
3393 : : int len;
3394 : :
3395 : : /* Report the command and possible options */
2661 rhaas@postgresql.org 3396 [ + + ]: 878 : if (tab->at_params.options & VACOPT_VACUUM)
7347 alvherre@alvh.no-ip. 3397 : 466 : snprintf(activity, MAX_AUTOVAC_ACTIV_LEN,
3398 : : "autovacuum: VACUUM%s",
2661 rhaas@postgresql.org 3399 [ + + ]: 466 : tab->at_params.options & VACOPT_ANALYZE ? " ANALYZE" : "");
3400 : : else
7347 alvherre@alvh.no-ip. 3401 : 412 : snprintf(activity, MAX_AUTOVAC_ACTIV_LEN,
3402 : : "autovacuum: ANALYZE");
3403 : :
3404 : : /*
3405 : : * Report the qualified name of the relation.
3406 : : */
6557 3407 : 878 : len = strlen(activity);
3408 : :
3409 : 878 : snprintf(activity + len, MAX_AUTOVAC_ACTIV_LEN - len,
3410 : : " %s.%s%s", tab->at_nspname, tab->at_relname,
4122 3411 [ - + ]: 878 : tab->at_params.is_wraparound ? " (to prevent wraparound)" : "");
3412 : :
3413 : : /* Set statement_timestamp() to current time for pg_stat_activity */
6855 tgl@sss.pgh.pa.us 3414 : 878 : SetCurrentStatementStartTimestamp();
3415 : :
5276 magnus@hagander.net 3416 : 878 : pgstat_report_activity(STATE_RUNNING, activity);
7347 alvherre@alvh.no-ip. 3417 : 878 : }
3418 : :
3419 : : /*
3420 : : * autovac_report_workitem
3421 : : * Report to pgstat that autovacuum is processing a work item
3422 : : */
3423 : : static void
3377 3424 : 6 : autovac_report_workitem(AutoVacuumWorkItem *workitem,
3425 : : const char *nspname, const char *relname)
3426 : : {
3427 : : char activity[MAX_AUTOVAC_ACTIV_LEN + 12 + 2];
3428 : : char blk[12 + 2];
3429 : : int len;
3430 : :
3431 [ + - ]: 6 : switch (workitem->avw_type)
3432 : : {
3433 : 6 : case AVW_BRINSummarizeRange:
3434 : 6 : snprintf(activity, MAX_AUTOVAC_ACTIV_LEN,
3435 : : "autovacuum: BRIN summarize");
3436 : 6 : break;
3437 : : }
3438 : :
3439 : : /*
3440 : : * Report the qualified name of the relation, and the block number if any
3441 : : */
3442 : 6 : len = strlen(activity);
3443 : :
3444 [ + - ]: 6 : if (BlockNumberIsValid(workitem->avw_blockNumber))
3445 : 6 : snprintf(blk, sizeof(blk), " %u", workitem->avw_blockNumber);
3446 : : else
3377 alvherre@alvh.no-ip. 3447 :UBC 0 : blk[0] = '\0';
3448 : :
3377 alvherre@alvh.no-ip. 3449 :CBC 6 : snprintf(activity + len, MAX_AUTOVAC_ACTIV_LEN - len,
3450 : : " %s.%s%s", nspname, relname, blk);
3451 : :
3452 : : /* Set statement_timestamp() to current time for pg_stat_activity */
3453 : 6 : SetCurrentStatementStartTimestamp();
3454 : :
3455 : 6 : pgstat_report_activity(STATE_RUNNING, activity);
3456 : 6 : }
3457 : :
3458 : : /*
3459 : : * AutoVacuumingActive
3460 : : * Check GUC vars and report whether the autovacuum process should be
3461 : : * running.
3462 : : */
3463 : : bool
7656 tgl@sss.pgh.pa.us 3464 : 77965 : AutoVacuumingActive(void)
3465 : : {
6854 3466 [ + + - + ]: 77965 : if (!autovacuum_start_daemon || !pgstat_track_counts)
7656 3467 : 5404 : return false;
3468 : 72561 : return true;
3469 : : }
3470 : :
3471 : : /*
3472 : : * Request one work item to the next autovacuum run processing our database.
3473 : : * Return false if the request can't be recorded.
3474 : : */
3475 : : bool
3377 alvherre@alvh.no-ip. 3476 : 6 : AutoVacuumRequestWork(AutoVacuumWorkItemType type, Oid relationId,
3477 : : BlockNumber blkno)
3478 : : {
3479 : : int i;
3030 3480 : 6 : bool result = false;
3481 : :
3377 3482 : 6 : LWLockAcquire(AutovacuumLock, LW_EXCLUSIVE);
3483 : :
3484 : : /*
3485 : : * Locate an unused work item and fill it with the given data.
3486 : : */
3241 3487 [ + - ]: 21 : for (i = 0; i < NUM_WORKITEMS; i++)
3488 : : {
3489 : 21 : AutoVacuumWorkItem *workitem = &AutoVacuumShmem->av_workItems[i];
3490 : :
3491 [ + + ]: 21 : if (workitem->avw_used)
3492 : 15 : continue;
3493 : :
3494 : 6 : workitem->avw_used = true;
3495 : 6 : workitem->avw_active = false;
3496 : 6 : workitem->avw_type = type;
3497 : 6 : workitem->avw_database = MyDatabaseId;
3498 : 6 : workitem->avw_relation = relationId;
3499 : 6 : workitem->avw_blockNumber = blkno;
3030 3500 : 6 : result = true;
3501 : :
3502 : : /* done */
3241 3503 : 6 : break;
3504 : : }
3505 : :
3377 3506 : 6 : LWLockRelease(AutovacuumLock);
3507 : :
3030 3508 : 6 : return result;
3509 : : }
3510 : :
3511 : : /*
3512 : : * autovac_init
3513 : : * This is called at postmaster initialization.
3514 : : *
3515 : : * All we do here is annoy the user if he got it wrong.
3516 : : */
3517 : : void
7656 tgl@sss.pgh.pa.us 3518 : 961 : autovac_init(void)
3519 : : {
540 nathan@postgresql.or 3520 [ + + ]: 961 : if (!autovacuum_start_daemon)
3521 : 131 : return;
3522 [ - + ]: 830 : else if (!pgstat_track_counts)
7656 tgl@sss.pgh.pa.us 3523 [ # # ]:UBC 0 : ereport(WARNING,
3524 : : (errmsg("autovacuum not started because of misconfiguration"),
3525 : : errhint("Enable the \"track_counts\" option.")));
3526 : : else
540 nathan@postgresql.or 3527 :CBC 830 : check_av_worker_gucs();
3528 : : }
3529 : :
3530 : : /*
3531 : : * AutoVacuumShmemRequest
3532 : : * Register shared memory space needed for autovacuum
3533 : : */
3534 : : static void
85 heikki.linnakangas@i 3535 :GNC 1212 : AutoVacuumShmemRequest(void *arg)
3536 : : {
3537 : : Size size;
3538 : :
3539 : : /*
3540 : : * Need the fixed struct and the array of WorkerInfoData.
3541 : : */
7015 alvherre@alvh.no-ip. 3542 :CBC 1212 : size = sizeof(AutoVacuumShmemStruct);
3543 : 1212 : size = MAXALIGN(size);
540 nathan@postgresql.or 3544 : 1212 : size = add_size(size, mul_size(autovacuum_worker_slots,
3545 : : sizeof(WorkerInfoData)));
3546 : :
85 heikki.linnakangas@i 3547 :GNC 1212 : ShmemRequestStruct(.name = "AutoVacuum Data",
3548 : : .size = size,
3549 : : .ptr = (void **) &AutoVacuumShmem,
3550 : : );
7075 alvherre@alvh.no-ip. 3551 :GIC 1212 : }
3552 : :
3553 : : /*
3554 : : * AutoVacuumShmemInit
3555 : : * Initialize autovacuum-related shared memory
3556 : : */
3557 : : static void
85 heikki.linnakangas@i 3558 :GNC 1209 : AutoVacuumShmemInit(void *arg)
3559 : : {
3560 : : WorkerInfo worker;
3561 : :
3562 : 1209 : AutoVacuumShmem->av_launcherpid = 0;
3563 : 1209 : dclist_init(&AutoVacuumShmem->av_freeWorkers);
3564 : 1209 : dlist_init(&AutoVacuumShmem->av_runningWorkers);
3565 : 1209 : AutoVacuumShmem->av_startingWorker = NULL;
3566 : 1209 : memset(AutoVacuumShmem->av_workItems, 0,
3567 : : sizeof(AutoVacuumWorkItem) * NUM_WORKITEMS);
3568 : :
3569 : 1209 : worker = (WorkerInfo) ((char *) AutoVacuumShmem +
3570 : : MAXALIGN(sizeof(AutoVacuumShmemStruct)));
3571 : :
3572 : : /* initialize the WorkerInfo free list */
3573 [ + + ]: 14389 : for (int i = 0; i < autovacuum_worker_slots; i++)
3574 : : {
3575 : 13180 : dclist_push_head(&AutoVacuumShmem->av_freeWorkers,
3576 : 13180 : &worker[i].wi_links);
3577 : 13180 : pg_atomic_init_flag(&worker[i].wi_dobalance);
3578 : : }
3579 : :
3580 : 1209 : pg_atomic_init_u32(&AutoVacuumShmem->av_nworkersForBalance, 0);
7656 tgl@sss.pgh.pa.us 3581 :CBC 1209 : }
3582 : :
3583 : : /*
3584 : : * GUC check_hook for autovacuum_work_mem
3585 : : */
3586 : : bool
1386 3587 : 1255 : check_autovacuum_work_mem(int *newval, void **extra, GucSource source)
3588 : : {
3589 : : /*
3590 : : * -1 indicates fallback.
3591 : : *
3592 : : * If we haven't yet changed the boot_val default of -1, just let it be.
3593 : : * Autovacuum will look to maintenance_work_mem instead.
3594 : : */
3595 [ + + ]: 1255 : if (*newval == -1)
3596 : 1253 : return true;
3597 : :
3598 : : /*
3599 : : * We clamp manually-set values to at least 64kB. Since
3600 : : * maintenance_work_mem is always set to at least this value, do the same
3601 : : * here.
3602 : : */
693 john.naylor@postgres 3603 [ + - ]: 2 : if (*newval < 64)
3604 : 2 : *newval = 64;
3605 : :
1386 tgl@sss.pgh.pa.us 3606 : 2 : return true;
3607 : : }
3608 : :
3609 : : /*
3610 : : * Returns whether there is a free autovacuum worker slot available.
3611 : : */
3612 : : static bool
540 nathan@postgresql.or 3613 : 4197 : av_worker_available(void)
3614 : : {
3615 : : int free_slots;
3616 : : int reserved_slots;
3617 : :
3618 : 4197 : free_slots = dclist_count(&AutoVacuumShmem->av_freeWorkers);
3619 : :
3620 : 4197 : reserved_slots = autovacuum_worker_slots - autovacuum_max_workers;
3621 : 4197 : reserved_slots = Max(0, reserved_slots);
3622 : :
3623 : 4197 : return free_slots > reserved_slots;
3624 : : }
3625 : :
3626 : : /*
3627 : : * Emits a WARNING if autovacuum_worker_slots < autovacuum_max_workers.
3628 : : */
3629 : : static void
3630 : 830 : check_av_worker_gucs(void)
3631 : : {
3632 [ - + ]: 830 : if (autovacuum_worker_slots < autovacuum_max_workers)
540 nathan@postgresql.or 3633 [ # # ]:UBC 0 : ereport(WARNING,
3634 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3635 : : errmsg("\"autovacuum_max_workers\" (%d) should be less than or equal to \"autovacuum_worker_slots\" (%d)",
3636 : : autovacuum_max_workers, autovacuum_worker_slots),
3637 : : errdetail("The server will only start up to \"autovacuum_worker_slots\" (%d) autovacuum workers at a given time.",
3638 : : autovacuum_worker_slots)));
540 nathan@postgresql.or 3639 :CBC 830 : }
3640 : :
3641 : : /*
3642 : : * pg_stat_get_autovacuum_scores
3643 : : *
3644 : : * Returns current autovacuum scores for all relevant tables in the current
3645 : : * database.
3646 : : */
3647 : : Datum
85 nathan@postgresql.or 3648 :UNC 0 : pg_stat_get_autovacuum_scores(PG_FUNCTION_ARGS)
3649 : : {
3650 : : int effective_multixact_freeze_max_age;
3651 : : Relation rel;
3652 : : TableScanDesc scan;
3653 : : HeapTuple tup;
3654 : 0 : ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
3655 : :
3656 : 0 : InitMaterializedSRF(fcinfo, 0);
3657 : :
3658 : : /* some prerequisite initialization */
3659 : 0 : effective_multixact_freeze_max_age = MultiXactMemberFreezeThreshold();
3660 : 0 : recentXid = ReadNextTransactionId();
3661 : 0 : recentMulti = ReadNextMultiXactId();
3662 : :
3663 : : /* scan pg_class */
3664 : 0 : rel = table_open(RelationRelationId, AccessShareLock);
3665 : 0 : scan = table_beginscan_catalog(rel, 0, NULL);
3666 [ # # ]: 0 : while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL)
3667 : : {
3668 : 0 : Form_pg_class form = (Form_pg_class) GETSTRUCT(tup);
3669 : : AutoVacOpts *avopts;
3670 : : bool dovacuum;
3671 : : bool doanalyze;
3672 : : bool wraparound;
3673 : : AutoVacuumScores scores;
3674 : : Datum vals[10];
3675 : 0 : bool nulls[10] = {false};
3676 : :
3677 : : /* skip ineligible entries */
3678 [ # # ]: 0 : if (form->relkind != RELKIND_RELATION &&
3679 [ # # ]: 0 : form->relkind != RELKIND_MATVIEW &&
3680 [ # # ]: 0 : form->relkind != RELKIND_TOASTVALUE)
3681 : 0 : continue;
3682 [ # # ]: 0 : if (form->relpersistence == RELPERSISTENCE_TEMP)
3683 : 0 : continue;
3684 : :
3685 : 0 : avopts = extract_autovac_opts(tup, RelationGetDescr(rel));
3686 : 0 : relation_needs_vacanalyze(form->oid, avopts, form,
3687 : : effective_multixact_freeze_max_age,
3688 : : LOG_NEVER,
3689 : : &dovacuum, &doanalyze, &wraparound,
3690 : : &scores);
3691 [ # # ]: 0 : if (avopts)
3692 : 0 : pfree(avopts);
3693 : :
3694 : 0 : vals[0] = ObjectIdGetDatum(form->oid);
3695 : 0 : vals[1] = Float8GetDatum(scores.max);
3696 : 0 : vals[2] = Float8GetDatum(scores.xid);
3697 : 0 : vals[3] = Float8GetDatum(scores.mxid);
3698 : 0 : vals[4] = Float8GetDatum(scores.vac);
3699 : 0 : vals[5] = Float8GetDatum(scores.vac_ins);
3700 : 0 : vals[6] = Float8GetDatum(scores.anl);
3701 : 0 : vals[7] = BoolGetDatum(dovacuum);
3702 : 0 : vals[8] = BoolGetDatum(doanalyze);
3703 : 0 : vals[9] = BoolGetDatum(wraparound);
3704 : :
3705 : 0 : tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, vals, nulls);
3706 : : }
3707 : 0 : table_endscan(scan);
3708 : 0 : table_close(rel, AccessShareLock);
3709 : :
3710 : 0 : return (Datum) 0;
3711 : : }
|