PostgreSQL Source Code git master
Loading...
Searching...
No Matches
vacuum.h File Reference
#include "access/htup.h"
#include "access/genam.h"
#include "access/parallel.h"
#include "access/tidstore.h"
#include "catalog/pg_class.h"
#include "catalog/pg_statistic.h"
#include "catalog/pg_type.h"
#include "parser/parse_node.h"
#include "storage/buf.h"
#include "utils/relcache.h"
Include dependency graph for vacuum.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  VacAttrStats
 
struct  VacuumParams
 
struct  VacuumCutoffs
 
struct  VacDeadItemsInfo
 
struct  PVWorkerStats
 
struct  PVWorkerUsage
 

Macros

#define VACUUM_OPTION_NO_PARALLEL   0
 
#define VACUUM_OPTION_PARALLEL_BULKDEL   (1 << 0)
 
#define VACUUM_OPTION_PARALLEL_COND_CLEANUP   (1 << 1)
 
#define VACUUM_OPTION_PARALLEL_CLEANUP   (1 << 2)
 
#define VACUUM_OPTION_MAX_VALID_VALUE   ((1 << 3) - 1)
 
#define VACOPT_VACUUM   0x01 /* do VACUUM */
 
#define VACOPT_ANALYZE   0x02 /* do ANALYZE */
 
#define VACOPT_VERBOSE   0x04 /* output INFO instrumentation messages */
 
#define VACOPT_FREEZE   0x08 /* FREEZE option */
 
#define VACOPT_FULL   0x10 /* FULL (non-concurrent) vacuum */
 
#define VACOPT_SKIP_LOCKED   0x20 /* skip if cannot get lock */
 
#define VACOPT_PROCESS_MAIN   0x40 /* process main relation */
 
#define VACOPT_PROCESS_TOAST   0x80 /* process the TOAST table, if any */
 
#define VACOPT_DISABLE_PAGE_SKIPPING   0x100 /* don't skip any pages */
 
#define VACOPT_SKIP_DATABASE_STATS   0x200 /* skip vac_update_datfrozenxid() */
 
#define VACOPT_ONLY_DATABASE_STATS   0x400 /* only vac_update_datfrozenxid() */
 
#define MAX_STATISTICS_TARGET   10000
 

Typedefs

typedef struct ParallelVacuumState ParallelVacuumState
 
typedef struct VacAttrStatsVacAttrStatsP
 
typedef Datum(* AnalyzeAttrFetchFunc) (VacAttrStatsP stats, int rownum, bool *isNull)
 
typedef void(* AnalyzeAttrComputeStatsFunc) (VacAttrStatsP stats, AnalyzeAttrFetchFunc fetchfunc, int samplerows, double totalrows)
 
typedef struct VacAttrStats VacAttrStats
 
typedef enum VacOptValue VacOptValue
 
typedef struct VacuumParams VacuumParams
 
typedef struct VacDeadItemsInfo VacDeadItemsInfo
 
typedef struct PVWorkerStats PVWorkerStats
 
typedef struct PVWorkerUsage PVWorkerUsage
 

Enumerations

enum  VacOptValue { VACOPTVALUE_UNSPECIFIED = 0 , VACOPTVALUE_AUTO , VACOPTVALUE_DISABLED , VACOPTVALUE_ENABLED }
 

Functions

void ExecVacuum (ParseState *pstate, VacuumStmt *vacstmt, bool isTopLevel)
 
void vacuum (List *relations, const VacuumParams *params, BufferAccessStrategy bstrategy, MemoryContext vac_context, bool isTopLevel)
 
void vac_open_indexes (Relation relation, LOCKMODE lockmode, int *nindexes, Relation **Irel)
 
void vac_close_indexes (int nindexes, Relation *Irel, LOCKMODE lockmode)
 
double vac_estimate_reltuples (Relation relation, BlockNumber total_pages, BlockNumber scanned_pages, double scanned_tuples)
 
void vac_update_relstats (Relation relation, BlockNumber num_pages, double num_tuples, BlockNumber num_all_visible_pages, BlockNumber num_all_frozen_pages, bool hasindex, TransactionId frozenxid, MultiXactId minmulti, bool *frozenxid_updated, bool *minmulti_updated, bool in_outer_xact)
 
bool vacuum_get_cutoffs (Relation rel, const VacuumParams *params, struct VacuumCutoffs *cutoffs)
 
bool vacuum_xid_failsafe_check (const struct VacuumCutoffs *cutoffs)
 
void vac_update_datfrozenxid (void)
 
void vacuum_delay_point (bool is_analyze)
 
bool vacuum_is_permitted_for_relation (Oid relid, Form_pg_class reltuple, uint32 options)
 
Relation vacuum_open_relation (Oid relid, RangeVar *relation, uint32 options, bool verbose, LOCKMODE lmode)
 
IndexBulkDeleteResultvac_bulkdel_one_index (IndexVacuumInfo *ivinfo, IndexBulkDeleteResult *istat, TidStore *dead_items, VacDeadItemsInfo *dead_items_info)
 
IndexBulkDeleteResultvac_cleanup_one_index (IndexVacuumInfo *ivinfo, IndexBulkDeleteResult *istat)
 
void AutoVacuumUpdateCostLimit (void)
 
void VacuumUpdateCosts (void)
 
ParallelVacuumStateparallel_vacuum_init (Relation rel, Relation *indrels, int nindexes, int nrequested_workers, int vac_work_mem, int elevel, BufferAccessStrategy bstrategy)
 
void parallel_vacuum_end (ParallelVacuumState *pvs, IndexBulkDeleteResult **istats)
 
TidStoreparallel_vacuum_get_dead_items (ParallelVacuumState *pvs, VacDeadItemsInfo **dead_items_info_p)
 
void parallel_vacuum_reset_dead_items (ParallelVacuumState *pvs)
 
void parallel_vacuum_bulkdel_all_indexes (ParallelVacuumState *pvs, long num_table_tuples, int num_index_scans, PVWorkerStats *wstats)
 
void parallel_vacuum_cleanup_all_indexes (ParallelVacuumState *pvs, long num_table_tuples, int num_index_scans, bool estimated_count, PVWorkerStats *wstats)
 
void parallel_vacuum_update_shared_delay_params (void)
 
void parallel_vacuum_propagate_shared_delay_params (void)
 
void parallel_vacuum_main (dsm_segment *seg, shm_toc *toc)
 
void analyze_rel (Oid relid, RangeVar *relation, const VacuumParams *params, List *va_cols, bool in_outer_xact, BufferAccessStrategy bstrategy)
 
bool attribute_is_analyzable (Relation onerel, int attnum, Form_pg_attribute attr, int *p_attstattarget)
 
bool std_typanalyze (VacAttrStats *stats)
 
double anl_random_fract (void)
 
double anl_init_selection_state (int n)
 
double anl_get_next_S (double t, int n, double *stateptr)
 

Variables

PGDLLIMPORT int default_statistics_target
 
PGDLLIMPORT int vacuum_freeze_min_age
 
PGDLLIMPORT int vacuum_freeze_table_age
 
PGDLLIMPORT int vacuum_multixact_freeze_min_age
 
PGDLLIMPORT int vacuum_multixact_freeze_table_age
 
PGDLLIMPORT int vacuum_failsafe_age
 
PGDLLIMPORT int vacuum_multixact_failsafe_age
 
PGDLLIMPORT bool track_cost_delay_timing
 
PGDLLIMPORT bool vacuum_truncate
 
PGDLLIMPORT double vacuum_max_eager_freeze_failure_rate
 
PGDLLIMPORT pg_atomic_uint32VacuumSharedCostBalance
 
PGDLLIMPORT pg_atomic_uint32VacuumActiveNWorkers
 
PGDLLIMPORT int VacuumCostBalanceLocal
 
PGDLLIMPORT bool VacuumFailsafeActive
 
PGDLLIMPORT double vacuum_cost_delay
 
PGDLLIMPORT int vacuum_cost_limit
 
PGDLLIMPORT int64 parallel_vacuum_worker_delay_ns
 

Macro Definition Documentation

◆ MAX_STATISTICS_TARGET

#define MAX_STATISTICS_TARGET   10000

Definition at line 350 of file vacuum.h.

◆ VACOPT_ANALYZE

#define VACOPT_ANALYZE   0x02 /* do ANALYZE */

Definition at line 180 of file vacuum.h.

◆ VACOPT_DISABLE_PAGE_SKIPPING

#define VACOPT_DISABLE_PAGE_SKIPPING   0x100 /* don't skip any pages */

Definition at line 187 of file vacuum.h.

◆ VACOPT_FREEZE

#define VACOPT_FREEZE   0x08 /* FREEZE option */

Definition at line 182 of file vacuum.h.

◆ VACOPT_FULL

#define VACOPT_FULL   0x10 /* FULL (non-concurrent) vacuum */

Definition at line 183 of file vacuum.h.

◆ VACOPT_ONLY_DATABASE_STATS

#define VACOPT_ONLY_DATABASE_STATS   0x400 /* only vac_update_datfrozenxid() */

Definition at line 189 of file vacuum.h.

◆ VACOPT_PROCESS_MAIN

#define VACOPT_PROCESS_MAIN   0x40 /* process main relation */

Definition at line 185 of file vacuum.h.

◆ VACOPT_PROCESS_TOAST

#define VACOPT_PROCESS_TOAST   0x80 /* process the TOAST table, if any */

Definition at line 186 of file vacuum.h.

◆ VACOPT_SKIP_DATABASE_STATS

#define VACOPT_SKIP_DATABASE_STATS   0x200 /* skip vac_update_datfrozenxid() */

Definition at line 188 of file vacuum.h.

◆ VACOPT_SKIP_LOCKED

#define VACOPT_SKIP_LOCKED   0x20 /* skip if cannot get lock */

Definition at line 184 of file vacuum.h.

◆ VACOPT_VACUUM

#define VACOPT_VACUUM   0x01 /* do VACUUM */

Definition at line 179 of file vacuum.h.

◆ VACOPT_VERBOSE

#define VACOPT_VERBOSE   0x04 /* output INFO instrumentation messages */

Definition at line 181 of file vacuum.h.

◆ VACUUM_OPTION_MAX_VALID_VALUE

#define VACUUM_OPTION_MAX_VALID_VALUE   ((1 << 3) - 1)

Definition at line 65 of file vacuum.h.

◆ VACUUM_OPTION_NO_PARALLEL

#define VACUUM_OPTION_NO_PARALLEL   0

Definition at line 41 of file vacuum.h.

◆ VACUUM_OPTION_PARALLEL_BULKDEL

#define VACUUM_OPTION_PARALLEL_BULKDEL   (1 << 0)

Definition at line 47 of file vacuum.h.

◆ VACUUM_OPTION_PARALLEL_CLEANUP

#define VACUUM_OPTION_PARALLEL_CLEANUP   (1 << 2)

Definition at line 62 of file vacuum.h.

◆ VACUUM_OPTION_PARALLEL_COND_CLEANUP

#define VACUUM_OPTION_PARALLEL_COND_CLEANUP   (1 << 1)

Definition at line 54 of file vacuum.h.

Typedef Documentation

◆ AnalyzeAttrComputeStatsFunc

typedef void(* AnalyzeAttrComputeStatsFunc) (VacAttrStatsP stats, AnalyzeAttrFetchFunc fetchfunc, int samplerows, double totalrows)

Definition at line 110 of file vacuum.h.

◆ AnalyzeAttrFetchFunc

typedef Datum(* AnalyzeAttrFetchFunc) (VacAttrStatsP stats, int rownum, bool *isNull)

Definition at line 107 of file vacuum.h.

◆ ParallelVacuumState

Definition at line 68 of file vacuum.h.

◆ PVWorkerStats

◆ PVWorkerUsage

◆ VacAttrStats

◆ VacAttrStatsP

Definition at line 105 of file vacuum.h.

◆ VacDeadItemsInfo

◆ VacOptValue

◆ VacuumParams

Enumeration Type Documentation

◆ VacOptValue

Enumerator
VACOPTVALUE_UNSPECIFIED 
VACOPTVALUE_AUTO 
VACOPTVALUE_DISABLED 
VACOPTVALUE_ENABLED 

Definition at line 199 of file vacuum.h.

