PostGIS  2.5.0dev-r@@SVN_REVISION@@
Datum gserialized_estimated_extent ( PG_FUNCTION_ARGS  )

Definition at line 2283 of file gserialized_estimate.c.

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(), text2cstring(), GBOX::xmax, GBOX::xmin, GBOX::ymax, and GBOX::ymin.

Referenced by geometry_estimated_extent().

2284 {
2285  char *nsp = NULL;
2286  char *tbl = NULL;
2287  text *col = NULL;
2288  char *nsp_tbl = NULL;
2289  Oid tbl_oid, idx_oid;
2290  ND_STATS *nd_stats;
2291  GBOX *gbox = NULL;
2292  bool only_parent = false;
2293  int key_type;
2294 
2295  if ( PG_NARGS() == 4 )
2296  {
2297  nsp = text2cstring(PG_GETARG_TEXT_P(0));
2298  tbl = text2cstring(PG_GETARG_TEXT_P(1));
2299  col = PG_GETARG_TEXT_P(2);
2300  only_parent = PG_GETARG_BOOL(3);
2301  nsp_tbl = palloc(strlen(nsp) + strlen(tbl) + 6);
2302  sprintf(nsp_tbl, "\"%s\".\"%s\"", nsp, tbl);
2303  tbl_oid = DatumGetObjectId(DirectFunctionCall1(regclassin, CStringGetDatum(nsp_tbl)));
2304  pfree(nsp_tbl);
2305  }
2306  else if ( PG_NARGS() == 3 )
2307  {
2308  nsp = text2cstring(PG_GETARG_TEXT_P(0));
2309  tbl = text2cstring(PG_GETARG_TEXT_P(1));
2310  col = PG_GETARG_TEXT_P(2);
2311  nsp_tbl = palloc(strlen(nsp) + strlen(tbl) + 6);
2312  sprintf(nsp_tbl, "\"%s\".\"%s\"", nsp, tbl);
2313  tbl_oid = DatumGetObjectId(DirectFunctionCall1(regclassin, CStringGetDatum(nsp_tbl)));
2314  pfree(nsp_tbl);
2315  }
2316  else if ( PG_NARGS() == 2 )
2317  {
2318  tbl = text2cstring(PG_GETARG_TEXT_P(0));
2319  col = PG_GETARG_TEXT_P(1);
2320  nsp_tbl = palloc(strlen(tbl) + 3);
2321  sprintf(nsp_tbl, "\"%s\"", tbl);
2322  tbl_oid = DatumGetObjectId(DirectFunctionCall1(regclassin, CStringGetDatum(nsp_tbl)));
2323  pfree(nsp_tbl);
2324  }
2325  else
2326  {
2327  elog(ERROR, "estimated_extent() called with wrong number of arguments");
2328  PG_RETURN_NULL();
2329  }
2330 
2331 #if 1
2332  /* Read the extent from the head of the spatial index, if there is one */
2333  idx_oid = table_get_spatial_index(tbl_oid, col, &key_type);
2334  if (!idx_oid)
2335  elog(DEBUG2, "index for \"%s.%s\" does not exist", tbl, text2cstring(col));
2336  gbox = spatial_index_read_extent(idx_oid, key_type);
2337 #endif
2338 
2339  /* Fall back to reading the stats, if no index answer */
2340  if (!gbox)
2341  {
2342  /* Estimated extent only returns 2D bounds, so use mode 2 */
2343  nd_stats = pg_get_nd_stats_by_name(tbl_oid, col, 2, only_parent);
2344 
2345  /* Error out on no stats */
2346  if ( ! nd_stats ) {
2347  elog(WARNING, "stats for \"%s.%s\" do not exist", tbl, text2cstring(col));
2348  PG_RETURN_NULL();
2349  }
2350 
2351  /* Construct the box */
2352  gbox = palloc(sizeof(GBOX));
2353  FLAGS_SET_GEODETIC(gbox->flags, 0);
2354  FLAGS_SET_Z(gbox->flags, 0);
2355  FLAGS_SET_M(gbox->flags, 0);
2356  gbox->xmin = nd_stats->extent.min[0];
2357  gbox->xmax = nd_stats->extent.max[0];
2358  gbox->ymin = nd_stats->extent.min[1];
2359  gbox->ymax = nd_stats->extent.max[1];
2360  pfree(nd_stats);
2361  }
2362 
2363  PG_RETURN_POINTER(gbox);
2364 }
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:292
#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:293
double xmin
Definition: liblwgeom.h:291
static Oid table_get_spatial_index(Oid tbl_oid, text *col, int *key_type)
char * text2cstring(const text *textptr)
double ymax
Definition: liblwgeom.h:294
uint8_t flags
Definition: liblwgeom.h:290
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: