PostGIS 3.7.0dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches
gserialized_estimate.c File Reference
#include "postgres.h"
#include "access/genam.h"
#include "access/gin.h"
#include "access/gist.h"
#include "access/gist_private.h"
#include "access/gistscan.h"
#include "access/detoast.h"
#include "utils/datum.h"
#include "access/heapam.h"
#include "catalog/index.h"
#include "catalog/pg_am.h"
#include "miscadmin.h"
#include "storage/lmgr.h"
#include "catalog/namespace.h"
#include "catalog/indexing.h"
#include "utils/regproc.h"
#include "utils/varlena.h"
#include "utils/builtins.h"
#include "utils/snapmgr.h"
#include "utils/fmgroids.h"
#include "funcapi.h"
#include "catalog/pg_type.h"
#include "access/relscan.h"
#include "executor/spi.h"
#include "fmgr.h"
#include "commands/vacuum.h"
#include "nodes/pathnodes.h"
#include "parser/parsetree.h"
#include "utils/array.h"
#include "utils/lsyscache.h"
#include "utils/syscache.h"
#include "utils/rel.h"
#include "utils/selfuncs.h"
#include "../postgis_config.h"
#include "access/htup_details.h"
#include "stringbuffer.h"
#include "liblwgeom.h"
#include "lwgeodetic.h"
#include "lwgeom_pg.h"
#include "gserialized_gist.h"
#include "gserialized_estimate_support.h"
#include <math.h>
#include <limits.h>
#include <float.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
Include dependency graph for gserialized_estimate.c:

Go to the source code of this file.

Data Structures

struct  GserializedAnalyzeExtraData
 

Macros

#define STATISTIC_KIND_ND   102
 
#define STATISTIC_KIND_2D   103
 
#define STATISTIC_SLOT_ND   3
 
#define STATISTIC_SLOT_2D   4
 
#define SDFACTOR   3.25
 
#define MIN_DIMENSION_WIDTH   0.000000001
 Minimum width of a dimension that we'll bother trying to compute statistics on.
 
#define MAX_DIMENSION_WIDTH   1.0E+20
 Maximum width of a dimension that we'll bother trying to compute statistics on.
 
#define DEFAULT_ND_SEL   0.0001
 Default geometry selectivity factor.
 
#define DEFAULT_ND_JOINSEL   0.001
 
#define FALLBACK_ND_SEL   0.2
 More modest fallback selectivity factor.
 
#define FALLBACK_ND_JOINSEL   0.3
 
#define MAX_NUM_BINS   50
 
#define BIN_MIN_SIZE   10
 

Functions

Datum gserialized_gist_joinsel (PG_FUNCTION_ARGS)
 
Datum gserialized_gist_joinsel_2d (PG_FUNCTION_ARGS)
 
Datum gserialized_gist_joinsel_nd (PG_FUNCTION_ARGS)
 
Datum gserialized_gist_sel (PG_FUNCTION_ARGS)
 
Datum gserialized_gist_sel_2d (PG_FUNCTION_ARGS)
 
Datum gserialized_gist_sel_nd (PG_FUNCTION_ARGS)
 
Datum gserialized_analyze_nd (PG_FUNCTION_ARGS)
 
Datum gserialized_estimated_extent (PG_FUNCTION_ARGS)
 
Datum _postgis_gserialized_index_extent (PG_FUNCTION_ARGS)
 
Datum _postgis_gserialized_sel (PG_FUNCTION_ARGS)
 
Datum _postgis_gserialized_joinsel (PG_FUNCTION_ARGS)
 
Datum _postgis_gserialized_stats (PG_FUNCTION_ARGS)
 
static Oid table_get_spatial_index (Oid tbl_oid, int16 attnum, int *key_type, int16 *idx_attnum)
 
static GBOXspatial_index_read_extent (Oid idx_oid, int idx_att_num, int key_type)
 
float8 gserialized_joinsel_internal (PlannerInfo *root, List *args, JoinType jointype, int mode)
 
float8 gserialized_sel_internal (PlannerInfo *root, List *args, int varRelid, int mode)
 This function should return an estimation of the number of rows returned by a query involving an overlap check ( it's the restrict function for the && operator )
 
Datum geometry_estimated_extent (PG_FUNCTION_ARGS)
 
static int gbox_ndims (const GBOX *gbox)
 Given that geodetic boxes are X/Y/Z regardless of the underlying geometry dimensionality and other boxes are guided by HAS_Z/HAS_M in their dimensionality, we have a little utility function to make it easy.
 
static int text_p_get_mode (const text *txt)
 Utility function to see if the first letter of the mode argument is 'N'.
 
static int cmp_int (const void *a, const void *b)
 Integer comparison function for qsort.
 
static int range_full (int *vals, int nvals)
 The difference between the fourth and first quintile values, the "inter-quintile range".
 
static double total_double (const double *vals, int nvals)
 Given double array, return sum of values.
 
static char * nd_box_to_json (const ND_BOX *nd_box, int ndims)
 Convert an ND_BOX to a JSON string for printing.
 
static char * nd_stats_to_json (const ND_STATS *nd_stats)
 Convert an ND_STATS to a JSON representation for external use.
 
static char * nd_stats_to_grid (const ND_STATS *stats)
 Create a printable view of the ND_STATS histogram.
 
static int nd_box_merge (const ND_BOX *source, ND_BOX *target)
 Expand the bounds of target to include source.
 
static int nd_box_init (ND_BOX *a)
 Zero out an ND_BOX.
 
static int nd_box_init_bounds (ND_BOX *a)
 Prepare an ND_BOX for bounds calculation: set the maxes to the smallest thing possible and the mins to the largest.
 
static void nd_box_from_gbox (const GBOX *gbox, ND_BOX *nd_box)
 Set the values of an ND_BOX from a GBOX.
 
static int nd_box_intersects (const ND_BOX *a, const ND_BOX *b, int ndims)
 Return true if ND_BOX a overlaps b, false otherwise.
 
static int nd_box_contains (const ND_BOX *a, const ND_BOX *b, int ndims)
 Return true if ND_BOX a contains b, false otherwise.
 
static int nd_box_expand (ND_BOX *nd_box, double expansion_factor)
 Expand an ND_BOX ever so slightly.
 
static int nd_box_overlap (const ND_STATS *nd_stats, const ND_BOX *nd_box, ND_IBOX *nd_ibox)
 What stats cells overlap with this ND_BOX? Put the lowest cell addresses in ND_IBOX->min and the highest in ND_IBOX->max.
 
static int nd_box_array_distribution (const ND_BOX **nd_boxes, int num_boxes, const ND_BOX *extent, int ndims, double *distribution)
 Calculate how much a set of boxes is homogeneously distributed or contentrated within one dimension, returning the range_quintile of of the overlap counts per cell in a uniform partition of the extent of the dimension.
 
static int nd_increment (ND_IBOX *ibox, int ndims, int *counter)
 Given an n-d index array (counter), and a domain to increment it in (ibox) increment it by one, unless it's already at the max of the domain, in which case return false.
 
static ND_STATSpg_nd_stats_from_tuple (HeapTuple stats_tuple, int mode)
 
static ND_STATSpg_get_nd_stats (const Oid table_oid, AttrNumber att_num, int mode, bool only_parent)
 Pull the stats object from the PgSQL system catalogs.
 
static ND_STATSpg_get_nd_stats_by_name (const Oid table_oid, const text *att_text, int mode, bool only_parent)
 Pull the stats object from the PgSQL system catalogs.
 
static float8 estimate_join_selectivity (const ND_STATS *s1, const ND_STATS *s2)
 Given two statistics histograms, what is the selectivity of a join driven by the && or &&& operator?
 
 PG_FUNCTION_INFO_V1 (gserialized_gist_joinsel_nd)
 For (geometry &&& geometry) and (geography && geography) we call into the N-D mode.
 
 PG_FUNCTION_INFO_V1 (gserialized_gist_joinsel_2d)
 For (geometry && geometry) we call into the 2-D mode.
 
 PG_FUNCTION_INFO_V1 (gserialized_gist_joinsel)
 Join selectivity of the && operator.
 
static void compute_gserialized_stats_mode (VacAttrStats *stats, AnalyzeAttrFetchFunc fetchfunc, int sample_rows, double total_rows, int mode)
 The gserialized_analyze_nd sets this function as a callback on the stats object when called by the ANALYZE command.
 
static void compute_gserialized_stats (VacAttrStats *stats, AnalyzeAttrFetchFunc fetchfunc, int sample_rows, double total_rows)
 In order to do useful selectivity calculations in both 2-D and N-D modes, we actually have to generate two stats objects, one for 2-D and one for N-D.
 
 PG_FUNCTION_INFO_V1 (gserialized_analyze_nd)
 This function will be called when the ANALYZE command is run on a column of the "geometry" or "geography" type.
 
static float8 estimate_selectivity (const GBOX *box, const ND_STATS *nd_stats, int mode)
 This function returns an estimate of the selectivity of a search GBOX by looking at data in the ND_STATS structure.
 
 PG_FUNCTION_INFO_V1 (_postgis_gserialized_stats)
 Utility function to print the statistics information for a given table/column in JSON.
 
 PG_FUNCTION_INFO_V1 (_postgis_gserialized_sel)
 Utility function to read the calculated selectivity for a given search box and table/column.
 
 PG_FUNCTION_INFO_V1 (_postgis_gserialized_joinsel)
 Utility function to read the calculated join selectivity for a pair of tables.
 
 PG_FUNCTION_INFO_V1 (gserialized_gist_sel_2d)
 For (geometry && geometry) we call into the 2-D mode.
 
 PG_FUNCTION_INFO_V1 (gserialized_gist_sel_nd)
 For (geometry &&& geometry) and (geography && geography) we call into the N-D mode.
 
 PG_FUNCTION_INFO_V1 (gserialized_gist_sel)
 
static int16 index_has_attr (Oid index_oid, Oid table_oid, int16 table_attnum)
 
static int index_get_am (Oid index_oid)
 
static int index_get_keytype (Oid index_oid, int16 index_attnum)
 
 PG_FUNCTION_INFO_V1 (_postgis_gserialized_index_extent)
 
static bool get_attnum_attypid (Oid table_oid, const char *col, int16 *attnum, Oid *atttypid)
 
 PG_FUNCTION_INFO_V1 (gserialized_estimated_extent)
 Return the estimated extent of the table looking at gathered statistics (or NULL if no statistics have been gathered).
 
 PG_FUNCTION_INFO_V1 (geometry_estimated_extent)