200{
VacOptValue
Definition vacuum.h:200
@ VACOPTVALUE_AUTO
Definition vacuum.h:202
@ VACOPTVALUE_ENABLED
Definition vacuum.h:204
@ VACOPTVALUE_UNSPECIFIED
Definition vacuum.h:201
@ VACOPTVALUE_DISABLED
Definition vacuum.h:203

Function Documentation

◆ analyze_rel()

void analyze_rel ( Oid  relid,
RangeVar relation,
const VacuumParams params,
List va_cols,
bool  in_outer_xact,
BufferAccessStrategy  bstrategy 
)
extern

Definition at line 110 of file analyze.c.

113{
115 int elevel;
117 BlockNumber relpages = 0;
118 bool stats_imported = false;
119
120 /* Select logging level */
121 if (params->options & VACOPT_VERBOSE)
122 elevel = INFO;
123 else
124 elevel = DEBUG2;
125
126 /* Set up static variables */
127 vac_strategy = bstrategy;
128
129 /*
130 * Check for user-requested abort.
131 */
133
134 /*
135 * Open the relation, getting ShareUpdateExclusiveLock to ensure that two
136 * ANALYZEs don't run on it concurrently. (This also locks out a
137 * concurrent VACUUM, which doesn't matter much at the moment but might
138 * matter if we ever try to accumulate stats on dead tuples.) If the rel
139 * has been dropped since we last saw it, we don't need to process it.
140 *
141 * Make sure to generate only logs for ANALYZE in this case.
142 */
143 onerel = vacuum_open_relation(relid, relation, params->options & ~(VACOPT_VACUUM),
144 params->log_analyze_min_duration >= 0,
146
147 /* leave if relation could not be opened or locked */
148 if (!onerel)
149 return;
150
151 /*
152 * Check if relation needs to be skipped based on privileges. This check
153 * happens also when building the relation list to analyze for a manual
154 * operation, and needs to be done additionally here as ANALYZE could
155 * happen across multiple transactions where privileges could have changed
156 * in-between. Make sure to generate only logs for ANALYZE in this case.
157 */
159 onerel->rd_rel,
160 params->options & ~VACOPT_VACUUM))
161 {
163 return;
164 }
165
166 /*
167 * Silently ignore tables that are temp tables of other backends ---
168 * trying to analyze these is rather pointless, since their contents are
169 * probably not up-to-date on disk. (We don't throw a warning here; it
170 * would just lead to chatter during a database-wide ANALYZE.)
171 */
173 {
175 return;
176 }
177
178 /*
179 * We can ANALYZE any table except pg_statistic. See update_attstats
180 */
182 {
184 return;
185 }
186
187 /*
188 * Check the given list of columns
189 */
190 if (va_cols != NIL)
192
193 /*
194 * Initialize progress reporting before setup for regular/foreign tables.
195 * (For the former, the time spent on it would be negligible, but for the
196 * latter, if FDWs support statistics import or analysis, they'd do some
197 * work that needs the remote access, so the time might be
198 * non-negligible.)
199 */
205 else
208
209 /*
210 * Check that it's of an analyzable relkind, and set up appropriately.
211 */
212 if (onerel->rd_rel->relkind == RELKIND_RELATION ||
213 onerel->rd_rel->relkind == RELKIND_MATVIEW)
214 {
215 /* Regular table, so we'll use the regular row acquisition function */
217 /* Also get regular table's size */
219 }
220 else if (onerel->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
221 {
222 /*
223 * For a foreign table, call the FDW's hook functions to see whether
224 * it supports statistics import or analysis.
225 */
226 FdwRoutine *fdwroutine;
227
228 fdwroutine = GetFdwRoutineForRelation(onerel, false);
229
230 if (fdwroutine->ImportForeignStatistics != NULL &&
231 fdwroutine->ImportForeignStatistics(onerel, va_cols, elevel))
232 stats_imported = true;
233 else
234 {
235 bool ok = false;
236
237 if (fdwroutine->AnalyzeForeignTable != NULL)
238 ok = fdwroutine->AnalyzeForeignTable(onerel,
240 &relpages);
241
242 if (!ok)
243 {
245 errmsg("skipping \"%s\" -- cannot analyze this foreign table.",
248 goto out;
249 }
250 }
251 }
252 else if (onerel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
253 {
254 /*
255 * For partitioned tables, we want to do the recursive ANALYZE below.
256 */
257 }
258 else
259 {
260 /* No need for a WARNING if we already complained during VACUUM */
261 if (!(params->options & VACOPT_VACUUM))
263 (errmsg("skipping \"%s\" --- cannot analyze non-tables or special system tables",
266 goto out;
267 }
268
269 /*
270 * Do the normal non-recursive ANALYZE. We can skip this for partitioned
271 * tables, which don't contain any rows, and foreign tables that
272 * successfully imported statistics.
273 */
274 if ((onerel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
275 && !stats_imported)
276 do_analyze_rel(onerel, params, va_cols, acquirefunc,
277 relpages, false, in_outer_xact, elevel);
278
279 /*
280 * If there are child tables, do recursive ANALYZE.
281 */
282 if (onerel->rd_rel->relhassubclass)
283 do_analyze_rel(onerel, params, va_cols, acquirefunc, relpages,
284 true, in_outer_xact, elevel);
285
286 /*
287 * Close source relation now, but keep lock so that no one deletes it
288 * before we commit. (If someone did, they'd fail to clean up the entries
289 * we made in pg_statistic. Also, releasing the lock before commit would
290 * expose us to concurrent-update failures in update_attstats.)
291 */
293
294out:
296}
void pgstat_progress_start_command(ProgressCommandType cmdtype, Oid relid)
void pgstat_progress_update_param(int index, int64 val)
void pgstat_progress_end_command(void)
@ PROGRESS_COMMAND_ANALYZE
uint32 BlockNumber
Definition block.h:31
#define RelationGetNumberOfBlocks(reln)
Definition bufmgr.h:309
static void do_analyze_rel(Relation onerel, const VacuumParams *params, List *va_cols, AcquireSampleRowsFunc acquirefunc, BlockNumber relpages, bool inh, bool in_outer_xact, int elevel)
Definition analyze.c:306
static BufferAccessStrategy vac_strategy
Definition analyze.c:75
static int acquire_sample_rows(Relation onerel, int elevel, HeapTuple *rows, int targrows, double *totalrows, double *totaldeadrows)
Definition analyze.c:1262
static void validate_va_cols_list(Relation onerel, List *va_cols)
Definition analyze.c:1047
#define WARNING
Definition elog.h:37
#define DEBUG2
Definition elog.h:30
#define INFO
Definition elog.h:35
#define ereport(elevel,...)
Definition elog.h:152
int(* AcquireSampleRowsFunc)(Relation relation, int elevel, HeapTuple *rows, int targrows, double *totalrows, double *totaldeadrows)
Definition fdwapi.h:151
FdwRoutine * GetFdwRoutineForRelation(Relation relation, bool makecopy)
Definition foreign.c:474
#define NoLock
Definition lockdefs.h:34
#define ShareUpdateExclusiveLock
Definition lockdefs.h:39
#define AmAutoVacuumWorkerProcess()
Definition miscadmin.h:398
#define CHECK_FOR_INTERRUPTS()
Definition miscadmin.h:125
static char * errmsg
#define NIL
Definition pg_list.h:68
static int fb(int x)
#define PROGRESS_ANALYZE_STARTED_BY
Definition progress.h:63
#define PROGRESS_ANALYZE_STARTED_BY_AUTOVACUUM
Definition progress.h:74
#define PROGRESS_ANALYZE_STARTED_BY_MANUAL
Definition progress.h:73
#define RelationGetRelid(relation)
Definition rel.h:516
#define RelationGetRelationName(relation)
Definition rel.h:550
#define RELATION_IS_OTHER_TEMP(relation)
Definition rel.h:669
void relation_close(Relation relation, LOCKMODE lockmode)
Definition relation.c:206
ImportForeignStatistics_function ImportForeignStatistics
Definition fdwapi.h:262
AnalyzeForeignTable_function AnalyzeForeignTable
Definition fdwapi.h:261
uint32 options
Definition vacuum.h:218
int log_analyze_min_duration
Definition vacuum.h:230
bool vacuum_is_permitted_for_relation(Oid relid, Form_pg_class reltuple, uint32 options)
Definition vacuum.c:720
Relation vacuum_open_relation(Oid relid, RangeVar *relation, uint32 options, bool verbose, LOCKMODE lmode)
Definition vacuum.c:772
#define VACOPT_VACUUM
Definition vacuum.h:179
#define VACOPT_VERBOSE
Definition vacuum.h:181

References acquire_sample_rows(), AmAutoVacuumWorkerProcess, FdwRoutine::AnalyzeForeignTable, CHECK_FOR_INTERRUPTS, DEBUG2, do_analyze_rel(), ereport, errmsg, fb(), GetFdwRoutineForRelation(), FdwRoutine::ImportForeignStatistics, INFO, VacuumParams::log_analyze_min_duration, NIL, NoLock, VacuumParams::options, pgstat_progress_end_command(), pgstat_progress_start_command(), pgstat_progress_update_param(), PROGRESS_ANALYZE_STARTED_BY, PROGRESS_ANALYZE_STARTED_BY_AUTOVACUUM, PROGRESS_ANALYZE_STARTED_BY_MANUAL, PROGRESS_COMMAND_ANALYZE, relation_close(), RELATION_IS_OTHER_TEMP, RelationGetNumberOfBlocks, RelationGetRelationName, RelationGetRelid, ShareUpdateExclusiveLock, vac_strategy, VACOPT_VACUUM, VACOPT_VERBOSE, vacuum_is_permitted_for_relation(), vacuum_open_relation(), validate_va_cols_list(), and WARNING.

Referenced by process_single_relation(), and vacuum().

◆ anl_get_next_S()

double anl_get_next_S ( double  t,
int  n,
double stateptr 
)
extern

Definition at line 296 of file sampling.c.

297{
298 double result;
299
300 oldrs.W = *stateptr;
302 *stateptr = oldrs.W;
303 return result;
304}
uint32 result
static ReservoirStateData oldrs
Definition sampling.c:262
double reservoir_get_next_S(ReservoirState rs, double t, int n)
Definition sampling.c:147

◆ anl_init_selection_state()

double anl_init_selection_state ( int  n)
extern

Definition at line 281 of file sampling.c.

282{
283 /* initialize if first time through */
285 {
288 oldrs_initialized = true;
289 }
290
291 /* Initial value of W (for use when Algorithm Z is first applied) */
293}
#define unlikely(x)
Definition c.h:438
uint32 pg_prng_uint32(pg_prng_state *state)
Definition pg_prng.c:227
pg_prng_state pg_global_prng_state
Definition pg_prng.c:34
static bool oldrs_initialized
Definition sampling.c:263
double sampler_random_fract(pg_prng_state *randstate)
Definition sampling.c:241
void sampler_random_init_state(uint32 seed, pg_prng_state *randstate)
Definition sampling.c:234
pg_prng_state randstate
Definition sampling.h:49

◆ anl_random_fract()

double anl_random_fract ( void  )
extern

Definition at line 266 of file sampling.c.

267{
268 /* initialize if first time through */
270 {
273 oldrs_initialized = true;
274 }
275
276 /* and compute a random fraction */
278}

◆ attribute_is_analyzable()

bool attribute_is_analyzable ( Relation  onerel,
int  attnum,
Form_pg_attribute  attr,
int p_attstattarget 
)
extern

Definition at line 1176 of file analyze.c.

1178{
1179 int attstattarget;
1181 Datum dat;
1182 bool isnull;
1183
1184 /* Never analyze dropped columns */
1185 if (attr->attisdropped)
1186 return false;
1187
1188 /* Don't analyze virtual generated columns */
1189 if (attr->attgenerated == ATTRIBUTE_GENERATED_VIRTUAL)
1190 return false;
1191
1192 /*
1193 * Get attstattarget value. Set to -1 if null. (Analyze functions expect
1194 * -1 to mean use default_statistics_target; see for example
1195 * std_typanalyze.)
1196 */
1199 elog(ERROR, "cache lookup failed for attribute %d of relation %u",
1202 attstattarget = isnull ? -1 : DatumGetInt16(dat);
1204
1205 /* Don't analyze column if user has specified not to */
1206 if (attstattarget == 0)
1207 return false;
1208
1209 if (p_attstattarget)
1210 *p_attstattarget = attstattarget;
1211 return true;
1212}
#define ERROR
Definition elog.h:40
#define elog(elevel,...)
Definition elog.h:228
#define HeapTupleIsValid(tuple)
Definition htup.h:78
int16 attnum
static Datum Int16GetDatum(int16 X)
Definition postgres.h:172
static Datum ObjectIdGetDatum(Oid X)
Definition postgres.h:252
uint64_t Datum
Definition postgres.h:70
static int16 DatumGetInt16(Datum X)
Definition postgres.h:162
void ReleaseSysCache(HeapTuple tuple)
Definition syscache.c:265
HeapTuple SearchSysCache2(SysCacheIdentifier cacheId, Datum key1, Datum key2)
Definition syscache.c:231
Datum SysCacheGetAttr(SysCacheIdentifier cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition syscache.c:596

References attnum, DatumGetInt16(), elog, ERROR, fb(), HeapTupleIsValid, Int16GetDatum(), ObjectIdGetDatum(), RelationGetRelid, ReleaseSysCache(), SearchSysCache2(), and SysCacheGetAttr().

Referenced by build_remattrmap(), and examine_attribute().

◆ AutoVacuumUpdateCostLimit()

void AutoVacuumUpdateCostLimit ( void  )
extern

Definition at line 1751 of file autovacuum.c.

1752{
1753 if (!MyWorkerInfo)
1754 return;
1755
1756 /*
1757 * note: in cost_limit, zero also means use value from elsewhere, because
1758 * zero is not a valid value.
1759 */
1760
1763 else
1764 {
1766
1769 else
1771
1772 /* Only balance limit if no cost-related storage parameters specified */
1774 return;
1775
1777
1779
1780 /* There is at least 1 autovac worker (this worker) */
1781 if (nworkers_for_balance <= 0)
1782 elog(ERROR, "nworkers_for_balance must be > 0");
1783
1785 }
1786}
static bool pg_atomic_unlocked_test_flag(volatile pg_atomic_flag *ptr)
Definition atomics.h:194
static uint32 pg_atomic_read_u32(volatile pg_atomic_uint32 *ptr)
Definition atomics.h:237
int autovacuum_vac_cost_limit
Definition autovacuum.c:143
static int av_storage_param_cost_limit
Definition autovacuum.c:162
static AutoVacuumShmemStruct * AutoVacuumShmem
Definition autovacuum.c:313
static WorkerInfo MyWorkerInfo
Definition autovacuum.c:365
#define Max(x, y)
Definition c.h:1085
#define Assert(condition)
Definition c.h:943
int VacuumCostLimit
Definition globals.c:157
pg_atomic_uint32 av_nworkersForBalance
Definition autovacuum.c:310
pg_atomic_flag wi_dobalance
Definition autovacuum.c:247
int vacuum_cost_limit
Definition vacuum.c:93

References Assert, autovacuum_vac_cost_limit, AutoVacuumShmem, AutoVacuumShmemStruct::av_nworkersForBalance, av_storage_param_cost_limit, elog, ERROR, fb(), Max, MyWorkerInfo, pg_atomic_read_u32(), pg_atomic_unlocked_test_flag(), vacuum_cost_limit, VacuumCostLimit, and WorkerInfoData::wi_dobalance.

Referenced by vacuum_delay_point(), and VacuumUpdateCosts().

◆ ExecVacuum()

void ExecVacuum ( ParseState pstate,
VacuumStmt vacstmt,
bool  isTopLevel 
)
extern

Definition at line 163 of file vacuum.c.

164{
165 VacuumParams params;
166 BufferAccessStrategy bstrategy = NULL;
167 bool verbose = false;
168 bool skip_locked = false;
169 bool analyze = false;
170 bool freeze = false;
171 bool full = false;
172 bool disable_page_skipping = false;
173 bool process_main = true;
174 bool process_toast = true;
175 int ring_size;
176 bool skip_database_stats = false;
177 bool only_database_stats = false;
179 ListCell *lc;
180
181 /* index_cleanup and truncate values unspecified for now */
184
185 /* By default parallel vacuum is enabled */
186 params.nworkers = 0;
187
188 /* Will be set later if we recurse to a TOAST table. */
189 params.toast_parent = InvalidOid;
190
191 /*
192 * Set this to an invalid value so it is clear whether or not a
193 * BUFFER_USAGE_LIMIT was specified when making the access strategy.
194 */
195 ring_size = -1;
196
197 /* Parse options list */
198 foreach(lc, vacstmt->options)
199 {
200 DefElem *opt = (DefElem *) lfirst(lc);
201
202 /* Parse common options for VACUUM and ANALYZE */
203 if (strcmp(opt->defname, "verbose") == 0)
204 verbose = defGetBoolean(opt);
205 else if (strcmp(opt->defname, "skip_locked") == 0)
206 skip_locked = defGetBoolean(opt);
207 else if (strcmp(opt->defname, "buffer_usage_limit") == 0)
208 {
209 const char *hintmsg;
210 int result;
211 char *vac_buffer_size;
212
214
215 /*
216 * Check that the specified value is valid and the size falls
217 * within the hard upper and lower limits if it is not 0.
218 */
220 (result != 0 &&
222 {
225 errmsg("%s option must be 0 or between %d kB and %d kB",
226 "BUFFER_USAGE_LIMIT",
228 hintmsg ? errhint_internal("%s", _(hintmsg)) : 0));
229 }
230
232 }
233 else if (!vacstmt->is_vacuumcmd)
236 errmsg("unrecognized %s option \"%s\"",
237 "ANALYZE", opt->defname),
238 parser_errposition(pstate, opt->location)));
239
240 /* Parse options available on VACUUM */
241 else if (strcmp(opt->defname, "analyze") == 0)
242 analyze = defGetBoolean(opt);
243 else if (strcmp(opt->defname, "freeze") == 0)
244 freeze = defGetBoolean(opt);
245 else if (strcmp(opt->defname, "full") == 0)
246 full = defGetBoolean(opt);
247 else if (strcmp(opt->defname, "disable_page_skipping") == 0)
248 disable_page_skipping = defGetBoolean(opt);
249 else if (strcmp(opt->defname, "index_cleanup") == 0)
250 {
251 /* Interpret no string as the default, which is 'auto' */
252 if (!opt->arg)
254 else
255 {
256 char *sval = defGetString(opt);
257
258 /* Try matching on 'auto' string, or fall back on boolean */
259 if (pg_strcasecmp(sval, "auto") == 0)
261 else
263 }
264 }
265 else if (strcmp(opt->defname, "process_main") == 0)
266 process_main = defGetBoolean(opt);
267 else if (strcmp(opt->defname, "process_toast") == 0)
268 process_toast = defGetBoolean(opt);
269 else if (strcmp(opt->defname, "truncate") == 0)
271 else if (strcmp(opt->defname, "parallel") == 0)
272 {
273 int nworkers = defGetInt32(opt);
274
278 errmsg("%s option must be between 0 and %d",
279 "PARALLEL",
281 parser_errposition(pstate, opt->location)));
282
283 /*
284 * Disable parallel vacuum, if user has specified parallel degree
285 * as zero.
286 */
287 if (nworkers == 0)
288 params.nworkers = -1;
289 else
290 params.nworkers = nworkers;
291 }
292 else if (strcmp(opt->defname, "skip_database_stats") == 0)
293 skip_database_stats = defGetBoolean(opt);
294 else if (strcmp(opt->defname, "only_database_stats") == 0)
296 else
299 errmsg("unrecognized %s option \"%s\"",
300 "VACUUM", opt->defname),
301 parser_errposition(pstate, opt->location)));
302 }
303
304 /* Set vacuum options */
305 params.options =
306 (vacstmt->is_vacuumcmd ? VACOPT_VACUUM : VACOPT_ANALYZE) |
307 (verbose ? VACOPT_VERBOSE : 0) |
308 (skip_locked ? VACOPT_SKIP_LOCKED : 0) |
309 (analyze ? VACOPT_ANALYZE : 0) |
310 (freeze ? VACOPT_FREEZE : 0) |
311 (full ? VACOPT_FULL : 0) |
312 (disable_page_skipping ? VACOPT_DISABLE_PAGE_SKIPPING : 0) |
313 (process_main ? VACOPT_PROCESS_MAIN : 0) |
314 (process_toast ? VACOPT_PROCESS_TOAST : 0) |
315 (skip_database_stats ? VACOPT_SKIP_DATABASE_STATS : 0) |
317
318 /* sanity checks on options */
320 Assert((params.options & VACOPT_VACUUM) ||
321 !(params.options & (VACOPT_FULL | VACOPT_FREEZE)));
322
323 if ((params.options & VACOPT_FULL) && params.nworkers > 0)
326 errmsg("VACUUM FULL cannot be performed in parallel")));
327
328 /*
329 * BUFFER_USAGE_LIMIT does nothing for VACUUM (FULL) so just raise an
330 * ERROR for that case. VACUUM (FULL, ANALYZE) does make use of it, so
331 * we'll permit that.
332 */
333 if (ring_size != -1 && (params.options & VACOPT_FULL) &&
334 !(params.options & VACOPT_ANALYZE))
337 errmsg("BUFFER_USAGE_LIMIT cannot be specified for VACUUM FULL")));
338
339 /*
340 * Make sure VACOPT_ANALYZE is specified if any column lists are present.
341 */
342 if (!(params.options & VACOPT_ANALYZE))
343 {
344 foreach(lc, vacstmt->rels)
345 {
347
348 if (vrel->va_cols != NIL)
351 errmsg("ANALYZE option must be specified when a column list is provided")));
352 }
353 }
354
355 /*
356 * Sanity check DISABLE_PAGE_SKIPPING option.
357 */
358 if ((params.options & VACOPT_FULL) != 0 &&
362 errmsg("VACUUM option DISABLE_PAGE_SKIPPING cannot be used with FULL")));
363
364 /* sanity check for PROCESS_TOAST */
365 if ((params.options & VACOPT_FULL) != 0 &&
366 (params.options & VACOPT_PROCESS_TOAST) == 0)
369 errmsg("PROCESS_TOAST required with VACUUM FULL")));
370
371 /* sanity check for ONLY_DATABASE_STATS */
373 {
374 Assert(params.options & VACOPT_VACUUM);
375 if (vacstmt->rels != NIL)
378 errmsg("ONLY_DATABASE_STATS cannot be specified with a list of tables")));
379 /* don't require people to turn off PROCESS_TOAST/MAIN explicitly */
380 if (params.options & ~(VACOPT_VACUUM |
387 errmsg("ONLY_DATABASE_STATS cannot be specified with other VACUUM options")));
388 }
389
390 /*
391 * All freeze ages are zero if the FREEZE option is given; otherwise pass
392 * them as -1 which means to use the default values.
393 */
394 if (params.options & VACOPT_FREEZE)
395 {
396 params.freeze_min_age = 0;
397 params.freeze_table_age = 0;
398 params.multixact_freeze_min_age = 0;
400 }
401 else
402 {
403 params.freeze_min_age = -1;
404 params.freeze_table_age = -1;
405 params.multixact_freeze_min_age = -1;
406 params.multixact_freeze_table_age = -1;
407 }
408
409 /* user-invoked vacuum is never "for wraparound" */
410 params.is_wraparound = false;
411
412 /*
413 * user-invoked vacuum uses VACOPT_VERBOSE instead of
414 * log_vacuum_min_duration and log_analyze_min_duration
415 */
416 params.log_vacuum_min_duration = -1;
417 params.log_analyze_min_duration = -1;
418
419 /*
420 * Later, in vacuum_rel(), we check if a reloption override was specified.
421 */
423
424 /*
425 * Create special memory context for cross-transaction storage.
426 *
427 * Since it is a child of PortalContext, it will go away eventually even
428 * if we suffer an error; there's no need for special abort cleanup logic.
429 */
431 "Vacuum",
433
434 /*
435 * Make a buffer strategy object in the cross-transaction memory context.
436 * We needn't bother making this for VACUUM (FULL) or VACUUM
437 * (ONLY_DATABASE_STATS) as they'll not make use of it. VACUUM (FULL,
438 * ANALYZE) is possible, so we'd better ensure that we make a strategy
439 * when we see ANALYZE.
440 */
441 if ((params.options & (VACOPT_ONLY_DATABASE_STATS |
442 VACOPT_FULL)) == 0 ||
443 (params.options & VACOPT_ANALYZE) != 0)
444 {
445
447
448 Assert(ring_size >= -1);
449
450 /*
451 * If BUFFER_USAGE_LIMIT was specified by the VACUUM or ANALYZE
452 * command, it overrides the value of VacuumBufferUsageLimit. Either
453 * value may be 0, in which case GetAccessStrategyWithSize() will
454 * return NULL, effectively allowing full use of shared buffers.
455 */
456 if (ring_size == -1)
458
460
462 }
463
464 /* Now go through the common routine */
465 vacuum(vacstmt->rels, &params, bstrategy, vac_context, isTopLevel);
466
467 /* Finally, clean up the vacuum memory context */
469}
#define MAX_PARALLEL_WORKER_LIMIT
@ BAS_VACUUM
Definition bufmgr.h:40
int32 defGetInt32(DefElem *def)
Definition define.c:148
char * defGetString(DefElem *def)
Definition define.c:34
bool defGetBoolean(DefElem *def)
Definition define.c:93
int errcode(int sqlerrcode)
Definition elog.c:875
#define _(x)
Definition elog.c:96
int int errhint_internal(const char *fmt,...) pg_attribute_printf(1
BufferAccessStrategy GetAccessStrategyWithSize(BufferAccessStrategyType btype, int ring_size_kb)
Definition freelist.c:511
int VacuumBufferUsageLimit
Definition globals.c:152
bool parse_int(const char *value, int *result, int flags, const char **hintmsg)
Definition guc.c:2775
#define GUC_UNIT_KB
Definition guc.h:232
void MemoryContextDelete(MemoryContext context)
Definition mcxt.c:472
MemoryContext PortalContext
Definition mcxt.c:175
#define AllocSetContextCreate
Definition memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition memutils.h:160
#define MIN_BAS_VAC_RING_SIZE_KB
Definition miscadmin.h:281
#define MAX_BAS_VAC_RING_SIZE_KB
Definition miscadmin.h:282
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition palloc.h:124
int parser_errposition(ParseState *pstate, int location)
Definition parse_node.c:106
static int verbose
#define lfirst(lc)
Definition pg_list.h:172
#define lfirst_node(type, lc)
Definition pg_list.h:176
int pg_strcasecmp(const char *s1, const char *s2)
#define InvalidOid
static long analyze(struct nfa *nfa)
Definition regc_nfa.c:3051
char * defname
Definition parsenodes.h:860
ParseLoc location
Definition parsenodes.h:864
Node * arg
Definition parsenodes.h:861
int nworkers
Definition vacuum.h:250
int freeze_table_age
Definition vacuum.h:220
VacOptValue truncate
Definition vacuum.h:235
int freeze_min_age
Definition vacuum.h:219
int log_vacuum_min_duration
Definition vacuum.h:226
bool is_wraparound
Definition vacuum.h:225
int multixact_freeze_min_age
Definition vacuum.h:221
int multixact_freeze_table_age
Definition vacuum.h:223
Oid toast_parent
Definition vacuum.h:236
VacOptValue index_cleanup
Definition vacuum.h:234
double max_eager_freeze_failure_rate
Definition vacuum.h:243
void vacuum(List *relations, const VacuumParams *params, BufferAccessStrategy bstrategy, MemoryContext vac_context, bool isTopLevel)
Definition vacuum.c:494
double vacuum_max_eager_freeze_failure_rate
Definition vacuum.c:82
static VacOptValue get_vacoptval_from_boolean(DefElem *def)
Definition vacuum.c:2647
#define VACOPT_FREEZE
Definition vacuum.h:182
#define VACOPT_SKIP_LOCKED
Definition vacuum.h:184
#define VACOPT_FULL
Definition vacuum.h:183
#define VACOPT_SKIP_DATABASE_STATS
Definition vacuum.h:188
#define VACOPT_PROCESS_TOAST
Definition vacuum.h:186
#define VACOPT_DISABLE_PAGE_SKIPPING
Definition vacuum.h:187
#define VACOPT_ONLY_DATABASE_STATS
Definition vacuum.h:189
#define VACOPT_PROCESS_MAIN
Definition vacuum.h:185
#define VACOPT_ANALYZE
Definition vacuum.h:180

References _, ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, analyze(), DefElem::arg, Assert, BAS_VACUUM, defGetBoolean(), defGetInt32(), defGetString(), DefElem::defname, ereport, errcode(), errhint_internal(), errmsg, ERROR, fb(), VacuumParams::freeze_min_age, VacuumParams::freeze_table_age, get_vacoptval_from_boolean(), GetAccessStrategyWithSize(), GUC_UNIT_KB, VacuumParams::index_cleanup, InvalidOid, VacuumParams::is_wraparound, lfirst, lfirst_node, DefElem::location, VacuumParams::log_analyze_min_duration, VacuumParams::log_vacuum_min_duration, MAX_BAS_VAC_RING_SIZE_KB, VacuumParams::max_eager_freeze_failure_rate, MAX_PARALLEL_WORKER_LIMIT, MemoryContextDelete(), MemoryContextSwitchTo(), MIN_BAS_VAC_RING_SIZE_KB, VacuumParams::multixact_freeze_min_age, VacuumParams::multixact_freeze_table_age, NIL, VacuumParams::nworkers, VacuumParams::options, parse_int(), parser_errposition(), pg_strcasecmp(), PortalContext, result, VacuumParams::toast_parent, VacuumParams::truncate, VACOPT_ANALYZE, VACOPT_DISABLE_PAGE_SKIPPING, VACOPT_FREEZE, VACOPT_FULL, VACOPT_ONLY_DATABASE_STATS, VACOPT_PROCESS_MAIN, VACOPT_PROCESS_TOAST, VACOPT_SKIP_DATABASE_STATS, VACOPT_SKIP_LOCKED, VACOPT_VACUUM, VACOPT_VERBOSE, VACOPTVALUE_AUTO, VACOPTVALUE_UNSPECIFIED, vacuum(), vacuum_max_eager_freeze_failure_rate, VacuumBufferUsageLimit, and verbose.

Referenced by standard_ProcessUtility().

◆ parallel_vacuum_bulkdel_all_indexes()

void parallel_vacuum_bulkdel_all_indexes ( ParallelVacuumState pvs,
long  num_table_tuples,
int  num_index_scans,
PVWorkerStats wstats 
)
extern

Definition at line 595 of file vacuumparallel.c.

597{
599
600 /*
601 * We can only provide an approximate value of num_heap_tuples, at least
602 * for now.
603 */
605 pvs->shared->estimated_count = true;
606
607 parallel_vacuum_process_all_indexes(pvs, num_index_scans, true, wstats);
608}
#define IsParallelWorker()
Definition parallel.h:62
double reltuples
bool estimated_count
static void parallel_vacuum_process_all_indexes(ParallelVacuumState *pvs, int num_index_scans, bool vacuum, PVWorkerStats *wstats)

References Assert, PVShared::estimated_count, fb(), IsParallelWorker, parallel_vacuum_process_all_indexes(), PVShared::reltuples, and ParallelVacuumState::shared.

Referenced by lazy_vacuum_all_indexes().

◆ parallel_vacuum_cleanup_all_indexes()

void parallel_vacuum_cleanup_all_indexes ( ParallelVacuumState pvs,
long  num_table_tuples,
int  num_index_scans,
bool  estimated_count,
PVWorkerStats wstats 
)
extern

Definition at line 614 of file vacuumparallel.c.

617{
619
620 /*
621 * We can provide a better estimate of total number of surviving tuples
622 * (we assume indexes are more interested in that than in the number of
623 * nominally live tuples).
624 */
626 pvs->shared->estimated_count = estimated_count;
627
628 parallel_vacuum_process_all_indexes(pvs, num_index_scans, false, wstats);
629}

References Assert, PVShared::estimated_count, fb(), IsParallelWorker, parallel_vacuum_process_all_indexes(), PVShared::reltuples, and ParallelVacuumState::shared.

Referenced by lazy_cleanup_all_indexes().

◆ parallel_vacuum_end()

void parallel_vacuum_end ( ParallelVacuumState pvs,
IndexBulkDeleteResult **  istats 
)
extern

Definition at line 515 of file vacuumparallel.c.

516{
518
519 /* Copy the updated statistics */
520 for (int i = 0; i < pvs->nindexes; i++)
521 {
522 PVIndStats *indstats = &(pvs->indstats[i]);
523
524 if (indstats->istat_updated)
525 {
527 memcpy(istats[i], &indstats->istat, sizeof(IndexBulkDeleteResult));
528 }
529 else
530 istats[i] = NULL;
531 }
532
534
537
540
542 pfree(pvs);
543}
void DestroyParallelContext(ParallelContext *pcxt)
Definition parallel.c:959
memcpy(sums, checksumBaseOffsets, sizeof(checksumBaseOffsets))
#define palloc0_object(type)
Definition fe_memutils.h:75
int i
Definition isn.c:77
void pfree(void *pointer)
Definition mcxt.c:1616
IndexBulkDeleteResult istat
ParallelContext * pcxt
void TidStoreDestroy(TidStore *ts)
Definition tidstore.c:317
static PVSharedCostParams * pv_shared_cost_params
void ExitParallelMode(void)
Definition xact.c:1094

References AmAutoVacuumWorkerProcess, Assert, ParallelVacuumState::dead_items, DestroyParallelContext(), ExitParallelMode(), fb(), i, ParallelVacuumState::indstats, IsParallelWorker, PVIndStats::istat, PVIndStats::istat_updated, memcpy(), ParallelVacuumState::nindexes, palloc0_object, ParallelVacuumState::pcxt, pfree(), pv_shared_cost_params, TidStoreDestroy(), and ParallelVacuumState::will_parallel_vacuum.

Referenced by dead_items_cleanup().

◆ parallel_vacuum_get_dead_items()

TidStore * parallel_vacuum_get_dead_items ( ParallelVacuumState pvs,
VacDeadItemsInfo **  dead_items_info_p 
)
extern

Definition at line 562 of file vacuumparallel.c.

563{
565 return pvs->dead_items;
566}
VacDeadItemsInfo dead_items_info

References ParallelVacuumState::dead_items, PVShared::dead_items_info, fb(), and ParallelVacuumState::shared.

Referenced by dead_items_alloc(), and dead_items_reset().

◆ parallel_vacuum_init()

ParallelVacuumState * parallel_vacuum_init ( Relation  rel,
Relation indrels,
int  nindexes,
int  nrequested_workers,
int  vac_work_mem,
int  elevel,
BufferAccessStrategy  bstrategy 
)
extern

Definition at line 305 of file vacuumparallel.c.

308{
310 ParallelContext *pcxt;
311 PVShared *shared;
312 TidStore *dead_items;
313 PVIndStats *indstats;
314 BufferUsage *buffer_usage;
315 WalUsage *wal_usage;
316 bool *will_parallel_vacuum;
319 int nindexes_mwm = 0;
320 int parallel_workers = 0;
321 int querylen;
322
323 /*
324 * A parallel vacuum must be requested and there must be indexes on the
325 * relation
326 */
328 Assert(nindexes > 0);
329
330 /*
331 * Compute the number of parallel vacuum workers to launch
332 */
333 will_parallel_vacuum = palloc0_array(bool, nindexes);
334 parallel_workers = parallel_vacuum_compute_workers(indrels, nindexes,
336 will_parallel_vacuum);
337 if (parallel_workers <= 0)
338 {
339 /* Can't perform vacuum in parallel -- return NULL */
340 pfree(will_parallel_vacuum);
341 return NULL;
342 }
343
345 pvs->indrels = indrels;
346 pvs->nindexes = nindexes;
347 pvs->will_parallel_vacuum = will_parallel_vacuum;
348 pvs->bstrategy = bstrategy;
349 pvs->heaprel = rel;
350
352 pcxt = CreateParallelContext("postgres", "parallel_vacuum_main",
353 parallel_workers);
354 Assert(pcxt->nworkers > 0);
355 pvs->pcxt = pcxt;
356
357 /* Estimate size for index vacuum stats -- PARALLEL_VACUUM_KEY_INDEX_STATS */
358 est_indstats_len = mul_size(sizeof(PVIndStats), nindexes);
361
362 /* Estimate size for shared information -- PARALLEL_VACUUM_KEY_SHARED */
363 est_shared_len = sizeof(PVShared);
366
367 /*
368 * Estimate space for BufferUsage and WalUsage --
369 * PARALLEL_VACUUM_KEY_BUFFER_USAGE and PARALLEL_VACUUM_KEY_WAL_USAGE.
370 *
371 * If there are no extensions loaded that care, we could skip this. We
372 * have no way of knowing whether anyone's looking at pgBufferUsage or
373 * pgWalUsage, so do it unconditionally.
374 */
376 mul_size(sizeof(BufferUsage), pcxt->nworkers));
379 mul_size(sizeof(WalUsage), pcxt->nworkers));
381
382 /* Finally, estimate PARALLEL_VACUUM_KEY_QUERY_TEXT space */
384 {
388 }
389 else
390 querylen = 0; /* keep compiler quiet */
391
393
394 /* Prepare index vacuum stats */
395 indstats = (PVIndStats *) shm_toc_allocate(pcxt->toc, est_indstats_len);
396 MemSet(indstats, 0, est_indstats_len);
397 for (int i = 0; i < nindexes; i++)
398 {
399 Relation indrel = indrels[i];
401
402 /*
403 * Cleanup option should be either disabled, always performing in
404 * parallel or conditionally performing in parallel.
405 */
409
410 if (!will_parallel_vacuum[i])
411 continue;
412
413 if (indrel->rd_indam->amusemaintenanceworkmem)
414 nindexes_mwm++;
415
416 /*
417 * Remember the number of indexes that support parallel operation for
418 * each phase.
419 */
426 }
428 pvs->indstats = indstats;
429
430 /* Prepare shared information */
431 shared = (PVShared *) shm_toc_allocate(pcxt->toc, est_shared_len);
432 MemSet(shared, 0, est_shared_len);
433 shared->relid = RelationGetRelid(rel);
434 shared->elevel = elevel;
437 (nindexes_mwm > 0) ?
438 vac_work_mem / Min(parallel_workers, nindexes_mwm) :
440
441 shared->dead_items_info.max_bytes = vac_work_mem * (size_t) 1024;
442
443 /* Prepare DSA space for dead items */
446 pvs->dead_items = dead_items;
447 shared->dead_items_handle = TidStoreGetHandle(dead_items);
449
450 /* Use the same buffer size for all workers */
451 shared->ring_nbuffers = GetAccessStrategyBufferCount(bstrategy);
452
453 pg_atomic_init_u32(&(shared->cost_balance), 0);
454 pg_atomic_init_u32(&(shared->active_nworkers), 0);
455 pg_atomic_init_u32(&(shared->idx), 0);
456
458
459 /*
460 * Initialize shared cost-based vacuum delay parameters if it's for
461 * autovacuum.
462 */
463 if (shared->is_autovacuum)
464 {
468
471 }
472
474 pvs->shared = shared;
475
476 /*
477 * Allocate space for each worker's BufferUsage and WalUsage; no need to
478 * initialize
479 */
480 buffer_usage = shm_toc_allocate(pcxt->toc,
481 mul_size(sizeof(BufferUsage), pcxt->nworkers));
483 pvs->buffer_usage = buffer_usage;
484 wal_usage = shm_toc_allocate(pcxt->toc,
485 mul_size(sizeof(WalUsage), pcxt->nworkers));
487 pvs->wal_usage = wal_usage;
488
489 /* Store query string for workers */
491 {
492 char *sharedquery;
493
494 sharedquery = (char *) shm_toc_allocate(pcxt->toc, querylen + 1);
496 sharedquery[querylen] = '\0';
497 shm_toc_insert(pcxt->toc,
499 }
500
501 /* Success -- return parallel vacuum state */
502 return pvs;
503}
static void pg_atomic_init_u32(volatile pg_atomic_uint32 *ptr, uint32 val)
Definition atomics.h:219
void InitializeParallelDSM(ParallelContext *pcxt)
Definition parallel.c:213
ParallelContext * CreateParallelContext(const char *library_name, const char *function_name, int nworkers)
Definition parallel.c:175
int64 pgstat_get_my_query_id(void)
#define Min(x, y)
Definition c.h:1091
uint8_t uint8
Definition c.h:622
#define MemSet(start, val, len)
Definition c.h:1107
size_t Size
Definition c.h:689
dsa_handle dsa_get_handle(dsa_area *area)
Definition dsa.c:498
void on_dsm_detach(dsm_segment *seg, on_dsm_detach_callback function, Datum arg)
Definition dsm.c:1140
#define palloc0_array(type, count)
Definition fe_memutils.h:77
int GetAccessStrategyBufferCount(BufferAccessStrategy strategy)
Definition freelist.c:551
const char * debug_query_string
Definition postgres.c:94
void * shm_toc_allocate(shm_toc *toc, Size nbytes)
Definition shm_toc.c:88
void shm_toc_insert(shm_toc *toc, uint64 key, void *address)
Definition shm_toc.c:171
#define shm_toc_estimate_chunk(e, sz)
Definition shm_toc.h:51
#define shm_toc_estimate_keys(e, cnt)
Definition shm_toc.h:53
Size mul_size(Size s1, Size s2)
Definition shmem.c:1063
static void SpinLockInit(volatile slock_t *lock)
Definition spin.h:50
uint8 amparallelvacuumoptions
Definition amapi.h:284
pg_atomic_uint32 generation
bool is_autovacuum
pg_atomic_uint32 cost_balance
int maintenance_work_mem_worker
pg_atomic_uint32 active_nworkers
PVSharedCostParams cost_params
dsa_pointer dead_items_handle
dsa_handle dead_items_dsa_handle
pg_atomic_uint32 idx
dsm_segment * seg
Definition parallel.h:44
shm_toc_estimator estimator
Definition parallel.h:43
shm_toc * toc
Definition parallel.h:46
BufferAccessStrategy bstrategy
BufferUsage * buffer_usage
const struct IndexAmRoutine * rd_indam
Definition rel.h:206
size_t max_bytes
Definition vacuum.h:298
dsa_area * TidStoreGetDSA(TidStore *ts)
Definition tidstore.c:544
dsa_pointer TidStoreGetHandle(TidStore *ts)
Definition tidstore.c:552
TidStore * TidStoreCreateShared(size_t max_bytes, int tranche_id)
Definition tidstore.c:208
#define VACUUM_OPTION_PARALLEL_CLEANUP
Definition vacuum.h:62
#define VACUUM_OPTION_PARALLEL_BULKDEL
Definition vacuum.h:47
#define VACUUM_OPTION_MAX_VALID_VALUE
Definition vacuum.h:65
#define VACUUM_OPTION_PARALLEL_COND_CLEANUP
Definition vacuum.h:54
static int parallel_vacuum_compute_workers(Relation *indrels, int nindexes, int nrequested, bool *will_parallel_vacuum)
#define PARALLEL_VACUUM_KEY_INDEX_STATS
#define PARALLEL_VACUUM_KEY_QUERY_TEXT
#define PARALLEL_VACUUM_KEY_BUFFER_USAGE
#define PARALLEL_VACUUM_KEY_SHARED
#define PARALLEL_VACUUM_KEY_WAL_USAGE
static void parallel_vacuum_dsm_detach(dsm_segment *seg, Datum arg)
static void parallel_vacuum_set_cost_parameters(PVSharedCostParams *params)
void EnterParallelMode(void)
Definition xact.c:1081

