PostGIS  3.4.0dev-r@@SVN_REVISION@@

◆ gserialized_estimated_extent()

Datum gserialized_estimated_extent ( PG_FUNCTION_ARGS  )

Definition at line 2294 of file gserialized_estimate.c.

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