/*------------------------------------------------------------------------- * * deparse.c * Query deparser for postgres_fdw * * This file includes functions that examine query WHERE clauses to see * whether they're safe to send to the remote server for execution, as * well as functions to construct the query text to be sent. The latter * functionality is annoyingly duplicative of ruleutils.c, but there are * enough special considerations that it seems best to keep this separate. * One saving grace is that we only need deparse logic for node types that * we consider safe to send. * * We assume that the remote session's search_path is exactly "pg_catalog", * and thus we need schema-qualify all and only names outside pg_catalog. * * Portions Copyright (c) 2012-2017, PostgreSQL Global Development Group * * IDENTIFICATION * contrib/postgres_fdw/deparse.c * *------------------------------------------------------------------------- */ #include "postgres.h" #include "vops_fdw.h" #include "access/heapam.h" #include "access/htup_details.h" #include "access/sysattr.h" #include "catalog/pg_aggregate.h" #include "catalog/pg_namespace.h" #include "catalog/pg_operator.h" #include "catalog/pg_proc.h" #include "catalog/pg_type.h" #include "commands/defrem.h" #include "nodes/makefuncs.h" #include "nodes/nodeFuncs.h" #include "nodes/plannodes.h" #include "optimizer/clauses.h" #include "optimizer/prep.h" #include "optimizer/tlist.h" #if PG_VERSION_NUM>=120000 #include "optimizer/optimizer.h" #else #include "optimizer/var.h" #endif #include "parser/parsetree.h" #include "utils/builtins.h" #include "utils/hsearch.h" #include "utils/lsyscache.h" #include "utils/rel.h" #include "utils/syscache.h" #include "utils/typcache.h" #include "vops.h" /* * Global context for foreign_expr_walker's search of an expression tree. */ typedef struct foreign_glob_cxt { PlannerInfo *root; /* global planner state */ RelOptInfo *foreignrel; /* the foreign relation we are planning for */ Relids relids; /* relids of base relations in the underlying * scan */ } foreign_glob_cxt; /* * Context for deparseExpr */ typedef struct deparse_expr_cxt { PlannerInfo *root; /* global planner state */ RelOptInfo *foreignrel; /* the foreign relation we are planning for */ RelOptInfo *scanrel; /* the underlying scan relation. Same as * foreignrel, when that represents a join or * a base relation. */ StringInfo buf; /* output buffer to append to */ List **params_list; /* exprs that will become remote Params */ } deparse_expr_cxt; #define REL_ALIAS_PREFIX "r" /* Handy macro to add relation name qualification */ #define ADD_REL_QUALIFIER(buf, varno) \ appendStringInfo((buf), "%s%d.", REL_ALIAS_PREFIX, (varno)) /* * Functions to determine whether an expression can be evaluated safely on * remote server. */ static bool foreign_expr_walker(Node *node, foreign_glob_cxt *glob_cxt); static void deparseStringLiteral(StringInfo buf, const char *val); /* * Functions to construct string representation of a node tree. */ static void deparseTargetList(StringInfo buf, PlannerInfo *root, Index rtindex, Relation rel, bool is_returning, Bitmapset *attrs_used, bool qualify_col, List **retrieved_attrs); static void deparseExplicitTargetList(List *tlist, List **retrieved_attrs, deparse_expr_cxt *context); static void deparseColumnRef(StringInfo buf, int varno, int varattno, PlannerInfo *root, bool qualify_col); static void deparseExpr(Expr *expr, deparse_expr_cxt *context); static void deparseVar(Var *node, deparse_expr_cxt *context); static void deparseConst(Const *node, deparse_expr_cxt *context, int showtype); static void deparseParam(Param *node, deparse_expr_cxt *context); #if PG_VERSION_NUM>=120000 static void deparseSubscriptingRef(SubscriptingRef *node, deparse_expr_cxt *context); #else static void deparseArrayRef(ArrayRef *node, deparse_expr_cxt *context); #endif static void deparseFuncExpr(FuncExpr *node, deparse_expr_cxt *context); static void deparseOpExpr(OpExpr *node, deparse_expr_cxt *context); static void deparseOperatorName(StringInfo buf, Form_pg_operator opform); static void deparseDistinctExpr(DistinctExpr *node, deparse_expr_cxt *context); static void deparseScalarArrayOpExpr(ScalarArrayOpExpr *node, deparse_expr_cxt *context); static void deparseRelabelType(RelabelType *node, deparse_expr_cxt *context); static void deparseBoolExpr(BoolExpr *node, deparse_expr_cxt *context); static void deparseNullTest(NullTest *node, deparse_expr_cxt *context); static void deparseArrayExpr(ArrayExpr *node, deparse_expr_cxt *context); static void printRemoteParam(int paramindex, Oid paramtype, int32 paramtypmod, deparse_expr_cxt *context); static void printRemotePlaceholder(Oid paramtype, int32 paramtypmod, deparse_expr_cxt *context); static void deparseSelectSql(List *tlist, List **retrieved_attrs, deparse_expr_cxt *context); static void deparseLockingClause(deparse_expr_cxt *context); static void appendConditions(List *exprs, deparse_expr_cxt *context); static void deparseFromExprForRel(StringInfo buf, PlannerInfo *root, RelOptInfo *joinrel, bool use_alias, List **params_list); static void deparseFromExpr(List *quals, deparse_expr_cxt *context); static void deparseAggref(Aggref *node, deparse_expr_cxt *context); static void appendGroupByClause(List *tlist, deparse_expr_cxt *context); static void appendAggOrderBy(List *orderList, List *targetList, deparse_expr_cxt *context); static void appendFunctionName(Oid funcid, deparse_expr_cxt *context); static Node *deparseSortGroupClause(Index ref, List *tlist, deparse_expr_cxt *context); /* * Examine each qual clause in input_conds, and classify them into two groups, * which are returned as two lists: * - remote_conds contains expressions that can be evaluated remotely * - local_conds contains expressions that can't be evaluated remotely */ void vopsClassifyConditions(PlannerInfo *root, RelOptInfo *baserel, List *input_conds, List **remote_conds, List **local_conds) { ListCell *lc; *remote_conds = NIL; *local_conds = NIL; foreach(lc, input_conds) { RestrictInfo *ri = (RestrictInfo *) lfirst(lc); if (vops_is_foreign_expr(root, baserel, ri->clause)) *remote_conds = lappend(*remote_conds, ri); else *local_conds = lappend(*local_conds, ri); } } /* * Returns true if given expr is safe to evaluate on the foreign server. */ bool vops_is_foreign_expr(PlannerInfo *root, RelOptInfo *baserel, Expr *expr) { foreign_glob_cxt glob_cxt; PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) (baserel->fdw_private); /* * Check that the expression consists of nodes that are safe to execute * remotely. */ glob_cxt.root = root; glob_cxt.foreignrel = baserel; /* * For an upper relation, use relids from its underneath scan relation, * because the upperrel's own relids currently aren't set to anything * meaningful by the core code. For other relation, use their own relids. */ if (baserel->reloptkind == RELOPT_UPPER_REL) glob_cxt.relids = fpinfo->upperrel->relids; else glob_cxt.relids = baserel->relids; if (!foreign_expr_walker((Node *) expr, &glob_cxt)) return false; /* OK to evaluate on the remote server */ return true; } /* * Check if expression is safe to execute remotely, and return true if so. */ static bool foreign_expr_walker(Node *node, foreign_glob_cxt *glob_cxt) { PgFdwRelationInfo *fpinfo; /* Need do nothing for empty subexpressions */ if (node == NULL) return true; /* May need server info from baserel's fdw_private struct */ fpinfo = (PgFdwRelationInfo *) (glob_cxt->foreignrel->fdw_private); switch (nodeTag(node)) { case T_Var: { Var *var = (Var *) node; /* If the Var is from the foreign table */ if (bms_is_member(var->varno, glob_cxt->relids) && var->varlevelsup == 0) { if (var->varattno <= 0 || !bms_is_member(var->varattno - FirstLowInvalidHeapAttributeNumber, fpinfo->vops_attrs)) { return false; } } else { return false; } break; } case T_Const: case T_Param: break; #if PG_VERSION_NUM>=120000 case T_SubscriptingRef: { SubscriptingRef *ar = (SubscriptingRef *) node; #else case T_ArrayRef: { ArrayRef *ar = (ArrayRef *) node; #endif /* Assignment should not be in restrictions. */ if (ar->refassgnexpr != NULL) return false; /* * Recurse to remaining subexpressions. */ if (!foreign_expr_walker((Node *) ar->refupperindexpr, glob_cxt)) return false; if (!foreign_expr_walker((Node *) ar->reflowerindexpr, glob_cxt)) return false; if (!foreign_expr_walker((Node *) ar->refexpr, glob_cxt)) return false; } break; case T_FuncExpr: { FuncExpr *fe = (FuncExpr *) node; /* * Recurse to input subexpressions. */ if (!foreign_expr_walker((Node *) fe->args, glob_cxt)) return false; } break; case T_OpExpr: { OpExpr *oe = (OpExpr *) node; /* TODO: check here if operator is supported by VOPS */ if (!foreign_expr_walker((Node *) oe->args, glob_cxt)) return false; break; } case T_DistinctExpr: /* struct-equivalent to OpExpr */ case T_ScalarArrayOpExpr: case T_ArrayExpr: return false; case T_RelabelType: { RelabelType *r = (RelabelType *) node; if (!foreign_expr_walker((Node *) r->arg, glob_cxt)) return false; } break; case T_BoolExpr: { BoolExpr *b = (BoolExpr *) node; if (!foreign_expr_walker((Node *) b->args, glob_cxt)) return false; } break; case T_NullTest: { NullTest *nt = (NullTest *) node; if (!foreign_expr_walker((Node *) nt->arg, glob_cxt)) return false; } break; case T_List: { List *l = (List *) node; ListCell *lc; /* * Recurse to component subexpressions. */ foreach(lc, l) { if (!foreign_expr_walker((Node *) lfirst(lc), glob_cxt)) return false; } } break; case T_Aggref: { Aggref *agg = (Aggref *) node; ListCell *lc; /* Not safe to pushdown when not in grouping context */ if (glob_cxt->foreignrel->reloptkind != RELOPT_UPPER_REL) return false; /* Only non-split aggregates are pushable. */ if (agg->aggsplit != AGGSPLIT_SIMPLE) return false; /* Ordered aggregates and aggregates with filter are not supported by VOPS */ if (agg->aggorder || agg->aggfilter) return false; /* * Recurse to input args. aggdirectargs, aggorder and * aggdistinct are all present in args, so no need to check * their shippability explicitly. */ foreach(lc, agg->args) { Node *n = (Node *) lfirst(lc); /* If TargetEntry, extract the expression from it */ if (IsA(n, TargetEntry)) { TargetEntry *tle = (TargetEntry *) n; n = (Node *) tle->expr; } if (!foreign_expr_walker(n, glob_cxt)) return false; } } break; default: /* * If it's anything else, assume it's unsafe. This list can be * expanded later, but don't forget to add deparse support below. */ return false; } /* It looks OK */ return true; } /* * Convert type OID + typmod info into a type name we can ship to the remote * server. Someplace else had better have verified that this type name is * expected to be known on the remote end. * * This is almost just format_type_with_typemod(), except that if left to its * own devices, that function will make schema-qualification decisions based * on the local search_path, which is wrong. We must schema-qualify all * type names that are not in pg_catalog. We assume here that built-in types * are all in pg_catalog and need not be qualified; otherwise, qualify. */ char * vops_deparse_type_name(Oid type_oid, int32 typemod) { #if PG_VERSION_NUM>=110000 uint8 flags = FORMAT_TYPE_TYPEMOD_GIVEN; if (type_oid >= FirstBootstrapObjectId) flags |= FORMAT_TYPE_FORCE_QUALIFY; return format_type_extended(type_oid, typemod, flags); #else if (type_oid < FirstBootstrapObjectId) return format_type_with_typemod(type_oid, typemod); else return format_type_with_typemod_qualified(type_oid, typemod); #endif } /* * Build the targetlist for given relation to be deparsed as SELECT clause. * * The output targetlist contains the columns that need to be fetched from the * foreign server for the given relation. If foreignrel is an upper relation, * then the output targetlist can also contain expressions to be evaluated on * foreign server. */ List * vops_build_tlist_to_deparse(RelOptInfo *foreignrel) { List *tlist = NIL; PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) foreignrel->fdw_private; /* * For an upper relation, we have already built the target list while * checking shippability, so just return that. */ if (foreignrel->reloptkind == RELOPT_UPPER_REL) return fpinfo->grouped_tlist; /* * We require columns specified in foreignrel->reltarget->exprs and those * required for evaluating the local conditions. */ tlist = add_to_flat_tlist(tlist, pull_var_clause((Node *) foreignrel->reltarget->exprs, PVC_RECURSE_PLACEHOLDERS)); tlist = add_to_flat_tlist(tlist, pull_var_clause((Node *) fpinfo->local_conds, PVC_RECURSE_PLACEHOLDERS)); return tlist; } /* * Deparse SELECT statement for given relation into buf. * * tlist contains the list of desired columns to be fetched from foreign server. * For a base relation fpinfo->attrs_used is used to construct SELECT clause, * hence the tlist is ignored for a base relation. * * remote_conds is the list of conditions to be deparsed into the WHERE clause * (or, in the case of upper relations, into the HAVING clause). * * If params_list is not NULL, it receives a list of Params and other-relation * Vars used in the clauses; these values must be transmitted to the remote * server as parameter values. * * If params_list is NULL, we're generating the query for EXPLAIN purposes, * so Params and other-relation Vars should be replaced by dummy values. * * pathkeys is the list of pathkeys to order the result by. * * List of columns selected is returned in retrieved_attrs. */ void vopsDeparseSelectStmtForRel(StringInfo buf, PlannerInfo *root, RelOptInfo *rel, List *tlist, List *remote_conds, List *pathkeys, List **retrieved_attrs, List **params_list) { deparse_expr_cxt context; PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) rel->fdw_private; List *quals; /* * We handle relations for foreign tables, joins between those and upper * relations. */ Assert(rel->reloptkind == RELOPT_JOINREL || rel->reloptkind == RELOPT_BASEREL || rel->reloptkind == RELOPT_OTHER_MEMBER_REL || rel->reloptkind == RELOPT_UPPER_REL); /* Fill portions of context common to upper, join and base relation */ context.buf = buf; context.root = root; context.foreignrel = rel; context.scanrel = (rel->reloptkind == RELOPT_UPPER_REL) ? fpinfo->upperrel : rel; context.params_list = params_list; /* Construct SELECT clause */ deparseSelectSql(tlist, retrieved_attrs, &context); /* * For upper relations, the WHERE clause is built from the remote * conditions of the underlying scan relation; otherwise, we can use the * supplied list of remote conditions directly. */ if (rel->reloptkind == RELOPT_UPPER_REL) { PgFdwRelationInfo *ofpinfo; ofpinfo = (PgFdwRelationInfo *) fpinfo->upperrel->fdw_private; quals = ofpinfo->remote_conds; } else quals = remote_conds; /* Construct FROM and WHERE clauses */ deparseFromExpr(quals, &context); if (rel->reloptkind == RELOPT_UPPER_REL) { /* Append GROUP BY clause */ appendGroupByClause(tlist, &context); /* Append HAVING clause */ if (remote_conds) { appendStringInfo(buf, " HAVING "); appendConditions(remote_conds, &context); } } Assert(!pathkeys); /* Add any necessary FOR UPDATE/SHARE. */ deparseLockingClause(&context); } /* * Construct a simple SELECT statement that retrieves desired columns * of the specified foreign table, and append it to "buf". The output * contains just "SELECT ... ". * * We also create an integer List of the columns being retrieved, which is * returned to *retrieved_attrs. * * tlist is the list of desired columns. Read prologue of * deparseSelectStmtForRel() for details. */ static void deparseSelectSql(List *tlist, List **retrieved_attrs, deparse_expr_cxt *context) { StringInfo buf = context->buf; RelOptInfo *foreignrel = context->foreignrel; PlannerInfo *root = context->root; PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) foreignrel->fdw_private; /* * Construct SELECT list */ appendStringInfoString(buf, "SELECT "); if (foreignrel->reloptkind == RELOPT_JOINREL || foreignrel->reloptkind == RELOPT_UPPER_REL) { /* For a join relation use the input tlist */ deparseExplicitTargetList(tlist, retrieved_attrs, context); } else { /* * For a base relation fpinfo->attrs_used gives the list of columns * required to be fetched from the foreign server. */ RangeTblEntry *rte = planner_rt_fetch(foreignrel->relid, root); /* * Core code already has some lock on each rel being planned, so we * can use NoLock here. */ Relation rel = heap_open(rte->relid, NoLock); deparseTargetList(buf, root, foreignrel->relid, rel, false, fpinfo->attrs_used, false, retrieved_attrs); heap_close(rel, NoLock); } } /* * Construct a FROM clause and, if needed, a WHERE clause, and append those to * "buf". * * quals is the list of clauses to be included in the WHERE clause. */ static void deparseFromExpr(List *quals, deparse_expr_cxt *context) { StringInfo buf = context->buf; RelOptInfo *scanrel = context->scanrel; /* For upper relations, scanrel must be either a joinrel or a baserel */ Assert(context->foreignrel->reloptkind != RELOPT_UPPER_REL || scanrel->reloptkind == RELOPT_JOINREL || scanrel->reloptkind == RELOPT_BASEREL); /* Construct FROM clause */ appendStringInfoString(buf, " FROM "); deparseFromExprForRel(buf, context->root, scanrel, (bms_num_members(scanrel->relids) > 1), context->params_list); /* Construct WHERE clause */ if (quals != NIL) { appendStringInfo(buf, " WHERE "); appendConditions(quals, context); } } /* * Emit a target list that retrieves the columns specified in attrs_used. * This is used for both SELECT and RETURNING targetlists; the is_returning * parameter is true only for a RETURNING targetlist. * * The tlist text is appended to buf, and we also create an integer List * of the columns being retrieved, which is returned to *retrieved_attrs. * * If qualify_col is true, add relation alias before the column name. */ static void deparseTargetList(StringInfo buf, PlannerInfo *root, Index rtindex, Relation rel, bool is_returning, Bitmapset *attrs_used, bool qualify_col, List **retrieved_attrs) { TupleDesc tupdesc = RelationGetDescr(rel); bool have_wholerow; bool first; int i; *retrieved_attrs = NIL; /* If there's a whole-row reference, we'll need all the columns. */ have_wholerow = bms_is_member(0 - FirstLowInvalidHeapAttributeNumber, attrs_used); first = true; for (i = 1; i <= tupdesc->natts; i++) { Form_pg_attribute attr = TupleDescAttr(tupdesc, i - 1); /* Ignore dropped attributes. */ if (attr->attisdropped) continue; if (have_wholerow || bms_is_member(i - FirstLowInvalidHeapAttributeNumber, attrs_used)) { if (!first) appendStringInfoString(buf, ", "); else if (is_returning) appendStringInfoString(buf, " RETURNING "); first = false; deparseColumnRef(buf, rtindex, i, root, qualify_col); *retrieved_attrs = lappend_int(*retrieved_attrs, i); } } /* * Add ctid and oid if needed. We currently don't support retrieving any * other system columns. */ if (bms_is_member(SelfItemPointerAttributeNumber - FirstLowInvalidHeapAttributeNumber, attrs_used)) { if (!first) appendStringInfoString(buf, ", "); else if (is_returning) appendStringInfoString(buf, " RETURNING "); first = false; if (qualify_col) ADD_REL_QUALIFIER(buf, rtindex); appendStringInfoString(buf, "ctid"); *retrieved_attrs = lappend_int(*retrieved_attrs, SelfItemPointerAttributeNumber); } #if PG_VERSION_NUM<120000 if (bms_is_member(ObjectIdAttributeNumber - FirstLowInvalidHeapAttributeNumber, attrs_used)) { if (!first) appendStringInfoString(buf, ", "); else if (is_returning) appendStringInfoString(buf, " RETURNING "); first = false; if (qualify_col) ADD_REL_QUALIFIER(buf, rtindex); appendStringInfoString(buf, "oid"); *retrieved_attrs = lappend_int(*retrieved_attrs, ObjectIdAttributeNumber); } #endif /* Don't generate bad syntax if no undropped columns */ if (first && !is_returning) appendStringInfoString(buf, "NULL"); } /* * Deparse the appropriate locking clause (FOR UPDATE or FOR SHARE) for a * given relation (context->scanrel). */ static void deparseLockingClause(deparse_expr_cxt *context) { StringInfo buf = context->buf; PlannerInfo *root = context->root; RelOptInfo *rel = context->scanrel; int relid = -1; while ((relid = bms_next_member(rel->relids, relid)) >= 0) { /* * Add FOR UPDATE/SHARE if appropriate. We apply locking during the * initial row fetch, rather than later on as is done for local * tables. The extra roundtrips involved in trying to duplicate the * local semantics exactly don't seem worthwhile (see also comments * for RowMarkType). * * Note: because we actually run the query as a cursor, this assumes * that DECLARE CURSOR ... FOR UPDATE is supported, which it isn't * before 8.3. */ if (relid == root->parse->resultRelation && (root->parse->commandType == CMD_UPDATE || root->parse->commandType == CMD_DELETE)) { /* Relation is UPDATE/DELETE target, so use FOR UPDATE */ appendStringInfoString(buf, " FOR UPDATE"); /* Add the relation alias if we are here for a join relation */ if (rel->reloptkind == RELOPT_JOINREL) appendStringInfo(buf, " OF %s%d", REL_ALIAS_PREFIX, relid); } else { PlanRowMark *rc = get_plan_rowmark(root->rowMarks, relid); if (rc) { /* * Relation is specified as a FOR UPDATE/SHARE target, so * handle that. (But we could also see LCS_NONE, meaning this * isn't a target relation after all.) * * For now, just ignore any [NO] KEY specification, since (a) * it's not clear what that means for a remote table that we * don't have complete information about, and (b) it wouldn't * work anyway on older remote servers. Likewise, we don't * worry about NOWAIT. */ switch (rc->strength) { case LCS_NONE: /* No locking needed */ break; case LCS_FORKEYSHARE: case LCS_FORSHARE: appendStringInfoString(buf, " FOR SHARE"); break; case LCS_FORNOKEYUPDATE: case LCS_FORUPDATE: appendStringInfoString(buf, " FOR UPDATE"); break; } /* Add the relation alias if we are here for a join relation */ if (bms_num_members(rel->relids) > 1 && rc->strength != LCS_NONE) appendStringInfo(buf, " OF %s%d", REL_ALIAS_PREFIX, relid); } } } } /* * Deparse conditions from the provided list and append them to buf. * * The conditions in the list are assumed to be ANDed. This function is used to * deparse WHERE clauses, JOIN .. ON clauses and HAVING clauses. */ static void appendConditions(List *exprs, deparse_expr_cxt *context) { ListCell *lc; bool is_first = true; StringInfo buf = context->buf; foreach(lc, exprs) { Expr *expr = (Expr *) lfirst(lc); /* * Extract clause from RestrictInfo, if required. See comments in * declaration of PgFdwRelationInfo for details. */ if (IsA(expr, RestrictInfo)) { RestrictInfo *ri = (RestrictInfo *) expr; expr = ri->clause; } /* Connect expressions with "AND" and parenthesize each condition. */ if (!is_first) appendStringInfoString(buf, " AND "); appendStringInfoChar(buf, '('); deparseExpr(expr, context); appendStringInfoChar(buf, ')'); is_first = false; } } /* * Deparse given targetlist and append it to context->buf. * * tlist is list of TargetEntry's which in turn contain Var nodes. * * retrieved_attrs is the list of continuously increasing integers starting * from 1. It has same number of entries as tlist. */ static void deparseExplicitTargetList(List *tlist, List **retrieved_attrs, deparse_expr_cxt *context) { ListCell *lc; StringInfo buf = context->buf; int i = 0; *retrieved_attrs = NIL; foreach(lc, tlist) { TargetEntry *tle = castNode(TargetEntry, lfirst(lc)); if (i > 0) appendStringInfoString(buf, ", "); deparseExpr((Expr *) tle->expr, context); *retrieved_attrs = lappend_int(*retrieved_attrs, i + 1); i++; } if (i == 0) appendStringInfoString(buf, "NULL"); } /* * Construct FROM clause for given relation * * The function constructs ... JOIN ... ON ... for join relation. For a base * relation it just returns schema-qualified tablename, with the appropriate * alias if so requested. */ static void deparseFromExprForRel(StringInfo buf, PlannerInfo *root, RelOptInfo *foreignrel, bool use_alias, List **params_list) { RangeTblEntry *rte = planner_rt_fetch(foreignrel->relid, root); /* * Core code already has some lock on each rel being planned, so we * can use NoLock here. */ Relation rel = heap_open(rte->relid, NoLock); Assert(foreignrel->reloptkind != RELOPT_JOINREL); vopsDeparseRelation(buf, rel); /* * Add a unique alias to avoid any conflict in relation names due to * pulled up subqueries in the query being built for a pushed down * join. */ if (use_alias) appendStringInfo(buf, " %s%d", REL_ALIAS_PREFIX, foreignrel->relid); heap_close(rel, NoLock); } /* * Construct name to use for given column, and emit it into buf. * If it has a column_name FDW option, use that instead of attribute name. * * If qualify_col is true, qualify column name with the alias of relation. */ static void deparseColumnRef(StringInfo buf, int varno, int varattno, PlannerInfo *root, bool qualify_col) { RangeTblEntry *rte; /* We support fetching the remote side's CTID and OID. */ if (varattno == SelfItemPointerAttributeNumber) { if (qualify_col) ADD_REL_QUALIFIER(buf, varno); appendStringInfoString(buf, "ctid"); } #if PG_VERSION_NUM<120000 else if (varattno == ObjectIdAttributeNumber) { if (qualify_col) ADD_REL_QUALIFIER(buf, varno); appendStringInfoString(buf, "oid"); } #endif else if (varattno < 0) { /* * All other system attributes are fetched as 0, except for table OID, * which is fetched as the local table OID. However, we must be * careful; the table could be beneath an outer join, in which case it * must go to NULL whenever the rest of the row does. */ Oid fetchval = 0; if (varattno == TableOidAttributeNumber) { rte = planner_rt_fetch(varno, root); fetchval = rte->relid; } if (qualify_col) { appendStringInfoString(buf, "CASE WHEN ("); ADD_REL_QUALIFIER(buf, varno); appendStringInfo(buf, "*)::text IS NOT NULL THEN %u END", fetchval); } else appendStringInfo(buf, "%u", fetchval); } else if (varattno == 0) { /* Whole row reference */ Relation rel; Bitmapset *attrs_used; /* Required only to be passed down to deparseTargetList(). */ List *retrieved_attrs; /* Get RangeTblEntry from array in PlannerInfo. */ rte = planner_rt_fetch(varno, root); /* * The lock on the relation will be held by upper callers, so it's * fine to open it with no lock here. */ rel = heap_open(rte->relid, NoLock); /* * The local name of the foreign table can not be recognized by the * foreign server and the table it references on foreign server might * have different column ordering or different columns than those * declared locally. Hence we have to deparse whole-row reference as * ROW(columns referenced locally). Construct this by deparsing a * "whole row" attribute. */ attrs_used = bms_add_member(NULL, 0 - FirstLowInvalidHeapAttributeNumber); /* * In case the whole-row reference is under an outer join then it has * to go NULL whenever the rest of the row goes NULL. Deparsing a join * query would always involve multiple relations, thus qualify_col * would be true. */ if (qualify_col) { appendStringInfoString(buf, "CASE WHEN ("); ADD_REL_QUALIFIER(buf, varno); appendStringInfo(buf, "*)::text IS NOT NULL THEN "); } appendStringInfoString(buf, "ROW("); deparseTargetList(buf, root, varno, rel, false, attrs_used, qualify_col, &retrieved_attrs); appendStringInfoString(buf, ")"); /* Complete the CASE WHEN statement started above. */ if (qualify_col) appendStringInfo(buf, " END"); heap_close(rel, NoLock); bms_free(attrs_used); } else { char *colname = NULL; List *options; ListCell *lc; /* varno must not be any of OUTER_VAR, INNER_VAR and INDEX_VAR. */ Assert(!IS_SPECIAL_VARNO(varno)); /* Get RangeTblEntry from array in PlannerInfo. */ rte = planner_rt_fetch(varno, root); /* * If it's a column of a foreign table, and it has the column_name FDW * option, use that value. */ options = GetForeignColumnOptions(rte->relid, varattno); foreach(lc, options) { DefElem *def = (DefElem *) lfirst(lc); if (strcmp(def->defname, "column_name") == 0) { colname = defGetString(def); break; } } /* * If it's a column of a regular table or it doesn't have column_name * FDW option, use attribute name. */ if (colname == NULL) #if PG_VERSION_NUM>=110000 colname = get_attname(rte->relid, varattno, false); #else colname = get_relid_attribute_name(rte->relid, varattno); #endif if (qualify_col) ADD_REL_QUALIFIER(buf, varno); appendStringInfoString(buf, quote_identifier(colname)); } } /* * Append remote name of specified foreign table to buf. * Use value of table_name FDW option (if any) instead of relation's name. * Similarly, schema_name FDW option overrides schema name. */ void vopsDeparseRelation(StringInfo buf, Relation rel) { ForeignTable *table; const char *nspname = NULL; const char *relname = NULL; ListCell *lc; /* obtain additional catalog information. */ table = GetForeignTable(RelationGetRelid(rel)); /* * Use value of FDW options if any, instead of the name of object itself. */ foreach(lc, table->options) { DefElem *def = (DefElem *) lfirst(lc); if (strcmp(def->defname, "schema_name") == 0) nspname = defGetString(def); else if (strcmp(def->defname, "table_name") == 0) relname = defGetString(def); } /* * Note: we could skip printing the schema name if it's pg_catalog, but * that doesn't seem worth the trouble. */ if (nspname == NULL) nspname = get_namespace_name(RelationGetNamespace(rel)); if (relname == NULL) relname = RelationGetRelationName(rel); appendStringInfo(buf, "%s.%s", quote_identifier(nspname), quote_identifier(relname)); } /* * Append a SQL string literal representing "val" to buf. */ static void deparseStringLiteral(StringInfo buf, const char *val) { const char *valptr; /* * Rather than making assumptions about the remote server's value of * standard_conforming_strings, always use E'foo' syntax if there are any * backslashes. This will fail on remote servers before 8.1, but those * are long out of support. */ if (strchr(val, '\\') != NULL) appendStringInfoChar(buf, ESCAPE_STRING_SYNTAX); appendStringInfoChar(buf, '\''); for (valptr = val; *valptr; valptr++) { char ch = *valptr; if (SQL_STR_DOUBLE(ch, true)) appendStringInfoChar(buf, ch); appendStringInfoChar(buf, ch); } appendStringInfoChar(buf, '\''); } /* * Deparse given expression into context->buf. * * This function must support all the same node types that foreign_expr_walker * accepts. * * Note: unlike ruleutils.c, we just use a simple hard-wired parenthesization * scheme: anything more complex than a Var, Const, function call or cast * should be self-parenthesized. */ static void deparseExpr(Expr *node, deparse_expr_cxt *context) { if (node == NULL) return; switch (nodeTag(node)) { case T_Var: deparseVar((Var *) node, context); break; case T_Const: deparseConst((Const *) node, context, 0); break; case T_Param: deparseParam((Param *) node, context); break; #if PG_VERSION_NUM>=120000 case T_SubscriptingRef: deparseSubscriptingRef((SubscriptingRef *) node, context); #else case T_ArrayRef: deparseArrayRef((ArrayRef *) node, context); #endif break; case T_FuncExpr: deparseFuncExpr((FuncExpr *) node, context); break; case T_OpExpr: deparseOpExpr((OpExpr *) node, context); break; case T_DistinctExpr: deparseDistinctExpr((DistinctExpr *) node, context); break; case T_ScalarArrayOpExpr: deparseScalarArrayOpExpr((ScalarArrayOpExpr *) node, context); break; case T_RelabelType: deparseRelabelType((RelabelType *) node, context); break; case T_BoolExpr: deparseBoolExpr((BoolExpr *) node, context); break; case T_NullTest: deparseNullTest((NullTest *) node, context); break; case T_ArrayExpr: deparseArrayExpr((ArrayExpr *) node, context); break; case T_Aggref: deparseAggref((Aggref *) node, context); break; default: elog(ERROR, "unsupported expression type for deparse: %d", (int) nodeTag(node)); break; } } /* * Deparse given Var node into context->buf. * * If the Var belongs to the foreign relation, just print its remote name. * Otherwise, it's effectively a Param (and will in fact be a Param at * run time). Handle it the same way we handle plain Params --- see * deparseParam for comments. */ static void deparseVar(Var *node, deparse_expr_cxt *context) { Relids relids = context->scanrel->relids; /* Qualify columns when multiple relations are involved. */ bool qualify_col = (bms_num_members(relids) > 1); if (bms_is_member(node->varno, relids) && node->varlevelsup == 0) deparseColumnRef(context->buf, node->varno, node->varattno, context->root, qualify_col); else { /* Treat like a Param */ if (context->params_list) { int pindex = 0; ListCell *lc; /* find its index in params_list */ foreach(lc, *context->params_list) { pindex++; if (equal(node, (Node *) lfirst(lc))) break; } if (lc == NULL) { /* not in list, so add it */ pindex++; *context->params_list = lappend(*context->params_list, node); } printRemoteParam(pindex, node->vartype, node->vartypmod, context); } else { printRemotePlaceholder(node->vartype, node->vartypmod, context); } } } /* * Deparse given constant value into context->buf. * * This function has to be kept in sync with ruleutils.c's get_const_expr. * As for that function, showtype can be -1 to never show "::typename" decoration, * or +1 to always show it, or 0 to show it only if the constant wouldn't be assumed * to be the right type by default. */ static void deparseConst(Const *node, deparse_expr_cxt *context, int showtype) { StringInfo buf = context->buf; Oid typoutput; bool typIsVarlena; char *extval; bool isfloat = false; bool needlabel; if (node->constisnull) { appendStringInfoString(buf, "NULL"); if (showtype >= 0) appendStringInfo(buf, "::%s", vops_deparse_type_name(node->consttype, node->consttypmod)); return; } getTypeOutputInfo(node->consttype, &typoutput, &typIsVarlena); extval = OidOutputFunctionCall(typoutput, node->constvalue); switch (node->consttype) { case INT2OID: case INT4OID: case INT8OID: case OIDOID: case FLOAT4OID: case FLOAT8OID: case NUMERICOID: { /* * No need to quote unless it's a special value such as 'NaN'. * See comments in get_const_expr(). */ if (strspn(extval, "0123456789+-eE.") == strlen(extval)) { if (extval[0] == '+' || extval[0] == '-') appendStringInfo(buf, "(%s)", extval); else appendStringInfoString(buf, extval); if (strcspn(extval, "eE.") != strlen(extval)) isfloat = true; /* it looks like a float */ } else appendStringInfo(buf, "'%s'", extval); } break; case BITOID: case VARBITOID: appendStringInfo(buf, "B'%s'", extval); break; case BOOLOID: if (strcmp(extval, "t") == 0) appendStringInfoString(buf, "true"); else appendStringInfoString(buf, "false"); break; default: deparseStringLiteral(buf, extval); break; } pfree(extval); if (showtype < 0) return; /* * For showtype == 0, append ::typename unless the constant will be * implicitly typed as the right type when it is read in. * * XXX this code has to be kept in sync with the behavior of the parser, * especially make_const. */ switch (node->consttype) { case BOOLOID: case INT4OID: case UNKNOWNOID: needlabel = false; break; case NUMERICOID: needlabel = !isfloat || (node->consttypmod >= 0); break; default: needlabel = true; break; } if (needlabel || showtype > 0) appendStringInfo(buf, "::%s", vops_deparse_type_name(node->consttype, node->consttypmod)); } /* * Deparse given Param node. * * If we're generating the query "for real", add the Param to * context->params_list if it's not already present, and then use its index * in that list as the remote parameter number. During EXPLAIN, there's * no need to identify a parameter number. */ static void deparseParam(Param *node, deparse_expr_cxt *context) { if (context->params_list) { int pindex = 0; ListCell *lc; /* find its index in params_list */ foreach(lc, *context->params_list) { pindex++; if (equal(node, (Node *) lfirst(lc))) break; } if (lc == NULL) { /* not in list, so add it */ pindex++; *context->params_list = lappend(*context->params_list, node); } printRemoteParam(pindex, node->paramtype, node->paramtypmod, context); } else { printRemotePlaceholder(node->paramtype, node->paramtypmod, context); } } /* * Deparse an array subscript expression. */ #if PG_VERSION_NUM>=120000 static void deparseSubscriptingRef(SubscriptingRef *node, deparse_expr_cxt *context) #else static void deparseArrayRef(ArrayRef *node, deparse_expr_cxt *context) #endif { StringInfo buf = context->buf; ListCell *lowlist_item; ListCell *uplist_item; /* Always parenthesize the expression. */ appendStringInfoChar(buf, '('); /* * Deparse referenced array expression first. If that expression includes * a cast, we have to parenthesize to prevent the array subscript from * being taken as typename decoration. We can avoid that in the typical * case of subscripting a Var, but otherwise do it. */ if (IsA(node->refexpr, Var)) deparseExpr(node->refexpr, context); else { appendStringInfoChar(buf, '('); deparseExpr(node->refexpr, context); appendStringInfoChar(buf, ')'); } /* Deparse subscript expressions. */ lowlist_item = list_head(node->reflowerindexpr); /* could be NULL */ foreach(uplist_item, node->refupperindexpr) { appendStringInfoChar(buf, '['); if (lowlist_item) { deparseExpr(lfirst(lowlist_item), context); appendStringInfoChar(buf, ':'); #if PG_VERSION_NUM>=130000 lowlist_item = lnext(node->reflowerindexpr, lowlist_item); #else lowlist_item = lnext(lowlist_item); #endif } deparseExpr(lfirst(uplist_item), context); appendStringInfoChar(buf, ']'); } appendStringInfoChar(buf, ')'); } /* * Deparse a function call. */ static void deparseFuncExpr(FuncExpr *node, deparse_expr_cxt *context) { StringInfo buf = context->buf; bool use_variadic; bool first; ListCell *arg; /* * If the function call came from an implicit coercion, then just show the * first argument. */ if (node->funcformat == COERCE_IMPLICIT_CAST) { deparseExpr((Expr *) linitial(node->args), context); return; } /* * If the function call came from a cast, then show the first argument * plus an explicit cast operation. */ if (node->funcformat == COERCE_EXPLICIT_CAST) { Oid rettype = node->funcresulttype; int32 coercedTypmod; /* Get the typmod if this is a length-coercion function */ (void) exprIsLengthCoercion((Node *) node, &coercedTypmod); deparseExpr((Expr *) linitial(node->args), context); appendStringInfo(buf, "::%s", vops_deparse_type_name(rettype, coercedTypmod)); return; } /* Check if need to print VARIADIC (cf. ruleutils.c) */ use_variadic = node->funcvariadic; /* * Normal function: display as proname(args). */ appendFunctionName(node->funcid, context); appendStringInfoChar(buf, '('); /* ... and all the arguments */ first = true; foreach(arg, node->args) { if (!first) appendStringInfoString(buf, ", "); #if PG_VERSION_NUM>=130000 if (use_variadic && lnext(node->args, arg) == NULL) #else if (use_variadic && lnext(arg) == NULL) #endif appendStringInfoString(buf, "VARIADIC "); deparseExpr((Expr *) lfirst(arg), context); first = false; } appendStringInfoChar(buf, ')'); } /* * Deparse given operator expression. To avoid problems around * priority of operations, we always parenthesize the arguments. */ static void deparseOpExpr(OpExpr *node, deparse_expr_cxt *context) { StringInfo buf = context->buf; HeapTuple tuple; Form_pg_operator form; char oprkind; ListCell *arg; /* Retrieve information about the operator from system catalog. */ tuple = SearchSysCache1(OPEROID, ObjectIdGetDatum(node->opno)); if (!HeapTupleIsValid(tuple)) elog(ERROR, "cache lookup failed for operator %u", node->opno); form = (Form_pg_operator) GETSTRUCT(tuple); oprkind = form->oprkind; /* Sanity check. */ Assert((oprkind == 'r' && list_length(node->args) == 1) || (oprkind == 'l' && list_length(node->args) == 1) || (oprkind == 'b' && list_length(node->args) == 2)); /* Always parenthesize the expression. */ appendStringInfoChar(buf, '('); /* Deparse left operand. */ if (oprkind == 'r' || oprkind == 'b') { arg = list_head(node->args); deparseExpr(lfirst(arg), context); appendStringInfoChar(buf, ' '); } /* Deparse operator name. */ deparseOperatorName(buf, form); /* Deparse right operand. */ if (oprkind == 'l' || oprkind == 'b') { arg = list_tail(node->args); appendStringInfoChar(buf, ' '); deparseExpr(lfirst(arg), context); } appendStringInfoChar(buf, ')'); ReleaseSysCache(tuple); } /* * Print the name of an operator. */ static void deparseOperatorName(StringInfo buf, Form_pg_operator opform) { char *opname; /* opname is not a SQL identifier, so we should not quote it. */ opname = NameStr(opform->oprname); /* Print schema name only if it's not pg_catalog */ if (opform->oprnamespace != PG_CATALOG_NAMESPACE) { const char *opnspname; opnspname = get_namespace_name(opform->oprnamespace); /* Print fully qualified operator name. */ appendStringInfo(buf, "OPERATOR(%s.%s)", quote_identifier(opnspname), opname); } else { /* Just print operator name. */ appendStringInfoString(buf, opname); } } /* * Deparse IS DISTINCT FROM. */ static void deparseDistinctExpr(DistinctExpr *node, deparse_expr_cxt *context) { StringInfo buf = context->buf; Assert(list_length(node->args) == 2); appendStringInfoChar(buf, '('); deparseExpr(linitial(node->args), context); appendStringInfoString(buf, " IS DISTINCT FROM "); deparseExpr(lsecond(node->args), context); appendStringInfoChar(buf, ')'); } /* * Deparse given ScalarArrayOpExpr expression. To avoid problems * around priority of operations, we always parenthesize the arguments. */ static void deparseScalarArrayOpExpr(ScalarArrayOpExpr *node, deparse_expr_cxt *context) { StringInfo buf = context->buf; HeapTuple tuple; Form_pg_operator form; Expr *arg1; Expr *arg2; /* Retrieve information about the operator from system catalog. */ tuple = SearchSysCache1(OPEROID, ObjectIdGetDatum(node->opno)); if (!HeapTupleIsValid(tuple)) elog(ERROR, "cache lookup failed for operator %u", node->opno); form = (Form_pg_operator) GETSTRUCT(tuple); /* Sanity check. */ Assert(list_length(node->args) == 2); /* Always parenthesize the expression. */ appendStringInfoChar(buf, '('); /* Deparse left operand. */ arg1 = linitial(node->args); deparseExpr(arg1, context); appendStringInfoChar(buf, ' '); /* Deparse operator name plus decoration. */ deparseOperatorName(buf, form); appendStringInfo(buf, " %s (", node->useOr ? "ANY" : "ALL"); /* Deparse right operand. */ arg2 = lsecond(node->args); deparseExpr(arg2, context); appendStringInfoChar(buf, ')'); /* Always parenthesize the expression. */ appendStringInfoChar(buf, ')'); ReleaseSysCache(tuple); } /* * Deparse a RelabelType (binary-compatible cast) node. */ static void deparseRelabelType(RelabelType *node, deparse_expr_cxt *context) { deparseExpr(node->arg, context); if (node->relabelformat != COERCE_IMPLICIT_CAST) appendStringInfo(context->buf, "::%s", vops_deparse_type_name(node->resulttype, node->resulttypmod)); } /* * Deparse a BoolExpr node. */ static void deparseBoolExpr(BoolExpr *node, deparse_expr_cxt *context) { StringInfo buf = context->buf; const char *op = NULL; /* keep compiler quiet */ bool first; ListCell *lc; switch (node->boolop) { case AND_EXPR: op = "AND"; break; case OR_EXPR: op = "OR"; break; case NOT_EXPR: appendStringInfoString(buf, "(NOT "); deparseExpr(linitial(node->args), context); appendStringInfoChar(buf, ')'); return; } appendStringInfoChar(buf, '('); first = true; foreach(lc, node->args) { if (!first) appendStringInfo(buf, " %s ", op); deparseExpr((Expr *) lfirst(lc), context); first = false; } appendStringInfoChar(buf, ')'); } /* * Deparse IS [NOT] NULL expression. */ static void deparseNullTest(NullTest *node, deparse_expr_cxt *context) { StringInfo buf = context->buf; appendStringInfoChar(buf, '('); deparseExpr(node->arg, context); /* * For scalar inputs, we prefer to print as IS [NOT] NULL, which is * shorter and traditional. If it's a rowtype input but we're applying a * scalar test, must print IS [NOT] DISTINCT FROM NULL to be semantically * correct. */ if (node->argisrow || !type_is_rowtype(exprType((Node *) node->arg))) { if (node->nulltesttype == IS_NULL) appendStringInfoString(buf, " IS NULL)"); else appendStringInfoString(buf, " IS NOT NULL)"); } else { if (node->nulltesttype == IS_NULL) appendStringInfoString(buf, " IS NOT DISTINCT FROM NULL)"); else appendStringInfoString(buf, " IS DISTINCT FROM NULL)"); } } /* * Deparse ARRAY[...] construct. */ static void deparseArrayExpr(ArrayExpr *node, deparse_expr_cxt *context) { StringInfo buf = context->buf; bool first = true; ListCell *lc; appendStringInfoString(buf, "ARRAY["); foreach(lc, node->elements) { if (!first) appendStringInfoString(buf, ", "); deparseExpr(lfirst(lc), context); first = false; } appendStringInfoChar(buf, ']'); /* If the array is empty, we need an explicit cast to the array type. */ if (node->elements == NIL) appendStringInfo(buf, "::%s", vops_deparse_type_name(node->array_typeid, -1)); } /* * Deparse an Aggref node. */ static void deparseAggref(Aggref *node, deparse_expr_cxt *context) { StringInfo buf = context->buf; bool use_variadic; /* Only basic, non-split aggregation accepted. */ Assert(node->aggsplit == AGGSPLIT_SIMPLE); /* Check if need to print VARIADIC (cf. ruleutils.c) */ use_variadic = node->aggvariadic; /* Find aggregate name from aggfnoid which is a pg_proc entry */ appendFunctionName(node->aggfnoid, context); appendStringInfoChar(buf, '('); /* Add DISTINCT */ appendStringInfo(buf, "%s", (node->aggdistinct != NIL) ? "DISTINCT " : ""); if (AGGKIND_IS_ORDERED_SET(node->aggkind)) { /* Add WITHIN GROUP (ORDER BY ..) */ ListCell *arg; bool first = true; Assert(!node->aggvariadic); Assert(node->aggorder != NIL); foreach(arg, node->aggdirectargs) { if (!first) appendStringInfoString(buf, ", "); first = false; deparseExpr((Expr *) lfirst(arg), context); } appendStringInfoString(buf, ") WITHIN GROUP (ORDER BY "); appendAggOrderBy(node->aggorder, node->args, context); } else { /* aggstar can be set only in zero-argument aggregates */ if (node->aggstar) appendStringInfoChar(buf, '*'); else { ListCell *arg; bool first = true; /* Add all the arguments */ foreach(arg, node->args) { TargetEntry *tle = (TargetEntry *) lfirst(arg); Node *n = (Node *) tle->expr; if (tle->resjunk) continue; if (!first) appendStringInfoString(buf, ", "); first = false; /* Add VARIADIC */ #if PG_VERSION_NUM>=130000 if (use_variadic && lnext(node->args, arg) == NULL) #else if (use_variadic && lnext(arg) == NULL) #endif appendStringInfoString(buf, "VARIADIC "); deparseExpr((Expr *) n, context); } } /* Add ORDER BY */ if (node->aggorder != NIL) { appendStringInfoString(buf, " ORDER BY "); appendAggOrderBy(node->aggorder, node->args, context); } } /* Add FILTER (WHERE ..) */ if (node->aggfilter != NULL) { appendStringInfoString(buf, ") FILTER (WHERE "); deparseExpr((Expr *) node->aggfilter, context); } appendStringInfoChar(buf, ')'); } /* * Append ORDER BY within aggregate function. */ static void appendAggOrderBy(List *orderList, List *targetList, deparse_expr_cxt *context) { StringInfo buf = context->buf; ListCell *lc; bool first = true; foreach(lc, orderList) { SortGroupClause *srt = (SortGroupClause *) lfirst(lc); Node *sortexpr; Oid sortcoltype; TypeCacheEntry *typentry; if (!first) appendStringInfoString(buf, ", "); first = false; sortexpr = deparseSortGroupClause(srt->tleSortGroupRef, targetList, context); sortcoltype = exprType(sortexpr); /* See whether operator is default < or > for datatype */ typentry = lookup_type_cache(sortcoltype, TYPECACHE_LT_OPR | TYPECACHE_GT_OPR); if (srt->sortop == typentry->lt_opr) appendStringInfoString(buf, " ASC"); else if (srt->sortop == typentry->gt_opr) appendStringInfoString(buf, " DESC"); else { HeapTuple opertup; Form_pg_operator operform; appendStringInfoString(buf, " USING "); /* Append operator name. */ opertup = SearchSysCache1(OPEROID, ObjectIdGetDatum(srt->sortop)); if (!HeapTupleIsValid(opertup)) elog(ERROR, "cache lookup failed for operator %u", srt->sortop); operform = (Form_pg_operator) GETSTRUCT(opertup); deparseOperatorName(buf, operform); ReleaseSysCache(opertup); } if (srt->nulls_first) appendStringInfoString(buf, " NULLS FIRST"); else appendStringInfoString(buf, " NULLS LAST"); } } /* * Print the representation of a parameter to be sent to the remote side. * * Note: we always label the Param's type explicitly rather than relying on * transmitting a numeric type OID in PQexecParams(). This allows us to * avoid assuming that types have the same OIDs on the remote side as they * do locally --- they need only have the same names. */ static void printRemoteParam(int paramindex, Oid paramtype, int32 paramtypmod, deparse_expr_cxt *context) { StringInfo buf = context->buf; char *ptypename = vops_deparse_type_name(paramtype, paramtypmod); appendStringInfo(buf, "$%d::%s", paramindex, ptypename); } /* * Print the representation of a placeholder for a parameter that will be * sent to the remote side at execution time. * * This is used when we're just trying to EXPLAIN the remote query. * We don't have the actual value of the runtime parameter yet, and we don't * want the remote planner to generate a plan that depends on such a value * anyway. Thus, we can't do something simple like "$1::paramtype". * Instead, we emit "((SELECT null::paramtype)::paramtype)". * In all extant versions of Postgres, the planner will see that as an unknown * constant value, which is what we want. This might need adjustment if we * ever make the planner flatten scalar subqueries. Note: the reason for the * apparently useless outer cast is to ensure that the representation as a * whole will be parsed as an a_expr and not a select_with_parens; the latter * would do the wrong thing in the context "x = ANY(...)". */ static void printRemotePlaceholder(Oid paramtype, int32 paramtypmod, deparse_expr_cxt *context) { StringInfo buf = context->buf; char *ptypename = vops_deparse_type_name(paramtype, paramtypmod); appendStringInfo(buf, "((SELECT null::%s)::%s)", ptypename, ptypename); } /* * Deparse GROUP BY clause. */ static void appendGroupByClause(List *tlist, deparse_expr_cxt *context) { StringInfo buf = context->buf; Query *query = context->root->parse; ListCell *lc; bool first = true; /* Nothing to be done, if there's no GROUP BY clause in the query. */ if (!query->groupClause) return; appendStringInfo(buf, " GROUP BY "); /* * Queries with grouping sets are not pushed down, so we don't expect * grouping sets here. */ Assert(!query->groupingSets); foreach(lc, query->groupClause) { SortGroupClause *grp = (SortGroupClause *) lfirst(lc); if (!first) appendStringInfoString(buf, ", "); first = false; deparseSortGroupClause(grp->tleSortGroupRef, tlist, context); } } /* * appendFunctionName * Deparses function name from given function oid. */ static void appendFunctionName(Oid funcid, deparse_expr_cxt *context) { StringInfo buf = context->buf; HeapTuple proctup; Form_pg_proc procform; const char *proname; proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid)); if (!HeapTupleIsValid(proctup)) elog(ERROR, "cache lookup failed for function %u", funcid); procform = (Form_pg_proc) GETSTRUCT(proctup); /* Print schema name only if it's not pg_catalog */ if (procform->pronamespace != PG_CATALOG_NAMESPACE) { const char *schemaname; schemaname = get_namespace_name(procform->pronamespace); appendStringInfo(buf, "%s.", quote_identifier(schemaname)); } /* Always print the function name */ proname = NameStr(procform->proname); appendStringInfo(buf, "%s", quote_identifier(proname)); ReleaseSysCache(proctup); } /* * Appends a sort or group clause. * * Like get_rule_sortgroupclause(), returns the expression tree, so caller * need not find it again. */ static Node * deparseSortGroupClause(Index ref, List *tlist, deparse_expr_cxt *context) { StringInfo buf = context->buf; TargetEntry *tle; Expr *expr; tle = get_sortgroupref_tle(ref, tlist); expr = tle->expr; if (expr && IsA(expr, Const)) { /* * Force a typecast here so that we don't emit something like "GROUP * BY 2", which will be misconstrued as a column position rather than * a constant. */ deparseConst((Const *) expr, context, 1); } else if (!expr || IsA(expr, Var)) deparseExpr(expr, context); else { /* Always parenthesize the expression. */ appendStringInfoString(buf, "("); deparseExpr(expr, context); appendStringInfoString(buf, ")"); } return (Node *) expr; }