References PVShared::active_nworkers, AmAutoVacuumWorkerProcess, IndexAmRoutine::amparallelvacuumoptions, Assert, ParallelVacuumState::bstrategy, ParallelVacuumState::buffer_usage, PVShared::cost_balance, PVShared::cost_params, CreateParallelContext(), ParallelVacuumState::dead_items, PVShared::dead_items_dsa_handle, PVShared::dead_items_handle, PVShared::dead_items_info, debug_query_string, dsa_get_handle(), PVShared::elevel, EnterParallelMode(), ParallelContext::estimator, fb(), PVSharedCostParams::generation, GetAccessStrategyBufferCount(), ParallelVacuumState::heaprel, i, PVShared::idx, ParallelVacuumState::indrels, ParallelVacuumState::indstats, InitializeParallelDSM(), PVShared::is_autovacuum, PVShared::maintenance_work_mem_worker, VacDeadItemsInfo::max_bytes, memcpy(), MemSet, Min, mul_size(), PVSharedCostParams::mutex, ParallelVacuumState::nindexes, ParallelVacuumState::nindexes_parallel_bulkdel, ParallelVacuumState::nindexes_parallel_cleanup, ParallelVacuumState::nindexes_parallel_condcleanup, ParallelContext::nworkers, on_dsm_detach(), palloc0_array, palloc0_object, parallel_vacuum_compute_workers(), parallel_vacuum_dsm_detach(), PARALLEL_VACUUM_KEY_BUFFER_USAGE, PARALLEL_VACUUM_KEY_INDEX_STATS, PARALLEL_VACUUM_KEY_QUERY_TEXT, PARALLEL_VACUUM_KEY_SHARED, PARALLEL_VACUUM_KEY_WAL_USAGE, parallel_vacuum_set_cost_parameters(), ParallelVacuumState::pcxt, pfree(), pg_atomic_init_u32(), pgstat_get_my_query_id(), pv_shared_cost_params, PVShared::queryid, RelationData::rd_indam, RelationGetRelid, PVShared::relid, PVShared::ring_nbuffers, ParallelContext::seg, ParallelVacuumState::shared, shm_toc_allocate(), shm_toc_estimate_chunk, shm_toc_estimate_keys, shm_toc_insert(), SpinLockInit(), TidStoreCreateShared(), TidStoreGetDSA(), TidStoreGetHandle(), ParallelContext::toc, VACUUM_OPTION_MAX_VALID_VALUE, VACUUM_OPTION_PARALLEL_BULKDEL, VACUUM_OPTION_PARALLEL_CLEANUP, VACUUM_OPTION_PARALLEL_COND_CLEANUP, ParallelVacuumState::wal_usage, and ParallelVacuumState::will_parallel_vacuum.

Referenced by dead_items_alloc().

◆ parallel_vacuum_main()

void parallel_vacuum_main ( dsm_segment seg,
shm_toc toc 
)
extern

Definition at line 1197 of file vacuumparallel.c.

1198{
1200 Relation rel;
1201 Relation *indrels;
1202 PVIndStats *indstats;
1203 PVShared *shared;
1204 TidStore *dead_items;
1205 BufferUsage *buffer_usage;
1206 WalUsage *wal_usage;
1207 int nindexes;
1208 char *sharedquery;
1209 ErrorContextCallback errcallback;
1210
1211 /*
1212 * A parallel vacuum worker must have only PROC_IN_VACUUM flag since we
1213 * don't support parallel vacuum for autovacuum as of now.
1214 */
1216
1217 elog(DEBUG1, "starting parallel vacuum worker");
1218
1219 shared = (PVShared *) shm_toc_lookup(toc, PARALLEL_VACUUM_KEY_SHARED, false);
1220
1221 /* Set debug_query_string for individual workers */
1225
1226 /* Track query ID */
1227 pgstat_report_query_id(shared->queryid, false);
1228
1229 /*
1230 * Open table. The lock mode is the same as the leader process. It's
1231 * okay because the lock mode does not conflict among the parallel
1232 * workers.
1233 */
1235
1236 /*
1237 * Open all indexes. indrels are sorted in order by OID, which should be
1238 * matched to the leader's one.
1239 */
1240 vac_open_indexes(rel, RowExclusiveLock, &nindexes, &indrels);
1241 Assert(nindexes > 0);
1242
1243 /*
1244 * Apply the desired value of maintenance_work_mem within this process.
1245 * Really we should use SetConfigOption() to change a GUC, but since we're
1246 * already in parallel mode guc.c would complain about that. Fortunately,
1247 * by the same token guc.c will not let any user-defined code change it.
1248 * So just avert your eyes while we do this:
1249 */
1250 if (shared->maintenance_work_mem_worker > 0)
1252
1253 /* Set index statistics */
1254 indstats = (PVIndStats *) shm_toc_lookup(toc,
1256 false);
1257
1258 /* Find dead_items in shared memory */
1259 dead_items = TidStoreAttach(shared->dead_items_dsa_handle,
1260 shared->dead_items_handle);
1261
1262 /* Set cost-based vacuum delay */
1263 if (shared->is_autovacuum)
1264 {
1265 /*
1266 * Parallel autovacuum workers initialize cost-based delay parameters
1267 * from the leader's shared state rather than GUC defaults, because
1268 * the leader may have applied per-table or autovacuum-specific
1269 * overrides. pv_shared_cost_params must be set before calling
1270 * parallel_vacuum_update_shared_delay_params().
1271 */
1274 }
1275 else
1277
1282
1283 /* Set parallel vacuum state */
1284 pvs.indrels = indrels;
1285 pvs.nindexes = nindexes;
1286 pvs.indstats = indstats;
1287 pvs.shared = shared;
1288 pvs.dead_items = dead_items;
1291 pvs.heaprel = rel;
1292
1293 /* These fields will be filled during index vacuum or cleanup */
1294 pvs.indname = NULL;
1296
1297 /* Each parallel VACUUM worker gets its own access strategy. */
1299 shared->ring_nbuffers * (BLCKSZ / 1024));
1300
1301 /* Setup error traceback support for ereport() */
1303 errcallback.arg = &pvs;
1304 errcallback.previous = error_context_stack;
1305 error_context_stack = &errcallback;
1306
1307 /* Prepare to track buffer usage during parallel execution */
1309
1310 /* Process indexes to perform vacuum/cleanup */
1312
1313 /* Report buffer/WAL usage during parallel execution */
1314 buffer_usage = shm_toc_lookup(toc, PARALLEL_VACUUM_KEY_BUFFER_USAGE, false);
1315 wal_usage = shm_toc_lookup(toc, PARALLEL_VACUUM_KEY_WAL_USAGE, false);
1317 &wal_usage[ParallelWorkerNumber]);
1318
1319 /* Report any remaining cost-based vacuum delay time */
1323
1324 TidStoreDetach(dead_items);
1325
1326 /* Pop the error context stack */
1327 error_context_stack = errcallback.previous;
1328
1329 vac_close_indexes(nindexes, indrels, RowExclusiveLock);
1332
1333 if (shared->is_autovacuum)
1335}
void VacuumUpdateCosts(void)
int ParallelWorkerNumber
Definition parallel.c:117
void pgstat_progress_parallel_incr_param(int index, int64 incr)
void pgstat_report_query_id(int64 query_id, bool force)
void pgstat_report_activity(BackendState state, const char *cmd_str)
@ STATE_RUNNING
ErrorContextCallback * error_context_stack
Definition elog.c:100
#define DEBUG1
Definition elog.h:31
void FreeAccessStrategy(BufferAccessStrategy strategy)
Definition freelist.c:608
int VacuumCostBalance
Definition globals.c:160
int maintenance_work_mem
Definition globals.c:135
void InstrEndParallelQuery(BufferUsage *bufusage, WalUsage *walusage)
Definition instrument.c:287
void InstrStartParallelQuery(void)
Definition instrument.c:279
#define RowExclusiveLock
Definition lockdefs.h:38
char * get_namespace_name(Oid nspid)
Definition lsyscache.c:3561
char * pstrdup(const char *in)
Definition mcxt.c:1781
#define PROC_IN_VACUUM
Definition proc.h:62
#define PROGRESS_VACUUM_DELAY_TIME
Definition progress.h:31
#define RelationGetNamespace(relation)
Definition rel.h:557
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
Definition shm_toc.c:239
PGPROC * MyProc
Definition proc.c:71
struct ErrorContextCallback * previous
Definition elog.h:299
void(* callback)(void *arg)
Definition elog.h:300
uint8 statusFlags
Definition proc.h:210
PVIndVacStatus status
void table_close(Relation relation, LOCKMODE lockmode)
Definition table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition table.c:40
void TidStoreDetach(TidStore *ts)
Definition tidstore.c:269
TidStore * TidStoreAttach(dsa_handle area_handle, dsa_pointer handle)
Definition tidstore.c:244
pg_atomic_uint32 * VacuumActiveNWorkers
Definition vacuum.c:118
bool track_cost_delay_timing
Definition vacuum.c:83
void vac_open_indexes(Relation relation, LOCKMODE lockmode, int *nindexes, Relation **Irel)
Definition vacuum.c:2369
int VacuumCostBalanceLocal
Definition vacuum.c:119
int64 parallel_vacuum_worker_delay_ns
Definition vacuum.c:96
void vac_close_indexes(int nindexes, Relation *Irel, LOCKMODE lockmode)
Definition vacuum.c:2412
pg_atomic_uint32 * VacuumSharedCostBalance
Definition vacuum.c:117
static void parallel_vacuum_error_callback(void *arg)
void parallel_vacuum_update_shared_delay_params(void)
static void parallel_vacuum_process_safe_indexes(ParallelVacuumState *pvs)
@ PARALLEL_INDVAC_STATUS_INITIAL

References PVShared::active_nworkers, ErrorContextCallback::arg, Assert, BAS_VACUUM, ParallelVacuumState::bstrategy, ErrorContextCallback::callback, PVShared::cost_balance, PVShared::cost_params, ParallelVacuumState::dead_items, PVShared::dead_items_dsa_handle, PVShared::dead_items_handle, DEBUG1, debug_query_string, elog, error_context_stack, fb(), FreeAccessStrategy(), get_namespace_name(), GetAccessStrategyWithSize(), ParallelVacuumState::heaprel, ParallelVacuumState::indname, ParallelVacuumState::indrels, ParallelVacuumState::indstats, InstrEndParallelQuery(), InstrStartParallelQuery(), PVShared::is_autovacuum, maintenance_work_mem, PVShared::maintenance_work_mem_worker, MyProc, ParallelVacuumState::nindexes, PARALLEL_INDVAC_STATUS_INITIAL, parallel_vacuum_error_callback(), PARALLEL_VACUUM_KEY_BUFFER_USAGE, PARALLEL_VACUUM_KEY_INDEX_STATS, PARALLEL_VACUUM_KEY_QUERY_TEXT, PARALLEL_VACUUM_KEY_SHARED, PARALLEL_VACUUM_KEY_WAL_USAGE, parallel_vacuum_process_safe_indexes(), parallel_vacuum_update_shared_delay_params(), parallel_vacuum_worker_delay_ns, ParallelWorkerNumber, pgstat_progress_parallel_incr_param(), pgstat_report_activity(), pgstat_report_query_id(), ErrorContextCallback::previous, PROC_IN_VACUUM, PROGRESS_VACUUM_DELAY_TIME, pstrdup(), pv_shared_cost_params, PVShared::queryid, RelationGetNamespace, RelationGetRelationName, PVShared::relid, ParallelVacuumState::relname, ParallelVacuumState::relnamespace, PVShared::ring_nbuffers, RowExclusiveLock, ParallelVacuumState::shared, ShareUpdateExclusiveLock, shm_toc_lookup(), STATE_RUNNING, ParallelVacuumState::status, PGPROC::statusFlags, table_close(), table_open(), TidStoreAttach(), TidStoreDetach(), track_cost_delay_timing, vac_close_indexes(), vac_open_indexes(), VacuumActiveNWorkers, VacuumCostBalance, VacuumCostBalanceLocal, VacuumSharedCostBalance, and VacuumUpdateCosts().

◆ parallel_vacuum_propagate_shared_delay_params()

void parallel_vacuum_propagate_shared_delay_params ( void  )
extern

Definition at line 695 of file vacuumparallel.c.

696{
698
699 /*
700 * Quick return if the leader process is not sharing the delay parameters.
701 */
703 return;
704
705 /*
706 * Check if any delay parameters have changed. We can read them without
707 * locks as only the leader can modify them.
708 */
714 return;
715
716 /* Update the shared delay parameters */
720
721 /*
722 * Increment the generation of the parameters, i.e. let parallel workers
723 * know that they should re-read shared cost params.
724 */
726}
static uint32 pg_atomic_fetch_add_u32(volatile pg_atomic_uint32 *ptr, int32 add_)
Definition atomics.h:366
int VacuumCostPageMiss
Definition globals.c:155
int VacuumCostPageDirty
Definition globals.c:156
int VacuumCostPageHit
Definition globals.c:154
static void SpinLockRelease(volatile slock_t *lock)
Definition spin.h:62
static void SpinLockAcquire(volatile slock_t *lock)
Definition spin.h:56
double vacuum_cost_delay
Definition vacuum.c:92

References AmAutoVacuumWorkerProcess, Assert, PVSharedCostParams::cost_delay, PVSharedCostParams::cost_limit, PVSharedCostParams::cost_page_dirty, PVSharedCostParams::cost_page_hit, PVSharedCostParams::cost_page_miss, fb(), PVSharedCostParams::generation, PVSharedCostParams::mutex, parallel_vacuum_set_cost_parameters(), pg_atomic_fetch_add_u32(), pv_shared_cost_params, SpinLockAcquire(), SpinLockRelease(), vacuum_cost_delay, vacuum_cost_limit, VacuumCostPageDirty, VacuumCostPageHit, and VacuumCostPageMiss.

Referenced by vacuum_delay_point().

◆ parallel_vacuum_reset_dead_items()

void parallel_vacuum_reset_dead_items ( ParallelVacuumState pvs)
extern

Definition at line 570 of file vacuumparallel.c.

