PostGIS  2.5.1dev-r@@SVN_REVISION@@

◆ gserialized_estimated_extent()

Datum gserialized_estimated_extent ( PG_FUNCTION_ARGS  )

Definition at line 2302 of file gserialized_estimate.c.

References ND_STATS_T::extent, GBOX::flags, FLAGS_SET_GEODETIC, FLAGS_SET_M, FLAGS_SET_Z, geometry_estimated_extent(), ND_BOX_T::max, ND_BOX_T::min, PG_FUNCTION_INFO_V1(), pg_get_nd_stats_by_name(), spatial_index_read_extent(), table_get_spatial_index(), text_to_cstring(), GBOX::xmax, GBOX::xmin, GBOX::ymax, and GBOX::ymin.

Referenced by geometry_estimated_extent(), and gserialized_gist_sel().

2303 {
2304  char *nsp = NULL;
2305  char *tbl = NULL;
2306  text *col = NULL;
2307  char *nsp_tbl = NULL;
2308  Oid tbl_oid, idx_oid;
2309  ND_STATS *nd_stats;
2310  GBOX *gbox = NULL;
2311  bool only_parent = false;
2312  int key_type;
2313 
2314  if ( PG_NARGS() == 4 )
2315  {
2316  nsp = text_to_cstring(PG_GETARG_TEXT_P(0));
2317  tbl = text_to_cstring(PG_GETARG_TEXT_P(1));
2318  col = PG_GETARG_TEXT_P(2);
2319  only_parent = PG_GETARG_BOOL(3);
2320  nsp_tbl = palloc(strlen(nsp) + strlen(tbl) + 6);
2321  sprintf(nsp_tbl, "\"%s\".\"%s\"", nsp, tbl);
2322  tbl_oid = DatumGetObjectId(DirectFunctionCall1(regclassin, CStringGetDatum(nsp_tbl)));
2323  pfree(nsp_tbl);
2324  }
2325  else if ( PG_NARGS() == 3 )
2326  {
2327  nsp = text_to_cstring(PG_GETARG_TEXT_P(0));
2328  tbl = text_to_cstring(PG_GETARG_TEXT_P(1));
2329  col = PG_GETARG_TEXT_P(2);
2330  nsp_tbl = palloc(strlen(nsp) + strlen(tbl) + 6);
2331  sprintf(nsp_tbl, "\"%s\".\"%s\"", nsp, tbl);
2332  tbl_oid = DatumGetObjectId(DirectFunctionCall1(regclassin, CStringGetDatum(nsp_tbl)));
2333  pfree(nsp_tbl);
2334  }
2335  else if ( PG_NARGS() == 2 )
2336  {
2337  tbl = text_to_cstring(PG_GETARG_TEXT_P(0));
2338  col = PG_GETARG_TEXT_P(1);
2339  nsp_tbl = palloc(strlen(tbl) + 3);
2340  sprintf(nsp_tbl, "\"%s\"", tbl);
2341  tbl_oid = DatumGetObjectId(DirectFunctionCall1(regclassin, CStringGetDatum(nsp_tbl)));
2342  pfree(nsp_tbl);
2343  }
2344  else
2345  {
2346  elog(ERROR, "estimated_extent() called with wrong number of arguments");
2347  PG_RETURN_NULL();
2348  }
2349 
2350 #if 1
2351  /* Read the extent from the head of the spatial index, if there is one */
2352  idx_oid = table_get_spatial_index(tbl_oid, col, &key_type);
2353  if (!idx_oid)
2354  elog(DEBUG2, "index for \"%s.%s\" does not exist", tbl, text_to_cstring(col));
2355  gbox = spatial_index_read_extent(idx_oid, key_type);
2356 #endif
2357 
2358  /* Fall back to reading the stats, if no index answer */
2359  if (!gbox)
2360  {
2361  /* Estimated extent only returns 2D bounds, so use mode 2 */
2362  nd_stats = pg_get_nd_stats_by_name(tbl_oid, col, 2, only_parent);
2363 
2364  /* Error out on no stats */
2365  if ( ! nd_stats ) {
2366  elog(WARNING, "stats for \"%s.%s\" do not exist", tbl, text_to_cstring(col));
2367  PG_RETURN_NULL();
2368  }
2369 
2370  /* Construct the box */
2371  gbox = palloc(sizeof(GBOX));
2372  FLAGS_SET_GEODETIC(gbox->flags, 0);
2373  FLAGS_SET_Z(gbox->flags, 0);
2374  FLAGS_SET_M(gbox->flags, 0);
2375  gbox->xmin = nd_stats->extent.min[0];
2376  gbox->xmax = nd_stats->extent.max[0];
2377  gbox->ymin = nd_stats->extent.min[1];
2378  gbox->ymax = nd_stats->extent.max[1];
2379  pfree(nd_stats);
2380  }
2381 
2382  PG_RETURN_POINTER(gbox);
2383 }
char * text_to_cstring(const text *textptr)
static ND_STATS * pg_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.
double xmax
Definition: liblwgeom.h:295
#define FLAGS_SET_GEODETIC(flags, value)
Definition: liblwgeom.h:148
#define FLAGS_SET_Z(flags, value)
Definition: liblwgeom.h:145
double ymin
Definition: liblwgeom.h:296
double xmin
Definition: liblwgeom.h:294
static Oid table_get_spatial_index(Oid tbl_oid, text *col, int *key_type)
double ymax
Definition: liblwgeom.h:297
uint8_t flags
Definition: liblwgeom.h:293
static GBOX * spatial_index_read_extent(Oid idx_oid, int key_type)
float4 max[ND_DIMS]
float4 min[ND_DIMS]
N-dimensional statistics structure.
#define FLAGS_SET_M(flags, value)
Definition: liblwgeom.h:146
Here is the call graph for this function:
Here is the caller graph for this function: