PostGIS 3.0.6dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches

◆ gserialized_estimated_extent()

Datum gserialized_estimated_extent ( PG_FUNCTION_ARGS  )

Definition at line 2304 of file gserialized_estimate.c.

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