571{
572 VacDeadItemsInfo *dead_items_info = &(pvs->shared->dead_items_info);
573
574 /*
575 * Free the current tidstore and return allocated DSA segments to the
576 * operating system. Then we recreate the tidstore with the same max_bytes
577 * limitation we just used.
578 */
580 pvs->dead_items = TidStoreCreateShared(dead_items_info->max_bytes,
582
583 /* Update the DSA pointer for dead_items to the new one */
586
587 /* Reset the counter */
588 dead_items_info->num_items = 0;
589}
int64 num_items
Definition vacuum.h:299

References ParallelVacuumState::dead_items, PVShared::dead_items_dsa_handle, PVShared::dead_items_handle, PVShared::dead_items_info, dsa_get_handle(), fb(), VacDeadItemsInfo::max_bytes, VacDeadItemsInfo::num_items, ParallelVacuumState::shared, TidStoreCreateShared(), TidStoreDestroy(), TidStoreGetDSA(), and TidStoreGetHandle().

Referenced by dead_items_reset().

◆ parallel_vacuum_update_shared_delay_params()

void parallel_vacuum_update_shared_delay_params ( void  )
extern

Definition at line 651 of file vacuumparallel.c.

652{
654
656
657 /* Quick return if the worker is not running for the autovacuum */
659 return;
660
663
664 /* Return if parameters had not changed in the leader */
666 return;
667
675
677
679
680 elog(DEBUG2,
681 "parallel autovacuum worker updated cost params: cost_limit=%d, cost_delay=%g, cost_page_miss=%d, cost_page_dirty=%d, cost_page_hit=%d",
687}
uint32_t uint32
Definition c.h:624
double VacuumCostDelay
Definition globals.c:158
static uint32 shared_params_generation_local

References Assert, PVSharedCostParams::cost_delay, PVSharedCostParams::cost_limit, PVSharedCostParams::cost_page_dirty, PVSharedCostParams::cost_page_hit, PVSharedCostParams::cost_page_miss, DEBUG2, elog, fb(), PVSharedCostParams::generation, IsParallelWorker, PVSharedCostParams::mutex, pg_atomic_read_u32(), pv_shared_cost_params, shared_params_generation_local, SpinLockAcquire(), SpinLockRelease(), vacuum_cost_delay, vacuum_cost_limit, VacuumCostDelay, VacuumCostLimit, VacuumCostPageDirty, VacuumCostPageHit, VacuumCostPageMiss, and VacuumUpdateCosts().

Referenced by parallel_vacuum_main(), and vacuum_delay_point().

◆ std_typanalyze()

bool std_typanalyze ( VacAttrStats stats)
extern

Definition at line 1949 of file analyze.c.

1950{
1951 Oid ltopr;
1952 Oid eqopr;
1954
1955 /* If the attstattarget column is negative, use the default value */
1956 if (stats->attstattarget < 0)
1958
1959 /* Look for default "<" and "=" operators for column's type */
1961 false, false, false,
1962 &ltopr, &eqopr, NULL,
1963 NULL);
1964
1965 /* Save the operator info for compute_stats routines */
1967 mystats->eqopr = eqopr;
1968 mystats->eqfunc = OidIsValid(eqopr) ? get_opcode(eqopr) : InvalidOid;
1969 mystats->ltopr = ltopr;
1970 stats->extra_data = mystats;
1971
1972 /*
1973 * Determine which standard statistics algorithm to use
1974 */
1975 if (OidIsValid(eqopr) && OidIsValid(ltopr))
1976 {
1977 /* Seems to be a scalar datatype */
1979 /*--------------------
1980 * The following choice of minrows is based on the paper
1981 * "Random sampling for histogram construction: how much is enough?"
1982 * by Surajit Chaudhuri, Rajeev Motwani and Vivek Narasayya, in
1983 * Proceedings of ACM SIGMOD International Conference on Management
1984 * of Data, 1998, Pages 436-447. Their Corollary 1 to Theorem 5
1985 * says that for table size n, histogram size k, maximum relative
1986 * error in bin size f, and error probability gamma, the minimum
1987 * random sample size is
1988 * r = 4 * k * ln(2*n/gamma) / f^2
1989 * Taking f = 0.5, gamma = 0.01, n = 10^6 rows, we obtain
1990 * r = 305.82 * k
1991 * Note that because of the log function, the dependence on n is
1992 * quite weak; even at n = 10^12, a 300*k sample gives <= 0.66
1993 * bin size error with probability 0.99. So there's no real need to
1994 * scale for n, which is a good thing because we don't necessarily
1995 * know it at this point.
1996 *--------------------
1997 */
1998 stats->minrows = 300 * stats->attstattarget;
1999 }
2000 else if (OidIsValid(eqopr))
2001 {
2002 /* We can still recognize distinct values */
2004 /* Might as well use the same minrows as above */
2005 stats->minrows = 300 * stats->attstattarget;
2006 }
2007 else
2008 {
2009 /* Can't do much but the trivial stuff */
2011 /* Might as well use the same minrows as above */
2012 stats->minrows = 300 * stats->attstattarget;
2013 }
2014
2015 return true;
2016}
#define OidIsValid(objectId)
Definition c.h:858
static void compute_scalar_stats(VacAttrStatsP stats, AnalyzeAttrFetchFunc fetchfunc, int samplerows, double totalrows)
Definition analyze.c:2460
int default_statistics_target
Definition analyze.c:71
static void compute_distinct_stats(VacAttrStatsP stats, AnalyzeAttrFetchFunc fetchfunc, int samplerows, double totalrows)
Definition analyze.c:2117
static void compute_trivial_stats(VacAttrStatsP stats, AnalyzeAttrFetchFunc fetchfunc, int samplerows, double totalrows)
Definition analyze.c:2027
#define palloc_object(type)
Definition fe_memutils.h:74
RegProcedure get_opcode(Oid opno)
Definition lsyscache.c:1478
void get_sort_group_operators(Oid argtype, bool needLT, bool needEQ, bool needGT, Oid *ltOpr, Oid *eqOpr, Oid *gtOpr, bool *isHashable)
Definition parse_oper.c:183
unsigned int Oid
Oid attrtypid
Definition vacuum.h:125
int minrows
Definition vacuum.h:136
int attstattarget
Definition vacuum.h:124
void * extra_data
Definition vacuum.h:137
AnalyzeAttrComputeStatsFunc compute_stats
Definition vacuum.h:135

References VacAttrStats::attrtypid, VacAttrStats::attstattarget, compute_distinct_stats(), compute_scalar_stats(), VacAttrStats::compute_stats, compute_trivial_stats(), default_statistics_target, VacAttrStats::extra_data, fb(), get_opcode(), get_sort_group_operators(), InvalidOid, VacAttrStats::minrows, OidIsValid, and palloc_object.

Referenced by array_typanalyze(), examine_attribute(), examine_attribute(), and examine_expression().

◆ vac_bulkdel_one_index()

IndexBulkDeleteResult * vac_bulkdel_one_index ( IndexVacuumInfo ivinfo,
IndexBulkDeleteResult istat,
TidStore dead_items,
VacDeadItemsInfo dead_items_info 
)
extern

Definition at line 2658 of file vacuum.c.

2660{
2661 /* Do bulk deletion */
2662 istat = index_bulk_delete(ivinfo, istat, vac_tid_reaped,
2663 dead_items);
2664
2665 ereport(ivinfo->message_level,
2666 (errmsg("scanned index \"%s\" to remove %" PRId64 " row versions",
2668 dead_items_info->num_items)));
2669
2670 return istat;
2671}
IndexBulkDeleteResult * index_bulk_delete(IndexVacuumInfo *info, IndexBulkDeleteResult *istat, IndexBulkDeleteCallback callback, void *callback_state)
Definition indexam.c:773
static bool vac_tid_reaped(ItemPointer itemptr, void *state)
Definition vacuum.c:2705

References ereport, errmsg, fb(), index_bulk_delete(), VacDeadItemsInfo::num_items, RelationGetRelationName, and vac_tid_reaped().

Referenced by lazy_vacuum_one_index(), and parallel_vacuum_process_one_index().

◆ vac_cleanup_one_index()

IndexBulkDeleteResult * vac_cleanup_one_index ( IndexVacuumInfo ivinfo,
IndexBulkDeleteResult istat 
)
extern

Definition at line 2679 of file vacuum.c.

2680{
2681 istat = index_vacuum_cleanup(ivinfo, istat);
2682
2683 if (istat)
2684 ereport(ivinfo->message_level,
2685 (errmsg("index \"%s\" now contains %.0f row versions in %u pages",
2687 istat->num_index_tuples,
2688 istat->num_pages),
2689 errdetail("%.0f index row versions were removed.\n"
2690 "%u index pages were newly deleted.\n"
2691 "%u index pages are currently deleted, of which %u are currently reusable.",
2692 istat->tuples_removed,
2693 istat->pages_newly_deleted,
2694 istat->pages_deleted, istat->pages_free)));
2695
2696 return istat;
2697}
int errdetail(const char *fmt,...) pg_attribute_printf(1
IndexBulkDeleteResult * index_vacuum_cleanup(IndexVacuumInfo *info, IndexBulkDeleteResult *istat)
Definition indexam.c:794
BlockNumber pages_deleted
Definition genam.h:90
BlockNumber pages_newly_deleted
Definition genam.h:89
BlockNumber pages_free
Definition genam.h:91
BlockNumber num_pages
Definition genam.h:85
double tuples_removed
Definition genam.h:88
double num_index_tuples
Definition genam.h:87

References ereport, errdetail(), errmsg, fb(), index_vacuum_cleanup(), IndexBulkDeleteResult::num_index_tuples, IndexBulkDeleteResult::num_pages, IndexBulkDeleteResult::pages_deleted, IndexBulkDeleteResult::pages_free, IndexBulkDeleteResult::pages_newly_deleted, RelationGetRelationName, and IndexBulkDeleteResult::tuples_removed.

Referenced by lazy_cleanup_one_index(), and parallel_vacuum_process_one_index().

◆ vac_close_indexes()

void vac_close_indexes ( int  nindexes,
Relation Irel,
LOCKMODE  lockmode 
)
extern

Definition at line 2412 of file vacuum.c.

2413{
2414 if (Irel == NULL)
2415 return;
2416
2417 while (nindexes--)
2418 {
2419 Relation ind = Irel[nindexes];
2420
2421 index_close(ind, lockmode);
2422 }
2423 pfree(Irel);
2424}
void index_close(Relation relation, LOCKMODE lockmode)
Definition indexam.c:178

References fb(), index_close(), and pfree().

Referenced by do_analyze_rel(), heap_vacuum_rel(), and parallel_vacuum_main().

◆ vac_estimate_reltuples()

double vac_estimate_reltuples ( Relation  relation,
BlockNumber  total_pages,
BlockNumber  scanned_pages,
double  scanned_tuples 
)
extern

Definition at line 1331 of file vacuum.c.

1335{
1336 BlockNumber old_rel_pages = relation->rd_rel->relpages;
1337 double old_rel_tuples = relation->rd_rel->reltuples;
1338 double old_density;
1339 double unscanned_pages;
1340 double total_tuples;
1341
1342 /* If we did scan the whole table, just use the count as-is */
1343 if (scanned_pages >= total_pages)
1344 return scanned_tuples;
1345
1346 /*
1347 * When successive VACUUM commands scan the same few pages again and
1348 * again, without anything from the table really changing, there is a risk
1349 * that our beliefs about tuple density will gradually become distorted.
1350 * This might be caused by vacuumlazy.c implementation details, such as
1351 * its tendency to always scan the last heap page. Handle that here.
1352 *
1353 * If the relation is _exactly_ the same size according to the existing
1354 * pg_class entry, and only a few of its pages (less than 2%) were
1355 * scanned, keep the existing value of reltuples. Also keep the existing
1356 * value when only a subset of rel's pages <= a single page were scanned.
1357 *
1358 * (Note: we might be returning -1 here.)
1359 */
1360 if (old_rel_pages == total_pages &&
1361 scanned_pages < (double) total_pages * 0.02)
1362 return old_rel_tuples;
1363 if (scanned_pages <= 1)
1364 return old_rel_tuples;
1365
1366 /*
1367 * If old density is unknown, we can't do much except scale up
1368 * scanned_tuples to match total_pages.
1369 */
1370 if (old_rel_tuples < 0 || old_rel_pages == 0)
1371 return floor((scanned_tuples / scanned_pages) * total_pages + 0.5);
1372
1373 /*
1374 * Okay, we've covered the corner cases. The normal calculation is to
1375 * convert the old measurement to a density (tuples per page), then
1376 * estimate the number of tuples in the unscanned pages using that figure,
1377 * and finally add on the number of tuples in the scanned pages.
1378 */
1380 unscanned_pages = (double) total_pages - (double) scanned_pages;
1381 total_tuples = old_density * unscanned_pages + scanned_tuples;
1382 return floor(total_tuples + 0.5);
1383}
Form_pg_class rd_rel
Definition rel.h:111

References fb(), and RelationData::rd_rel.

Referenced by lazy_scan_heap(), and statapprox_heap().

◆ vac_open_indexes()

void vac_open_indexes ( Relation  relation,
LOCKMODE  lockmode,
int nindexes,
Relation **  Irel 
)
extern

Definition at line 2369 of file vacuum.c.

2371{
2374 int i;
2375
2376 Assert(lockmode != NoLock);
2377
2379
2380 /* allocate enough memory for all indexes */
2382
2383 if (i > 0)
2384 *Irel = (Relation *) palloc(i * sizeof(Relation));
2385 else
2386 *Irel = NULL;
2387
2388 /* collect just the ready indexes */
2389 i = 0;
2390 foreach(indexoidscan, indexoidlist)
2391 {
2392 Oid indexoid = lfirst_oid(indexoidscan);
2394
2395 indrel = index_open(indexoid, lockmode);
2396 if (indrel->rd_index->indisready)
2397 (*Irel)[i++] = indrel;
2398 else
2399 index_close(indrel, lockmode);
2400 }
2401
2402 *nindexes = i;
2403
2405}
Relation index_open(Oid relationId, LOCKMODE lockmode)
Definition indexam.c:134
void list_free(List *list)
Definition list.c:1546
void * palloc(Size size)
Definition mcxt.c:1387
static int list_length(const List *l)
Definition pg_list.h:152
#define lfirst_oid(lc)
Definition pg_list.h:174
List * RelationGetIndexList(Relation relation)
Definition relcache.c:4827
Definition pg_list.h:54

References Assert, fb(), i, index_close(), index_open(), lfirst_oid, list_free(), list_length(), NoLock, palloc(), and RelationGetIndexList().

Referenced by do_analyze_rel(), heap_vacuum_rel(), and parallel_vacuum_main().

◆ vac_update_datfrozenxid()

void vac_update_datfrozenxid ( void  )
extern

Definition at line 1609 of file vacuum.c.

1610{
1611 HeapTuple tuple;
1613 Relation relation;
1614 SysScanDesc scan;
1620 bool bogus = false;
1621 bool dirty = false;
1622 ScanKeyData key[1];
1623 void *inplace_state;
1624
1625 /*
1626 * Restrict this task to one backend per database. This avoids race
1627 * conditions that would move datfrozenxid or datminmxid backward. It
1628 * avoids calling vac_truncate_clog() with a datfrozenxid preceding a
1629 * datfrozenxid passed to an earlier vac_truncate_clog() call.
1630 */
1632
1633 /*
1634 * Initialize the "min" calculation with
1635 * GetOldestNonRemovableTransactionId(), which is a reasonable
1636 * approximation to the minimum relfrozenxid for not-yet-committed
1637 * pg_class entries for new tables; see AddNewRelationTuple(). So we
1638 * cannot produce a wrong minimum by starting with this.
1639 */
1641
1642 /*
1643 * Similarly, initialize the MultiXact "min" with the value that would be
1644 * used on pg_class for new tables. See AddNewRelationTuple().
1645 */
1647
1648 /*
1649 * Identify the latest relfrozenxid and relminmxid values that we could
1650 * validly see during the scan. These are conservative values, but it's
1651 * not really worth trying to be more exact.
1652 */
1655
1656 /*
1657 * We must seqscan pg_class to find the minimum Xid, because there is no
1658 * index that can help us here.
1659 *
1660 * See vac_truncate_clog() for the race condition to prevent.
1661 */
1663
1664 scan = systable_beginscan(relation, InvalidOid, false,
1665 NULL, 0, NULL);
1666
1667 while ((classTup = systable_getnext(scan)) != NULL)
1668 {
1670 volatile TransactionId *relfrozenxid_p = &classForm->relfrozenxid;
1671 volatile TransactionId *relminmxid_p = &classForm->relminmxid;
1672 TransactionId relfrozenxid = *relfrozenxid_p;
1673 TransactionId relminmxid = *relminmxid_p;
1674
1675 /*
1676 * Only consider relations able to hold unfrozen XIDs (anything else
1677 * should have InvalidTransactionId in relfrozenxid anyway).
1678 */
1679 if (classForm->relkind != RELKIND_RELATION &&
1680 classForm->relkind != RELKIND_MATVIEW &&
1681 classForm->relkind != RELKIND_TOASTVALUE)
1682 {
1683 Assert(!TransactionIdIsValid(relfrozenxid));
1684 Assert(!MultiXactIdIsValid(relminmxid));
1685 continue;
1686 }
1687
1688 /*
1689 * Some table AMs might not need per-relation xid / multixid horizons.
1690 * It therefore seems reasonable to allow relfrozenxid and relminmxid
1691 * to not be set (i.e. set to their respective Invalid*Id)
1692 * independently. Thus validate and compute horizon for each only if
1693 * set.
1694 *
1695 * If things are working properly, no relation should have a
1696 * relfrozenxid or relminmxid that is "in the future". However, such
1697 * cases have been known to arise due to bugs in pg_upgrade. If we
1698 * see any entries that are "in the future", chicken out and don't do
1699 * anything. This ensures we won't truncate clog & multixact SLRUs
1700 * before those relations have been scanned and cleaned up.
1701 */
1702
1703 if (TransactionIdIsValid(relfrozenxid))
1704 {
1705 Assert(TransactionIdIsNormal(relfrozenxid));
1706
1707 /* check for values in the future */
1708 if (TransactionIdPrecedes(lastSaneFrozenXid, relfrozenxid))
1709 {
1710 bogus = true;
1711 break;
1712 }
1713
1714 /* determine new horizon */
1715 if (TransactionIdPrecedes(relfrozenxid, newFrozenXid))
1716 newFrozenXid = relfrozenxid;
1717 }
1718
1719 if (MultiXactIdIsValid(relminmxid))
1720 {
1721 /* check for values in the future */
1722 if (MultiXactIdPrecedes(lastSaneMinMulti, relminmxid))
1723 {
1724 bogus = true;
1725 break;
1726 }
1727
1728 /* determine new horizon */
1729 if (MultiXactIdPrecedes(relminmxid, newMinMulti))
1730 newMinMulti = relminmxid;
1731 }
1732 }
1733
1734 /* we're done with pg_class */
1735 systable_endscan(scan);
1736 table_close(relation, AccessShareLock);
1737
1738 /* chicken out if bogus data found */
1739 if (bogus)
1740 return;
1741
1744
1745 /* Now fetch the pg_database tuple we need to update. */
1747
1748 /*
1749 * Fetch a copy of the tuple to scribble on. We could check the syscache
1750 * tuple first. If that concluded !dirty, we'd avoid waiting on
1751 * concurrent heap_update() and would avoid exclusive-locking the buffer.
1752 * For now, don't optimize that.
1753 */
1754 ScanKeyInit(&key[0],
1758
1760 NULL, 1, key, &tuple, &inplace_state);
1761
1762 if (!HeapTupleIsValid(tuple))
1763 elog(ERROR, "could not find tuple for database %u", MyDatabaseId);
1764
1766
1767 /*
1768 * As in vac_update_relstats(), we ordinarily don't want to let
1769 * datfrozenxid go backward; but if it's "in the future" then it must be
1770 * corrupt and it seems best to overwrite it.
1771 */
1772 if (dbform->datfrozenxid != newFrozenXid &&
1773 (TransactionIdPrecedes(dbform->datfrozenxid, newFrozenXid) ||
1775 {
1776 dbform->datfrozenxid = newFrozenXid;
1777 dirty = true;
1778 }
1779 else
1780 newFrozenXid = dbform->datfrozenxid;
1781
1782 /* Ditto for datminmxid */
1783 if (dbform->datminmxid != newMinMulti &&
1784 (MultiXactIdPrecedes(dbform->datminmxid, newMinMulti) ||
1786 {
1787 dbform->datminmxid = newMinMulti;
1788 dirty = true;
1789 }
1790 else
1791 newMinMulti = dbform->datminmxid;
1792
1793 if (dirty)
1795 else
1797
1798 heap_freetuple(tuple);
1799 table_close(relation, RowExclusiveLock);
1800
1801 /*
1802 * If we were able to advance datfrozenxid or datminmxid, see if we can
1803 * truncate pg_xact and/or pg_multixact. Also do it if the shared
1804 * XID-wrap-limit info is stale, since this action will update that too.
1805 */
1806 if (dirty || ForceTransactionIdLimitUpdate())
1809}
TransactionId MultiXactId
Definition c.h:746
uint32 TransactionId
Definition c.h:736
void systable_endscan(SysScanDesc sysscan)
Definition genam.c:612
void systable_inplace_update_cancel(void *state)
Definition genam.c:913
void systable_inplace_update_begin(Relation relation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, const ScanKeyData *key, HeapTuple *oldtupcopy, void **state)
Definition genam.c:818
void systable_inplace_update_finish(void *state, HeapTuple tuple)
Definition genam.c:894
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition genam.c:523
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition genam.c:388
Oid MyDatabaseId
Definition globals.c:96
void heap_freetuple(HeapTuple htup)
Definition heaptuple.c:1372
static void * GETSTRUCT(const HeapTupleData *tuple)
void LockDatabaseFrozenIds(LOCKMODE lockmode)
Definition lmgr.c:491
#define AccessShareLock
Definition lockdefs.h:36
#define ExclusiveLock
Definition lockdefs.h:42
bool MultiXactIdPrecedes(MultiXactId multi1, MultiXactId multi2)
Definition multixact.c:2865
MultiXactId GetOldestMultiXactId(void)
Definition multixact.c:2378
MultiXactId ReadNextMultiXactId(void)
Definition multixact.c:679
#define MultiXactIdIsValid(multi)
Definition multixact.h:29
FormData_pg_class * Form_pg_class
Definition pg_class.h:160
END_CATALOG_STRUCT typedef FormData_pg_database * Form_pg_database
TransactionId GetOldestNonRemovableTransactionId(Relation rel)
Definition procarray.c:1944
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition scankey.c:76
#define BTEqualStrategyNumber
Definition stratnum.h:31
static TransactionId ReadNextTransactionId(void)
Definition transam.h:375
#define TransactionIdIsValid(xid)
Definition transam.h:41
#define TransactionIdIsNormal(xid)
Definition transam.h:42
static bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition transam.h:263
static void vac_truncate_clog(TransactionId frozenXID, MultiXactId minMulti, TransactionId lastSaneFrozenXid, MultiXactId lastSaneMinMulti)
Definition vacuum.c:1830
bool ForceTransactionIdLimitUpdate(void)
Definition varsup.c:516

References AccessShareLock, Assert, BTEqualStrategyNumber, elog, ERROR, ExclusiveLock, fb(), ForceTransactionIdLimitUpdate(), Form_pg_database, GetOldestMultiXactId(), GetOldestNonRemovableTransactionId(), GETSTRUCT(), heap_freetuple(), HeapTupleIsValid, InvalidOid, LockDatabaseFrozenIds(), MultiXactIdIsValid, MultiXactIdPrecedes(), MyDatabaseId, ObjectIdGetDatum(), ReadNextMultiXactId(), ReadNextTransactionId(), RowExclusiveLock, ScanKeyInit(), systable_beginscan(), systable_endscan(), systable_getnext(), systable_inplace_update_begin(), systable_inplace_update_cancel(), systable_inplace_update_finish(), table_close(), table_open(), TransactionIdIsNormal, TransactionIdIsValid, TransactionIdPrecedes(), and vac_truncate_clog().

Referenced by do_autovacuum(), and vacuum().

◆ vac_update_relstats()

void vac_update_relstats ( Relation  relation,
BlockNumber  num_pages,
double  num_tuples,
BlockNumber  num_all_visible_pages,
BlockNumber  num_all_frozen_pages,
bool  hasindex,
TransactionId  frozenxid,
MultiXactId  minmulti,
bool frozenxid_updated,
bool minmulti_updated,
bool  in_outer_xact 
)
extern

Definition at line 1427 of file vacuum.c.

1435{
1436 Oid relid = RelationGetRelid(relation);
1437 Relation rd;
1438 ScanKeyData key[1];
1440 void *inplace_state;
1442 bool dirty,
1443 futurexid,
1444 futuremxid;
1447
1449
1450 /* Fetch a copy of the tuple to scribble on */
1451 ScanKeyInit(&key[0],
1454 ObjectIdGetDatum(relid));
1456 NULL, 1, key, &ctup, &inplace_state);
1457 if (!HeapTupleIsValid(ctup))
1458 elog(ERROR, "pg_class entry for relid %u vanished during vacuuming",
1459 relid);
1461
1462 /* Apply statistical updates, if any, to copied tuple */
1463
1464 dirty = false;
1465 if (pgcform->relpages != (int32) num_pages)
1466 {
1467 pgcform->relpages = (int32) num_pages;
1468 dirty = true;
1469 }
1470 if (pgcform->reltuples != (float4) num_tuples)
1471 {
1472 pgcform->reltuples = (float4) num_tuples;
1473 dirty = true;
1474 }
1475 if (pgcform->relallvisible != (int32) num_all_visible_pages)
1476 {
1477 pgcform->relallvisible = (int32) num_all_visible_pages;
1478 dirty = true;
1479 }
1480 if (pgcform->relallfrozen != (int32) num_all_frozen_pages)
1481 {
1482 pgcform->relallfrozen = (int32) num_all_frozen_pages;
1483 dirty = true;
1484 }
1485
1486 /* Apply DDL updates, but not inside an outer transaction (see above) */
1487
1488 if (!in_outer_xact)
1489 {
1490 /*
1491 * If we didn't find any indexes, reset relhasindex.
1492 */
1493 if (pgcform->relhasindex && !hasindex)
1494 {
1495 pgcform->relhasindex = false;
1496 dirty = true;
1497 }
1498
1499 /* We also clear relhasrules and relhastriggers if needed */
1500 if (pgcform->relhasrules && relation->rd_rules == NULL)
1501 {
1502 pgcform->relhasrules = false;
1503 dirty = true;
1504 }
1505 if (pgcform->relhastriggers && relation->trigdesc == NULL)
1506 {
1507 pgcform->relhastriggers = false;
1508 dirty = true;
1509 }
1510 }
1511
1512 /*
1513 * Update relfrozenxid, unless caller passed InvalidTransactionId
1514 * indicating it has no new data.
1515 *
1516 * Ordinarily, we don't let relfrozenxid go backwards. However, if the
1517 * stored relfrozenxid is "in the future" then it seems best to assume
1518 * it's corrupt, and overwrite with the oldest remaining XID in the table.
1519 * This should match vac_update_datfrozenxid() concerning what we consider
1520 * to be "in the future".
1521 */
1522 oldfrozenxid = pgcform->relfrozenxid;
1523 futurexid = false;
1525 *frozenxid_updated = false;
1526 if (TransactionIdIsNormal(frozenxid) && oldfrozenxid != frozenxid)
1527 {
1528 bool update = false;
1529
1530 if (TransactionIdPrecedes(oldfrozenxid, frozenxid))
1531 update = true;
1533 futurexid = update = true;
1534
1535 if (update)
1536 {
1537 pgcform->relfrozenxid = frozenxid;
1538 dirty = true;
1540 *frozenxid_updated = true;
1541 }
1542 }
1543
1544 /* Similarly for relminmxid */
1545 oldminmulti = pgcform->relminmxid;
1546 futuremxid = false;
1547 if (minmulti_updated)
1548 *minmulti_updated = false;
1550 {
1551 bool update = false;
1552
1554 update = true;
1556 futuremxid = update = true;
1557
1558 if (update)
1559 {
1560 pgcform->relminmxid = minmulti;
1561 dirty = true;
1562 if (minmulti_updated)
1563 *minmulti_updated = true;
1564 }
1565 }
1566
1567 /* If anything changed, write out the tuple. */
1568 if (dirty)
1570 else
1572
1574
1575 if (futurexid)
1578 errmsg_internal("overwrote invalid relfrozenxid value %u with new value %u for table \"%s\"",
1579 oldfrozenxid, frozenxid,
1580 RelationGetRelationName(relation))));
1581 if (futuremxid)
1584 errmsg_internal("overwrote invalid relminmxid value %u with new value %u for table \"%s\"",
1586 RelationGetRelationName(relation))));
1587}
int32_t int32
Definition c.h:620
float float4
Definition c.h:713
int int errmsg_internal(const char *fmt,...) pg_attribute_printf(1
#define ERRCODE_DATA_CORRUPTED
TriggerDesc * trigdesc
Definition rel.h:117
RuleLock * rd_rules
Definition rel.h:115

References BTEqualStrategyNumber, elog, ereport, errcode(), ERRCODE_DATA_CORRUPTED, errmsg_internal(), ERROR, fb(), GETSTRUCT(), HeapTupleIsValid, MultiXactIdIsValid, MultiXactIdPrecedes(), ObjectIdGetDatum(), RelationData::rd_rules, ReadNextMultiXactId(), ReadNextTransactionId(), RelationGetRelationName, RelationGetRelid, RowExclusiveLock, ScanKeyInit(), systable_inplace_update_begin(), systable_inplace_update_cancel(), systable_inplace_update_finish(), table_close(), table_open(), TransactionIdIsNormal, TransactionIdPrecedes(), RelationData::trigdesc, and WARNING.

Referenced by do_analyze_rel(), heap_vacuum_rel(), and update_relstats_all_indexes().

◆ vacuum()

void vacuum ( List relations,
const VacuumParams params,
BufferAccessStrategy  bstrategy,
MemoryContext  vac_context,
bool  isTopLevel 
)
extern

Definition at line 494 of file vacuum.c.

496{
497 static bool in_vacuum = false;
498
499 const char *stmttype;
500 volatile bool in_outer_xact,
502
503 stmttype = (params->options & VACOPT_VACUUM) ? "VACUUM" : "ANALYZE";
504
505 /*
506 * We cannot run VACUUM inside a user transaction block; if we were inside
507 * a transaction, then our commit- and start-transaction-command calls
508 * would not have the intended effect! There are numerous other subtle
509 * dependencies on this, too.
510 *
511 * ANALYZE (without VACUUM) can run either way.
512 */
513 if (params->options & VACOPT_VACUUM)
514 {
516 in_outer_xact = false;
517 }
518 else
520
521 /*
522 * Check for and disallow recursive calls. This could happen when VACUUM
523 * FULL or ANALYZE calls a hostile index expression that itself calls
524 * ANALYZE.
525 */
526 if (in_vacuum)
529 errmsg("%s cannot be executed from VACUUM or ANALYZE",
530 stmttype)));
531
532 /*
533 * Build list of relation(s) to process, putting any new data in
534 * vac_context for safekeeping.
535 */
537 {
538 /* We don't process any tables in this case */
539 Assert(relations == NIL);
540 }
541 else if (relations != NIL)
542 {
543 List *newrels = NIL;
544 ListCell *lc;
545
546 foreach(lc, relations)
547 {
549 List *sublist;
551
556 }
557 relations = newrels;
558 }
559 else
560 relations = get_all_vacuum_rels(vac_context, params->options);
561
562 /*
563 * Decide whether we need to start/commit our own transactions.
564 *
565 * For VACUUM (with or without ANALYZE): always do so, so that we can
566 * release locks as soon as possible. (We could possibly use the outer
567 * transaction for a one-table VACUUM, but handling TOAST tables would be
568 * problematic.)
569 *
570 * For ANALYZE (no VACUUM): if inside a transaction block, we cannot
571 * start/commit our own transactions. Also, there's no need to do so if
572 * only processing one relation. For multiple relations when not within a
573 * transaction block, and also in an autovacuum worker, use own
574 * transactions so we can release locks sooner.
575 */
576 if (params->options & VACOPT_VACUUM)
577 use_own_xacts = true;
578 else
579 {
580 Assert(params->options & VACOPT_ANALYZE);
582 use_own_xacts = true;
583 else if (in_outer_xact)
584 use_own_xacts = false;
585 else if (list_length(relations) > 1)
586 use_own_xacts = true;
587 else
588 use_own_xacts = false;
589 }
590
591 /*
592 * vacuum_rel expects to be entered with no transaction active; it will
593 * start and commit its own transaction. But we are called by an SQL
594 * command, and so we are executing inside a transaction already. We
595 * commit the transaction started in PostgresMain() here, and start
596 * another one before exiting to match the commit waiting for us back in
597 * PostgresMain().
598 */
599 if (use_own_xacts)
600 {
602
603 /* ActiveSnapshot is not set by autovacuum */
604 if (ActiveSnapshotSet())
606
607 /* matches the StartTransaction in PostgresMain() */
609 }
610
611 /* Turn vacuum cost accounting on or off, and set/clear in_vacuum */
612 PG_TRY();
613 {
614 ListCell *cur;
615
616 in_vacuum = true;
617 VacuumFailsafeActive = false;
623
624 /*
625 * Loop to process each selected relation.
626 */
627 foreach(cur, relations)
628 {
630
631 if (params->options & VACOPT_VACUUM)
632 {
633 if (!vacuum_rel(vrel->oid, vrel->relation, *params, bstrategy,
634 isTopLevel))
635 continue;
636 }
637
638 if (params->options & VACOPT_ANALYZE)
639 {
640 /*
641 * If using separate xacts, start one for analyze. Otherwise,
642 * we can use the outer transaction.
643 */
644 if (use_own_xacts)
645 {
647 /* functions in indexes may want a snapshot set */
649 }
650
651 analyze_rel(vrel->oid, vrel->relation, params,
652 vrel->va_cols, in_outer_xact, bstrategy);
653
654 if (use_own_xacts)
655 {
657 /* standard_ProcessUtility() does CCI if !use_own_xacts */
660 }
661 else
662 {
663 /*
664 * If we're not using separate xacts, better separate the
665 * ANALYZE actions with CCIs. This avoids trouble if user
666 * says "ANALYZE t, t".
667 */
669 }
670 }
671
672 /*
673 * Ensure VacuumFailsafeActive has been reset before vacuuming the
674 * next relation.
675 */
676 VacuumFailsafeActive = false;
677 }
678 }
679 PG_FINALLY();
680 {
681 in_vacuum = false;
682 VacuumCostActive = false;
683 VacuumFailsafeActive = false;
685 }
686 PG_END_TRY();
687
688 /*
689 * Finish up processing.
690 */
691 if (use_own_xacts)
692 {
693 /* here, we are not in a transaction */
694
695 /*
696 * This matches the CommitTransaction waiting for us in
697 * PostgresMain().
698 */
700 }
701
702 if ((params->options & VACOPT_VACUUM) &&
704 {
705 /*
706 * Update pg_database.datfrozenxid, and truncate pg_xact if possible.
707 */
709 }
710
711}
void analyze_rel(Oid relid, RangeVar *relation, const VacuumParams *params, List *va_cols, bool in_outer_xact, BufferAccessStrategy bstrategy)
Definition analyze.c:110
struct cursor * cur
Definition ecpg.c:29
#define PG_TRY(...)
Definition elog.h:374
#define PG_END_TRY(...)
Definition elog.h:399
#define PG_FINALLY(...)
Definition elog.h:391
bool VacuumCostActive
Definition globals.c:161
List * list_concat(List *list1, const List *list2)
Definition list.c:561
Snapshot GetTransactionSnapshot(void)
Definition snapmgr.c:272
void PushActiveSnapshot(Snapshot snapshot)
Definition snapmgr.c:682
bool ActiveSnapshotSet(void)
Definition snapmgr.c:812
void PopActiveSnapshot(void)
Definition snapmgr.c:775
static List * expand_vacuum_rel(VacuumRelation *vrel, MemoryContext vac_context, int options)
Definition vacuum.c:884
static bool vacuum_rel(Oid relid, RangeVar *relation, VacuumParams params, BufferAccessStrategy bstrategy, bool isTopLevel)
Definition vacuum.c:2007
static List * get_all_vacuum_rels(MemoryContext vac_context, int options)
Definition vacuum.c:1039
void vac_update_datfrozenxid(void)
Definition vacuum.c:1609
bool VacuumFailsafeActive
Definition vacuum.c:111
bool IsInTransactionBlock(bool isTopLevel)
Definition xact.c:3820
void CommandCounterIncrement(void)
Definition xact.c:1130
void PreventInTransactionBlock(bool isTopLevel, const char *stmtType)
Definition xact.c:3698
void StartTransactionCommand(void)
Definition xact.c:3109
void CommitTransactionCommand(void)
Definition xact.c:3207

References ActiveSnapshotSet(), AmAutoVacuumWorkerProcess, analyze_rel(), Assert, CommandCounterIncrement(), CommitTransactionCommand(), cur, ereport, errcode(), errmsg, ERROR, expand_vacuum_rel(), fb(), get_all_vacuum_rels(), GetTransactionSnapshot(), IsInTransactionBlock(), lfirst_node, list_concat(), list_length(), MemoryContextSwitchTo(), NIL, VacuumParams::options, PG_END_TRY, PG_FINALLY, PG_TRY, PopActiveSnapshot(), PreventInTransactionBlock(), PushActiveSnapshot(), StartTransactionCommand(), vac_update_datfrozenxid(), VACOPT_ANALYZE, VACOPT_ONLY_DATABASE_STATS, VACOPT_SKIP_DATABASE_STATS, VACOPT_VACUUM, vacuum_rel(), VacuumActiveNWorkers, VacuumCostActive, VacuumCostBalance, VacuumCostBalanceLocal, VacuumFailsafeActive, VacuumSharedCostBalance, and VacuumUpdateCosts().

Referenced by autovacuum_do_vac_analyze(), ExecVacuum(), parallel_vacuum_index_is_parallel_safe(), and parallel_vacuum_process_all_indexes().

◆ vacuum_delay_point()

void vacuum_delay_point ( bool  is_analyze)
extern

Definition at line 2433 of file vacuum.c.

2434{
2435 double msec = 0;
2436
2437 /* Always check for interrupts */
2439
2440 if (InterruptPending)
2441 return;
2442
2443 if (IsParallelWorker())
2444 {
2445 /*
2446 * Update cost-based vacuum delay parameters for a parallel autovacuum
2447 * worker if any changes are detected. It might enable cost-based
2448 * delay so it needs to be called before VacuumCostActive check.
2449 */
2451 }
2452
2454 return;
2455
2456 /*
2457 * Autovacuum workers should reload the configuration file if requested.
2458 * This allows changes to [autovacuum_]vacuum_cost_limit and
2459 * [autovacuum_]vacuum_cost_delay to take effect while a table is being
2460 * vacuumed or analyzed.
2461 */
2463 {
2464 ConfigReloadPending = false;
2467
2468 /*
2469 * Propagate cost-based vacuum delay parameters to shared memory if
2470 * any of them have changed during the config reload.
2471 */
2473 }
2474
2475 /*
2476 * If we disabled cost-based delays after reloading the config file,
2477 * return.
2478 */
2479 if (!VacuumCostActive)
2480 return;
2481
2482 /*
2483 * For parallel vacuum, the delay is computed based on the shared cost
2484 * balance. See compute_parallel_delay.
2485 */
2490
2491 /* Nap if appropriate */
2492 if (msec > 0)
2493 {
2495
2496 if (msec > vacuum_cost_delay * 4)
2497 msec = vacuum_cost_delay * 4;
2498
2501
2503 pg_usleep(msec * 1000);
2505
2507 {
2510
2514
2515 /*
2516 * For parallel workers, we only report the delay time every once
2517 * in a while to avoid overloading the leader with messages and
2518 * interrupts.
2519 */
2520 if (IsParallelWorker())
2521 {
2524
2526
2527 /* Accumulate the delay time */
2529
2530 /* Calculate interval since last report */
2533
2534 /* If we haven't reported in a while, do so now */
2537 {
2540
2541 /* Reset variables */
2544 }
2545 }
2546 else if (is_analyze)
2549 else
2552 }
2553
2554 /*
2555 * We don't want to ignore postmaster death during very long vacuums
2556 * with vacuum_cost_delay configured. We can't use the usual
2557 * WaitLatch() approach here because we want microsecond-based sleep
2558 * durations above.
2559 */
2561 exit(1);
2562
2564
2565 /*
2566 * Balance and update limit values for autovacuum workers. We must do
2567 * this periodically, as the number of workers across which we are
2568 * balancing the limit may have changed.
2569 *
2570 * TODO: There may be better criteria for determining when to do this
2571 * besides "check after napping".
2572 */
2574
2575 /* Might have gotten an interrupt while sleeping */
2577 }
2578}
void AutoVacuumUpdateCostLimit(void)
void pgstat_progress_incr_param(int index, int64 incr)
volatile sig_atomic_t InterruptPending
Definition globals.c:32
bool IsUnderPostmaster
Definition globals.c:122
void ProcessConfigFile(GucContext context)
Definition guc-file.l:120
@ PGC_SIGHUP
Definition guc.h:75
#define INSTR_TIME_SET_CURRENT(t)
Definition instr_time.h:426
#define INSTR_TIME_GET_NANOSEC(t)
Definition instr_time.h:445
#define INSTR_TIME_SET_ZERO(t)
Definition instr_time.h:421
#define INSTR_TIME_ACCUM_DIFF(x, y, z)
Definition instr_time.h:439
volatile sig_atomic_t ConfigReloadPending
Definition interrupt.c:27
#define PostmasterIsAlive()
Definition pmsignal.h:106
#define PROGRESS_ANALYZE_DELAY_TIME
Definition progress.h:62
void pg_usleep(long microsec)
Definition signal.c:53
static double compute_parallel_delay(void)
Definition vacuum.c:2603
#define PARALLEL_VACUUM_DELAY_REPORT_INTERVAL_NS
Definition vacuum.c:71
void parallel_vacuum_propagate_shared_delay_params(void)
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition wait_event.h:67
static void pgstat_report_wait_end(void)
Definition wait_event.h:83

References AmAutoVacuumWorkerProcess, Assert, AutoVacuumUpdateCostLimit(), CHECK_FOR_INTERRUPTS, compute_parallel_delay(), ConfigReloadPending, fb(), INSTR_TIME_ACCUM_DIFF, INSTR_TIME_GET_NANOSEC, INSTR_TIME_SET_CURRENT, INSTR_TIME_SET_ZERO, InterruptPending, IsParallelWorker, IsUnderPostmaster, PARALLEL_VACUUM_DELAY_REPORT_INTERVAL_NS, parallel_vacuum_propagate_shared_delay_params(), parallel_vacuum_update_shared_delay_params(), parallel_vacuum_worker_delay_ns, pg_usleep(), PGC_SIGHUP, pgstat_progress_incr_param(), pgstat_progress_parallel_incr_param(), pgstat_report_wait_end(), pgstat_report_wait_start(), PostmasterIsAlive, ProcessConfigFile(), PROGRESS_ANALYZE_DELAY_TIME, PROGRESS_VACUUM_DELAY_TIME, track_cost_delay_timing, vacuum_cost_delay, vacuum_cost_limit, VacuumCostActive, VacuumCostBalance, VacuumSharedCostBalance, and VacuumUpdateCosts().

Referenced by acquire_sample_rows(), blbulkdelete(), blvacuumcleanup(), btvacuumpage(), btvacuumscan(), compute_array_stats(), compute_distinct_stats(), compute_index_stats(), compute_range_stats(), compute_scalar_stats(), compute_trivial_stats(), compute_tsvector_stats(), file_acquire_sample_rows(), ginbulkdelete(), ginInsertCleanup(), ginvacuumcleanup(), gistvacuumpage(), gistvacuumscan(), hashbucketcleanup(), lazy_scan_heap(), lazy_vacuum_heap_rel(), ProcessSingleRelationFork(), spgprocesspending(), and spgvacuumscan().

◆ vacuum_get_cutoffs()

bool vacuum_get_cutoffs ( Relation  rel,
const VacuumParams params,
struct VacuumCutoffs cutoffs 
)
extern

Definition at line 1101 of file vacuum.c.

1103{
1104 int freeze_min_age,
1105 multixact_freeze_min_age,
1106 freeze_table_age,
1107 multixact_freeze_table_age,
1115
1116 /* Use mutable copies of freeze age parameters */
1117 freeze_min_age = params->freeze_min_age;
1118 multixact_freeze_min_age = params->multixact_freeze_min_age;
1119 freeze_table_age = params->freeze_table_age;
1120 multixact_freeze_table_age = params->multixact_freeze_table_age;
1121
1122 /* Set pg_class fields in cutoffs */
1123 cutoffs->relfrozenxid = rel->rd_rel->relfrozenxid;
1124 cutoffs->relminmxid = rel->rd_rel->relminmxid;
1125
1126 /*
1127 * Acquire OldestXmin.
1128 *
1129 * We can always ignore processes running lazy vacuum. This is because we
1130 * use these values only for deciding which tuples we must keep in the
1131 * tables. Since lazy vacuum doesn't write its XID anywhere (usually no
1132 * XID assigned), it's safe to ignore it. In theory it could be
1133 * problematic to ignore lazy vacuums in a full vacuum, but keep in mind
1134 * that only one vacuum process can be working on a particular table at
1135 * any time, and that each vacuum is always an independent transaction.
1136 */
1138
1140
1141 /* Acquire OldestMxact */
1142 cutoffs->OldestMxact = GetOldestMultiXactId();
1144
1145 /* Acquire next XID/next MXID values used to apply age-based settings */
1148
1149 /*
1150 * Also compute the multixact age for which freezing is urgent. This is
1151 * normally autovacuum_multixact_freeze_max_age, but may be less if
1152 * multixact members are bloated.
1153 */
1155
1156 /*
1157 * Almost ready to set freeze output parameters; check if OldestXmin or
1158 * OldestMxact are held back to an unsafe degree before we start on that
1159 */
1168 (errmsg("cutoff for removing and freezing tuples is far in the past"),
1169 errhint("Close open transactions soon to avoid wraparound problems.\n"
1170 "You might also need to commit or roll back old prepared transactions, or drop stale replication slots.")));
1173 (errmsg("cutoff for freezing multixacts is far in the past"),
1174 errhint("Close open transactions soon to avoid wraparound problems.\n"
1175 "You might also need to commit or roll back old prepared transactions, or drop stale replication slots.")));
1176
1177 /*
1178 * Determine the minimum freeze age to use: as specified by the caller, or
1179 * vacuum_freeze_min_age, but in any case not more than half
1180 * autovacuum_freeze_max_age, so that autovacuums to prevent XID
1181 * wraparound won't occur too frequently.
1182 */
1183 if (freeze_min_age < 0)
1184 freeze_min_age = vacuum_freeze_min_age;
1185 freeze_min_age = Min(freeze_min_age, autovacuum_freeze_max_age / 2);
1186 Assert(freeze_min_age >= 0);
1187
1188 /* Compute FreezeLimit, being careful to generate a normal XID */
1189 cutoffs->FreezeLimit = nextXID - freeze_min_age;
1190 if (!TransactionIdIsNormal(cutoffs->FreezeLimit))
1192 /* FreezeLimit must always be <= OldestXmin */
1193 if (TransactionIdPrecedes(cutoffs->OldestXmin, cutoffs->FreezeLimit))
1194 cutoffs->FreezeLimit = cutoffs->OldestXmin;
1195
1196 /*
1197 * Determine the minimum multixact freeze age to use: as specified by
1198 * caller, or vacuum_multixact_freeze_min_age, but in any case not more
1199 * than half effective_multixact_freeze_max_age, so that autovacuums to
1200 * prevent MultiXact wraparound won't occur too frequently.
1201 */
1202 if (multixact_freeze_min_age < 0)
1203 multixact_freeze_min_age = vacuum_multixact_freeze_min_age;
1204 multixact_freeze_min_age = Min(multixact_freeze_min_age,
1206 Assert(multixact_freeze_min_age >= 0);
1207
1208 /* Compute MultiXactCutoff, being careful to generate a valid value */
1209 cutoffs->MultiXactCutoff = nextMXID - multixact_freeze_min_age;
1210 if (cutoffs->MultiXactCutoff < FirstMultiXactId)
1212 /* MultiXactCutoff must always be <= OldestMxact */
1213 if (MultiXactIdPrecedes(cutoffs->OldestMxact, cutoffs->MultiXactCutoff))
1214 cutoffs->MultiXactCutoff = cutoffs->OldestMxact;
1215
1216 /*
1217 * Finally, figure out if caller needs to do an aggressive VACUUM or not.
1218 *
1219 * Determine the table freeze age to use: as specified by the caller, or
1220 * the value of the vacuum_freeze_table_age GUC, but in any case not more
1221 * than autovacuum_freeze_max_age * 0.95, so that if you have e.g nightly
1222 * VACUUM schedule, the nightly VACUUM gets a chance to freeze XIDs before
1223 * anti-wraparound autovacuum is launched.
1224 */
1225 if (freeze_table_age < 0)
1226 freeze_table_age = vacuum_freeze_table_age;
1227 freeze_table_age = Min(freeze_table_age, autovacuum_freeze_max_age * 0.95);
1228 Assert(freeze_table_age >= 0);
1229 aggressiveXIDCutoff = nextXID - freeze_table_age;
1234 return true;
1235
1236 /*
1237 * Similar to the above, determine the table freeze age to use for
1238 * multixacts: as specified by the caller, or the value of the
1239 * vacuum_multixact_freeze_table_age GUC, but in any case not more than
1240 * effective_multixact_freeze_max_age * 0.95, so that if you have e.g.
1241 * nightly VACUUM schedule, the nightly VACUUM gets a chance to freeze
1242 * multixacts before anti-wraparound autovacuum is launched.
1243 */
1244 if (multixact_freeze_table_age < 0)
1245 multixact_freeze_table_age = vacuum_multixact_freeze_table_age;
1246 multixact_freeze_table_age =
1247 Min(multixact_freeze_table_age,
1249 Assert(multixact_freeze_table_age >= 0);
1250 aggressiveMXIDCutoff = nextMXID - multixact_freeze_table_age;
1255 return true;
1256
1257 /* Non-aggressive VACUUM */
1258 return false;
1259}
int autovacuum_freeze_max_age
Definition autovacuum.c:135
int errhint(const char *fmt,...) pg_attribute_printf(1
bool MultiXactIdPrecedesOrEquals(MultiXactId multi1, MultiXactId multi2)
Definition multixact.c:2879
int MultiXactMemberFreezeThreshold(void)
Definition multixact.c:2590
#define FirstMultiXactId
Definition multixact.h:26
TransactionId FreezeLimit
Definition vacuum.h:288
TransactionId OldestXmin
Definition vacuum.h:278
TransactionId relfrozenxid
Definition vacuum.h:262
MultiXactId relminmxid
Definition vacuum.h:263
MultiXactId MultiXactCutoff
Definition vacuum.h:289
MultiXactId OldestMxact
Definition vacuum.h:279
static bool TransactionIdPrecedesOrEquals(TransactionId id1, TransactionId id2)
Definition transam.h:282
#define FirstNormalTransactionId
Definition transam.h:34
int vacuum_freeze_min_age
Definition vacuum.c:76
int vacuum_multixact_freeze_table_age
Definition vacuum.c:79
int vacuum_freeze_table_age
Definition vacuum.c:77
int vacuum_multixact_freeze_min_age
Definition vacuum.c:78

References Assert, autovacuum_freeze_max_age, ereport, errhint(), errmsg, fb(), FirstMultiXactId, FirstNormalTransactionId, VacuumParams::freeze_min_age, VacuumParams::freeze_table_age, VacuumCutoffs::FreezeLimit, GetOldestMultiXactId(), GetOldestNonRemovableTransactionId(), Min, VacuumParams::multixact_freeze_min_age, VacuumParams::multixact_freeze_table_age, VacuumCutoffs::MultiXactCutoff, MultiXactIdIsValid, MultiXactIdPrecedes(), MultiXactIdPrecedesOrEquals(), MultiXactMemberFreezeThreshold(), VacuumCutoffs::OldestMxact, VacuumCutoffs::OldestXmin, RelationData::rd_rel, ReadNextMultiXactId(), ReadNextTransactionId(), VacuumCutoffs::relfrozenxid, VacuumCutoffs::relminmxid, TransactionIdIsNormal, TransactionIdPrecedes(), TransactionIdPrecedesOrEquals(), vacuum_freeze_min_age, vacuum_freeze_table_age, vacuum_multixact_freeze_min_age, vacuum_multixact_freeze_table_age, and WARNING.

Referenced by copy_table_data(), and heap_vacuum_rel().

◆ vacuum_is_permitted_for_relation()

bool vacuum_is_permitted_for_relation ( Oid  relid,
Form_pg_class  reltuple,
uint32  options 
)
extern

Definition at line 720 of file vacuum.c.

722{
723 char *relname;
724
726
727 /*----------
728 * A role has privileges to vacuum or analyze the relation if any of the
729 * following are true:
730 * - the role owns the current database and the relation is not shared
731 * - the role has the MAINTAIN privilege on the relation
732 *----------
733 */
735 !reltuple->relisshared) ||
737 return true;
738
739 relname = NameStr(reltuple->relname);
740
741 if ((options & VACOPT_VACUUM) != 0)
742 {
744 (errmsg("permission denied to vacuum \"%s\", skipping it",
745 relname)));
746
747 /*
748 * For VACUUM ANALYZE, both logs could show up, but just generate
749 * information for VACUUM as that would be the first one to be
750 * processed.
751 */
752 return false;
753 }
754
755 if ((options & VACOPT_ANALYZE) != 0)
757 (errmsg("permission denied to analyze \"%s\", skipping it",
758 relname)));
759
760 return false;
761}
@ ACLCHECK_OK
Definition acl.h:184
bool object_ownercheck(Oid classid, Oid objectid, Oid roleid)
Definition aclchk.c:4133
AclResult pg_class_aclcheck(Oid table_oid, Oid roleid, AclMode mode)
Definition aclchk.c:4082
#define NameStr(name)
Definition c.h:835
Oid GetUserId(void)
Definition miscinit.c:470
#define ACL_MAINTAIN
Definition parsenodes.h:90
NameData relname
Definition pg_class.h:40

