1 /*-------------------------------------------------------------------------
4 * header file for postgres vacuum cleaner and statistics analyzer
7 * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
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.
55 typedef struct VacAttrStats
*VacAttrStatsP
;
57 typedef Datum (*AnalyzeAttrFetchFunc
) (VacAttrStatsP stats
, int rownum
,
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
74 void (*compute_stats
) (VacAttrStatsP stats
,
75 AnalyzeAttrFetchFunc fetchfunc
,
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.)
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 */
115 Datum
*exprvals
; /* access info for index fetch function */
122 extern PGDLLIMPORT
int default_statistics_target
; /* PGDLLIMPORT for
124 extern int vacuum_freeze_min_age
;
127 /* in commands/vacuum.c */
128 extern void vacuum(VacuumStmt
*vacstmt
, Oid relid
, bool do_toast
,
129 BufferAccessStrategy bstrategy
, bool for_wraparound
, bool isTopLevel
);
130 extern void vac_open_indexes(Relation relation
, LOCKMODE lockmode
,
131 int *nindexes
, Relation
**Irel
);
132 extern void vac_close_indexes(int nindexes
, Relation
*Irel
, LOCKMODE lockmode
);
133 extern void vac_update_relstats(Relation relation
,
134 BlockNumber num_pages
,
137 TransactionId frozenxid
);
138 extern void vacuum_set_xid_limits(int freeze_min_age
, bool sharedRel
,
139 TransactionId
*oldestXmin
,
140 TransactionId
*freezeLimit
);
141 extern void vac_update_datfrozenxid(void);
142 extern bool vac_is_partial_index(Relation indrel
);
143 extern void vacuum_delay_point(void);
145 /* in commands/vacuumlazy.c */
146 extern void lazy_vacuum_rel(Relation onerel
, VacuumStmt
*vacstmt
,
147 BufferAccessStrategy bstrategy
);
149 /* in commands/analyze.c */
150 extern void analyze_rel(Oid relid
, VacuumStmt
*vacstmt
,
151 BufferAccessStrategy bstrategy
);
153 #endif /* VACUUM_H */