PostgreSQL Source Code git master
Loading...
Searching...
No Matches
autovacuum.h File Reference
#include "storage/block.h"
Include dependency graph for autovacuum.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Enumerations

enum  AutoVacuumWorkItemType { AVW_BRINSummarizeRange }
 

Functions

bool AutoVacuumingActive (void)
 
void autovac_init (void)
 
void AutoVacWorkerFailed (void)
 
pg_noreturn void AutoVacLauncherMain (const void *startup_data, size_t startup_data_len)
 
pg_noreturn void AutoVacWorkerMain (const void *startup_data, size_t startup_data_len)
 
bool AutoVacuumRequestWork (AutoVacuumWorkItemType type, Oid relationId, BlockNumber blkno)
 

Variables

PGDLLIMPORT bool autovacuum_start_daemon
 
PGDLLIMPORT int autovacuum_worker_slots
 
PGDLLIMPORT int autovacuum_max_workers
 
PGDLLIMPORT int autovacuum_work_mem
 
PGDLLIMPORT int autovacuum_naptime
 
PGDLLIMPORT int autovacuum_vac_thresh
 
PGDLLIMPORT int autovacuum_vac_max_thresh
 
PGDLLIMPORT double autovacuum_vac_scale
 
PGDLLIMPORT int autovacuum_vac_ins_thresh
 
PGDLLIMPORT double autovacuum_vac_ins_scale
 
PGDLLIMPORT int autovacuum_anl_thresh
 
PGDLLIMPORT double autovacuum_anl_scale
 
PGDLLIMPORT int autovacuum_freeze_max_age
 
PGDLLIMPORT int autovacuum_multixact_freeze_max_age
 
PGDLLIMPORT double autovacuum_vac_cost_delay
 
PGDLLIMPORT int autovacuum_vac_cost_limit
 
PGDLLIMPORT double autovacuum_freeze_score_weight
 
PGDLLIMPORT double autovacuum_multixact_freeze_score_weight
 
PGDLLIMPORT double autovacuum_vacuum_score_weight
 
PGDLLIMPORT double autovacuum_vacuum_insert_score_weight
 
PGDLLIMPORT double autovacuum_analyze_score_weight
 
PGDLLIMPORT int Log_autovacuum_min_duration
 
PGDLLIMPORT int Log_autoanalyze_min_duration
 

Enumeration Type Documentation

◆ AutoVacuumWorkItemType

Enumerator
AVW_BRINSummarizeRange 

Definition at line 23 of file autovacuum.h.

24{
AutoVacuumWorkItemType
Definition autovacuum.h:24
@ AVW_BRINSummarizeRange
Definition autovacuum.h:25

Function Documentation

◆ autovac_init()

void autovac_init ( void  )
extern

Definition at line 3520 of file autovacuum.c.

3521{
3523 return;
3524 else if (!pgstat_track_counts)
3526 (errmsg("autovacuum not started because of misconfiguration"),
3527 errhint("Enable the \"track_counts\" option.")));
3528 else
3530}
static void check_av_worker_gucs(void)
bool autovacuum_start_daemon
Definition autovacuum.c:123
int errhint(const char *fmt,...) pg_attribute_printf(1
#define WARNING
Definition elog.h:37
#define ereport(elevel,...)
Definition elog.h:152
static char * errmsg
bool pgstat_track_counts
Definition pgstat.c:204

References autovacuum_start_daemon, check_av_worker_gucs(), ereport, errhint(), errmsg, pgstat_track_counts, and WARNING.

Referenced by PostmasterMain().

◆ AutoVacLauncherMain()

pg_noreturn void AutoVacLauncherMain ( const void startup_data,
size_t  startup_data_len 
)
extern

Definition at line 411 of file autovacuum.c.

412{
414
416
417 /* Release postmaster's working memory context */
419 {
422 }
423
425
427 (errmsg_internal("autovacuum launcher started")));
428
429 if (PostAuthDelay)
430 pg_usleep(PostAuthDelay * 1000000L);
431
433
434 /*
435 * Set up signal handlers. We operate on databases much like a regular
436 * backend, so we use the same signal handling. See equivalent code in
437 * tcop/postgres.c.
438 */
442 /* SIGQUIT handler was already set up by InitPostmasterChild */
443
444 InitializeTimeouts(); /* establishes SIGALRM handler */
445
451
452 /*
453 * Create a per-backend PGPROC struct in shared memory. We must do this
454 * before we can use LWLocks or access any shared memory.
455 */
456 InitProcess();
457
458 /* Early initialization */
459 BaseInit();
460
462
464
465 /*
466 * Create a memory context that we will do all our work in. We do this so
467 * that we can reset the context during error recovery and thereby avoid
468 * possible memory leaks.
469 */
471 "Autovacuum Launcher",
474
475 /*
476 * If an exception is encountered, processing resumes here.
477 *
478 * This code is a stripped down version of PostgresMain error recovery.
479 *
480 * Note that we use sigsetjmp(..., 1), so that the prevailing signal mask
481 * (to wit, BlockSig) will be restored when longjmp'ing to here. Thus,
482 * signals other than SIGQUIT will be blocked until we complete error
483 * recovery. It might seem that this policy makes the HOLD_INTERRUPTS()
484 * call redundant, but it is not since InterruptPending might be set
485 * already.
486 */
487 if (sigsetjmp(local_sigjmp_buf, 1) != 0)
488 {
489 /* since not using PG_TRY, must reset error stack by hand */
491
492 /* Prevents interrupts while cleaning up */
494
495 /* Forget any pending QueryCancel or timeout request */
497 QueryCancelPending = false; /* second to avoid race condition */
498
499 /* Report the error to the server log */
501
502 /* Abort the current transaction in order to recover */
504
505 /*
506 * Release any other resources, for the case where we were not in a
507 * transaction.
508 */
513 /* this is probably dead code, but let's be safe: */
516 AtEOXact_Buffers(false);
518 AtEOXact_Files(false);
519 AtEOXact_HashTables(false);
520
521 /*
522 * Now return to normal top-level context and clear ErrorContext for
523 * next time.
524 */
527
528 /* Flush any leaked data in the top-level context */
530
531 /* don't leave dangling pointers to freed memory */
534
535 /* Now we can allow interrupts again */
537
538 /* if in shutdown mode, no need for anything further; just go away */
541
542 /*
543 * Sleep at least 1 second after any error. We don't want to be
544 * filling the error logs as fast as we can.
545 */
546 pg_usleep(1000000L);
547 }
548
549 /* We can now handle ereport(ERROR) */
551
552 /* must unblock signals before calling rebuild_database_list */
554
555 /*
556 * Set always-secure search path. Launcher doesn't connect to a database,
557 * so this has no effect.
558 */
559 SetConfigOption("search_path", "", PGC_SUSET, PGC_S_OVERRIDE);
560
561 /*
562 * Force zero_damaged_pages OFF in the autovac process, even if it is set
563 * in postgresql.conf. We don't really want such a dangerous option being
564 * applied non-interactively.
565 */
566 SetConfigOption("zero_damaged_pages", "false", PGC_SUSET, PGC_S_OVERRIDE);
567
568 /*
569 * Force settable timeouts off to avoid letting these settings prevent
570 * regular maintenance from being executed.
571 */
572 SetConfigOption("statement_timeout", "0", PGC_SUSET, PGC_S_OVERRIDE);
573 SetConfigOption("transaction_timeout", "0", PGC_SUSET, PGC_S_OVERRIDE);
574 SetConfigOption("lock_timeout", "0", PGC_SUSET, PGC_S_OVERRIDE);
575 SetConfigOption("idle_in_transaction_session_timeout", "0",
577
578 /*
579 * Force default_transaction_isolation to READ COMMITTED. We don't want
580 * to pay the overhead of serializable mode, nor add any risk of causing
581 * deadlocks or delaying other transactions.
582 */
583 SetConfigOption("default_transaction_isolation", "read committed",
585
586 /*
587 * Even when system is configured to use a different fetch consistency,
588 * for autovac we always want fresh stats.
589 */
590 SetConfigOption("stats_fetch_consistency", "none", PGC_SUSET, PGC_S_OVERRIDE);
591
592 /*
593 * In emergency mode, just start a worker (unless shutdown was requested)
594 * and go away.
595 */
596 if (!AutoVacuumingActive())
597 {
600 proc_exit(0); /* done */
601 }
602
603 /*
604 * Create the initial database list. The invariant we want this list to
605 * keep is that it's ordered by decreasing next_worker. As soon as an
606 * entry is updated to a higher time, it will be moved to the front (which
607 * is correct because the only operation is to add autovacuum_naptime to
608 * the entry, and time always increases).
609 */
611
612 /* loop until shutdown request */
614 {
615 struct timeval nap;
617 bool can_launch;
618
619 /*
620 * This loop is a bit different from the normal use of WaitLatch,
621 * because we'd like to sleep before the first launch of a child
622 * process. So it's WaitLatch, then ResetLatch, then check for
623 * wakening conditions.
624 */
625
627
628 /*
629 * Wait until naptime expires or we get some type of signal (all the
630 * signal handlers will wake us by calling SetLatch).
631 */
634 (nap.tv_sec * 1000L) + (nap.tv_usec / 1000L),
636
638
640
641 /*
642 * a worker finished, or postmaster signaled failure to start a worker
643 */
644 if (got_SIGUSR2)
645 {
646 got_SIGUSR2 = false;
647
648 /* rebalance cost limits, if needed */
650 {
655 }
656
658 {
659 /*
660 * If the postmaster failed to start a new worker, we sleep
661 * for a little while and resend the signal. The new worker's
662 * state is still in memory, so this is sufficient. After
663 * that, we restart the main loop.
664 *
665 * XXX should we put a limit to the number of times we retry?
666 * I don't think it makes much sense, because a future start
667 * of a worker will continue to fail in the same way.
668 */
670 pg_usleep(1000000L); /* 1s */
672 continue;
673 }
674 }
675
676 /*
677 * There are some conditions that we need to check before trying to
678 * start a worker. First, we need to make sure that there is a worker
679 * slot available. Second, we need to make sure that no other worker
680 * failed while starting up.
681 */
682
685
687
689 {
690 int waittime;
692
693 /*
694 * We can't launch another worker when another one is still
695 * starting up (or failed while doing so), so just sleep for a bit
696 * more; that worker will wake us up again as soon as it's ready.
697 * We will only wait autovacuum_naptime seconds (up to a maximum
698 * of 60 seconds) for this to happen however. Note that failure
699 * to connect to a particular database is not a problem here,
700 * because the worker removes itself from the startingWorker
701 * pointer before trying to connect. Problems detected by the
702 * postmaster (like fork() failure) are also reported and handled
703 * differently. The only problems that may cause this code to
704 * fire are errors in the earlier sections of AutoVacWorkerMain,
705 * before the worker removes the WorkerInfo from the
706 * startingWorker pointer.
707 */
708 waittime = Min(autovacuum_naptime, 60) * 1000;
710 waittime))
711 {
714
715 /*
716 * No other process can put a worker in starting mode, so if
717 * startingWorker is still INVALID after exchanging our lock,
718 * we assume it's the same one we saw above (so we don't
719 * recheck the launch time).
720 */
722 {
724 worker->wi_dboid = InvalidOid;
725 worker->wi_tableoid = InvalidOid;
726 worker->wi_sharedrel = false;
727 worker->wi_proc = NULL;
728 worker->wi_launchtime = 0;
730 &worker->wi_links);
733 errmsg("autovacuum worker took too long to start; canceled"));
734 }
735 }
736 else
737 can_launch = false;
738 }
739 LWLockRelease(AutovacuumLock); /* either shared or exclusive */
740
741 /* if we can't do anything, just go back to sleep */
742 if (!can_launch)
743 continue;
744
745 /* We're OK to start a new worker */
746
748 {
749 /*
750 * Special case when the list is empty: start a worker right away.
751 * This covers the initial case, when no database is in pgstats
752 * (thus the list is empty). Note that the constraints in
753 * launcher_determine_sleep keep us from starting workers too
754 * quickly (at most once every autovacuum_naptime when the list is
755 * empty).
756 */
758 }
759 else
760 {
761 /*
762 * because rebuild_database_list constructs a list with most
763 * distant adl_next_worker first, we obtain our database from the
764 * tail of the list.
765 */
767
769
770 /*
771 * launch a worker if next_worker is right now or it is in the
772 * past
773 */
774 if (TimestampDifferenceExceeds(avdb->adl_next_worker,
775 current_time, 0))
777 }
778 }
779
781}
void pgaio_error_cleanup(void)
Definition aio.c:1175
static Oid do_start_worker(void)
static void launcher_determine_sleep(bool canlaunch, bool recursing, struct timeval *nap)
Definition autovacuum.c:847
static volatile sig_atomic_t got_SIGUSR2
Definition autovacuum.c:165
static void avl_sigusr2_handler(SIGNAL_ARGS)
static bool av_worker_available(void)
int autovacuum_naptime
Definition autovacuum.c:127
bool AutoVacuumingActive(void)
static pg_noreturn void AutoVacLauncherShutdown(void)
Definition autovacuum.c:832
@ AutoVacRebalance
Definition autovacuum.c:261
@ AutoVacForkFailed
Definition autovacuum.c:260
static void launch_worker(TimestampTz now)
static dlist_head DatabaseList
Definition autovacuum.c:325
static void rebuild_database_list(Oid newdb)
Definition autovacuum.c:931
static AutoVacuumShmemStruct * AutoVacuumShmem
Definition autovacuum.c:311
static MemoryContext DatabaseListCxt
Definition autovacuum.c:326
static void autovac_recalculate_workers_for_balance(void)
static MemoryContext AutovacMemCxt
Definition autovacuum.c:178
static void ProcessAutoVacLauncherInterrupts(void)
Definition autovacuum.c:787
sigset_t UnBlockSig
Definition pqsignal.c:22
bool TimestampDifferenceExceeds(TimestampTz start_time, TimestampTz stop_time, int msec)
Definition timestamp.c:1789
TimestampTz GetCurrentTimestamp(void)
Definition timestamp.c:1649
void AtEOXact_Buffers(bool isCommit)
Definition bufmgr.c:4222
void UnlockBuffers(void)
Definition bufmgr.c:5875
#define Min(x, y)
Definition c.h:1131
#define Assert(condition)
Definition c.h:1002
int64 TimestampTz
Definition timestamp.h:39
void AtEOXact_HashTables(bool isCommit)
Definition dynahash.c:1864
void EmitErrorReport(void)
Definition elog.c:1883
ErrorContextCallback * error_context_stack
Definition elog.c:100
void FlushErrorState(void)
Definition elog.c:2063
sigjmp_buf * PG_exception_stack
Definition elog.c:102
int int errmsg_internal(const char *fmt,...) pg_attribute_printf(1
#define DEBUG1
Definition elog.h:31
void AtEOXact_Files(bool isCommit)
Definition fd.c:3212
volatile sig_atomic_t QueryCancelPending
Definition globals.c:33
struct Latch * MyLatch
Definition globals.c:65
void SetConfigOption(const char *name, const char *value, GucContext context, GucSource source)
Definition guc.c:4234
@ PGC_S_OVERRIDE
Definition guc.h:123
@ PGC_SUSET
Definition guc.h:78
static void dlist_init(dlist_head *head)
Definition ilist.h:314
#define dlist_tail_element(type, membername, lhead)
Definition ilist.h:612
static bool dlist_is_empty(const dlist_head *head)
Definition ilist.h:336
static void dclist_push_head(dclist_head *head, dlist_node *node)
Definition ilist.h:693
void SignalHandlerForShutdownRequest(SIGNAL_ARGS)
Definition interrupt.c:104
volatile sig_atomic_t ShutdownRequestPending
Definition interrupt.c:28
void SignalHandlerForConfigReload(SIGNAL_ARGS)
Definition interrupt.c:61
void proc_exit(int code)
Definition ipc.c:105
void ResetLatch(Latch *latch)
Definition latch.c:374
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition latch.c:172
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition lwlock.c:1150
void LWLockRelease(LWLock *lock)
Definition lwlock.c:1767
void LWLockReleaseAll(void)
Definition lwlock.c:1866
@ LW_SHARED
Definition lwlock.h:105
@ LW_EXCLUSIVE
Definition lwlock.h:104
void MemoryContextReset(MemoryContext context)
Definition mcxt.c:406
MemoryContext TopMemoryContext
Definition mcxt.c:167
MemoryContext PostmasterContext
Definition mcxt.c:169
void MemoryContextDelete(MemoryContext context)
Definition mcxt.c:475
#define AllocSetContextCreate
Definition memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition memutils.h:160
#define RESUME_INTERRUPTS()
Definition miscadmin.h:138
@ NormalProcessing
Definition miscadmin.h:481
@ InitProcessing
Definition miscadmin.h:480
#define GetProcessingMode()
Definition miscadmin.h:490
#define HOLD_INTERRUPTS()
Definition miscadmin.h:136
#define SetProcessingMode(mode)
Definition miscadmin.h:492
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition palloc.h:138
void SendPostmasterSignal(PMSignalReason reason)
Definition pmsignal.c:164
@ PMSIGNAL_START_AUTOVAC_WORKER
Definition pmsignal.h:40
#define pqsignal
Definition port.h:548
#define PG_SIG_IGN
Definition port.h:552
#define PG_SIG_DFL
Definition port.h:551
int PostAuthDelay
Definition postgres.c:105
void FloatExceptionHandler(SIGNAL_ARGS)
Definition postgres.c:3172
void StatementCancelHandler(SIGNAL_ARGS)
Definition postgres.c:3155
#define InvalidOid
void BaseInit(void)
Definition postinit.c:622
void InitPostgres(const char *in_dbname, Oid dboid, const char *username, Oid useroid, uint32 flags, char *out_dbname)
Definition postinit.c:722
static int fb(int x)
void procsignal_sigusr1_handler(SIGNAL_ARGS)
Definition procsignal.c:696
void init_ps_display(const char *fixed_part)
Definition ps_status.c:286
void ReleaseAuxProcessResources(bool isCommit)
Definition resowner.c:1026
ResourceOwner AuxProcessResourceOwner
Definition resowner.c:176
void pg_usleep(long microsec)
Definition signal.c:53
void AtEOXact_SMgr(void)
Definition smgr.c:1017
void InitProcess(void)
Definition proc.c:393
dclist_head av_freeWorkers
Definition autovacuum.c:304
WorkerInfo av_startingWorker
Definition autovacuum.c:306
sig_atomic_t av_signal[AutoVacNumSignals]
Definition autovacuum.c:303
TimestampTz wi_launchtime
Definition autovacuum.c:246
dlist_node wi_links
Definition autovacuum.c:242
PGPROC * wi_proc
Definition autovacuum.c:245
void disable_all_timeouts(bool keep_indicators)
Definition timeout.c:751
void InitializeTimeouts(void)
Definition timeout.c:470
static void pgstat_report_wait_end(void)
Definition wait_event.h:83
#define WL_TIMEOUT
#define WL_EXIT_ON_PM_DEATH
#define WL_LATCH_SET
#define SIGCHLD
Definition win32_port.h:168
#define SIGHUP
Definition win32_port.h:158
#define SIGPIPE
Definition win32_port.h:163
#define SIGUSR1
Definition win32_port.h:170
#define SIGUSR2
Definition win32_port.h:171
void AbortCurrentTransaction(void)
Definition xact.c:3504

References AbortCurrentTransaction(), ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, Assert, AtEOXact_Buffers(), AtEOXact_Files(), AtEOXact_HashTables(), AtEOXact_SMgr(), autovac_recalculate_workers_for_balance(), AutoVacForkFailed, AutoVacLauncherShutdown(), AutovacMemCxt, AutoVacRebalance, autovacuum_naptime, AutoVacuumingActive(), AutoVacuumShmem, AuxProcessResourceOwner, AutoVacuumShmemStruct::av_freeWorkers, AutoVacuumShmemStruct::av_signal, AutoVacuumShmemStruct::av_startingWorker, av_worker_available(), avl_sigusr2_handler(), BaseInit(), DatabaseList, DatabaseListCxt, dclist_push_head(), DEBUG1, disable_all_timeouts(), dlist_init(), dlist_is_empty(), dlist_tail_element, do_start_worker(), EmitErrorReport(), ereport, errmsg, errmsg_internal(), error_context_stack, fb(), FloatExceptionHandler(), FlushErrorState(), GetCurrentTimestamp(), GetProcessingMode, got_SIGUSR2, HOLD_INTERRUPTS, init_ps_display(), InitializeTimeouts(), InitPostgres(), InitProcess(), InitProcessing, InvalidOid, launch_worker(), launcher_determine_sleep(), LW_EXCLUSIVE, LW_SHARED, LWLockAcquire(), LWLockRelease(), LWLockReleaseAll(), MemoryContextDelete(), MemoryContextReset(), MemoryContextSwitchTo(), Min, MyLatch, NormalProcessing, PG_exception_stack, PG_SIG_DFL, PG_SIG_IGN, pg_usleep(), pgaio_error_cleanup(), PGC_S_OVERRIDE, PGC_SUSET, pgstat_report_wait_end(), PMSIGNAL_START_AUTOVAC_WORKER, PostAuthDelay, PostmasterContext, pqsignal, proc_exit(), ProcessAutoVacLauncherInterrupts(), procsignal_sigusr1_handler(), QueryCancelPending, rebuild_database_list(), ReleaseAuxProcessResources(), ResetLatch(), RESUME_INTERRUPTS, SendPostmasterSignal(), SetConfigOption(), SetProcessingMode, ShutdownRequestPending, SIGCHLD, SIGHUP, SignalHandlerForConfigReload(), SignalHandlerForShutdownRequest(), SIGPIPE, SIGUSR1, SIGUSR2, StatementCancelHandler(), TimestampDifferenceExceeds(), TopMemoryContext, UnBlockSig, UnlockBuffers(), WaitLatch(), WARNING, WorkerInfoData::wi_dboid, WorkerInfoData::wi_launchtime, WorkerInfoData::wi_links, WorkerInfoData::wi_proc, WorkerInfoData::wi_sharedrel, WorkerInfoData::wi_tableoid, WL_EXIT_ON_PM_DEATH, WL_LATCH_SET, and WL_TIMEOUT.

◆ AutoVacuumingActive()

bool AutoVacuumingActive ( void  )
extern

◆ AutoVacuumRequestWork()

bool AutoVacuumRequestWork ( AutoVacuumWorkItemType  type,
Oid  relationId,
BlockNumber  blkno 
)
extern

Definition at line 3478 of file autovacuum.c.

3480{
3481 int i;
3482 bool result = false;
3483
3485
3486 /*
3487 * Locate an unused work item and fill it with the given data.
3488 */
3489 for (i = 0; i < NUM_WORKITEMS; i++)
3490 {
3492
3493 if (workitem->avw_used)
3494 continue;
3495
3496 workitem->avw_used = true;
3497 workitem->avw_active = false;
3498 workitem->avw_type = type;
3499 workitem->avw_database = MyDatabaseId;
3500 workitem->avw_relation = relationId;
3501 workitem->avw_blockNumber = blkno;
3502 result = true;
3503
3504 /* done */
3505 break;
3506 }
3507
3509
3510 return result;
3511}
#define NUM_WORKITEMS
Definition autovacuum.c:282
uint32 result
Oid MyDatabaseId
Definition globals.c:96
int i
Definition isn.c:77
AutoVacuumWorkItem av_workItems[NUM_WORKITEMS]
Definition autovacuum.c:307
const char * type

References AutoVacuumShmem, AutoVacuumShmemStruct::av_workItems, AutoVacuumWorkItem::avw_used, fb(), i, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MyDatabaseId, NUM_WORKITEMS, result, and type.

Referenced by brininsert().

◆ AutoVacWorkerFailed()

void AutoVacWorkerFailed ( void  )
extern

Definition at line 1396 of file autovacuum.c.

1397{
1399}

References AutoVacForkFailed, AutoVacuumShmem, and AutoVacuumShmemStruct::av_signal.

Referenced by StartAutovacuumWorker().

◆ AutoVacWorkerMain()

pg_noreturn void AutoVacWorkerMain ( const void startup_data,
size_t  startup_data_len 
)
extern

Definition at line 1418 of file autovacuum.c.

1419{
1421 Oid dbid;
1422
1424
1425 /* Release postmaster's working memory context */
1427 {
1430 }
1431
1433
1435
1436 /*
1437 * Set up signal handlers. We operate on databases much like a regular
1438 * backend, so we use the same signal handling. See equivalent code in
1439 * tcop/postgres.c.
1440 */
1442
1443 /*
1444 * SIGINT is used to signal canceling the current table's vacuum; SIGTERM
1445 * means abort and exit cleanly, and SIGQUIT means abandon ship.
1446 */
1449 /* SIGQUIT handler was already set up by InitPostmasterChild */
1450
1451 InitializeTimeouts(); /* establishes SIGALRM handler */
1452
1458
1459 /*
1460 * Create a per-backend PGPROC struct in shared memory. We must do this
1461 * before we can use LWLocks or access any shared memory.
1462 */
1463 InitProcess();
1464
1465 /* Early initialization */
1466 BaseInit();
1467
1468 /*
1469 * If an exception is encountered, processing resumes here.
1470 *
1471 * Unlike most auxiliary processes, we don't attempt to continue
1472 * processing after an error; we just clean up and exit. The autovac
1473 * launcher is responsible for spawning another worker later.
1474 *
1475 * Note that we use sigsetjmp(..., 1), so that the prevailing signal mask
1476 * (to wit, BlockSig) will be restored when longjmp'ing to here. Thus,
1477 * signals other than SIGQUIT will be blocked until we exit. It might
1478 * seem that this policy makes the HOLD_INTERRUPTS() call redundant, but
1479 * it is not since InterruptPending might be set already.
1480 */
1481 if (sigsetjmp(local_sigjmp_buf, 1) != 0)
1482 {
1483 /* since not using PG_TRY, must reset error stack by hand */
1485
1486 /* Prevents interrupts while cleaning up */
1488
1489 /* Report the error to the server log */
1491
1492 /*
1493 * We can now go away. Note that because we called InitProcess, a
1494 * callback was registered to do ProcKill, which will clean up
1495 * necessary state.
1496 */
1497 proc_exit(0);
1498 }
1499
1500 /* We can now handle ereport(ERROR) */
1502
1504
1505 /*
1506 * Set always-secure search path, so malicious users can't redirect user
1507 * code (e.g. pg_index.indexprs). (That code runs in a
1508 * SECURITY_RESTRICTED_OPERATION sandbox, so malicious users could not
1509 * take control of the entire autovacuum worker in any case.)
1510 */
1511 SetConfigOption("search_path", "", PGC_SUSET, PGC_S_OVERRIDE);
1512
1513 /*
1514 * Force zero_damaged_pages OFF in the autovac process, even if it is set
1515 * in postgresql.conf. We don't really want such a dangerous option being
1516 * applied non-interactively.
1517 */
1518 SetConfigOption("zero_damaged_pages", "false", PGC_SUSET, PGC_S_OVERRIDE);
1519
1520 /*
1521 * Force settable timeouts off to avoid letting these settings prevent
1522 * regular maintenance from being executed.
1523 */
1524 SetConfigOption("statement_timeout", "0", PGC_SUSET, PGC_S_OVERRIDE);
1525 SetConfigOption("transaction_timeout", "0", PGC_SUSET, PGC_S_OVERRIDE);
1526 SetConfigOption("lock_timeout", "0", PGC_SUSET, PGC_S_OVERRIDE);
1527 SetConfigOption("idle_in_transaction_session_timeout", "0",
1529
1530 /*
1531 * Force default_transaction_isolation to READ COMMITTED. We don't want
1532 * to pay the overhead of serializable mode, nor add any risk of causing
1533 * deadlocks or delaying other transactions.
1534 */
1535 SetConfigOption("default_transaction_isolation", "read committed",
1537
1538 /*
1539 * Force synchronous replication off to allow regular maintenance even if
1540 * we are waiting for standbys to connect. This is important to ensure we
1541 * aren't blocked from performing anti-wraparound tasks.
1542 */
1544 SetConfigOption("synchronous_commit", "local",
1546
1547 /*
1548 * Even when system is configured to use a different fetch consistency,
1549 * for autovac we always want fresh stats.
1550 */
1551 SetConfigOption("stats_fetch_consistency", "none", PGC_SUSET, PGC_S_OVERRIDE);
1552
1553 /*
1554 * Get the info about the database we're going to work on.
1555 */
1557
1558 /*
1559 * beware of startingWorker being INVALID; this should normally not
1560 * happen, but if a worker fails after forking and before this, the
1561 * launcher might have decided to remove it from the queue and start
1562 * again.
1563 */
1565 {
1567
1569 dbid = MyWorkerInfo->wi_dboid;
1571
1572 /* insert into the running list */
1575
1576 /*
1577 * remove from the "starting" pointer, so that the launcher can start
1578 * a new worker if required
1579 */
1582
1584
1585 /* wake up the launcher */
1588 {
1589 int pid = GetPGProcByNumber(launcherProc)->pid;
1590
1591 if (pid != 0)
1592 kill(pid, SIGUSR2);
1593 }
1594 }
1595 else
1596 {
1597 /* no worker entry for me, go away */
1598 elog(WARNING, "autovacuum worker started without a worker entry");
1599 dbid = InvalidOid;
1601 }
1602
1603 if (OidIsValid(dbid))
1604 {
1605 char dbname[NAMEDATALEN];
1606
1607 /*
1608 * Report autovac startup to the cumulative stats system. We
1609 * deliberately do this before InitPostgres, so that the
1610 * last_autovac_time will get updated even if the connection attempt
1611 * fails. This is to prevent autovac from getting "stuck" repeatedly
1612 * selecting an unopenable database, rather than making any progress
1613 * on stuff it can connect to.
1614 */
1616
1617 /*
1618 * Connect to the selected database, specifying no particular user,
1619 * and ignoring datallowconn. Collect the database's name for
1620 * display.
1621 *
1622 * Note: if we have selected a just-deleted database (due to using
1623 * stale stats info), we'll fail and exit here.
1624 */
1627 dbname);
1631 (errmsg_internal("autovacuum: processing database \"%s\"", dbname)));
1632
1633 if (PostAuthDelay)
1634 pg_usleep(PostAuthDelay * 1000000L);
1635
1636 /* And do an appropriate amount of work */
1639 do_autovacuum();
1640 }
1641
1642 /* All done, go away */
1643 proc_exit(0);
1644}
static uint32 pg_atomic_read_u32(volatile pg_atomic_uint32 *ptr)
Definition atomics.h:232
static void FreeWorkerInfo(int code, Datum arg)
static TransactionId recentXid
Definition autovacuum.c:168
static void do_autovacuum(void)
static MultiXactId recentMulti
Definition autovacuum.c:169
static WorkerInfo MyWorkerInfo
Definition autovacuum.c:363
#define OidIsValid(objectId)
Definition c.h:917
#define elog(elevel,...)
Definition elog.h:228
static void dlist_push_head(dlist_head *head, dlist_node *node)
Definition ilist.h:347
void on_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition ipc.c:372
#define INIT_PG_OVERRIDE_ALLOW_CONNS
Definition miscadmin.h:509
MultiXactId ReadNextMultiXactId(void)
Definition multixact.c:679
#define NAMEDATALEN
#define die(msg)
void pgstat_report_autovac(Oid dboid)
unsigned int Oid
#define GetPGProcByNumber(n)
Definition proc.h:506
#define INVALID_PROC_NUMBER
Definition procnumber.h:26
int ProcNumber
Definition procnumber.h:24
static void set_ps_display(const char *activity)
Definition ps_status.h:40
PGPROC * MyProc
Definition proc.c:71
PROC_HDR * ProcGlobal
Definition proc.c:74
char * dbname
Definition streamutil.c:49
dlist_head av_runningWorkers
Definition autovacuum.c:305
pg_atomic_uint32 avLauncherProc
Definition proc.h:491
static TransactionId ReadNextTransactionId(void)
Definition transam.h:375
#define kill(pid, sig)
Definition win32_port.h:507
int synchronous_commit
Definition xact.c:89
@ SYNCHRONOUS_COMMIT_LOCAL_FLUSH
Definition xact.h:72

References Assert, AutoVacuumShmem, AutoVacuumShmemStruct::av_runningWorkers, AutoVacuumShmemStruct::av_startingWorker, PROC_HDR::avLauncherProc, BaseInit(), dbname, DEBUG1, die, dlist_push_head(), do_autovacuum(), elog, EmitErrorReport(), ereport, errmsg_internal(), error_context_stack, fb(), FloatExceptionHandler(), FreeWorkerInfo(), GetPGProcByNumber, GetProcessingMode, HOLD_INTERRUPTS, INIT_PG_OVERRIDE_ALLOW_CONNS, init_ps_display(), InitializeTimeouts(), InitPostgres(), InitProcess(), InitProcessing, INVALID_PROC_NUMBER, InvalidOid, kill, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MemoryContextDelete(), MyProc, MyWorkerInfo, NAMEDATALEN, NormalProcessing, OidIsValid, on_shmem_exit(), pg_atomic_read_u32(), PG_exception_stack, PG_SIG_DFL, PG_SIG_IGN, pg_usleep(), PGC_S_OVERRIDE, PGC_SUSET, pgstat_report_autovac(), PostAuthDelay, PostmasterContext, pqsignal, proc_exit(), ProcGlobal, procsignal_sigusr1_handler(), ReadNextMultiXactId(), ReadNextTransactionId(), recentMulti, recentXid, set_ps_display(), SetConfigOption(), SetProcessingMode, SIGCHLD, SIGHUP, SignalHandlerForConfigReload(), SIGPIPE, SIGUSR1, SIGUSR2, StatementCancelHandler(), synchronous_commit, SYNCHRONOUS_COMMIT_LOCAL_FLUSH, UnBlockSig, WARNING, WorkerInfoData::wi_dboid, WorkerInfoData::wi_links, and WorkerInfoData::wi_proc.

Variable Documentation

◆ autovacuum_analyze_score_weight

PGDLLIMPORT double autovacuum_analyze_score_weight
extern

Definition at line 141 of file autovacuum.c.

Referenced by do_autovacuum(), and relation_needs_vacanalyze().

◆ autovacuum_anl_scale

PGDLLIMPORT double autovacuum_anl_scale
extern

Definition at line 134 of file autovacuum.c.

Referenced by relation_needs_vacanalyze().

◆ autovacuum_anl_thresh

PGDLLIMPORT int autovacuum_anl_thresh
extern

Definition at line 133 of file autovacuum.c.

Referenced by relation_needs_vacanalyze().

◆ autovacuum_freeze_max_age

◆ autovacuum_freeze_score_weight

PGDLLIMPORT double autovacuum_freeze_score_weight
extern

Definition at line 137 of file autovacuum.c.

Referenced by do_autovacuum(), and relation_needs_vacanalyze().

◆ autovacuum_max_workers

PGDLLIMPORT int autovacuum_max_workers
extern

◆ autovacuum_multixact_freeze_max_age

PGDLLIMPORT int autovacuum_multixact_freeze_max_age
extern

◆ autovacuum_multixact_freeze_score_weight

PGDLLIMPORT double autovacuum_multixact_freeze_score_weight
extern

Definition at line 138 of file autovacuum.c.

Referenced by do_autovacuum(), and relation_needs_vacanalyze().

◆ autovacuum_naptime

◆ autovacuum_start_daemon

PGDLLIMPORT bool autovacuum_start_daemon
extern

Definition at line 123 of file autovacuum.c.

Referenced by autovac_init(), AutoVacuumingActive(), and removable_cutoff().

◆ autovacuum_vac_cost_delay

PGDLLIMPORT double autovacuum_vac_cost_delay
extern

Definition at line 142 of file autovacuum.c.

Referenced by VacuumUpdateCosts().

◆ autovacuum_vac_cost_limit

PGDLLIMPORT int autovacuum_vac_cost_limit
extern

Definition at line 143 of file autovacuum.c.

Referenced by AutoVacuumUpdateCostLimit().

◆ autovacuum_vac_ins_scale

PGDLLIMPORT double autovacuum_vac_ins_scale
extern

Definition at line 132 of file autovacuum.c.

Referenced by relation_needs_vacanalyze().

◆ autovacuum_vac_ins_thresh

PGDLLIMPORT int autovacuum_vac_ins_thresh
extern

Definition at line 131 of file autovacuum.c.

Referenced by relation_needs_vacanalyze().

◆ autovacuum_vac_max_thresh

PGDLLIMPORT int autovacuum_vac_max_thresh
extern

Definition at line 129 of file autovacuum.c.

Referenced by relation_needs_vacanalyze().

◆ autovacuum_vac_scale

PGDLLIMPORT double autovacuum_vac_scale
extern

Definition at line 130 of file autovacuum.c.

Referenced by relation_needs_vacanalyze().

◆ autovacuum_vac_thresh

PGDLLIMPORT int autovacuum_vac_thresh
extern

Definition at line 128 of file autovacuum.c.

Referenced by relation_needs_vacanalyze().

◆ autovacuum_vacuum_insert_score_weight

PGDLLIMPORT double autovacuum_vacuum_insert_score_weight
extern

Definition at line 140 of file autovacuum.c.

Referenced by do_autovacuum(), and relation_needs_vacanalyze().

◆ autovacuum_vacuum_score_weight

PGDLLIMPORT double autovacuum_vacuum_score_weight
extern

Definition at line 139 of file autovacuum.c.

Referenced by do_autovacuum(), and relation_needs_vacanalyze().

◆ autovacuum_work_mem

PGDLLIMPORT int autovacuum_work_mem
extern

Definition at line 126 of file autovacuum.c.

Referenced by dead_items_alloc(), and ginInsertCleanup().

◆ autovacuum_worker_slots

◆ Log_autoanalyze_min_duration

PGDLLIMPORT int Log_autoanalyze_min_duration
extern

Definition at line 146 of file autovacuum.c.

Referenced by table_recheck_autovac().

◆ Log_autovacuum_min_duration

PGDLLIMPORT int Log_autovacuum_min_duration
extern

Definition at line 145 of file autovacuum.c.

Referenced by table_recheck_autovac().