PostgreSQL Source Code  git master
vacuum.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * vacuum.h
4  * header file for postgres vacuum cleaner and statistics analyzer
5  *
6  *
7  * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * src/include/commands/vacuum.h
11  *
12  *-------------------------------------------------------------------------
13  */
14 #ifndef VACUUM_H
15 #define VACUUM_H
16 
17 #include "access/htup.h"
18 #include "catalog/pg_class.h"
19 #include "catalog/pg_statistic.h"
20 #include "catalog/pg_type.h"
21 #include "parser/parse_node.h"
22 #include "storage/buf.h"
23 #include "storage/lock.h"
24 #include "utils/relcache.h"
25 
26 /*
27  * Flags for amparallelvacuumoptions to control the participation of bulkdelete
28  * and vacuumcleanup in parallel vacuum.
29  */
30 
31 /*
32  * Both bulkdelete and vacuumcleanup are disabled by default. This will be
33  * used by IndexAM's that don't want to or cannot participate in parallel
34  * vacuum. For example, if an index AM doesn't have a way to communicate the
35  * index statistics allocated by the first ambulkdelete call to the subsequent
36  * ones until amvacuumcleanup, the index AM cannot participate in parallel
37  * vacuum.
38  */
39 #define VACUUM_OPTION_NO_PARALLEL 0
40 
41 /*
42  * bulkdelete can be performed in parallel. This option can be used by
43  * IndexAm's that need to scan the index to delete the tuples.
44  */
45 #define VACUUM_OPTION_PARALLEL_BULKDEL (1 << 0)
46 
47 /*
48  * vacuumcleanup can be performed in parallel if bulkdelete is not performed
49  * yet. This will be used by IndexAM's that can scan the index if the
50  * bulkdelete is not performed.
51  */
52 #define VACUUM_OPTION_PARALLEL_COND_CLEANUP (1 << 1)
53 
54 /*
55  * vacuumcleanup can be performed in parallel even if bulkdelete has already
56  * processed the index. This will be used by IndexAM's that scan the index
57  * during the cleanup phase of index irrespective of whether the index is
58  * already scanned or not during bulkdelete phase.
59  */
60 #define VACUUM_OPTION_PARALLEL_CLEANUP (1 << 2)
61 
62 /* value for checking vacuum flags */
63 #define VACUUM_OPTION_MAX_VALID_VALUE ((1 << 3) - 1)
64 
65 /*----------
66  * ANALYZE builds one of these structs for each attribute (column) that is
67  * to be analyzed. The struct and subsidiary data are in anl_context,
68  * so they live until the end of the ANALYZE operation.
69  *
70  * The type-specific typanalyze function is passed a pointer to this struct
71  * and must return true to continue analysis, false to skip analysis of this
72  * column. In the true case it must set the compute_stats and minrows fields,
73  * and can optionally set extra_data to pass additional info to compute_stats.
74  * minrows is its request for the minimum number of sample rows to be gathered
75  * (but note this request might not be honored, eg if there are fewer rows
76  * than that in the table).
77  *
78  * The compute_stats routine will be called after sample rows have been
79  * gathered. Aside from this struct, it is passed:
80  * fetchfunc: a function for accessing the column values from the
81  * sample rows
82  * samplerows: the number of sample tuples
83  * totalrows: estimated total number of rows in relation
84  * The fetchfunc may be called with rownum running from 0 to samplerows-1.
85  * It returns a Datum and an isNull flag.
86  *
87  * compute_stats should set stats_valid true if it is able to compute
88  * any useful statistics. If it does, the remainder of the struct holds
89  * the information to be stored in a pg_statistic row for the column. Be
90  * careful to allocate any pointed-to data in anl_context, which will NOT
91  * be CurrentMemoryContext when compute_stats is called.
92  *
93  * Note: all comparisons done for statistical purposes should use the
94  * underlying column's collation (attcollation), except in situations
95  * where a noncollatable container type contains a collatable type;
96  * in that case use the type's default collation. Be sure to record
97  * the appropriate collation in stacoll.
98  *----------
99  */
100 typedef struct VacAttrStats *VacAttrStatsP;
101 
102 typedef Datum (*AnalyzeAttrFetchFunc) (VacAttrStatsP stats, int rownum,
103  bool *isNull);
104 
105 typedef void (*AnalyzeAttrComputeStatsFunc) (VacAttrStatsP stats,
106  AnalyzeAttrFetchFunc fetchfunc,
107  int samplerows,
108  double totalrows);
109 
110 typedef struct VacAttrStats
111 {
112  /*
113  * These fields are set up by the main ANALYZE code before invoking the
114  * type-specific typanalyze function.
115  *
116  * Note: do not assume that the data being analyzed has the same datatype
117  * shown in attr, ie do not trust attr->atttypid, attlen, etc. This is
118  * because some index opclasses store a different type than the underlying
119  * column/expression. Instead use attrtypid, attrtypmod, and attrtype for
120  * information about the datatype being fed to the typanalyze function.
121  * Likewise, use attrcollid not attr->attcollation.
122  */
123  Form_pg_attribute attr; /* copy of pg_attribute row for column */
124  Oid attrtypid; /* type of data being analyzed */
125  int32 attrtypmod; /* typmod of data being analyzed */
126  Form_pg_type attrtype; /* copy of pg_type row for attrtypid */
127  Oid attrcollid; /* collation of data being analyzed */
128  MemoryContext anl_context; /* where to save long-lived data */
129 
130  /*
131  * These fields must be filled in by the typanalyze routine, unless it
132  * returns false.
133  */
135  int minrows; /* Minimum # of rows wanted for stats */
136  void *extra_data; /* for extra type-specific data */
137 
138  /*
139  * These fields are to be filled in by the compute_stats routine. (They
140  * are initialized to zero when the struct is created.)
141  */
143  float4 stanullfrac; /* fraction of entries that are NULL */
144  int32 stawidth; /* average width of column values */
145  float4 stadistinct; /* # distinct values */
153 
154  /*
155  * These fields describe the stavalues[n] element types. They will be
156  * initialized to match attrtypid, but a custom typanalyze function might
157  * want to store an array of something other than the analyzed column's
158  * elements. It should then overwrite these fields.
159  */
164 
165  /*
166  * These fields are private to the main ANALYZE code and should not be
167  * looked at by type-specific functions.
168  */
169  int tupattnum; /* attribute number within tuples */
170  HeapTuple *rows; /* access info for std fetch function */
172  Datum *exprvals; /* access info for index fetch function */
173  bool *exprnulls;
175 } VacAttrStats;
176 
177 typedef enum VacuumOption
178 {
179  VACOPT_VACUUM = 1 << 0, /* do VACUUM */
180  VACOPT_ANALYZE = 1 << 1, /* do ANALYZE */
181  VACOPT_VERBOSE = 1 << 2, /* print progress info */
182  VACOPT_FREEZE = 1 << 3, /* FREEZE option */
183  VACOPT_FULL = 1 << 4, /* FULL (non-concurrent) vacuum */
184  VACOPT_SKIP_LOCKED = 1 << 5, /* skip if cannot get lock */
185  VACOPT_SKIPTOAST = 1 << 6, /* don't process the TOAST table, if any */
186  VACOPT_DISABLE_PAGE_SKIPPING = 1 << 7 /* don't skip any pages */
187 } VacuumOption;
188 
189 /*
190  * A ternary value used by vacuum parameters.
191  *
192  * DEFAULT value is used to determine the value based on other
193  * configurations, e.g. reloptions.
194  */
195 typedef enum VacOptTernaryValue
196 {
201 
202 /*
203  * Parameters customizing behavior of VACUUM and ANALYZE.
204  *
205  * Note that at least one of VACOPT_VACUUM and VACOPT_ANALYZE must be set
206  * in options.
207  */
208 typedef struct VacuumParams
209 {
210  int options; /* bitmask of VacuumOption */
211  int freeze_min_age; /* min freeze age, -1 to use default */
212  int freeze_table_age; /* age at which to scan whole table */
213  int multixact_freeze_min_age; /* min multixact freeze age, -1 to
214  * use default */
215  int multixact_freeze_table_age; /* multixact age at which to scan
216  * whole table */
217  bool is_wraparound; /* force a for-wraparound vacuum */
218  int log_min_duration; /* minimum execution threshold in ms at
219  * which verbose logs are activated, -1
220  * to use default */
221  VacOptTernaryValue index_cleanup; /* Do index vacuum and cleanup,
222  * default value depends on reloptions */
223  VacOptTernaryValue truncate; /* Truncate empty pages at the end,
224  * default value depends on reloptions */
225 
226  /*
227  * The number of parallel vacuum workers. 0 by default which means choose
228  * based on the number of indexes. -1 indicates parallel vacuum is
229  * disabled.
230  */
231  int nworkers;
232 } VacuumParams;
233 
234 /* GUC parameters */
235 extern PGDLLIMPORT int default_statistics_target; /* PGDLLIMPORT for PostGIS */
236 extern int vacuum_freeze_min_age;
237 extern int vacuum_freeze_table_age;
240 
241 /* Variables for cost-based parallel vacuum */
244 extern int VacuumCostBalanceLocal;
245 
246 
247 /* in commands/vacuum.c */
248 extern void ExecVacuum(ParseState *pstate, VacuumStmt *vacstmt, bool isTopLevel);
249 extern void vacuum(List *relations, VacuumParams *params,
250  BufferAccessStrategy bstrategy, bool isTopLevel);
251 extern void vac_open_indexes(Relation relation, LOCKMODE lockmode,
252  int *nindexes, Relation **Irel);
253 extern void vac_close_indexes(int nindexes, Relation *Irel, LOCKMODE lockmode);
254 extern double vac_estimate_reltuples(Relation relation,
255  BlockNumber total_pages,
256  BlockNumber scanned_pages,
257  double scanned_tuples);
258 extern void vac_update_relstats(Relation relation,
259  BlockNumber num_pages,
260  double num_tuples,
261  BlockNumber num_all_visible_pages,
262  bool hasindex,
263  TransactionId frozenxid,
264  MultiXactId minmulti,
265  bool in_outer_xact);
266 extern void vacuum_set_xid_limits(Relation rel,
267  int freeze_min_age, int freeze_table_age,
268  int multixact_freeze_min_age,
269  int multixact_freeze_table_age,
270  TransactionId *oldestXmin,
271  TransactionId *freezeLimit,
272  TransactionId *xidFullScanLimit,
273  MultiXactId *multiXactCutoff,
274  MultiXactId *mxactFullScanLimit);
275 extern void vac_update_datfrozenxid(void);
276 extern void vacuum_delay_point(void);
277 extern bool vacuum_is_relation_owner(Oid relid, Form_pg_class reltuple,
278  int options);
279 extern Relation vacuum_open_relation(Oid relid, RangeVar *relation,
280  int options, bool verbose, LOCKMODE lmode);
281 
282 /* in commands/analyze.c */
283 extern void analyze_rel(Oid relid, RangeVar *relation,
284  VacuumParams *params, List *va_cols, bool in_outer_xact,
285  BufferAccessStrategy bstrategy);
286 extern bool std_typanalyze(VacAttrStats *stats);
287 
288 /* in utils/misc/sampling.c --- duplicate of declarations in utils/sampling.h */
289 extern double anl_random_fract(void);
290 extern double anl_init_selection_state(int n);
291 extern double anl_get_next_S(double t, int n, double *stateptr);
292 
293 #endif /* VACUUM_H */
int rowstride
Definition: vacuum.h:174
void vacuum(List *relations, VacuumParams *params, BufferAccessStrategy bstrategy, bool isTopLevel)
Definition: vacuum.c:272
signed short int16
Definition: c.h:354
int multixact_freeze_table_age
Definition: vacuum.h:215
int minrows
Definition: vacuum.h:135
void vac_open_indexes(Relation relation, LOCKMODE lockmode, int *nindexes, Relation **Irel)
Definition: vacuum.c:1931
pg_atomic_uint32 * VacuumSharedCostBalance
Definition: vacuum.c:76
int vacuum_freeze_table_age
Definition: vacuum.c:62
uint32 TransactionId
Definition: c.h:513
int vacuum_multixact_freeze_min_age
Definition: vacuum.c:63
int LOCKMODE
Definition: lockdefs.h:26
HeapTuple * rows
Definition: vacuum.h:170
void vac_update_datfrozenxid(void)
Definition: vacuum.c:1333
Datum * stavalues[STATISTIC_NUM_SLOTS]
Definition: vacuum.h:152
void ExecVacuum(ParseState *pstate, VacuumStmt *vacstmt, bool isTopLevel)
Definition: vacuum.c:98
int tupattnum
Definition: vacuum.h:169
pg_atomic_uint32 * VacuumActiveNWorkers
Definition: vacuum.c:77
int vacuum_multixact_freeze_table_age
Definition: vacuum.c:64
void vacuum_delay_point(void)
Definition: vacuum.c:1995
TupleDesc tupDesc
Definition: vacuum.h:171
uint32 BlockNumber
Definition: block.h:31
VacOptTernaryValue
Definition: vacuum.h:195
bool statypbyval[STATISTIC_NUM_SLOTS]
Definition: vacuum.h:162
unsigned int Oid
Definition: postgres_ext.h:31
VacuumOption
Definition: vacuum.h:177
int32 attrtypmod
Definition: vacuum.h:125
int freeze_table_age
Definition: vacuum.h:212
char statypalign[STATISTIC_NUM_SLOTS]
Definition: vacuum.h:163
Form_pg_attribute attr
Definition: vacuum.h:123
#define PGDLLIMPORT
Definition: c.h:1280
signed int int32
Definition: c.h:355
double anl_get_next_S(double t, int n, double *stateptr)
Definition: sampling.c:288
Oid attrtypid
Definition: vacuum.h:124
int32 stawidth
Definition: vacuum.h:144
bool * exprnulls
Definition: vacuum.h:173
Oid stacoll[STATISTIC_NUM_SLOTS]
Definition: vacuum.h:148
int freeze_min_age
Definition: vacuum.h:211
void vac_update_relstats(Relation relation, BlockNumber num_pages, double num_tuples, BlockNumber num_all_visible_pages, bool hasindex, TransactionId frozenxid, MultiXactId minmulti, bool in_outer_xact)
Definition: vacuum.c:1206
bool is_wraparound
Definition: vacuum.h:217
int vacuum_freeze_min_age
Definition: vacuum.c:61
int numnumbers[STATISTIC_NUM_SLOTS]
Definition: vacuum.h:149
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:193
PGDLLIMPORT int default_statistics_target
Definition: analyze.c:81
struct VacAttrStats * VacAttrStatsP
Definition: vacuum.h:100
float4 stanullfrac
Definition: vacuum.h:143
VacOptTernaryValue index_cleanup
Definition: vacuum.h:221
Datum * exprvals
Definition: vacuum.h:172
static int verbose
Oid staop[STATISTIC_NUM_SLOTS]
Definition: vacuum.h:147
bool stats_valid
Definition: vacuum.h:142
float float4
Definition: c.h:490
uintptr_t Datum
Definition: postgres.h:367
int16 stakind[STATISTIC_NUM_SLOTS]
Definition: vacuum.h:146
int VacuumCostBalanceLocal
Definition: vacuum.c:78
Oid statypid[STATISTIC_NUM_SLOTS]
Definition: vacuum.h:160
VacOptTernaryValue truncate
Definition: vacuum.h:223
#define STATISTIC_NUM_SLOTS
Definition: pg_statistic.h:126
void analyze_rel(Oid relid, RangeVar *relation, VacuumParams *params, List *va_cols, bool in_outer_xact, BufferAccessStrategy bstrategy)
Definition: analyze.c:119
struct VacAttrStats VacAttrStats
TransactionId MultiXactId
Definition: c.h:523
void vac_close_indexes(int nindexes, Relation *Irel, LOCKMODE lockmode)
Definition: vacuum.c:1974
float4 * stanumbers[STATISTIC_NUM_SLOTS]
Definition: vacuum.h:150
void(* AnalyzeAttrComputeStatsFunc)(VacAttrStatsP stats, AnalyzeAttrFetchFunc fetchfunc, int samplerows, double totalrows)
Definition: vacuum.h:105
struct VacuumParams VacuumParams
Datum(* AnalyzeAttrFetchFunc)(VacAttrStatsP stats, int rownum, bool *isNull)
Definition: vacuum.h:102
int nworkers
Definition: vacuum.h:231
FormData_pg_type * Form_pg_type
Definition: pg_type.h:255
double vac_estimate_reltuples(Relation relation, BlockNumber total_pages, BlockNumber scanned_pages, double scanned_tuples)
Definition: vacuum.c:1121
int log_min_duration
Definition: vacuum.h:218
Oid attrcollid
Definition: vacuum.h:127
Relation vacuum_open_relation(Oid relid, RangeVar *relation, int options, bool verbose, LOCKMODE lmode)
Definition: vacuum.c:607
MemoryContext anl_context
Definition: vacuum.h:128
FormData_pg_class * Form_pg_class
Definition: pg_class.h:153
int numvalues[STATISTIC_NUM_SLOTS]
Definition: vacuum.h:151
Form_pg_type attrtype
Definition: vacuum.h:126
int16 statyplen[STATISTIC_NUM_SLOTS]
Definition: vacuum.h:161
double anl_random_fract(void)
Definition: sampling.c:266
int options
Definition: vacuum.h:210
AnalyzeAttrComputeStatsFunc compute_stats
Definition: vacuum.h:134
void * extra_data
Definition: vacuum.h:136
bool std_typanalyze(VacAttrStats *stats)
Definition: analyze.c:1691
void vacuum_set_xid_limits(Relation rel, int freeze_min_age, int freeze_table_age, int multixact_freeze_min_age, int multixact_freeze_table_age, TransactionId *oldestXmin, TransactionId *freezeLimit, TransactionId *xidFullScanLimit, MultiXactId *multiXactCutoff, MultiXactId *mxactFullScanLimit)
Definition: vacuum.c:929
Definition: pg_list.h:50
int multixact_freeze_min_age
Definition: vacuum.h:213
bool vacuum_is_relation_owner(Oid relid, Form_pg_class reltuple, int options)
Definition: vacuum.c:533
double anl_init_selection_state(int n)
Definition: sampling.c:277
float4 stadistinct
Definition: vacuum.h:145