22 #include "../postgis_config.h"
24 #if POSTGIS_PGSQL_VERSION >= 120
29 #include "access/htup_details.h"
30 #include "access/stratnum.h"
31 #include "catalog/namespace.h"
32 #include "catalog/pg_opfamily.h"
33 #include "catalog/pg_type_d.h"
34 #include "catalog/pg_am_d.h"
35 #include "nodes/supportnodes.h"
36 #include "nodes/nodeFuncs.h"
37 #include "nodes/makefuncs.h"
38 #include "optimizer/optimizer.h"
39 #include "parser/parse_func.h"
40 #include "utils/array.h"
41 #include "utils/builtins.h"
42 #include "utils/lsyscache.h"
43 #include "utils/numeric.h"
44 #include "utils/syscache.h"
48 #include "lwgeom_pg.h"
122 if (first_type == postgis_oid(GEOMETRYOID))
127 if (first_type == postgis_oid(GEOGRAPHYOID))
132 return InvalidStrategy;
192 {
"gist_geometry_ops_2d", 2},
193 {
"gist_geometry_ops_nd", 3},
194 {
"brin_geometry_inclusion_ops_2d", 2},
195 {
"brin_geometry_inclusion_ops_3d", 3},
196 {
"brin_geometry_inclusion_ops_4d", 3},
197 {
"spgist_geometry_ops_2d", 2},
198 {
"spgist_geometry_ops_3d", 3},
199 {
"spgist_geometry_ops_nd", 3},
215 const char *fn_name = get_func_name(funcid);
219 if(strcmp(idxfns->
fn_name, fn_name) == 0)
239 return idxdims->
dims;
256 Form_pg_opfamily familyform;
259 HeapTuple familytup = SearchSysCache1(OPFAMILYOID, ObjectIdGetDatum(opfamilyoid));
260 if (!HeapTupleIsValid(familytup))
261 elog(ERROR,
"cache lookup failed for operator family %u", opfamilyoid);
262 familyform = (Form_pg_opfamily) GETSTRUCT(familytup);
263 opfamilyam = familyform->opfmethod;
264 opfamilyname = NameStr(familyform->opfname);
265 elog(DEBUG3,
"%s: found opfamily %s [%u]", __func__, opfamilyname, opfamilyam);
269 ReleaseSysCache(familytup);
282 const Oid radiustype = FLOAT8OID;
283 const Oid expandfn_args[2] = {geotype, radiustype};
284 const bool noError =
true;
286 char *nspname = get_namespace_name(get_func_namespace(callingfunc));
287 List *expandfn_name = list_make2(makeString(nspname), makeString(
"st_expand"));
288 Oid expandfn_oid = LookupFuncName(expandfn_name, 2, expandfn_args, noError);
289 if (expandfn_oid == InvalidOid)
297 expandfn_name = list_make2(makeString(nspname), makeString(
"_st_expand"));
298 expandfn_oid = LookupFuncName(expandfn_name, 2, expandfn_args, noError);
299 if (expandfn_oid == InvalidOid)
300 elog(ERROR,
"%s: unable to lookup 'st_expand(Oid[%u], Oid[%u])'", __func__, geotype, radiustype);
324 Node *rawreq = (Node *) PG_GETARG_POINTER(0);
331 postgis_initialize_cache();
333 if (IsA(rawreq, SupportRequestSelectivity))
335 SupportRequestSelectivity *req = (SupportRequestSelectivity *) rawreq;
345 POSTGIS_DEBUGF(2,
"%s: got selectivity %g", __func__, req->selectivity);
346 PG_RETURN_POINTER(req);
353 if (IsA(rawreq, SupportRequestIndexCondition))
355 SupportRequestIndexCondition *req = (SupportRequestIndexCondition *) rawreq;
357 if (is_funcclause(req->node))
359 FuncExpr *clause = (FuncExpr *) req->node;
360 Oid funcid = clause->funcid;
362 Oid opfamilyoid = req->opfamily;
366 int nargs = list_length(clause->args);
367 Node *leftarg, *rightarg;
368 Oid leftdatatype, rightdatatype, oproid;
369 bool swapped =
false;
378 uint8_t opfamilydims;
380 if (opfamilyam != GIST_AM_OID &&
381 opfamilyam != SPGIST_AM_OID &&
382 opfamilyam != BRIN_AM_OID)
384 PG_RETURN_POINTER((Node *)NULL);
391 if (req->indexarg > 1)
392 PG_RETURN_POINTER((Node *)NULL);
399 if (opfamilydims == 3 && idxfn.dims != 3)
400 PG_RETURN_POINTER((Node *)NULL);
405 if (nargs < 2 || nargs < idxfn.expand_arg)
406 elog(ERROR,
"%s: associated with function with %d arguments", __func__, nargs);
418 if (req->indexarg == 0)
420 leftarg = linitial(clause->args);
421 rightarg = lsecond(clause->args);
425 rightarg = linitial(clause->args);
426 leftarg = lsecond(clause->args);
434 leftdatatype = exprType(leftarg);
435 rightdatatype = exprType(rightarg);
442 oproid = get_opfamily_member(opfamilyoid,
446 if (!OidIsValid(oproid))
448 "no spatial operator found for '%s': opfamily %u type %d",
460 if (idxfn.expand_arg)
463 Node *radiusarg = (Node *) list_nth(clause->args, idxfn.expand_arg-1);
466 FuncExpr *expandexpr = makeFuncExpr(expandfn_oid, rightdatatype,
467 list_make2(rightarg, radiusarg),
468 InvalidOid, InvalidOid, COERCE_EXPLICIT_CALL);
474 #if POSTGIS_PGSQL_VERSION >= 140
475 if (!is_pseudo_constant_for_index(req->root, (Node*)expandexpr, req->index))
477 if (!is_pseudo_constant_for_index((Node*)expandexpr, req->index))
479 PG_RETURN_POINTER((Node*)NULL);
482 expr = make_opclause(oproid, BOOLOID,
false,
483 (Expr *) leftarg, (Expr *) expandexpr,
484 InvalidOid, InvalidOid);
486 ret = (Node *)(list_make1(expr));
501 #if POSTGIS_PGSQL_VERSION >= 140
502 if (!is_pseudo_constant_for_index(req->root, rightarg, req->index))
504 if (!is_pseudo_constant_for_index(rightarg, req->index))
506 PG_RETURN_POINTER((Node*)NULL);
515 oproid = get_commutator(oproid);
516 if (!OidIsValid(oproid))
517 PG_RETURN_POINTER((Node *)NULL);
520 expr = make_opclause(oproid, BOOLOID,
false,
521 (Expr *) leftarg, (Expr *) rightarg,
522 InvalidOid, InvalidOid);
524 ret = (Node *)(list_make1(expr));
534 PG_RETURN_POINTER(ret);
538 elog(WARNING,
"support function '%s' called from unsupported spatial function", __func__);
543 PG_RETURN_POINTER(ret);
static const IndexableFunction IndexableFunctions[]
static const int16 GeometryStrategies[]
@ ST_LINECROSSINGDIRECTION_IDX
@ ST_CONTAINSPROPERLY_IDX
static bool needsSpatialIndex(Oid funcid, IndexableFunction *idxfn)
Datum postgis_index_supportfn(PG_FUNCTION_ARGS)
static Oid expandFunctionOid(Oid geotype, Oid callingfunc)
PG_FUNCTION_INFO_V1(postgis_index_supportfn)
static const OpFamilyDim OpFamilyDims[]
static Oid opFamilyAmOid(Oid opfamilyoid, uint8_t *dims)
float8 gserialized_sel_internal(PlannerInfo *root, List *args, int varRelid, int mode)
This function should return an estimation of the number of rows returned by a query involving an over...
float8 gserialized_joinsel_internal(PlannerInfo *root, List *args, JoinType jointype, int mode)
static uint8_t opFamilyDim(const char *opfamily)
static const int16 GeographyStrategies[]
static int16 get_strategy_by_type(Oid first_type, uint16_t index)
This library is the generic geometry handling section of PostGIS.
const char * opfamilyname