commit db618933b50f5f2b29b3a79325d43883cb1af521 Author: Alvaro Herrera AuthorDate: Mon Mar 20 18:51:11 2017 -0300 CommitDate: Mon Mar 20 19:11:36 2017 -0300 Header reorganization: move stuff to statistics/analyze.h diff --git a/contrib/postgres_fdw/postgres_fdw.c b/contrib/postgres_fdw/postgres_fdw.c index e8cb2d0..9101ed1 100644 --- a/contrib/postgres_fdw/postgres_fdw.c +++ b/contrib/postgres_fdw/postgres_fdw.c @@ -17,6 +17,7 @@ #include "access/htup_details.h" #include "access/sysattr.h" #include "catalog/pg_class.h" +#include "catalog/pg_type.h" #include "commands/defrem.h" #include "commands/explain.h" #include "commands/vacuum.h" diff --git a/src/backend/access/hash/hash.c b/src/backend/access/hash/hash.c index cfcec34..9475591 100644 --- a/src/backend/access/hash/hash.c +++ b/src/backend/access/hash/hash.c @@ -22,6 +22,7 @@ #include "access/hash_xlog.h" #include "access/relscan.h" #include "catalog/index.h" +#include "catalog/pg_type.h" #include "commands/vacuum.h" #include "miscadmin.h" #include "optimizer/plancat.h" diff --git a/src/backend/commands/analyze.c b/src/backend/commands/analyze.c index b91df98..ca8235e 100644 --- a/src/backend/commands/analyze.c +++ b/src/backend/commands/analyze.c @@ -39,6 +39,7 @@ #include "parser/parse_relation.h" #include "pgstat.h" #include "postmaster/autovacuum.h" +#include "statistics/analyze.h" #include "storage/bufmgr.h" #include "storage/lmgr.h" #include "storage/proc.h" diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c index 33ca749..5b8d072 100644 --- a/src/backend/postmaster/autovacuum.c +++ b/src/backend/postmaster/autovacuum.c @@ -74,6 +74,7 @@ #include "catalog/dependency.h" #include "catalog/namespace.h" #include "catalog/pg_database.h" +#include "catalog/pg_statistic.h" #include "commands/dbcommands.h" #include "commands/vacuum.h" #include "lib/ilist.h" diff --git a/src/backend/tsearch/ts_typanalyze.c b/src/backend/tsearch/ts_typanalyze.c index 017435c..30f7f26 100644 --- a/src/backend/tsearch/ts_typanalyze.c +++ b/src/backend/tsearch/ts_typanalyze.c @@ -16,6 +16,7 @@ #include "access/hash.h" #include "catalog/pg_operator.h" #include "commands/vacuum.h" +#include "statistics/analyze.h" #include "tsearch/ts_type.h" #include "utils/builtins.h" diff --git a/src/backend/utils/adt/array_typanalyze.c b/src/backend/utils/adt/array_typanalyze.c index 85b7a43..1a7a5ef 100644 --- a/src/backend/utils/adt/array_typanalyze.c +++ b/src/backend/utils/adt/array_typanalyze.c @@ -22,6 +22,7 @@ #include "utils/datum.h" #include "utils/lsyscache.h" #include "utils/typcache.h" +#include "statistics/analyze.h" /* diff --git a/src/backend/utils/adt/rangetypes_typanalyze.c b/src/backend/utils/adt/rangetypes_typanalyze.c index a8d585ce..5f7a160 100644 --- a/src/backend/utils/adt/rangetypes_typanalyze.c +++ b/src/backend/utils/adt/rangetypes_typanalyze.c @@ -29,6 +29,7 @@ #include "utils/builtins.h" #include "utils/lsyscache.h" #include "utils/rangetypes.h" +#include "statistics/analyze.h" static int float8_qsort_cmp(const void *a1, const void *a2); static int range_bound_qsort_cmp(const void *a1, const void *a2, void *arg); diff --git a/src/backend/utils/misc/guc.c b/src/backend/utils/misc/guc.c index 4feb26a..c61753d 100644 --- a/src/backend/utils/misc/guc.c +++ b/src/backend/utils/misc/guc.c @@ -34,6 +34,7 @@ #include "access/xact.h" #include "access/xlog_internal.h" #include "catalog/namespace.h" +#include "catalog/pg_type.h" #include "commands/async.h" #include "commands/prepare.h" #include "commands/user.h" diff --git a/src/include/commands/vacuum.h b/src/include/commands/vacuum.h index 541c2fa..26fa238 100644 --- a/src/include/commands/vacuum.h +++ b/src/include/commands/vacuum.h @@ -14,122 +14,13 @@ #ifndef VACUUM_H #define VACUUM_H -#include "access/htup.h" -#include "catalog/pg_statistic.h" -#include "catalog/pg_type.h" #include "nodes/parsenodes.h" +#include "storage/block.h" #include "storage/buf.h" #include "storage/lock.h" #include "utils/relcache.h" -/*---------- - * ANALYZE builds one of these structs for each attribute (column) that is - * to be analyzed. The struct and subsidiary data are in anl_context, - * so they live until the end of the ANALYZE operation. - * - * The type-specific typanalyze function is passed a pointer to this struct - * and must return TRUE to continue analysis, FALSE to skip analysis of this - * column. In the TRUE case it must set the compute_stats and minrows fields, - * and can optionally set extra_data to pass additional info to compute_stats. - * minrows is its request for the minimum number of sample rows to be gathered - * (but note this request might not be honored, eg if there are fewer rows - * than that in the table). - * - * The compute_stats routine will be called after sample rows have been - * gathered. Aside from this struct, it is passed: - * fetchfunc: a function for accessing the column values from the - * sample rows - * samplerows: the number of sample tuples - * totalrows: estimated total number of rows in relation - * The fetchfunc may be called with rownum running from 0 to samplerows-1. - * It returns a Datum and an isNull flag. - * - * compute_stats should set stats_valid TRUE if it is able to compute - * any useful statistics. If it does, the remainder of the struct holds - * the information to be stored in a pg_statistic row for the column. Be - * careful to allocate any pointed-to data in anl_context, which will NOT - * be CurrentMemoryContext when compute_stats is called. - * - * Note: for the moment, all comparisons done for statistical purposes - * should use the database's default collation (DEFAULT_COLLATION_OID). - * This might change in some future release. - *---------- - */ -typedef struct VacAttrStats *VacAttrStatsP; - -typedef Datum (*AnalyzeAttrFetchFunc) (VacAttrStatsP stats, int rownum, - bool *isNull); - -typedef void (*AnalyzeAttrComputeStatsFunc) (VacAttrStatsP stats, - AnalyzeAttrFetchFunc fetchfunc, - int samplerows, - double totalrows); - -typedef struct VacAttrStats -{ - /* - * These fields are set up by the main ANALYZE code before invoking the - * type-specific typanalyze function. - * - * Note: do not assume that the data being analyzed has the same datatype - * shown in attr, ie do not trust attr->atttypid, attlen, etc. This is - * because some index opclasses store a different type than the underlying - * column/expression. Instead use attrtypid, attrtypmod, and attrtype for - * information about the datatype being fed to the typanalyze function. - */ - Form_pg_attribute attr; /* copy of pg_attribute row for column */ - Oid attrtypid; /* type of data being analyzed */ - int32 attrtypmod; /* typmod of data being analyzed */ - Form_pg_type attrtype; /* copy of pg_type row for attrtypid */ - MemoryContext anl_context; /* where to save long-lived data */ - - /* - * These fields must be filled in by the typanalyze routine, unless it - * returns FALSE. - */ - AnalyzeAttrComputeStatsFunc compute_stats; /* function pointer */ - int minrows; /* Minimum # of rows wanted for stats */ - void *extra_data; /* for extra type-specific data */ - - /* - * These fields are to be filled in by the compute_stats routine. (They - * are initialized to zero when the struct is created.) - */ - bool stats_valid; - float4 stanullfrac; /* fraction of entries that are NULL */ - int32 stawidth; /* average width of column values */ - float4 stadistinct; /* # distinct values */ - int16 stakind[STATISTIC_NUM_SLOTS]; - Oid staop[STATISTIC_NUM_SLOTS]; - int numnumbers[STATISTIC_NUM_SLOTS]; - float4 *stanumbers[STATISTIC_NUM_SLOTS]; - int numvalues[STATISTIC_NUM_SLOTS]; - Datum *stavalues[STATISTIC_NUM_SLOTS]; - - /* - * These fields describe the stavalues[n] element types. They will be - * initialized to match attrtypid, but a custom typanalyze function might - * want to store an array of something other than the analyzed column's - * elements. It should then overwrite these fields. - */ - Oid statypid[STATISTIC_NUM_SLOTS]; - int16 statyplen[STATISTIC_NUM_SLOTS]; - bool statypbyval[STATISTIC_NUM_SLOTS]; - char statypalign[STATISTIC_NUM_SLOTS]; - - /* - * These fields are private to the main ANALYZE code and should not be - * looked at by type-specific functions. - */ - int tupattnum; /* attribute number within tuples */ - HeapTuple *rows; /* access info for std fetch function */ - TupleDesc tupDesc; - Datum *exprvals; /* access info for index fetch function */ - bool *exprnulls; - int rowstride; -} VacAttrStats; - /* * Parameters customizing behavior of VACUUM and ANALYZE. */ @@ -196,7 +87,6 @@ extern void lazy_vacuum_rel(Relation onerel, int options, extern void analyze_rel(Oid relid, RangeVar *relation, int options, VacuumParams *params, List *va_cols, bool in_outer_xact, BufferAccessStrategy bstrategy); -extern bool std_typanalyze(VacAttrStats *stats); /* in utils/misc/sampling.c --- duplicate of declarations in utils/sampling.h */ extern double anl_random_fract(void); diff --git a/src/include/statistics/analyze.h b/src/include/statistics/analyze.h new file mode 100644 index 0000000..e3fa969 --- /dev/null +++ b/src/include/statistics/analyze.h @@ -0,0 +1,134 @@ +/*------------------------------------------------------------------------- + * + * analyze.h + * header file for postgres's ANALYZE statistics + * + * + * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California + * + * src/include/statistics/analyze.h + * + *------------------------------------------------------------------------- + */ +#ifndef STATISTICS_ANALYZE_H +#define STATISTICS_ANALYZE_H + +#include "access/htup.h" +#include "access/tupdesc.h" +#include "catalog/pg_attribute.h" +#include "catalog/pg_statistic.h" +#include "catalog/pg_type.h" +#include "nodes/parsenodes.h" + + +/*---------- + * ANALYZE builds one of these structs for each attribute (column) that is + * to be analyzed. The struct and subsidiary data are in anl_context, + * so they live until the end of the ANALYZE operation. + * + * The type-specific typanalyze function is passed a pointer to this struct + * and must return TRUE to continue analysis, FALSE to skip analysis of this + * column. In the TRUE case it must set the compute_stats and minrows fields, + * and can optionally set extra_data to pass additional info to compute_stats. + * minrows is its request for the minimum number of sample rows to be gathered + * (but note this request might not be honored, eg if there are fewer rows + * than that in the table). + * + * The compute_stats routine will be called after sample rows have been + * gathered. Aside from this struct, it is passed: + * fetchfunc: a function for accessing the column values from the + * sample rows + * samplerows: the number of sample tuples + * totalrows: estimated total number of rows in relation + * The fetchfunc may be called with rownum running from 0 to samplerows-1. + * It returns a Datum and an isNull flag. + * + * compute_stats should set stats_valid TRUE if it is able to compute + * any useful statistics. If it does, the remainder of the struct holds + * the information to be stored in a pg_statistic row for the column. Be + * careful to allocate any pointed-to data in anl_context, which will NOT + * be CurrentMemoryContext when compute_stats is called. + * + * Note: for the moment, all comparisons done for statistical purposes + * should use the database's default collation (DEFAULT_COLLATION_OID). + * This might change in some future release. + *---------- + */ +typedef struct VacAttrStats *VacAttrStatsP; + +typedef Datum (*AnalyzeAttrFetchFunc) (VacAttrStatsP stats, int rownum, + bool *isNull); + +typedef void (*AnalyzeAttrComputeStatsFunc) (VacAttrStatsP stats, + AnalyzeAttrFetchFunc fetchfunc, + int samplerows, + double totalrows); + +typedef struct VacAttrStats +{ + /* + * These fields are set up by the main ANALYZE code before invoking the + * type-specific typanalyze function. + * + * Note: do not assume that the data being analyzed has the same datatype + * shown in attr, ie do not trust attr->atttypid, attlen, etc. This is + * because some index opclasses store a different type than the underlying + * column/expression. Instead use attrtypid, attrtypmod, and attrtype for + * information about the datatype being fed to the typanalyze function. + */ + Form_pg_attribute attr; /* copy of pg_attribute row for column */ + Oid attrtypid; /* type of data being analyzed */ + int32 attrtypmod; /* typmod of data being analyzed */ + Form_pg_type attrtype; /* copy of pg_type row for attrtypid */ + MemoryContext anl_context; /* where to save long-lived data */ + + /* + * These fields must be filled in by the typanalyze routine, unless it + * returns FALSE. + */ + AnalyzeAttrComputeStatsFunc compute_stats; /* function pointer */ + int minrows; /* Minimum # of rows wanted for stats */ + void *extra_data; /* for extra type-specific data */ + + /* + * These fields are to be filled in by the compute_stats routine. (They + * are initialized to zero when the struct is created.) + */ + bool stats_valid; + float4 stanullfrac; /* fraction of entries that are NULL */ + int32 stawidth; /* average width of column values */ + float4 stadistinct; /* # distinct values */ + int16 stakind[STATISTIC_NUM_SLOTS]; + Oid staop[STATISTIC_NUM_SLOTS]; + int numnumbers[STATISTIC_NUM_SLOTS]; + float4 *stanumbers[STATISTIC_NUM_SLOTS]; + int numvalues[STATISTIC_NUM_SLOTS]; + Datum *stavalues[STATISTIC_NUM_SLOTS]; + + /* + * These fields describe the stavalues[n] element types. They will be + * initialized to match attrtypid, but a custom typanalyze function might + * want to store an array of something other than the analyzed column's + * elements. It should then overwrite these fields. + */ + Oid statypid[STATISTIC_NUM_SLOTS]; + int16 statyplen[STATISTIC_NUM_SLOTS]; + bool statypbyval[STATISTIC_NUM_SLOTS]; + char statypalign[STATISTIC_NUM_SLOTS]; + + /* + * These fields are private to the main ANALYZE code and should not be + * looked at by type-specific functions. + */ + int tupattnum; /* attribute number within tuples */ + HeapTuple *rows; /* access info for std fetch function */ + TupleDesc tupDesc; + Datum *exprvals; /* access info for index fetch function */ + bool *exprnulls; + int rowstride; +} VacAttrStats; + +extern bool std_typanalyze(VacAttrStats *stats); + +#endif /* STATISTICS_ANALYZE_H */