codekingpro/portable-devtools
114k
1/*-------------------------------------------------------------------------2 *3 * vacuum.h4 * header file for postgres vacuum cleaner and statistics analyzer5 *6 *7 * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group8 * Portions Copyright (c) 1994, Regents of the University of California9 *10 * src/include/commands/vacuum.h11 *12 *-------------------------------------------------------------------------13 */14#ifndef VACUUM_H15#define VACUUM_H16 17#include "access/htup.h"18#include "access/genam.h"19#include "access/parallel.h"20#include "catalog/pg_class.h"21#include "catalog/pg_statistic.h"22#include "catalog/pg_type.h"23#include "parser/parse_node.h"24#include "storage/buf.h"25#include "storage/lock.h"26#include "utils/relcache.h"27 28/*29 * Flags for amparallelvacuumoptions to control the participation of bulkdelete30 * and vacuumcleanup in parallel vacuum.31 */32 33/*34 * Both bulkdelete and vacuumcleanup are disabled by default. This will be35 * used by IndexAM's that don't want to or cannot participate in parallel36 * vacuum. For example, if an index AM doesn't have a way to communicate the37 * index statistics allocated by the first ambulkdelete call to the subsequent38 * ones until amvacuumcleanup, the index AM cannot participate in parallel39 * vacuum.40 */41#define VACUUM_OPTION_NO_PARALLEL 042 43/*44 * bulkdelete can be performed in parallel. This option can be used by45 * index AMs that need to scan indexes to delete tuples.46 */47#define VACUUM_OPTION_PARALLEL_BULKDEL (1 << 0)48 49/*50 * vacuumcleanup can be performed in parallel if bulkdelete is not performed51 * yet. This will be used by IndexAM's that can scan the index if the52 * bulkdelete is not performed.53 */54#define VACUUM_OPTION_PARALLEL_COND_CLEANUP (1 << 1)55 56/*57 * vacuumcleanup can be performed in parallel even if bulkdelete has already58 * processed the index. This will be used by IndexAM's that scan the index59 * during the cleanup phase of index irrespective of whether the index is60 * already scanned or not during bulkdelete phase.61 */62#define VACUUM_OPTION_PARALLEL_CLEANUP (1 << 2)63 64/* value for checking vacuum flags */65#define VACUUM_OPTION_MAX_VALID_VALUE ((1 << 3) - 1)66 67/* Abstract type for parallel vacuum state */68typedef struct ParallelVacuumState ParallelVacuumState;69 70/*----------71 * ANALYZE builds one of these structs for each attribute (column) that is72 * to be analyzed. The struct and subsidiary data are in anl_context,73 * so they live until the end of the ANALYZE operation.74 *75 * The type-specific typanalyze function is passed a pointer to this struct76 * and must return true to continue analysis, false to skip analysis of this77 * column. In the true case it must set the compute_stats and minrows fields,78 * and can optionally set extra_data to pass additional info to compute_stats.79 * minrows is its request for the minimum number of sample rows to be gathered80 * (but note this request might not be honored, eg if there are fewer rows81 * than that in the table).82 *83 * The compute_stats routine will be called after sample rows have been84 * gathered. Aside from this struct, it is passed:85 * fetchfunc: a function for accessing the column values from the86 * sample rows87 * samplerows: the number of sample tuples88 * totalrows: estimated total number of rows in relation89 * The fetchfunc may be called with rownum running from 0 to samplerows-1.90 * It returns a Datum and an isNull flag.91 *92 * compute_stats should set stats_valid true if it is able to compute93 * any useful statistics. If it does, the remainder of the struct holds94 * the information to be stored in a pg_statistic row for the column. Be95 * careful to allocate any pointed-to data in anl_context, which will NOT96 * be CurrentMemoryContext when compute_stats is called.97 *98 * Note: all comparisons done for statistical purposes should use the99 * underlying column's collation (attcollation), except in situations100 * where a noncollatable container type contains a collatable type;101 * in that case use the type's default collation. Be sure to record102 * the appropriate collation in stacoll.103 *----------104 */105typedef struct VacAttrStats *VacAttrStatsP;106 107typedef Datum (*AnalyzeAttrFetchFunc) (VacAttrStatsP stats, int rownum,108 bool *isNull);109 110typedef void (*AnalyzeAttrComputeStatsFunc) (VacAttrStatsP stats,111 AnalyzeAttrFetchFunc fetchfunc,112 int samplerows,113 double totalrows);114 115typedef struct VacAttrStats116{117 /*118 * These fields are set up by the main ANALYZE code before invoking the119 * type-specific typanalyze function.120 *121 * Note: do not assume that the data being analyzed has the same datatype122 * shown in attr, ie do not trust attr->atttypid, attlen, etc. This is123 * because some index opclasses store a different type than the underlying124 * column/expression. Instead use attrtypid, attrtypmod, and attrtype for125 * information about the datatype being fed to the typanalyze function.126 * Likewise, use attrcollid not attr->attcollation.127 */128 Form_pg_attribute attr; /* copy of pg_attribute row for column */129 Oid attrtypid; /* type of data being analyzed */130 int32 attrtypmod; /* typmod of data being analyzed */131 Form_pg_type attrtype; /* copy of pg_type row for attrtypid */132 Oid attrcollid; /* collation of data being analyzed */133 MemoryContext anl_context; /* where to save long-lived data */134 135 /*136 * These fields must be filled in by the typanalyze routine, unless it137 * returns false.138 */139 AnalyzeAttrComputeStatsFunc compute_stats; /* function pointer */140 int minrows; /* Minimum # of rows wanted for stats */141 void *extra_data; /* for extra type-specific data */142 143 /*144 * These fields are to be filled in by the compute_stats routine. (They145 * are initialized to zero when the struct is created.)146 */147 bool stats_valid;148 float4 stanullfrac; /* fraction of entries that are NULL */149 int32 stawidth; /* average width of column values */150 float4 stadistinct; /* # distinct values */151 int16 stakind[STATISTIC_NUM_SLOTS];152 Oid staop[STATISTIC_NUM_SLOTS];153 Oid stacoll[STATISTIC_NUM_SLOTS];154 int numnumbers[STATISTIC_NUM_SLOTS];155 float4 *stanumbers[STATISTIC_NUM_SLOTS];156 int numvalues[STATISTIC_NUM_SLOTS];157 Datum *stavalues[STATISTIC_NUM_SLOTS];158 159 /*160 * These fields describe the stavalues[n] element types. They will be161 * initialized to match attrtypid, but a custom typanalyze function might162 * want to store an array of something other than the analyzed column's163 * elements. It should then overwrite these fields.164 */165 Oid statypid[STATISTIC_NUM_SLOTS];166 int16 statyplen[STATISTIC_NUM_SLOTS];167 bool statypbyval[STATISTIC_NUM_SLOTS];168 char statypalign[STATISTIC_NUM_SLOTS];169 170 /*171 * These fields are private to the main ANALYZE code and should not be172 * looked at by type-specific functions.173 */174 int tupattnum; /* attribute number within tuples */175 HeapTuple *rows; /* access info for std fetch function */176 TupleDesc tupDesc;177 Datum *exprvals; /* access info for index fetch function */178 bool *exprnulls;179 int rowstride;180} VacAttrStats;181 182/* flag bits for VacuumParams->options */183#define VACOPT_VACUUM 0x01 /* do VACUUM */184#define VACOPT_ANALYZE 0x02 /* do ANALYZE */185#define VACOPT_VERBOSE 0x04 /* output INFO instrumentation messages */186#define VACOPT_FREEZE 0x08 /* FREEZE option */187#define VACOPT_FULL 0x10 /* FULL (non-concurrent) vacuum */188#define VACOPT_SKIP_LOCKED 0x20 /* skip if cannot get lock */189#define VACOPT_PROCESS_MAIN 0x40 /* process main relation */190#define VACOPT_PROCESS_TOAST 0x80 /* process the TOAST table, if any */191#define VACOPT_DISABLE_PAGE_SKIPPING 0x100 /* don't skip any pages */192#define VACOPT_SKIP_DATABASE_STATS 0x200 /* skip vac_update_datfrozenxid() */193#define VACOPT_ONLY_DATABASE_STATS 0x400 /* only vac_update_datfrozenxid() */194 195/*196 * Values used by index_cleanup and truncate params.197 *198 * VACOPTVALUE_UNSPECIFIED is used as an initial placeholder when VACUUM199 * command has no explicit value. When that happens the final usable value200 * comes from the corresponding reloption (though the reloption default is201 * usually used).202 */203typedef enum VacOptValue204{205 VACOPTVALUE_UNSPECIFIED = 0,206 VACOPTVALUE_AUTO,207 VACOPTVALUE_DISABLED,208 VACOPTVALUE_ENABLED,209} VacOptValue;210 211/*212 * Parameters customizing behavior of VACUUM and ANALYZE.213 *214 * Note that at least one of VACOPT_VACUUM and VACOPT_ANALYZE must be set215 * in options.216 *217 * When adding a new VacuumParam member, consider adding it to vacuumdb as218 * well.219 */220typedef struct VacuumParams221{222 bits32 options; /* bitmask of VACOPT_* */223 int freeze_min_age; /* min freeze age, -1 to use default */224 int freeze_table_age; /* age at which to scan whole table */225 int multixact_freeze_min_age; /* min multixact freeze age, -1 to226 * use default */227 int multixact_freeze_table_age; /* multixact age at which to scan228 * whole table */229 bool is_wraparound; /* force a for-wraparound vacuum */230 int log_min_duration; /* minimum execution threshold in ms at231 * which autovacuum is logged, -1 to use232 * default */233 VacOptValue index_cleanup; /* Do index vacuum and cleanup */234 VacOptValue truncate; /* Truncate empty pages at the end */235 236 /*237 * The number of parallel vacuum workers. 0 by default which means choose238 * based on the number of indexes. -1 indicates parallel vacuum is239 * disabled.240 */241 int nworkers;242} VacuumParams;243 244/*245 * VacuumCutoffs is immutable state that describes the cutoffs used by VACUUM.246 * Established at the beginning of each VACUUM operation.247 */248struct VacuumCutoffs249{250 /*251 * Existing pg_class fields at start of VACUUM252 */253 TransactionId relfrozenxid;254 MultiXactId relminmxid;255 256 /*257 * OldestXmin is the Xid below which tuples deleted by any xact (that258 * committed) should be considered DEAD, not just RECENTLY_DEAD.259 *260 * OldestMxact is the Mxid below which MultiXacts are definitely not seen261 * as visible by any running transaction.262 *263 * OldestXmin and OldestMxact are also the most recent values that can264 * ever be passed to vac_update_relstats() as frozenxid and minmulti265 * arguments at the end of VACUUM. These same values should be passed266 * when it turns out that VACUUM will leave no unfrozen XIDs/MXIDs behind267 * in the table.268 */269 TransactionId OldestXmin;270 MultiXactId OldestMxact;271 272 /*273 * FreezeLimit is the Xid below which all Xids are definitely frozen or274 * removed in pages VACUUM scans and cleanup locks.275 *276 * MultiXactCutoff is the value below which all MultiXactIds are277 * definitely removed from Xmax in pages VACUUM scans and cleanup locks.278 */279 TransactionId FreezeLimit;280 MultiXactId MultiXactCutoff;281};282 283/*284 * VacDeadItems stores TIDs whose index tuples are deleted by index vacuuming.285 */286typedef struct VacDeadItems287{288 int max_items; /* # slots allocated in array */289 int num_items; /* current # of entries */290 291 /* Sorted array of TIDs to delete from indexes */292 ItemPointerData items[FLEXIBLE_ARRAY_MEMBER];293} VacDeadItems;294 295#define MAXDEADITEMS(avail_mem) \296 (((avail_mem) - offsetof(VacDeadItems, items)) / sizeof(ItemPointerData))297 298/* GUC parameters */299extern PGDLLIMPORT int default_statistics_target; /* PGDLLIMPORT for PostGIS */300extern PGDLLIMPORT int vacuum_freeze_min_age;301extern PGDLLIMPORT int vacuum_freeze_table_age;302extern PGDLLIMPORT int vacuum_multixact_freeze_min_age;303extern PGDLLIMPORT int vacuum_multixact_freeze_table_age;304extern PGDLLIMPORT int vacuum_failsafe_age;305extern PGDLLIMPORT int vacuum_multixact_failsafe_age;306 307/* Variables for cost-based parallel vacuum */308extern PGDLLIMPORT pg_atomic_uint32 *VacuumSharedCostBalance;309extern PGDLLIMPORT pg_atomic_uint32 *VacuumActiveNWorkers;310extern PGDLLIMPORT int VacuumCostBalanceLocal;311 312extern PGDLLIMPORT bool VacuumFailsafeActive;313extern PGDLLIMPORT double vacuum_cost_delay;314extern PGDLLIMPORT int vacuum_cost_limit;315 316/* in commands/vacuum.c */317extern void ExecVacuum(ParseState *pstate, VacuumStmt *vacstmt, bool isTopLevel);318extern void vacuum(List *relations, VacuumParams *params,319 BufferAccessStrategy bstrategy, MemoryContext vac_context,320 bool isTopLevel);321extern void vac_open_indexes(Relation relation, LOCKMODE lockmode,322 int *nindexes, Relation **Irel);323extern void vac_close_indexes(int nindexes, Relation *Irel, LOCKMODE lockmode);324extern double vac_estimate_reltuples(Relation relation,325 BlockNumber total_pages,326 BlockNumber scanned_pages,327 double scanned_tuples);328extern void vac_update_relstats(Relation relation,329 BlockNumber num_pages,330 double num_tuples,331 BlockNumber num_all_visible_pages,332 bool hasindex,333 TransactionId frozenxid,334 MultiXactId minmulti,335 bool *frozenxid_updated,336 bool *minmulti_updated,337 bool in_outer_xact);338extern bool vacuum_get_cutoffs(Relation rel, const VacuumParams *params,339 struct VacuumCutoffs *cutoffs);340extern bool vacuum_xid_failsafe_check(const struct VacuumCutoffs *cutoffs);341extern void vac_update_datfrozenxid(void);342extern void vacuum_delay_point(void);343extern bool vacuum_is_relation_owner(Oid relid, Form_pg_class reltuple,344 bits32 options);345extern Relation vacuum_open_relation(Oid relid, RangeVar *relation,346 bits32 options, bool verbose,347 LOCKMODE lmode);348extern IndexBulkDeleteResult *vac_bulkdel_one_index(IndexVacuumInfo *ivinfo,349 IndexBulkDeleteResult *istat,350 VacDeadItems *dead_items);351extern IndexBulkDeleteResult *vac_cleanup_one_index(IndexVacuumInfo *ivinfo,352 IndexBulkDeleteResult *istat);353extern Size vac_max_items_to_alloc_size(int max_items);354 355/* In postmaster/autovacuum.c */356extern void AutoVacuumUpdateCostLimit(void);357extern void VacuumUpdateCosts(void);358 359/* in commands/vacuumparallel.c */360extern ParallelVacuumState *parallel_vacuum_init(Relation rel, Relation *indrels,361 int nindexes, int nrequested_workers,362 int max_items, int elevel,363 BufferAccessStrategy bstrategy);364extern void parallel_vacuum_end(ParallelVacuumState *pvs, IndexBulkDeleteResult **istats);365extern VacDeadItems *parallel_vacuum_get_dead_items(ParallelVacuumState *pvs);366extern void parallel_vacuum_bulkdel_all_indexes(ParallelVacuumState *pvs,367 long num_table_tuples,368 int num_index_scans);369extern void parallel_vacuum_cleanup_all_indexes(ParallelVacuumState *pvs,370 long num_table_tuples,371 int num_index_scans,372 bool estimated_count);373extern void parallel_vacuum_main(dsm_segment *seg, shm_toc *toc);374 375/* in commands/analyze.c */376extern void analyze_rel(Oid relid, RangeVar *relation,377 VacuumParams *params, List *va_cols, bool in_outer_xact,378 BufferAccessStrategy bstrategy);379extern bool std_typanalyze(VacAttrStats *stats);380 381/* in utils/misc/sampling.c --- duplicate of declarations in utils/sampling.h */382extern double anl_random_fract(void);383extern double anl_init_selection_state(int n);384extern double anl_get_next_S(double t, int n, double *stateptr);385 386#endif /* VACUUM_H */387 