1 /*-------------------------------------------------------------------------
4 * header file for postgres vacuum cleaner and statistics analyzer
7 * Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
10 * src/include/commands/vacuum.h
12 *-------------------------------------------------------------------------
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"
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
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.
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.
59 typedef struct VacAttrStats *VacAttrStatsP;
61 typedef Datum (*AnalyzeAttrFetchFunc) (VacAttrStatsP stats, int rownum,
64 typedef struct VacAttrStats
67 * These fields are set up by the main ANALYZE code before invoking the
68 * type-specific typanalyze function.
70 * Note: do not assume that the data being analyzed has the same datatype
71 * shown in attr, ie do not trust attr->atttypid, attlen, etc. This is
72 * because some index opclasses store a different type than the underlying
73 * column/expression. Instead use attrtypid, attrtypmod, and attrtype for
74 * information about the datatype being fed to the typanalyze function.
76 Form_pg_attribute attr; /* copy of pg_attribute row for column */
77 Oid attrtypid; /* type of data being analyzed */
78 int32 attrtypmod; /* typmod of data being analyzed */
79 Form_pg_type attrtype; /* copy of pg_type row for attrtypid */
80 MemoryContext anl_context; /* where to save long-lived data */
83 * These fields must be filled in by the typanalyze routine, unless it
86 void (*compute_stats) (VacAttrStatsP stats,
87 AnalyzeAttrFetchFunc fetchfunc,
90 int minrows; /* Minimum # of rows wanted for stats */
91 void *extra_data; /* for extra type-specific data */
94 * These fields are to be filled in by the compute_stats routine. (They
95 * are initialized to zero when the struct is created.)
98 float4 stanullfrac; /* fraction of entries that are NULL */
99 int4 stawidth; /* average width of column values */
100 float4 stadistinct; /* # distinct values */
101 int2 stakind[STATISTIC_NUM_SLOTS];
102 Oid staop[STATISTIC_NUM_SLOTS];
103 int numnumbers[STATISTIC_NUM_SLOTS];
104 float4 *stanumbers[STATISTIC_NUM_SLOTS];
105 int numvalues[STATISTIC_NUM_SLOTS];
106 Datum *stavalues[STATISTIC_NUM_SLOTS];
109 * These fields describe the stavalues[n] element types. They will be
110 * initialized to match attrtypid, but a custom typanalyze function might
111 * want to store an array of something other than the analyzed column's
112 * elements. It should then overwrite these fields.
114 Oid statypid[STATISTIC_NUM_SLOTS];
115 int2 statyplen[STATISTIC_NUM_SLOTS];
116 bool statypbyval[STATISTIC_NUM_SLOTS];
117 char statypalign[STATISTIC_NUM_SLOTS];
120 * These fields are private to the main ANALYZE code and should not be
121 * looked at by type-specific functions.
123 int tupattnum; /* attribute number within tuples */
124 HeapTuple *rows; /* access info for std fetch function */
126 Datum *exprvals; /* access info for index fetch function */
133 extern PGDLLIMPORT int default_statistics_target; /* PGDLLIMPORT for
135 extern int vacuum_freeze_min_age;
136 extern int vacuum_freeze_table_age;
139 /* in commands/vacuum.c */
140 extern void vacuum(VacuumStmt *vacstmt, Oid relid, bool do_toast,
141 BufferAccessStrategy bstrategy, bool for_wraparound, bool isTopLevel);
142 extern void vac_open_indexes(Relation relation, LOCKMODE lockmode,
143 int *nindexes, Relation **Irel);
144 extern void vac_close_indexes(int nindexes, Relation *Irel, LOCKMODE lockmode);
145 extern void vac_update_relstats(Relation relation,
146 BlockNumber num_pages,
149 TransactionId frozenxid);
150 extern void vacuum_set_xid_limits(int freeze_min_age, int freeze_table_age,
152 TransactionId *oldestXmin,
153 TransactionId *freezeLimit,
154 TransactionId *freezeTableLimit);
155 extern void vac_update_datfrozenxid(void);
156 extern void vacuum_delay_point(void);
158 /* in commands/vacuumlazy.c */
159 extern void lazy_vacuum_rel(Relation onerel, VacuumStmt *vacstmt,
160 BufferAccessStrategy bstrategy, bool *scanned_all);
162 /* in commands/analyze.c */
163 extern void analyze_rel(Oid relid, VacuumStmt *vacstmt,
164 BufferAccessStrategy bstrategy, bool update_reltuples);
166 #endif /* VACUUM_H */