References ACL_MAINTAIN, ACLCHECK_OK, Assert, ereport, errmsg, fb(), GetUserId(), MyDatabaseId, NameStr, object_ownercheck(), pg_class_aclcheck(), relname, VACOPT_ANALYZE, VACOPT_VACUUM, and WARNING.

Referenced by analyze_rel(), expand_vacuum_rel(), get_all_vacuum_rels(), and vacuum_rel().

◆ vacuum_open_relation()

Relation vacuum_open_relation ( Oid  relid,
RangeVar relation,
uint32  options,
bool  verbose,
LOCKMODE  lmode 
)
extern

Definition at line 772 of file vacuum.c.

774{
775 Relation rel;
776 bool rel_lock = true;
777 int elevel;
778
780
781 /*
782 * Open the relation and get the appropriate lock on it.
783 *
784 * There's a race condition here: the relation may have gone away since
785 * the last time we saw it. If so, we don't need to vacuum or analyze it.
786 *
787 * If we've been asked not to wait for the relation lock, acquire it first
788 * in non-blocking mode, before calling try_relation_open().
789 */
791 rel = try_relation_open(relid, lmode);
792 else if (ConditionalLockRelationOid(relid, lmode))
793 rel = try_relation_open(relid, NoLock);
794 else
795 {
796 rel = NULL;
797 rel_lock = false;
798 }
799
800 /* if relation is opened, leave */
801 if (rel)
802 return rel;
803
804 /*
805 * Relation could not be opened, hence generate if possible a log
806 * informing on the situation.
807 *
808 * If the RangeVar is not defined, we do not have enough information to
809 * provide a meaningful log statement. Chances are that the caller has
810 * intentionally not provided this information so that this logging is
811 * skipped, anyway.
812 */
813 if (relation == NULL)
814 return NULL;
815
816 /*
817 * Determine the log level.
818 *
819 * For manual VACUUM or ANALYZE, we emit a WARNING to match the log
820 * statements in the permission checks; otherwise, only log if the caller
821 * so requested.
822 */
824 elevel = WARNING;
825 else if (verbose)
826 elevel = LOG;
827 else
828 return NULL;
829
830 if ((options & VACOPT_VACUUM) != 0)
831 {
832 if (!rel_lock)
833 ereport(elevel,
835 errmsg("skipping vacuum of \"%s\" --- lock not available",
836 relation->relname)));
837 else
838 ereport(elevel,
840 errmsg("skipping vacuum of \"%s\" --- relation no longer exists",
841 relation->relname)));
842
843 /*
844 * For VACUUM ANALYZE, both logs could show up, but just generate
845 * information for VACUUM as that would be the first one to be
846 * processed.
847 */
848 return NULL;
849 }
850
851 if ((options & VACOPT_ANALYZE) != 0)
852 {
853 if (!rel_lock)
854 ereport(elevel,
856 errmsg("skipping analyze of \"%s\" --- lock not available",
857 relation->relname)));
858 else
859 ereport(elevel,
861 errmsg("skipping analyze of \"%s\" --- relation no longer exists",
862 relation->relname)));
863 }
864
865 return NULL;
866}
#define LOG
Definition elog.h:32
bool ConditionalLockRelationOid(Oid relid, LOCKMODE lockmode)
Definition lmgr.c:151
#define ERRCODE_UNDEFINED_TABLE
Definition pgbench.c:79
Relation try_relation_open(Oid relationId, LOCKMODE lockmode)
Definition relation.c:89
char * relname
Definition primnodes.h:84

