Header And Logo

PostgreSQL
| The world's most advanced open source database.

vacuum.h

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  * vacuum.h
00004  *    header file for postgres vacuum cleaner and statistics analyzer
00005  *
00006  *
00007  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
00008  * Portions Copyright (c) 1994, Regents of the University of California
00009  *
00010  * src/include/commands/vacuum.h
00011  *
00012  *-------------------------------------------------------------------------
00013  */
00014 #ifndef VACUUM_H
00015 #define VACUUM_H
00016 
00017 #include "access/htup.h"
00018 #include "catalog/pg_statistic.h"
00019 #include "catalog/pg_type.h"
00020 #include "nodes/parsenodes.h"
00021 #include "storage/buf.h"
00022 #include "storage/lock.h"
00023 #include "utils/relcache.h"
00024 
00025 
00026 /*----------
00027  * ANALYZE builds one of these structs for each attribute (column) that is
00028  * to be analyzed.  The struct and subsidiary data are in anl_context,
00029  * so they live until the end of the ANALYZE operation.
00030  *
00031  * The type-specific typanalyze function is passed a pointer to this struct
00032  * and must return TRUE to continue analysis, FALSE to skip analysis of this
00033  * column.  In the TRUE case it must set the compute_stats and minrows fields,
00034  * and can optionally set extra_data to pass additional info to compute_stats.
00035  * minrows is its request for the minimum number of sample rows to be gathered
00036  * (but note this request might not be honored, eg if there are fewer rows
00037  * than that in the table).
00038  *
00039  * The compute_stats routine will be called after sample rows have been
00040  * gathered.  Aside from this struct, it is passed:
00041  *      fetchfunc: a function for accessing the column values from the
00042  *                 sample rows
00043  *      samplerows: the number of sample tuples
00044  *      totalrows: estimated total number of rows in relation
00045  * The fetchfunc may be called with rownum running from 0 to samplerows-1.
00046  * It returns a Datum and an isNull flag.
00047  *
00048  * compute_stats should set stats_valid TRUE if it is able to compute
00049  * any useful statistics.  If it does, the remainder of the struct holds
00050  * the information to be stored in a pg_statistic row for the column.  Be
00051  * careful to allocate any pointed-to data in anl_context, which will NOT
00052  * be CurrentMemoryContext when compute_stats is called.
00053  *
00054  * Note: for the moment, all comparisons done for statistical purposes
00055  * should use the database's default collation (DEFAULT_COLLATION_OID).
00056  * This might change in some future release.
00057  *----------
00058  */
00059 typedef struct VacAttrStats *VacAttrStatsP;
00060 
00061 typedef Datum (*AnalyzeAttrFetchFunc) (VacAttrStatsP stats, int rownum,
00062                                                    bool *isNull);
00063 
00064 typedef void (*AnalyzeAttrComputeStatsFunc) (VacAttrStatsP stats,
00065                                               AnalyzeAttrFetchFunc fetchfunc,
00066                                                          int samplerows,
00067                                                          double totalrows);
00068 
00069 typedef struct VacAttrStats
00070 {
00071     /*
00072      * These fields are set up by the main ANALYZE code before invoking the
00073      * type-specific typanalyze function.
00074      *
00075      * Note: do not assume that the data being analyzed has the same datatype
00076      * shown in attr, ie do not trust attr->atttypid, attlen, etc.  This is
00077      * because some index opclasses store a different type than the underlying
00078      * column/expression.  Instead use attrtypid, attrtypmod, and attrtype for
00079      * information about the datatype being fed to the typanalyze function.
00080      */
00081     Form_pg_attribute attr;     /* copy of pg_attribute row for column */
00082     Oid         attrtypid;      /* type of data being analyzed */
00083     int32       attrtypmod;     /* typmod of data being analyzed */
00084     Form_pg_type attrtype;      /* copy of pg_type row for attrtypid */
00085     MemoryContext anl_context;  /* where to save long-lived data */
00086 
00087     /*
00088      * These fields must be filled in by the typanalyze routine, unless it
00089      * returns FALSE.
00090      */
00091     AnalyzeAttrComputeStatsFunc compute_stats;  /* function pointer */
00092     int         minrows;        /* Minimum # of rows wanted for stats */
00093     void       *extra_data;     /* for extra type-specific data */
00094 
00095     /*
00096      * These fields are to be filled in by the compute_stats routine. (They
00097      * are initialized to zero when the struct is created.)
00098      */
00099     bool        stats_valid;
00100     float4      stanullfrac;    /* fraction of entries that are NULL */
00101     int32       stawidth;       /* average width of column values */
00102     float4      stadistinct;    /* # distinct values */
00103     int16       stakind[STATISTIC_NUM_SLOTS];
00104     Oid         staop[STATISTIC_NUM_SLOTS];
00105     int         numnumbers[STATISTIC_NUM_SLOTS];
00106     float4     *stanumbers[STATISTIC_NUM_SLOTS];
00107     int         numvalues[STATISTIC_NUM_SLOTS];
00108     Datum      *stavalues[STATISTIC_NUM_SLOTS];
00109 
00110     /*
00111      * These fields describe the stavalues[n] element types. They will be
00112      * initialized to match attrtypid, but a custom typanalyze function might
00113      * want to store an array of something other than the analyzed column's
00114      * elements. It should then overwrite these fields.
00115      */
00116     Oid         statypid[STATISTIC_NUM_SLOTS];
00117     int16       statyplen[STATISTIC_NUM_SLOTS];
00118     bool        statypbyval[STATISTIC_NUM_SLOTS];
00119     char        statypalign[STATISTIC_NUM_SLOTS];
00120 
00121     /*
00122      * These fields are private to the main ANALYZE code and should not be
00123      * looked at by type-specific functions.
00124      */
00125     int         tupattnum;      /* attribute number within tuples */
00126     HeapTuple  *rows;           /* access info for std fetch function */
00127     TupleDesc   tupDesc;
00128     Datum      *exprvals;       /* access info for index fetch function */
00129     bool       *exprnulls;
00130     int         rowstride;
00131 } VacAttrStats;
00132 
00133 
00134 /* GUC parameters */
00135 extern PGDLLIMPORT int default_statistics_target;       /* PGDLLIMPORT for
00136                                                          * PostGIS */
00137 extern int  vacuum_freeze_min_age;
00138 extern int  vacuum_freeze_table_age;
00139 
00140 
00141 /* in commands/vacuum.c */
00142 extern void vacuum(VacuumStmt *vacstmt, Oid relid, bool do_toast,
00143        BufferAccessStrategy bstrategy, bool for_wraparound, bool isTopLevel);
00144 extern void vac_open_indexes(Relation relation, LOCKMODE lockmode,
00145                  int *nindexes, Relation **Irel);
00146 extern void vac_close_indexes(int nindexes, Relation *Irel, LOCKMODE lockmode);
00147 extern double vac_estimate_reltuples(Relation relation, bool is_analyze,
00148                        BlockNumber total_pages,
00149                        BlockNumber scanned_pages,
00150                        double scanned_tuples);
00151 extern void vac_update_relstats(Relation relation,
00152                     BlockNumber num_pages,
00153                     double num_tuples,
00154                     BlockNumber num_all_visible_pages,
00155                     bool hasindex,
00156                     TransactionId frozenxid,
00157                     MultiXactId minmulti);
00158 extern void vacuum_set_xid_limits(int freeze_min_age, int freeze_table_age,
00159                       bool sharedRel,
00160                       TransactionId *oldestXmin,
00161                       TransactionId *freezeLimit,
00162                       TransactionId *freezeTableLimit,
00163                       MultiXactId *multiXactFrzLimit);
00164 extern void vac_update_datfrozenxid(void);
00165 extern void vacuum_delay_point(void);
00166 
00167 /* in commands/vacuumlazy.c */
00168 extern void lazy_vacuum_rel(Relation onerel, VacuumStmt *vacstmt,
00169                 BufferAccessStrategy bstrategy);
00170 
00171 /* in commands/analyze.c */
00172 extern void analyze_rel(Oid relid, VacuumStmt *vacstmt,
00173             BufferAccessStrategy bstrategy);
00174 extern bool std_typanalyze(VacAttrStats *stats);
00175 extern double anl_random_fract(void);
00176 extern double anl_init_selection_state(int n);
00177 extern double anl_get_next_S(double t, int n, double *stateptr);
00178 
00179 #endif   /* VACUUM_H */