Add vacuum_freeze_table_age GUC option, to control when VACUUM should
[PostgreSQL.git] / src / include / commands / vacuum.h
blobda7d20e9c7b6de9cc358d5a6cf2e21f64a336ed9
1 /*-------------------------------------------------------------------------
3 * vacuum.h
4 * header file for postgres vacuum cleaner and statistics analyzer
7 * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
10 * $PostgreSQL$
12 *-------------------------------------------------------------------------
14 #ifndef VACUUM_H
15 #define VACUUM_H
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"
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.
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).
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.
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 *----------
55 typedef struct VacAttrStats *VacAttrStatsP;
57 typedef Datum (*AnalyzeAttrFetchFunc) (VacAttrStatsP stats, int rownum,
58 bool *isNull);
60 typedef struct VacAttrStats
63 * These fields are set up by the main ANALYZE code before invoking the
64 * type-specific typanalyze function.
66 Form_pg_attribute attr; /* copy of pg_attribute row for column */
67 Form_pg_type attrtype; /* copy of pg_type row for column */
68 MemoryContext anl_context; /* where to save long-lived data */
71 * These fields must be filled in by the typanalyze routine, unless it
72 * returns FALSE.
74 void (*compute_stats) (VacAttrStatsP stats,
75 AnalyzeAttrFetchFunc fetchfunc,
76 int samplerows,
77 double totalrows);
78 int minrows; /* Minimum # of rows wanted for stats */
79 void *extra_data; /* for extra type-specific data */
82 * These fields are to be filled in by the compute_stats routine. (They
83 * are initialized to zero when the struct is created.)
85 bool stats_valid;
86 float4 stanullfrac; /* fraction of entries that are NULL */
87 int4 stawidth; /* average width of column values */
88 float4 stadistinct; /* # distinct values */
89 int2 stakind[STATISTIC_NUM_SLOTS];
90 Oid staop[STATISTIC_NUM_SLOTS];
91 int numnumbers[STATISTIC_NUM_SLOTS];
92 float4 *stanumbers[STATISTIC_NUM_SLOTS];
93 int numvalues[STATISTIC_NUM_SLOTS];
94 Datum *stavalues[STATISTIC_NUM_SLOTS];
97 * These fields describe the stavalues[n] element types. They will
98 * be initialized to be the same as the column's that's underlying the
99 * slot, but a custom typanalyze function might want to store an array of
100 * something other than the analyzed column's elements. It should then
101 * overwrite these fields.
103 Oid statypid[STATISTIC_NUM_SLOTS];
104 int2 statyplen[STATISTIC_NUM_SLOTS];
105 bool statypbyval[STATISTIC_NUM_SLOTS];
106 char statypalign[STATISTIC_NUM_SLOTS];
109 * These fields are private to the main ANALYZE code and should not be
110 * looked at by type-specific functions.
112 int tupattnum; /* attribute number within tuples */
113 HeapTuple *rows; /* access info for std fetch function */
114 TupleDesc tupDesc;
115 Datum *exprvals; /* access info for index fetch function */
116 bool *exprnulls;
117 int rowstride;
118 } VacAttrStats;
121 /* GUC parameters */
122 extern PGDLLIMPORT int default_statistics_target; /* PGDLLIMPORT for
123 * PostGIS */
124 extern int vacuum_freeze_min_age;
125 extern int vacuum_freeze_table_age;
128 /* in commands/vacuum.c */
129 extern void vacuum(VacuumStmt *vacstmt, Oid relid, bool do_toast,
130 BufferAccessStrategy bstrategy, bool for_wraparound, bool isTopLevel);
131 extern void vac_open_indexes(Relation relation, LOCKMODE lockmode,
132 int *nindexes, Relation **Irel);
133 extern void vac_close_indexes(int nindexes, Relation *Irel, LOCKMODE lockmode);
134 extern void vac_update_relstats(Relation relation,
135 BlockNumber num_pages,
136 double num_tuples,
137 bool hasindex,
138 TransactionId frozenxid);
139 extern void vacuum_set_xid_limits(int freeze_min_age, int freeze_table_age,
140 bool sharedRel,
141 TransactionId *oldestXmin,
142 TransactionId *freezeLimit,
143 TransactionId *freezeTableLimit);
144 extern void vac_update_datfrozenxid(void);
145 extern bool vac_is_partial_index(Relation indrel);
146 extern void vacuum_delay_point(void);
148 /* in commands/vacuumlazy.c */
149 extern void lazy_vacuum_rel(Relation onerel, VacuumStmt *vacstmt,
150 BufferAccessStrategy bstrategy, bool *scanned_all);
152 /* in commands/analyze.c */
153 extern void analyze_rel(Oid relid, VacuumStmt *vacstmt,
154 BufferAccessStrategy bstrategy, bool update_reltuples);
156 #endif /* VACUUM_H */