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