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-2017, 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_statistic.h"
19 #include "catalog/pg_type.h"
20 #include "nodes/parsenodes.h"
21 #include "storage/buf.h"
22 #include "storage/lock.h"
23 #include "utils/relcache.h"
24 
25 
26 /*----------
27  * ANALYZE builds one of these structs for each attribute (column) that is
28  * to be analyzed. The struct and subsidiary data are in anl_context,
29  * so they live until the end of the ANALYZE operation.
30  *
31  * The type-specific typanalyze function is passed a pointer to this struct
32  * and must return true to continue analysis, false to skip analysis of this
33  * column. In the true case it must set the compute_stats and minrows fields,
34  * and can optionally set extra_data to pass additional info to compute_stats.
35  * minrows is its request for the minimum number of sample rows to be gathered
36  * (but note this request might not be honored, eg if there are fewer rows
37  * than that in the table).
38  *
39  * The compute_stats routine will be called after sample rows have been
40  * gathered. Aside from this struct, it is passed:
41  * fetchfunc: a function for accessing the column values from the
42  * sample rows
43  * samplerows: the number of sample tuples
44  * totalrows: estimated total number of rows in relation
45  * The fetchfunc may be called with rownum running from 0 to samplerows-1.
46  * It returns a Datum and an isNull flag.
47  *
48  * compute_stats should set stats_valid true if it is able to compute
49  * any useful statistics. If it does, the remainder of the struct holds
50  * the information to be stored in a pg_statistic row for the column. Be
51  * careful to allocate any pointed-to data in anl_context, which will NOT
52  * be CurrentMemoryContext when compute_stats is called.
53  *
54  * Note: for the moment, all comparisons done for statistical purposes
55  * should use the database's default collation (DEFAULT_COLLATION_OID).
56  * This might change in some future release.
57  *----------
58  */
59 typedef struct VacAttrStats *VacAttrStatsP;
60 
61 typedef Datum (*AnalyzeAttrFetchFunc) (VacAttrStatsP stats, int rownum,
62  bool *isNull);
63 
64 typedef void (*AnalyzeAttrComputeStatsFunc) (VacAttrStatsP stats,
65  AnalyzeAttrFetchFunc fetchfunc,
66  int samplerows,
67  double totalrows);
68 
69 typedef struct VacAttrStats
70 {
71  /*
72  * These fields are set up by the main ANALYZE code before invoking the
73  * type-specific typanalyze function.
74  *
75  * Note: do not assume that the data being analyzed has the same datatype
76  * shown in attr, ie do not trust attr->atttypid, attlen, etc. This is
77  * because some index opclasses store a different type than the underlying
78  * column/expression. Instead use attrtypid, attrtypmod, and attrtype for
79  * information about the datatype being fed to the typanalyze function.
80  */
81  Form_pg_attribute attr; /* copy of pg_attribute row for column */
82  Oid attrtypid; /* type of data being analyzed */
83  int32 attrtypmod; /* typmod of data being analyzed */
84  Form_pg_type attrtype; /* copy of pg_type row for attrtypid */
85  MemoryContext anl_context; /* where to save long-lived data */
86 
87  /*
88  * These fields must be filled in by the typanalyze routine, unless it
89  * returns false.
90  */
92  int minrows; /* Minimum # of rows wanted for stats */
93  void *extra_data; /* for extra type-specific data */
94 
95  /*
96  * These fields are to be filled in by the compute_stats routine. (They
97  * are initialized to zero when the struct is created.)
98  */
100  float4 stanullfrac; /* fraction of entries that are NULL */
101  int32 stawidth; /* average width of column values */
102  float4 stadistinct; /* # distinct values */
109 
110  /*
111  * These fields describe the stavalues[n] element types. They will be
112  * initialized to match attrtypid, but a custom typanalyze function might
113  * want to store an array of something other than the analyzed column's
114  * elements. It should then overwrite these fields.
115  */
120 
121  /*
122  * These fields are private to the main ANALYZE code and should not be
123  * looked at by type-specific functions.
124  */
125  int tupattnum; /* attribute number within tuples */
126  HeapTuple *rows; /* access info for std fetch function */
128  Datum *exprvals; /* access info for index fetch function */
129  bool *exprnulls;
131 } VacAttrStats;
132 
133 /*
134  * Parameters customizing behavior of VACUUM and ANALYZE.
135  */
136 typedef struct VacuumParams
137 {
138  int freeze_min_age; /* min freeze age, -1 to use default */
139  int freeze_table_age; /* age at which to scan whole table */
140  int multixact_freeze_min_age; /* min multixact freeze age, -1 to
141  * use default */
142  int multixact_freeze_table_age; /* multixact age at which to scan
143  * whole table */
144  bool is_wraparound; /* force a for-wraparound vacuum */
145  int log_min_duration; /* minimum execution threshold in ms at
146  * which verbose logs are activated, -1
147  * to use default */
148 } VacuumParams;
149 
150 /* GUC parameters */
151 extern PGDLLIMPORT int default_statistics_target; /* PGDLLIMPORT for PostGIS */
152 extern int vacuum_freeze_min_age;
153 extern int vacuum_freeze_table_age;
156 
157 
158 /* in commands/vacuum.c */
159 extern void ExecVacuum(VacuumStmt *vacstmt, bool isTopLevel);
160 extern void vacuum(int options, List *relations, VacuumParams *params,
161  BufferAccessStrategy bstrategy, bool isTopLevel);
162 extern void vac_open_indexes(Relation relation, LOCKMODE lockmode,
163  int *nindexes, Relation **Irel);
164 extern void vac_close_indexes(int nindexes, Relation *Irel, LOCKMODE lockmode);
165 extern double vac_estimate_reltuples(Relation relation, bool is_analyze,
166  BlockNumber total_pages,
167  BlockNumber scanned_pages,
168  double scanned_tuples);
169 extern void vac_update_relstats(Relation relation,
170  BlockNumber num_pages,
171  double num_tuples,
172  BlockNumber num_all_visible_pages,
173  bool hasindex,
174  TransactionId frozenxid,
175  MultiXactId minmulti,
176  bool in_outer_xact);
177 extern void vacuum_set_xid_limits(Relation rel,
178  int freeze_min_age, int freeze_table_age,
179  int multixact_freeze_min_age,
180  int multixact_freeze_table_age,
181  TransactionId *oldestXmin,
182  TransactionId *freezeLimit,
183  TransactionId *xidFullScanLimit,
184  MultiXactId *multiXactCutoff,
185  MultiXactId *mxactFullScanLimit);
186 extern void vac_update_datfrozenxid(void);
187 extern void vacuum_delay_point(void);
188 
189 /* in commands/vacuumlazy.c */
190 extern void lazy_vacuum_rel(Relation onerel, int options,
191  VacuumParams *params, BufferAccessStrategy bstrategy);
192 
193 /* in commands/analyze.c */
194 extern void analyze_rel(Oid relid, RangeVar *relation, int options,
195  VacuumParams *params, List *va_cols, bool in_outer_xact,
196  BufferAccessStrategy bstrategy);
197 extern bool std_typanalyze(VacAttrStats *stats);
198 
199 /* in utils/misc/sampling.c --- duplicate of declarations in utils/sampling.h */
200 extern double anl_random_fract(void);
201 extern double anl_init_selection_state(int n);
202 extern double anl_get_next_S(double t, int n, double *stateptr);
203 
204 #endif /* VACUUM_H */
int rowstride
Definition: vacuum.h:130
signed short int16
Definition: c.h:293
int multixact_freeze_table_age
Definition: vacuum.h:142
int minrows
Definition: vacuum.h:92
void lazy_vacuum_rel(Relation onerel, int options, VacuumParams *params, BufferAccessStrategy bstrategy)
Definition: vacuumlazy.c:182
void vac_open_indexes(Relation relation, LOCKMODE lockmode, int *nindexes, Relation **Irel)
Definition: vacuum.c:1631
int vacuum_freeze_table_age
Definition: vacuum.c:60
uint32 TransactionId
Definition: c.h:455
int vacuum_multixact_freeze_min_age
Definition: vacuum.c:61
int LOCKMODE
Definition: lockdefs.h:26
HeapTuple * rows
Definition: vacuum.h:126
void vac_update_datfrozenxid(void)
Definition: vacuum.c:1014
Datum * stavalues[STATISTIC_NUM_SLOTS]
Definition: vacuum.h:108
int tupattnum
Definition: vacuum.h:125
int vacuum_multixact_freeze_table_age
Definition: vacuum.c:62
void vacuum_delay_point(void)
Definition: vacuum.c:1695
TupleDesc tupDesc
Definition: vacuum.h:127
uint32 BlockNumber
Definition: block.h:31
bool statypbyval[STATISTIC_NUM_SLOTS]
Definition: vacuum.h:118
FormData_pg_type * Form_pg_type
Definition: pg_type.h:233
unsigned int Oid
Definition: postgres_ext.h:31
int32 attrtypmod
Definition: vacuum.h:83
int freeze_table_age
Definition: vacuum.h:139
char statypalign[STATISTIC_NUM_SLOTS]
Definition: vacuum.h:119
Form_pg_attribute attr
Definition: vacuum.h:81
#define PGDLLIMPORT
Definition: c.h:1060
signed int int32
Definition: c.h:294
double anl_get_next_S(double t, int n, double *stateptr)
Definition: sampling.c:284
Oid attrtypid
Definition: vacuum.h:82
int32 stawidth
Definition: vacuum.h:101
bool * exprnulls
Definition: vacuum.h:129
int freeze_min_age
Definition: vacuum.h:138
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:877
bool is_wraparound
Definition: vacuum.h:144
int vacuum_freeze_min_age
Definition: vacuum.c:59
void analyze_rel(Oid relid, RangeVar *relation, int options, VacuumParams *params, List *va_cols, bool in_outer_xact, BufferAccessStrategy bstrategy)
Definition: analyze.c:115
int numnumbers[STATISTIC_NUM_SLOTS]
Definition: vacuum.h:105
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:187
PGDLLIMPORT int default_statistics_target
Definition: analyze.c:77
struct VacAttrStats * VacAttrStatsP
Definition: vacuum.h:59
float4 stanullfrac
Definition: vacuum.h:100
Datum * exprvals
Definition: vacuum.h:128
double vac_estimate_reltuples(Relation relation, bool is_analyze, BlockNumber total_pages, BlockNumber scanned_pages, double scanned_tuples)
Definition: vacuum.c:777
Oid staop[STATISTIC_NUM_SLOTS]
Definition: vacuum.h:104
bool stats_valid
Definition: vacuum.h:99
float float4
Definition: c.h:438
uintptr_t Datum
Definition: postgres.h:372
int16 stakind[STATISTIC_NUM_SLOTS]
Definition: vacuum.h:103
void vacuum(int options, List *relations, VacuumParams *params, BufferAccessStrategy bstrategy, bool isTopLevel)
Definition: vacuum.c:166
Oid statypid[STATISTIC_NUM_SLOTS]
Definition: vacuum.h:116
#define STATISTIC_NUM_SLOTS
Definition: pg_statistic.h:121
struct VacAttrStats VacAttrStats
TransactionId MultiXactId
Definition: c.h:465
void vac_close_indexes(int nindexes, Relation *Irel, LOCKMODE lockmode)
Definition: vacuum.c:1674
float4 * stanumbers[STATISTIC_NUM_SLOTS]
Definition: vacuum.h:106
void(* AnalyzeAttrComputeStatsFunc)(VacAttrStatsP stats, AnalyzeAttrFetchFunc fetchfunc, int samplerows, double totalrows)
Definition: vacuum.h:64
struct VacuumParams VacuumParams
Datum(* AnalyzeAttrFetchFunc)(VacAttrStatsP stats, int rownum, bool *isNull)
Definition: vacuum.h:61
int log_min_duration
Definition: vacuum.h:145
MemoryContext anl_context
Definition: vacuum.h:85
void ExecVacuum(VacuumStmt *vacstmt, bool isTopLevel)
Definition: vacuum.c:87
int numvalues[STATISTIC_NUM_SLOTS]
Definition: vacuum.h:107
Form_pg_type attrtype
Definition: vacuum.h:84
int16 statyplen[STATISTIC_NUM_SLOTS]
Definition: vacuum.h:117
double anl_random_fract(void)
Definition: sampling.c:262
AnalyzeAttrComputeStatsFunc compute_stats
Definition: vacuum.h:91
void * extra_data
Definition: vacuum.h:93
bool std_typanalyze(VacAttrStats *stats)
Definition: analyze.c:1775
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:593
Definition: pg_list.h:45
int multixact_freeze_min_age
Definition: vacuum.h:140
double anl_init_selection_state(int n)
Definition: sampling.c:273
float4 stadistinct
Definition: vacuum.h:102