2446{
2447 text *coltxt = NULL;
2448 char *col = NULL;
2449 int16 attnum, idx_attnum;
2450 Oid atttypid = InvalidOid;
2451 char nsp_tbl[2*NAMEDATALEN+6];
2452 char *tbl;
2453 Oid tbl_oid, idx_oid = 0;
2456 bool only_parent = false;
2457 int key_type;
2458 Oid geographyOid = postgis_oid(GEOGRAPHYOID);
2459 Oid geometryOid = postgis_oid(GEOMETRYOID);
2460
2461
2462 postgis_initialize_cache();
2463
2464 if (PG_NARGS() < 2 || PG_NARGS() > 4)
2465 elog(ERROR, "ST_EstimatedExtent() called with wrong number of arguments");
2466
2467 if ( PG_NARGS() == 4 )
2468 {
2469 only_parent = PG_GETARG_BOOL(3);
2470 }
2471 if ( PG_NARGS() >= 3 )
2472 {
2473 char *nsp = text_to_cstring(PG_GETARG_TEXT_P(0));
2474 tbl = text_to_cstring(PG_GETARG_TEXT_P(1));
2475 coltxt = PG_GETARG_TEXT_P(2);
2476 snprintf(nsp_tbl, sizeof(nsp_tbl), "\"%s\".\"%s\"", nsp, tbl);
2477 }
2478 if ( PG_NARGS() == 2 )
2479 {
2480 tbl = text_to_cstring(PG_GETARG_TEXT_P(0));
2481 coltxt = PG_GETARG_TEXT_P(1);
2482 snprintf(nsp_tbl, sizeof(nsp_tbl), "\"%s\"", tbl);
2483 }
2484
2485
2486 tbl_oid = DatumGetObjectId(DirectFunctionCall1(regclassin, CStringGetDatum(nsp_tbl)));
2487 if (!tbl_oid)
2488 elog(ERROR, "cannot lookup table %s", nsp_tbl);
2489
2490
2491 col = text_to_cstring(coltxt);
2493 elog(ERROR, "column %s.\"%s\" does not exist", nsp_tbl, col);
2494
2495
2496 if ((atttypid != geographyOid) && (atttypid != geometryOid))
2497 {
2498 elog(ERROR, "column %s.\"%s\" must be a geometry or geography", nsp_tbl, col);
2499 }
2500
2501
2502
2504 if (idx_oid != InvalidOid)
2505 {
2506
2508 elog(DEBUG3, "index for %s.\"%s\" exists, reading gbox from there", nsp_tbl, col);
2509 if (!gbox) PG_RETURN_NULL();
2510 }
2511
2512
2513 else
2514 {
2515 int stats_mode = 2;
2516 elog(DEBUG3, "index for %s.\"%s\" does not exist", nsp_tbl, col);
2517
2518
2519 if (atttypid == geographyOid)
2520 stats_mode = 3;
2521
2522
2524
2525
2526 if (!nd_stats)
2527 {
2528 elog(WARNING, "stats for \"%s.%s\" do not exist", tbl, col);
2529 PG_RETURN_NULL();
2530 }
2531
2532
2538 if (stats_mode != 2)
2539 {
2543 }
2544
2545 pfree(nd_stats);
2546 }
2547
2548
2549
2550 if (atttypid == geographyOid)
2551 {
2554 PG_RETURN_POINTER(gbox_planar);
2555 }
2556 else
2557 PG_RETURN_POINTER(gbox);
2558}
GBOX * gbox_new(lwflags_t flags)
Create a new gbox with the dimensionality indicated by the flags.
static bool get_attnum_attypid(Oid table_oid, const char *col, int16 *attnum, Oid *atttypid)
static GBOX * spatial_index_read_extent(Oid idx_oid, int idx_att_num, int key_type)
static Oid table_get_spatial_index(Oid tbl_oid, int16 attnum, int *key_type, int16 *idx_attnum)
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_Z(flags, value)
int gbox_geocentric_get_gbox_cartesian(const GBOX *gbox_geocentric, GBOX *gbox_planar)