References AmAutoVacuumWorkerProcess, Assert, ConditionalLockRelationOid(), ereport, errcode(), ERRCODE_UNDEFINED_TABLE, errmsg, fb(), LOG, NoLock, RangeVar::relname, try_relation_open(), VACOPT_ANALYZE, VACOPT_SKIP_LOCKED, VACOPT_VACUUM, verbose, and WARNING.

Referenced by analyze_rel(), and vacuum_rel().

◆ vacuum_xid_failsafe_check()

bool vacuum_xid_failsafe_check ( const struct VacuumCutoffs cutoffs)
extern

Definition at line 1269 of file vacuum.c.

1270{
1271 TransactionId relfrozenxid = cutoffs->relfrozenxid;
1272 MultiXactId relminmxid = cutoffs->relminmxid;
1276
1277 Assert(TransactionIdIsNormal(relfrozenxid));
1278 Assert(MultiXactIdIsValid(relminmxid));
1279
1280 /*
1281 * Determine the index skipping age to use. In any case no less than
1282 * autovacuum_freeze_max_age * 1.05.
1283 */
1285
1289
1290 if (TransactionIdPrecedes(relfrozenxid, xid_skip_limit))
1291 {
1292 /* The table's relfrozenxid is too old */
1293 return true;
1294 }
1295
1296 /*
1297 * Similar to above, determine the index skipping age to use for
1298 * multixact. In any case no less than autovacuum_multixact_freeze_max_age *
1299 * 1.05.
1300 */
1303
1307
1308 if (MultiXactIdPrecedes(relminmxid, multi_skip_limit))
1309 {
1310 /* The table's relminmxid is too old */
1311 return true;
1312 }
1313
1314 return false;
1315}
int autovacuum_multixact_freeze_max_age
Definition autovacuum.c:136
int vacuum_multixact_failsafe_age
Definition vacuum.c:81
int vacuum_failsafe_age
Definition vacuum.c:80

References Assert, autovacuum_freeze_max_age, autovacuum_multixact_freeze_max_age, fb(), FirstMultiXactId, FirstNormalTransactionId, Max, MultiXactIdIsValid, MultiXactIdPrecedes(), ReadNextMultiXactId(), ReadNextTransactionId(), VacuumCutoffs::relfrozenxid, VacuumCutoffs::relminmxid, TransactionIdIsNormal, TransactionIdPrecedes(), vacuum_failsafe_age, and vacuum_multixact_failsafe_age.

Referenced by lazy_check_wraparound_failsafe().

◆ VacuumUpdateCosts()

void VacuumUpdateCosts ( void  )
extern

Definition at line 1682 of file autovacuum.c.

1683{
1684 if (MyWorkerInfo)
1685 {
1688 else if (autovacuum_vac_cost_delay >= 0)
1690 else
1691 /* fall back to VacuumCostDelay */
1693
1695 }
1696 else
1697 {
1698 /* Must be explicit VACUUM or ANALYZE or parallel autovacuum worker */
1701 }
1702
1703 /*
1704 * If configuration changes are allowed to impact VacuumCostActive, make
1705 * sure it is updated.
1706 */
1709 else if (vacuum_cost_delay > 0)
1710 VacuumCostActive = true;
1711 else
1712 {
1713 VacuumCostActive = false;
1715 }
1716
1717 /*
1718 * Since the cost logging requires a lock, avoid rendering the log message
1719 * in case we are using a message level where the log wouldn't be emitted.
1720 */
1722 {
1723 Oid dboid,
1724 tableoid;
1725
1727
1729 dboid = MyWorkerInfo->wi_dboid;
1730 tableoid = MyWorkerInfo->wi_tableoid;
1732
1733 elog(DEBUG2,
1734 "Autovacuum VacuumUpdateCosts(db=%u, rel=%u, dobalance=%s, cost_limit=%d, cost_delay=%g active=%s failsafe=%s)",
1735 dboid, tableoid, pg_atomic_unlocked_test_flag(&MyWorkerInfo->wi_dobalance) ? "no" : "yes",
1737 vacuum_cost_delay > 0 ? "yes" : "no",
1738 VacuumFailsafeActive ? "yes" : "no");
1739 }
1740}
static double av_storage_param_cost_delay
Definition autovacuum.c:161
double autovacuum_vac_cost_delay
Definition autovacuum.c:142
bool message_level_is_interesting(int elevel)
Definition elog.c:285
bool LWLockHeldByMe(LWLock *lock)
Definition lwlock.c:1885
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition lwlock.c:1150
void LWLockRelease(LWLock *lock)
Definition lwlock.c:1767
@ LW_SHARED
Definition lwlock.h:105

References Assert, autovacuum_vac_cost_delay, AutoVacuumUpdateCostLimit(), av_storage_param_cost_delay, DEBUG2, elog, fb(), LW_SHARED, LWLockAcquire(), LWLockHeldByMe(), LWLockRelease(), message_level_is_interesting(), MyWorkerInfo, pg_atomic_unlocked_test_flag(), vacuum_cost_delay, vacuum_cost_limit, VacuumCostActive, VacuumCostBalance, VacuumCostDelay, VacuumCostLimit, VacuumFailsafeActive, WorkerInfoData::wi_dboid, WorkerInfoData::wi_dobalance, and WorkerInfoData::wi_tableoid.

Referenced by do_autovacuum(), parallel_vacuum_main(), parallel_vacuum_update_shared_delay_params(), vacuum(), and vacuum_delay_point().

Variable Documentation

◆ default_statistics_target

◆ parallel_vacuum_worker_delay_ns

PGDLLIMPORT int64 parallel_vacuum_worker_delay_ns
extern

Definition at line 96 of file vacuum.c.

Referenced by parallel_vacuum_main(), and vacuum_delay_point().

◆ track_cost_delay_timing

PGDLLIMPORT bool track_cost_delay_timing
extern

Definition at line 83 of file vacuum.c.

Referenced by do_analyze_rel(), heap_vacuum_rel(), parallel_vacuum_main(), and vacuum_delay_point().

◆ vacuum_cost_delay

◆ vacuum_cost_limit

◆ vacuum_failsafe_age

PGDLLIMPORT int vacuum_failsafe_age
extern

Definition at line 80 of file vacuum.c.

Referenced by relation_needs_vacanalyze(), and vacuum_xid_failsafe_check().

◆ vacuum_freeze_min_age

PGDLLIMPORT int vacuum_freeze_min_age
extern

Definition at line 76 of file vacuum.c.

Referenced by do_autovacuum(), and vacuum_get_cutoffs().

◆ vacuum_freeze_table_age

PGDLLIMPORT int vacuum_freeze_table_age
extern

Definition at line 77 of file vacuum.c.

Referenced by do_autovacuum(), and vacuum_get_cutoffs().

◆ vacuum_max_eager_freeze_failure_rate

PGDLLIMPORT double vacuum_max_eager_freeze_failure_rate
extern

Definition at line 82 of file vacuum.c.

Referenced by default_reloptions(), ExecVacuum(), and table_recheck_autovac().

◆ vacuum_multixact_failsafe_age

PGDLLIMPORT int vacuum_multixact_failsafe_age
extern

Definition at line 81 of file vacuum.c.

Referenced by relation_needs_vacanalyze(), and vacuum_xid_failsafe_check().

◆ vacuum_multixact_freeze_min_age

PGDLLIMPORT int vacuum_multixact_freeze_min_age
extern

Definition at line 78 of file vacuum.c.

Referenced by do_autovacuum(), and vacuum_get_cutoffs().

◆ vacuum_multixact_freeze_table_age

PGDLLIMPORT int vacuum_multixact_freeze_table_age
extern

Definition at line 79 of file vacuum.c.

Referenced by do_autovacuum(), and vacuum_get_cutoffs().

◆ vacuum_truncate

PGDLLIMPORT bool vacuum_truncate
extern

Definition at line 84 of file vacuum.c.

Referenced by default_reloptions(), and vacuum_rel().

◆ VacuumActiveNWorkers

◆ VacuumCostBalanceLocal

PGDLLIMPORT int VacuumCostBalanceLocal
extern

◆ VacuumFailsafeActive

◆ VacuumSharedCostBalance