PostGIS  3.1.6dev-r@@SVN_REVISION@@

◆ gserialized_estimated_extent()

Datum gserialized_estimated_extent ( PG_FUNCTION_ARGS  )

Definition at line 2314 of file gserialized_estimate.c.

2315 {
2316  char *nsp = NULL;
2317  char *tbl = NULL;
2318  text *col = NULL;
2319  char *nsp_tbl = NULL;
2320  Oid tbl_oid, idx_oid = 0;
2321  ND_STATS *nd_stats;
2322  GBOX *gbox = NULL;
2323  bool only_parent = false;
2324  int key_type, att_num;
2325  size_t sz;
2326 
2327  /* We need to initialize the internal cache to access it later via postgis_oid() */
2328  postgis_initialize_cache();
2329 
2330  if ( PG_NARGS() == 4 )
2331  {
2332  nsp = text_to_cstring(PG_GETARG_TEXT_P(0));
2333  tbl = text_to_cstring(PG_GETARG_TEXT_P(1));
2334  col = PG_GETARG_TEXT_P(2);
2335  only_parent = PG_GETARG_BOOL(3);
2336  sz = strlen(nsp) + strlen(tbl) + 6;
2337  nsp_tbl = palloc(sz);
2338  snprintf(nsp_tbl, sz, "\"%s\".\"%s\"", nsp, tbl);
2339  tbl_oid = DatumGetObjectId(DirectFunctionCall1(regclassin, CStringGetDatum(nsp_tbl)));
2340  pfree(nsp_tbl);
2341  }
2342  else if ( PG_NARGS() == 3 )
2343  {
2344  nsp = text_to_cstring(PG_GETARG_TEXT_P(0));
2345  tbl = text_to_cstring(PG_GETARG_TEXT_P(1));
2346  col = PG_GETARG_TEXT_P(2);
2347  sz = strlen(nsp) + strlen(tbl) + 6;
2348  nsp_tbl = palloc(sz);
2349  snprintf(nsp_tbl, sz, "\"%s\".\"%s\"", nsp, tbl);
2350  tbl_oid = DatumGetObjectId(DirectFunctionCall1(regclassin, CStringGetDatum(nsp_tbl)));
2351  pfree(nsp_tbl);
2352  }
2353  else if ( PG_NARGS() == 2 )
2354  {
2355  tbl = text_to_cstring(PG_GETARG_TEXT_P(0));
2356  col = PG_GETARG_TEXT_P(1);
2357  sz = strlen(tbl) + 3;
2358  nsp_tbl = palloc(sz);
2359  snprintf(nsp_tbl, sz, "\"%s\"", tbl);
2360  tbl_oid = DatumGetObjectId(DirectFunctionCall1(regclassin, CStringGetDatum(nsp_tbl)));
2361  pfree(nsp_tbl);
2362  }
2363  else
2364  {
2365  elog(ERROR, "estimated_extent() called with wrong number of arguments");
2366  PG_RETURN_NULL();
2367  }
2368 
2369  /* Read the extent from the head of the spatial index, if there is one */
2370 
2371  idx_oid = table_get_spatial_index(tbl_oid, col, &key_type, &att_num);
2372  if (idx_oid)
2373  {
2374  /* TODO: how about only_parent ? */
2375  gbox = spatial_index_read_extent(idx_oid, key_type, att_num);
2376  POSTGIS_DEBUGF(2, "index for \"%s.%s\" exists, reading gbox from there", tbl, text_to_cstring(col));
2377  if ( ! gbox ) PG_RETURN_NULL();
2378  }
2379  else
2380  {
2381  POSTGIS_DEBUGF(2, "index for \"%s.%s\" does not exist", tbl, text_to_cstring(col));
2382 
2383  /* Fall back to reading the stats, if no index is found */
2384 
2385  /* Estimated extent only returns 2D bounds, so use mode 2 */
2386  nd_stats = pg_get_nd_stats_by_name(tbl_oid, col, 2, only_parent);
2387 
2388  /* Error out on no stats */
2389  if ( ! nd_stats ) {
2390  elog(WARNING, "stats for \"%s.%s\" do not exist", tbl, text_to_cstring(col));
2391  PG_RETURN_NULL();
2392  }
2393 
2394  /* Construct the box */
2395  gbox = palloc(sizeof(GBOX));
2396  FLAGS_SET_GEODETIC(gbox->flags, 0);
2397  FLAGS_SET_Z(gbox->flags, 0);
2398  FLAGS_SET_M(gbox->flags, 0);
2399  gbox->xmin = nd_stats->extent.min[0];
2400  gbox->xmax = nd_stats->extent.max[0];
2401  gbox->ymin = nd_stats->extent.min[1];
2402  gbox->ymax = nd_stats->extent.max[1];
2403  pfree(nd_stats);
2404  }
2405 
2406  PG_RETURN_POINTER(gbox);
2407 }
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.
static GBOX * spatial_index_read_extent(Oid idx_oid, int key_type, int att_num)
static Oid table_get_spatial_index(Oid tbl_oid, text *col, int *key_type, int *att_num)
#define FLAGS_SET_GEODETIC(flags, value)
Definition: liblwgeom.h:189
#define FLAGS_SET_M(flags, value)
Definition: liblwgeom.h:187
#define FLAGS_SET_Z(flags, value)
Definition: liblwgeom.h:186
double ymax
Definition: liblwgeom.h:371
double xmax
Definition: liblwgeom.h:369
double ymin
Definition: liblwgeom.h:370
double xmin
Definition: liblwgeom.h:368
lwflags_t flags
Definition: liblwgeom.h:367
float4 max[ND_DIMS]
float4 min[ND_DIMS]
N-dimensional statistics structure.

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

Referenced by geometry_estimated_extent().

Here is the call graph for this function:
Here is the caller graph for this function: