PostGIS  2.5.0dev-r@@SVN_REVISION@@

◆ gserialized_estimated_extent()

Datum gserialized_estimated_extent ( PG_FUNCTION_ARGS  )

Definition at line 2286 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().

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