PostGIS  3.3.9dev-r@@SVN_REVISION@@

◆ gserialized_estimated_extent()

Datum gserialized_estimated_extent ( PG_FUNCTION_ARGS  )

Definition at line 2296 of file gserialized_estimate.c.

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