Branch data Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * deparse.c
4 : : * Query deparser for postgres_fdw
5 : : *
6 : : * This file includes functions that examine query WHERE clauses to see
7 : : * whether they're safe to send to the remote server for execution, as
8 : : * well as functions to construct the query text to be sent. The latter
9 : : * functionality is annoyingly duplicative of ruleutils.c, but there are
10 : : * enough special considerations that it seems best to keep this separate.
11 : : * One saving grace is that we only need deparse logic for node types that
12 : : * we consider safe to send.
13 : : *
14 : : * We assume that the remote session's search_path is exactly "pg_catalog",
15 : : * and thus we need schema-qualify all and only names outside pg_catalog.
16 : : *
17 : : * We do not consider that it is ever safe to send COLLATE expressions to
18 : : * the remote server: it might not have the same collation names we do.
19 : : * (Later we might consider it safe to send COLLATE "C", but even that would
20 : : * fail on old remote servers.) An expression is considered safe to send
21 : : * only if all operator/function input collations used in it are traceable to
22 : : * Var(s) of the foreign table. That implies that if the remote server gets
23 : : * a different answer than we do, the foreign table's columns are not marked
24 : : * with collations that match the remote table's columns, which we can
25 : : * consider to be user error.
26 : : *
27 : : * Portions Copyright (c) 2012-2026, PostgreSQL Global Development Group
28 : : *
29 : : * IDENTIFICATION
30 : : * contrib/postgres_fdw/deparse.c
31 : : *
32 : : *-------------------------------------------------------------------------
33 : : */
34 : : #include "postgres.h"
35 : :
36 : : #include "access/htup_details.h"
37 : : #include "access/sysattr.h"
38 : : #include "access/table.h"
39 : : #include "catalog/pg_aggregate.h"
40 : : #include "catalog/pg_authid.h"
41 : : #include "catalog/pg_collation.h"
42 : : #include "catalog/pg_database.h"
43 : : #include "catalog/pg_namespace.h"
44 : : #include "catalog/pg_operator.h"
45 : : #include "catalog/pg_opfamily.h"
46 : : #include "catalog/pg_proc.h"
47 : : #include "catalog/pg_ts_config.h"
48 : : #include "catalog/pg_ts_dict.h"
49 : : #include "catalog/pg_type.h"
50 : : #include "commands/defrem.h"
51 : : #include "nodes/nodeFuncs.h"
52 : : #include "nodes/plannodes.h"
53 : : #include "optimizer/optimizer.h"
54 : : #include "optimizer/prep.h"
55 : : #include "optimizer/tlist.h"
56 : : #include "parser/parsetree.h"
57 : : #include "postgres_fdw.h"
58 : : #include "utils/builtins.h"
59 : : #include "utils/lsyscache.h"
60 : : #include "utils/rel.h"
61 : : #include "utils/syscache.h"
62 : : #include "utils/typcache.h"
63 : :
64 : : /*
65 : : * Global context for foreign_expr_walker's search of an expression tree.
66 : : */
67 : : typedef struct foreign_glob_cxt
68 : : {
69 : : PlannerInfo *root; /* global planner state */
70 : : RelOptInfo *foreignrel; /* the foreign relation we are planning for */
71 : : Relids relids; /* relids of base relations in the underlying
72 : : * scan */
73 : : } foreign_glob_cxt;
74 : :
75 : : /*
76 : : * Local (per-tree-level) context for foreign_expr_walker's search.
77 : : * This is concerned with identifying collations used in the expression.
78 : : */
79 : : typedef enum
80 : : {
81 : : FDW_COLLATE_NONE, /* expression is of a noncollatable type, or
82 : : * it has default collation that is not
83 : : * traceable to a foreign Var */
84 : : FDW_COLLATE_SAFE, /* collation derives from a foreign Var */
85 : : FDW_COLLATE_UNSAFE, /* collation is non-default and derives from
86 : : * something other than a foreign Var */
87 : : } FDWCollateState;
88 : :
89 : : typedef struct foreign_loc_cxt
90 : : {
91 : : Oid collation; /* OID of current collation, if any */
92 : : FDWCollateState state; /* state of current collation choice */
93 : : } foreign_loc_cxt;
94 : :
95 : : /*
96 : : * Context for deparseExpr
97 : : */
98 : : typedef struct deparse_expr_cxt
99 : : {
100 : : PlannerInfo *root; /* global planner state */
101 : : RelOptInfo *foreignrel; /* the foreign relation we are planning for */
102 : : RelOptInfo *scanrel; /* the underlying scan relation. Same as
103 : : * foreignrel, when that represents a join or
104 : : * a base relation. */
105 : : StringInfo buf; /* output buffer to append to */
106 : : List **params_list; /* exprs that will become remote Params */
107 : : } deparse_expr_cxt;
108 : :
109 : : #define REL_ALIAS_PREFIX "r"
110 : : /* Handy macro to add relation name qualification */
111 : : #define ADD_REL_QUALIFIER(buf, varno) \
112 : : appendStringInfo((buf), "%s%d.", REL_ALIAS_PREFIX, (varno))
113 : : #define SUBQUERY_REL_ALIAS_PREFIX "s"
114 : : #define SUBQUERY_COL_ALIAS_PREFIX "c"
115 : :
116 : : /*
117 : : * Functions to determine whether an expression can be evaluated safely on
118 : : * remote server.
119 : : */
120 : : static bool foreign_expr_walker(Node *node,
121 : : foreign_glob_cxt *glob_cxt,
122 : : foreign_loc_cxt *outer_cxt,
123 : : foreign_loc_cxt *case_arg_cxt);
124 : : static char *deparse_type_name(Oid type_oid, int32 typemod);
125 : :
126 : : /*
127 : : * Functions to construct string representation of a node tree.
128 : : */
129 : : static void deparseTargetList(StringInfo buf,
130 : : RangeTblEntry *rte,
131 : : Index rtindex,
132 : : Relation rel,
133 : : bool is_returning,
134 : : Bitmapset *attrs_used,
135 : : bool qualify_col,
136 : : List **retrieved_attrs);
137 : : static void deparseExplicitTargetList(List *tlist,
138 : : bool is_returning,
139 : : List **retrieved_attrs,
140 : : deparse_expr_cxt *context);
141 : : static void deparseSubqueryTargetList(deparse_expr_cxt *context);
142 : : static void deparseReturningList(StringInfo buf, RangeTblEntry *rte,
143 : : Index rtindex, Relation rel,
144 : : bool trig_after_row,
145 : : List *withCheckOptionList,
146 : : List *returningList,
147 : : List **retrieved_attrs);
148 : : static void deparseColumnRef(StringInfo buf, int varno, int varattno,
149 : : RangeTblEntry *rte, bool qualify_col);
150 : : static void deparseRelation(StringInfo buf, Relation rel);
151 : : static void deparseExpr(Expr *node, deparse_expr_cxt *context);
152 : : static void deparseVar(Var *node, deparse_expr_cxt *context);
153 : : static void deparseConst(Const *node, deparse_expr_cxt *context, int showtype);
154 : : static void deparseParam(Param *node, deparse_expr_cxt *context);
155 : : static void deparseSubscriptingRef(SubscriptingRef *node, deparse_expr_cxt *context);
156 : : static void deparseFuncExpr(FuncExpr *node, deparse_expr_cxt *context);
157 : : static void deparseOpExpr(OpExpr *node, deparse_expr_cxt *context);
158 : : static bool isPlainForeignVar(Expr *node, deparse_expr_cxt *context);
159 : : static void deparseOperatorName(StringInfo buf, Form_pg_operator opform);
160 : : static void deparseDistinctExpr(DistinctExpr *node, deparse_expr_cxt *context);
161 : : static void deparseScalarArrayOpExpr(ScalarArrayOpExpr *node,
162 : : deparse_expr_cxt *context);
163 : : static void deparseRelabelType(RelabelType *node, deparse_expr_cxt *context);
164 : : static void deparseArrayCoerceExpr(ArrayCoerceExpr *node, deparse_expr_cxt *context);
165 : : static void deparseBoolExpr(BoolExpr *node, deparse_expr_cxt *context);
166 : : static void deparseNullTest(NullTest *node, deparse_expr_cxt *context);
167 : : static void deparseCaseExpr(CaseExpr *node, deparse_expr_cxt *context);
168 : : static void deparseArrayExpr(ArrayExpr *node, deparse_expr_cxt *context);
169 : : static void printRemoteParam(int paramindex, Oid paramtype, int32 paramtypmod,
170 : : deparse_expr_cxt *context);
171 : : static void printRemotePlaceholder(Oid paramtype, int32 paramtypmod,
172 : : deparse_expr_cxt *context);
173 : : static void deparseSelectSql(List *tlist, bool is_subquery, List **retrieved_attrs,
174 : : deparse_expr_cxt *context);
175 : : static void deparseLockingClause(deparse_expr_cxt *context);
176 : : static void appendOrderByClause(List *pathkeys, bool has_final_sort,
177 : : deparse_expr_cxt *context);
178 : : static void appendLimitClause(deparse_expr_cxt *context);
179 : : static void appendConditions(List *exprs, deparse_expr_cxt *context);
180 : : static void deparseFromExprForRel(StringInfo buf, PlannerInfo *root,
181 : : RelOptInfo *foreignrel, bool use_alias,
182 : : Index ignore_rel, List **ignore_conds,
183 : : List **additional_conds,
184 : : List **params_list);
185 : : static void appendWhereClause(List *exprs, List *additional_conds,
186 : : deparse_expr_cxt *context);
187 : : static void deparseFromExpr(List *quals, deparse_expr_cxt *context);
188 : : static void deparseRangeTblRef(StringInfo buf, PlannerInfo *root,
189 : : RelOptInfo *foreignrel, bool make_subquery,
190 : : Index ignore_rel, List **ignore_conds,
191 : : List **additional_conds, List **params_list);
192 : : static void deparseAggref(Aggref *node, deparse_expr_cxt *context);
193 : : static void appendGroupByClause(List *tlist, deparse_expr_cxt *context);
194 : : static void appendOrderBySuffix(Oid sortop, Oid sortcoltype, bool nulls_first,
195 : : deparse_expr_cxt *context);
196 : : static void appendAggOrderBy(List *orderList, List *targetList,
197 : : deparse_expr_cxt *context);
198 : : static void appendFunctionName(Oid funcid, deparse_expr_cxt *context);
199 : : static Node *deparseSortGroupClause(Index ref, List *tlist, bool force_colno,
200 : : deparse_expr_cxt *context);
201 : :
202 : : /*
203 : : * Helper functions
204 : : */
205 : : static bool is_subquery_var(Var *node, RelOptInfo *foreignrel,
206 : : int *relno, int *colno);
207 : : static void get_relation_column_alias_ids(Var *node, RelOptInfo *foreignrel,
208 : : int *relno, int *colno);
209 : :
210 : :
211 : : /*
212 : : * Examine each qual clause in input_conds, and classify them into two groups,
213 : : * which are returned as two lists:
214 : : * - remote_conds contains expressions that can be evaluated remotely
215 : : * - local_conds contains expressions that can't be evaluated remotely
216 : : */
217 : : void
218 : 2549 : classifyConditions(PlannerInfo *root,
219 : : RelOptInfo *baserel,
220 : : List *input_conds,
221 : : List **remote_conds,
222 : : List **local_conds)
223 : : {
224 : : ListCell *lc;
225 : :
226 : 2549 : *remote_conds = NIL;
227 : 2549 : *local_conds = NIL;
228 : :
229 [ + + + + : 3331 : foreach(lc, input_conds)
+ + ]
230 : : {
231 : 782 : RestrictInfo *ri = lfirst_node(RestrictInfo, lc);
232 : :
233 [ + + ]: 782 : if (is_foreign_expr(root, baserel, ri->clause))
234 : 695 : *remote_conds = lappend(*remote_conds, ri);
235 : : else
236 : 87 : *local_conds = lappend(*local_conds, ri);
237 : : }
238 : 2549 : }
239 : :
240 : : /*
241 : : * Returns true if given expr is safe to evaluate on the foreign server.
242 : : */
243 : : bool
244 : 3846 : is_foreign_expr(PlannerInfo *root,
245 : : RelOptInfo *baserel,
246 : : Expr *expr)
247 : : {
248 : : foreign_glob_cxt glob_cxt;
249 : : foreign_loc_cxt loc_cxt;
250 : 3846 : PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) (baserel->fdw_private);
251 : :
252 : : /*
253 : : * Check that the expression consists of nodes that are safe to execute
254 : : * remotely.
255 : : */
256 : 3846 : glob_cxt.root = root;
257 : 3846 : glob_cxt.foreignrel = baserel;
258 : :
259 : : /*
260 : : * For an upper relation, use relids from its underneath scan relation,
261 : : * because the upperrel's own relids currently aren't set to anything
262 : : * meaningful by the core code. For other relation, use their own relids.
263 : : */
264 [ + + + + ]: 3846 : if (IS_UPPER_REL(baserel))
265 : 456 : glob_cxt.relids = fpinfo->outerrel->relids;
266 : : else
267 : 3390 : glob_cxt.relids = baserel->relids;
268 : 3846 : loc_cxt.collation = InvalidOid;
269 : 3846 : loc_cxt.state = FDW_COLLATE_NONE;
270 [ + + ]: 3846 : if (!foreign_expr_walker((Node *) expr, &glob_cxt, &loc_cxt, NULL))
271 : 154 : return false;
272 : :
273 : : /*
274 : : * If the expression has a valid collation that does not arise from a
275 : : * foreign var, the expression can not be sent over.
276 : : */
277 [ + + ]: 3692 : if (loc_cxt.state == FDW_COLLATE_UNSAFE)
278 : 2 : return false;
279 : :
280 : : /*
281 : : * An expression which includes any mutable functions can't be sent over
282 : : * because its result is not stable. For example, sending now() remote
283 : : * side could cause confusion from clock offsets. Future versions might
284 : : * be able to make this choice with more granularity. (We check this last
285 : : * because it requires a lot of expensive catalog lookups.)
286 : : */
287 [ + + ]: 3690 : if (contain_mutable_functions((Node *) expr))
288 : 25 : return false;
289 : :
290 : : /* OK to evaluate on the remote server */
291 : 3665 : return true;
292 : : }
293 : :
294 : : /*
295 : : * Check if expression is safe to execute remotely, and return true if so.
296 : : *
297 : : * In addition, *outer_cxt is updated with collation information.
298 : : *
299 : : * case_arg_cxt is NULL if this subexpression is not inside a CASE-with-arg.
300 : : * Otherwise, it points to the collation info derived from the arg expression,
301 : : * which must be consulted by any CaseTestExpr.
302 : : *
303 : : * We must check that the expression contains only node types we can deparse,
304 : : * that all types/functions/operators are safe to send (they are "shippable"),
305 : : * and that all collations used in the expression derive from Vars of the
306 : : * foreign table. Because of the latter, the logic is pretty close to
307 : : * assign_collations_walker() in parse_collate.c, though we can assume here
308 : : * that the given expression is valid. Note function mutability is not
309 : : * currently considered here.
310 : : */
311 : : static bool
312 : 9630 : foreign_expr_walker(Node *node,
313 : : foreign_glob_cxt *glob_cxt,
314 : : foreign_loc_cxt *outer_cxt,
315 : : foreign_loc_cxt *case_arg_cxt)
316 : : {
317 : 9630 : bool check_type = true;
318 : : PgFdwRelationInfo *fpinfo;
319 : : foreign_loc_cxt inner_cxt;
320 : : Oid collation;
321 : : FDWCollateState state;
322 : :
323 : : /* Need do nothing for empty subexpressions */
324 [ + + ]: 9630 : if (node == NULL)
325 : 335 : return true;
326 : :
327 : : /* May need server info from baserel's fdw_private struct */
328 : 9295 : fpinfo = (PgFdwRelationInfo *) (glob_cxt->foreignrel->fdw_private);
329 : :
330 : : /* Set up inner_cxt for possible recursion to child nodes */
331 : 9295 : inner_cxt.collation = InvalidOid;
332 : 9295 : inner_cxt.state = FDW_COLLATE_NONE;
333 : :
334 [ + + + + : 9295 : switch (nodeTag(node))
+ + + + +
+ + + + +
+ + + ]
335 : : {
336 : 4280 : case T_Var:
337 : : {
338 : 4280 : Var *var = (Var *) node;
339 : :
340 : : /*
341 : : * If the Var is from the foreign table, we consider its
342 : : * collation (if any) safe to use. If it is from another
343 : : * table, we treat its collation the same way as we would a
344 : : * Param's collation, ie it's not safe for it to have a
345 : : * non-default collation.
346 : : */
347 [ + + ]: 4280 : if (bms_is_member(var->varno, glob_cxt->relids) &&
348 [ + - ]: 3815 : var->varlevelsup == 0)
349 : : {
350 : : /* Var belongs to foreign table */
351 : :
352 : : /*
353 : : * System columns other than ctid should not be sent to
354 : : * the remote, since we don't make any effort to ensure
355 : : * that local and remote values match (tableoid, in
356 : : * particular, almost certainly doesn't match).
357 : : */
358 [ + + ]: 3815 : if (var->varattno < 0 &&
359 [ + + ]: 10 : var->varattno != SelfItemPointerAttributeNumber)
360 : 8 : return false;
361 : :
362 : : /* Else check the collation */
363 : 3807 : collation = var->varcollid;
364 : 3807 : state = OidIsValid(collation) ? FDW_COLLATE_SAFE : FDW_COLLATE_NONE;
365 : : }
366 : : else
367 : : {
368 : : /* Var belongs to some other table */
369 : 465 : collation = var->varcollid;
370 [ + + + - ]: 465 : if (collation == InvalidOid ||
371 : : collation == DEFAULT_COLLATION_OID)
372 : : {
373 : : /*
374 : : * It's noncollatable, or it's safe to combine with a
375 : : * collatable foreign Var, so set state to NONE.
376 : : */
377 : 465 : state = FDW_COLLATE_NONE;
378 : : }
379 : : else
380 : : {
381 : : /*
382 : : * Do not fail right away, since the Var might appear
383 : : * in a collation-insensitive context.
384 : : */
385 : 0 : state = FDW_COLLATE_UNSAFE;
386 : : }
387 : : }
388 : : }
389 : 4272 : break;
390 : 944 : case T_Const:
391 : : {
392 : 944 : Const *c = (Const *) node;
393 : :
394 : : /*
395 : : * Constants of regproc and related types can't be shipped
396 : : * unless the referenced object is shippable. But NULL's ok.
397 : : * (See also the related code in dependency.c.)
398 : : */
399 [ + + ]: 944 : if (!c->constisnull)
400 : : {
401 [ - - - - : 935 : switch (c->consttype)
- + - - -
- + ]
402 : : {
403 : 0 : case REGPROCOID:
404 : : case REGPROCEDUREOID:
405 [ # # ]: 0 : if (!is_shippable(DatumGetObjectId(c->constvalue),
406 : : ProcedureRelationId, fpinfo))
407 : 0 : return false;
408 : 0 : break;
409 : 0 : case REGOPEROID:
410 : : case REGOPERATOROID:
411 [ # # ]: 0 : if (!is_shippable(DatumGetObjectId(c->constvalue),
412 : : OperatorRelationId, fpinfo))
413 : 0 : return false;
414 : 0 : break;
415 : 0 : case REGCLASSOID:
416 [ # # ]: 0 : if (!is_shippable(DatumGetObjectId(c->constvalue),
417 : : RelationRelationId, fpinfo))
418 : 0 : return false;
419 : 0 : break;
420 : 0 : case REGTYPEOID:
421 [ # # ]: 0 : if (!is_shippable(DatumGetObjectId(c->constvalue),
422 : : TypeRelationId, fpinfo))
423 : 0 : return false;
424 : 0 : break;
425 : 0 : case REGCOLLATIONOID:
426 [ # # ]: 0 : if (!is_shippable(DatumGetObjectId(c->constvalue),
427 : : CollationRelationId, fpinfo))
428 : 0 : return false;
429 : 0 : break;
430 : 4 : case REGCONFIGOID:
431 : :
432 : : /*
433 : : * For text search objects only, we weaken the
434 : : * normal shippability criterion to allow all OIDs
435 : : * below FirstNormalObjectId. Without this, none
436 : : * of the initdb-installed TS configurations would
437 : : * be shippable, which would be quite annoying.
438 : : */
439 [ + - ]: 4 : if (DatumGetObjectId(c->constvalue) >= FirstNormalObjectId &&
440 [ + + ]: 4 : !is_shippable(DatumGetObjectId(c->constvalue),
441 : : TSConfigRelationId, fpinfo))
442 : 2 : return false;
443 : 2 : break;
444 : 0 : case REGDICTIONARYOID:
445 [ # # ]: 0 : if (DatumGetObjectId(c->constvalue) >= FirstNormalObjectId &&
446 [ # # ]: 0 : !is_shippable(DatumGetObjectId(c->constvalue),
447 : : TSDictionaryRelationId, fpinfo))
448 : 0 : return false;
449 : 0 : break;
450 : 0 : case REGNAMESPACEOID:
451 [ # # ]: 0 : if (!is_shippable(DatumGetObjectId(c->constvalue),
452 : : NamespaceRelationId, fpinfo))
453 : 0 : return false;
454 : 0 : break;
455 : 0 : case REGROLEOID:
456 [ # # ]: 0 : if (!is_shippable(DatumGetObjectId(c->constvalue),
457 : : AuthIdRelationId, fpinfo))
458 : 0 : return false;
459 : 0 : break;
460 : 0 : case REGDATABASEOID:
461 [ # # ]: 0 : if (!is_shippable(DatumGetObjectId(c->constvalue),
462 : : DatabaseRelationId, fpinfo))
463 : 0 : return false;
464 : 0 : break;
465 : : }
466 : : }
467 : :
468 : : /*
469 : : * If the constant has nondefault collation, either it's of a
470 : : * non-builtin type, or it reflects folding of a CollateExpr.
471 : : * It's unsafe to send to the remote unless it's used in a
472 : : * non-collation-sensitive context.
473 : : */
474 : 942 : collation = c->constcollid;
475 [ + + + + ]: 942 : if (collation == InvalidOid ||
476 : : collation == DEFAULT_COLLATION_OID)
477 : 940 : state = FDW_COLLATE_NONE;
478 : : else
479 : 2 : state = FDW_COLLATE_UNSAFE;
480 : : }
481 : 942 : break;
482 : 32 : case T_Param:
483 : : {
484 : 32 : Param *p = (Param *) node;
485 : :
486 : : /*
487 : : * If it's a MULTIEXPR Param, punt. We can't tell from here
488 : : * whether the referenced sublink/subplan contains any remote
489 : : * Vars; if it does, handling that is too complicated to
490 : : * consider supporting at present. Fortunately, MULTIEXPR
491 : : * Params are not reduced to plain PARAM_EXEC until the end of
492 : : * planning, so we can easily detect this case. (Normal
493 : : * PARAM_EXEC Params are safe to ship because their values
494 : : * come from somewhere else in the plan tree; but a MULTIEXPR
495 : : * references a sub-select elsewhere in the same targetlist,
496 : : * so we'd be on the hook to evaluate it somehow if we wanted
497 : : * to handle such cases as direct foreign updates.)
498 : : */
499 [ + + ]: 32 : if (p->paramkind == PARAM_MULTIEXPR)
500 : 3 : return false;
501 : :
502 : : /*
503 : : * Collation rule is same as for Consts and non-foreign Vars.
504 : : */
505 : 29 : collation = p->paramcollid;
506 [ + + + - ]: 29 : if (collation == InvalidOid ||
507 : : collation == DEFAULT_COLLATION_OID)
508 : 29 : state = FDW_COLLATE_NONE;
509 : : else
510 : 0 : state = FDW_COLLATE_UNSAFE;
511 : : }
512 : 29 : break;
513 : 1 : case T_SubscriptingRef:
514 : : {
515 : 1 : SubscriptingRef *sr = (SubscriptingRef *) node;
516 : :
517 : : /* Assignment should not be in restrictions. */
518 [ - + ]: 1 : if (sr->refassgnexpr != NULL)
519 : 0 : return false;
520 : :
521 : : /*
522 : : * Recurse into the remaining subexpressions. The container
523 : : * subscripts will not affect collation of the SubscriptingRef
524 : : * result, so do those first and reset inner_cxt afterwards.
525 : : */
526 [ - + ]: 1 : if (!foreign_expr_walker((Node *) sr->refupperindexpr,
527 : : glob_cxt, &inner_cxt, case_arg_cxt))
528 : 0 : return false;
529 : 1 : inner_cxt.collation = InvalidOid;
530 : 1 : inner_cxt.state = FDW_COLLATE_NONE;
531 [ - + ]: 1 : if (!foreign_expr_walker((Node *) sr->reflowerindexpr,
532 : : glob_cxt, &inner_cxt, case_arg_cxt))
533 : 0 : return false;
534 : 1 : inner_cxt.collation = InvalidOid;
535 : 1 : inner_cxt.state = FDW_COLLATE_NONE;
536 [ - + ]: 1 : if (!foreign_expr_walker((Node *) sr->refexpr,
537 : : glob_cxt, &inner_cxt, case_arg_cxt))
538 : 0 : return false;
539 : :
540 : : /*
541 : : * Container subscripting typically yields same collation as
542 : : * refexpr's, but in case it doesn't, use same logic as for
543 : : * function nodes.
544 : : */
545 : 1 : collation = sr->refcollid;
546 [ + - ]: 1 : if (collation == InvalidOid)
547 : 1 : state = FDW_COLLATE_NONE;
548 [ # # ]: 0 : else if (inner_cxt.state == FDW_COLLATE_SAFE &&
549 [ # # ]: 0 : collation == inner_cxt.collation)
550 : 0 : state = FDW_COLLATE_SAFE;
551 [ # # ]: 0 : else if (collation == DEFAULT_COLLATION_OID)
552 : 0 : state = FDW_COLLATE_NONE;
553 : : else
554 : 0 : state = FDW_COLLATE_UNSAFE;
555 : : }
556 : 1 : break;
557 : 138 : case T_FuncExpr:
558 : : {
559 : 138 : FuncExpr *fe = (FuncExpr *) node;
560 : :
561 : : /*
562 : : * If function used by the expression is not shippable, it
563 : : * can't be sent to remote because it might have incompatible
564 : : * semantics on remote side.
565 : : */
566 [ + + ]: 138 : if (!is_shippable(fe->funcid, ProcedureRelationId, fpinfo))
567 : 8 : return false;
568 : :
569 : : /*
570 : : * Recurse to input subexpressions.
571 : : */
572 [ + + ]: 130 : if (!foreign_expr_walker((Node *) fe->args,
573 : : glob_cxt, &inner_cxt, case_arg_cxt))
574 : 13 : return false;
575 : :
576 : : /*
577 : : * If function's input collation is not derived from a foreign
578 : : * Var, it can't be sent to remote.
579 : : */
580 [ + + ]: 117 : if (fe->inputcollid == InvalidOid)
581 : : /* OK, inputs are all noncollatable */ ;
582 [ + + ]: 33 : else if (inner_cxt.state != FDW_COLLATE_SAFE ||
583 [ - + ]: 20 : fe->inputcollid != inner_cxt.collation)
584 : 13 : return false;
585 : :
586 : : /*
587 : : * Detect whether node is introducing a collation not derived
588 : : * from a foreign Var. (If so, we just mark it unsafe for now
589 : : * rather than immediately returning false, since the parent
590 : : * node might not care.)
591 : : */
592 : 104 : collation = fe->funccollid;
593 [ + + ]: 104 : if (collation == InvalidOid)
594 : 85 : state = FDW_COLLATE_NONE;
595 [ + + ]: 19 : else if (inner_cxt.state == FDW_COLLATE_SAFE &&
596 [ + - ]: 18 : collation == inner_cxt.collation)
597 : 18 : state = FDW_COLLATE_SAFE;
598 [ - + ]: 1 : else if (collation == DEFAULT_COLLATION_OID)
599 : 0 : state = FDW_COLLATE_NONE;
600 : : else
601 : 1 : state = FDW_COLLATE_UNSAFE;
602 : : }
603 : 104 : break;
604 : 1643 : case T_OpExpr:
605 : : case T_DistinctExpr: /* struct-equivalent to OpExpr */
606 : : {
607 : 1643 : OpExpr *oe = (OpExpr *) node;
608 : :
609 : : /*
610 : : * Similarly, only shippable operators can be sent to remote.
611 : : * (If the operator is shippable, we assume its underlying
612 : : * function is too.)
613 : : */
614 [ + + ]: 1643 : if (!is_shippable(oe->opno, OperatorRelationId, fpinfo))
615 : 47 : return false;
616 : :
617 : : /*
618 : : * Recurse to input subexpressions.
619 : : */
620 [ + + ]: 1596 : if (!foreign_expr_walker((Node *) oe->args,
621 : : glob_cxt, &inner_cxt, case_arg_cxt))
622 : 66 : return false;
623 : :
624 : : /*
625 : : * If operator's input collation is not derived from a foreign
626 : : * Var, it can't be sent to remote.
627 : : */
628 [ + + ]: 1530 : if (oe->inputcollid == InvalidOid)
629 : : /* OK, inputs are all noncollatable */ ;
630 [ + + ]: 70 : else if (inner_cxt.state != FDW_COLLATE_SAFE ||
631 [ - + ]: 64 : oe->inputcollid != inner_cxt.collation)
632 : 6 : return false;
633 : :
634 : : /* Result-collation handling is same as for functions */
635 : 1524 : collation = oe->opcollid;
636 [ + + ]: 1524 : if (collation == InvalidOid)
637 : 1510 : state = FDW_COLLATE_NONE;
638 [ + - ]: 14 : else if (inner_cxt.state == FDW_COLLATE_SAFE &&
639 [ + - ]: 14 : collation == inner_cxt.collation)
640 : 14 : state = FDW_COLLATE_SAFE;
641 [ # # ]: 0 : else if (collation == DEFAULT_COLLATION_OID)
642 : 0 : state = FDW_COLLATE_NONE;
643 : : else
644 : 0 : state = FDW_COLLATE_UNSAFE;
645 : : }
646 : 1524 : break;
647 : 7 : case T_ScalarArrayOpExpr:
648 : : {
649 : 7 : ScalarArrayOpExpr *oe = (ScalarArrayOpExpr *) node;
650 : :
651 : : /*
652 : : * Again, only shippable operators can be sent to remote.
653 : : */
654 [ - + ]: 7 : if (!is_shippable(oe->opno, OperatorRelationId, fpinfo))
655 : 0 : return false;
656 : :
657 : : /*
658 : : * Recurse to input subexpressions.
659 : : */
660 [ + + ]: 7 : if (!foreign_expr_walker((Node *) oe->args,
661 : : glob_cxt, &inner_cxt, case_arg_cxt))
662 : 1 : return false;
663 : :
664 : : /*
665 : : * If operator's input collation is not derived from a foreign
666 : : * Var, it can't be sent to remote.
667 : : */
668 [ + + ]: 6 : if (oe->inputcollid == InvalidOid)
669 : : /* OK, inputs are all noncollatable */ ;
670 [ + - ]: 3 : else if (inner_cxt.state != FDW_COLLATE_SAFE ||
671 [ - + ]: 3 : oe->inputcollid != inner_cxt.collation)
672 : 0 : return false;
673 : :
674 : : /* Output is always boolean and so noncollatable. */
675 : 6 : collation = InvalidOid;
676 : 6 : state = FDW_COLLATE_NONE;
677 : : }
678 : 6 : break;
679 : 69 : case T_RelabelType:
680 : : {
681 : 69 : RelabelType *r = (RelabelType *) node;
682 : :
683 : : /*
684 : : * Recurse to input subexpression.
685 : : */
686 [ - + ]: 69 : if (!foreign_expr_walker((Node *) r->arg,
687 : : glob_cxt, &inner_cxt, case_arg_cxt))
688 : 0 : return false;
689 : :
690 : : /*
691 : : * RelabelType must not introduce a collation not derived from
692 : : * an input foreign Var (same logic as for a real function).
693 : : */
694 : 69 : collation = r->resultcollid;
695 [ - + ]: 69 : if (collation == InvalidOid)
696 : 0 : state = FDW_COLLATE_NONE;
697 [ + + ]: 69 : else if (inner_cxt.state == FDW_COLLATE_SAFE &&
698 [ + + ]: 65 : collation == inner_cxt.collation)
699 : 59 : state = FDW_COLLATE_SAFE;
700 [ + + ]: 10 : else if (collation == DEFAULT_COLLATION_OID)
701 : 3 : state = FDW_COLLATE_NONE;
702 : : else
703 : 7 : state = FDW_COLLATE_UNSAFE;
704 : : }
705 : 69 : break;
706 : 7 : case T_ArrayCoerceExpr:
707 : : {
708 : 7 : ArrayCoerceExpr *e = (ArrayCoerceExpr *) node;
709 : :
710 : : /*
711 : : * Push down only when the per-element coercion is a plain
712 : : * relabeling, that is, elemexpr is a RelabelType or a bare
713 : : * CaseTestExpr. Any other element coercion -- a cast
714 : : * function, an I/O conversion (CoerceViaIO), or a domain
715 : : * coercion -- is kept local. We ship only a bare
716 : : * "arg::resulttype" cast (nothing at all for an
717 : : * implicit-format coercion), so a non-relabeling conversion
718 : : * would be re-resolved against the remote server's catalogs
719 : : * and session state and could silently change the result.
720 : : * This matches the handling of the scalar coercions for the
721 : : * I/O and domain cases (never shipped); an element cast
722 : : * function is kept local too, which is more conservative than
723 : : * the scalar case (a scalar cast function is shipped when it
724 : : * is shippable).
725 : : */
726 [ + + ]: 7 : if (!IsA(e->elemexpr, RelabelType) &&
727 [ + - ]: 4 : !IsA(e->elemexpr, CaseTestExpr))
728 : 4 : return false;
729 : :
730 : : /*
731 : : * Recurse to input subexpression.
732 : : */
733 [ - + ]: 3 : if (!foreign_expr_walker((Node *) e->arg,
734 : : glob_cxt, &inner_cxt, case_arg_cxt))
735 : 0 : return false;
736 : :
737 : : /*
738 : : * T_ArrayCoerceExpr must not introduce a collation not
739 : : * derived from an input foreign Var (same logic as for a
740 : : * function).
741 : : */
742 : 3 : collation = e->resultcollid;
743 [ - + ]: 3 : if (collation == InvalidOid)
744 : 0 : state = FDW_COLLATE_NONE;
745 [ + + ]: 3 : else if (inner_cxt.state == FDW_COLLATE_SAFE &&
746 [ + - ]: 2 : collation == inner_cxt.collation)
747 : 2 : state = FDW_COLLATE_SAFE;
748 [ + - ]: 1 : else if (collation == DEFAULT_COLLATION_OID)
749 : 1 : state = FDW_COLLATE_NONE;
750 : : else
751 : 0 : state = FDW_COLLATE_UNSAFE;
752 : : }
753 : 3 : break;
754 : 36 : case T_BoolExpr:
755 : : {
756 : 36 : BoolExpr *b = (BoolExpr *) node;
757 : :
758 : : /*
759 : : * Recurse to input subexpressions.
760 : : */
761 [ - + ]: 36 : if (!foreign_expr_walker((Node *) b->args,
762 : : glob_cxt, &inner_cxt, case_arg_cxt))
763 : 0 : return false;
764 : :
765 : : /* Output is always boolean and so noncollatable. */
766 : 36 : collation = InvalidOid;
767 : 36 : state = FDW_COLLATE_NONE;
768 : : }
769 : 36 : break;
770 : 25 : case T_NullTest:
771 : : {
772 : 25 : NullTest *nt = (NullTest *) node;
773 : :
774 : : /*
775 : : * Recurse to input subexpressions.
776 : : */
777 [ - + ]: 25 : if (!foreign_expr_walker((Node *) nt->arg,
778 : : glob_cxt, &inner_cxt, case_arg_cxt))
779 : 0 : return false;
780 : :
781 : : /* Output is always boolean and so noncollatable. */
782 : 25 : collation = InvalidOid;
783 : 25 : state = FDW_COLLATE_NONE;
784 : : }
785 : 25 : break;
786 : 13 : case T_CaseExpr:
787 : : {
788 : 13 : CaseExpr *ce = (CaseExpr *) node;
789 : : foreign_loc_cxt arg_cxt;
790 : : foreign_loc_cxt tmp_cxt;
791 : : ListCell *lc;
792 : :
793 : : /*
794 : : * Recurse to CASE's arg expression, if any. Its collation
795 : : * has to be saved aside for use while examining CaseTestExprs
796 : : * within the WHEN expressions.
797 : : */
798 : 13 : arg_cxt.collation = InvalidOid;
799 : 13 : arg_cxt.state = FDW_COLLATE_NONE;
800 [ + + ]: 13 : if (ce->arg)
801 : : {
802 [ - + ]: 7 : if (!foreign_expr_walker((Node *) ce->arg,
803 : : glob_cxt, &arg_cxt, case_arg_cxt))
804 : 1 : return false;
805 : : }
806 : :
807 : : /* Examine the CaseWhen subexpressions. */
808 [ + - + + : 29 : foreach(lc, ce->args)
+ + ]
809 : : {
810 : 17 : CaseWhen *cw = lfirst_node(CaseWhen, lc);
811 : :
812 [ + + ]: 17 : if (ce->arg)
813 : : {
814 : : /*
815 : : * In a CASE-with-arg, the parser should have produced
816 : : * WHEN clauses of the form "CaseTestExpr = RHS",
817 : : * possibly with an implicit coercion inserted above
818 : : * the CaseTestExpr. However in an expression that's
819 : : * been through the optimizer, the WHEN clause could
820 : : * be almost anything (since the equality operator
821 : : * could have been expanded into an inline function).
822 : : * In such cases forbid pushdown, because
823 : : * deparseCaseExpr can't handle it.
824 : : */
825 : 11 : Node *whenExpr = (Node *) cw->expr;
826 : : List *opArgs;
827 : :
828 [ - + ]: 11 : if (!IsA(whenExpr, OpExpr))
829 : 1 : return false;
830 : :
831 : 11 : opArgs = ((OpExpr *) whenExpr)->args;
832 [ + - ]: 11 : if (list_length(opArgs) != 2 ||
833 [ - + ]: 11 : !IsA(strip_implicit_coercions(linitial(opArgs)),
834 : : CaseTestExpr))
835 : 0 : return false;
836 : : }
837 : :
838 : : /*
839 : : * Recurse to WHEN expression, passing down the arg info.
840 : : * Its collation doesn't affect the result (really, it
841 : : * should be boolean and thus not have a collation).
842 : : */
843 : 17 : tmp_cxt.collation = InvalidOid;
844 : 17 : tmp_cxt.state = FDW_COLLATE_NONE;
845 [ + + ]: 17 : if (!foreign_expr_walker((Node *) cw->expr,
846 : : glob_cxt, &tmp_cxt, &arg_cxt))
847 : 1 : return false;
848 : :
849 : : /* Recurse to THEN expression. */
850 [ - + ]: 16 : if (!foreign_expr_walker((Node *) cw->result,
851 : : glob_cxt, &inner_cxt, case_arg_cxt))
852 : 0 : return false;
853 : : }
854 : :
855 : : /* Recurse to ELSE expression. */
856 [ - + ]: 12 : if (!foreign_expr_walker((Node *) ce->defresult,
857 : : glob_cxt, &inner_cxt, case_arg_cxt))
858 : 0 : return false;
859 : :
860 : : /*
861 : : * Detect whether node is introducing a collation not derived
862 : : * from a foreign Var. (If so, we just mark it unsafe for now
863 : : * rather than immediately returning false, since the parent
864 : : * node might not care.) This is the same as for function
865 : : * nodes, except that the input collation is derived from only
866 : : * the THEN and ELSE subexpressions.
867 : : */
868 : 12 : collation = ce->casecollid;
869 [ + - ]: 12 : if (collation == InvalidOid)
870 : 12 : state = FDW_COLLATE_NONE;
871 [ # # ]: 0 : else if (inner_cxt.state == FDW_COLLATE_SAFE &&
872 [ # # ]: 0 : collation == inner_cxt.collation)
873 : 0 : state = FDW_COLLATE_SAFE;
874 [ # # ]: 0 : else if (collation == DEFAULT_COLLATION_OID)
875 : 0 : state = FDW_COLLATE_NONE;
876 : : else
877 : 0 : state = FDW_COLLATE_UNSAFE;
878 : : }
879 : 12 : break;
880 : 11 : case T_CaseTestExpr:
881 : : {
882 : 11 : CaseTestExpr *c = (CaseTestExpr *) node;
883 : :
884 : : /* Punt if we seem not to be inside a CASE arg WHEN. */
885 [ - + ]: 11 : if (!case_arg_cxt)
886 : 0 : return false;
887 : :
888 : : /*
889 : : * Otherwise, any nondefault collation attached to the
890 : : * CaseTestExpr node must be derived from foreign Var(s) in
891 : : * the CASE arg.
892 : : */
893 : 11 : collation = c->collation;
894 [ + + ]: 11 : if (collation == InvalidOid)
895 : 8 : state = FDW_COLLATE_NONE;
896 [ + + ]: 3 : else if (case_arg_cxt->state == FDW_COLLATE_SAFE &&
897 [ + - ]: 2 : collation == case_arg_cxt->collation)
898 : 2 : state = FDW_COLLATE_SAFE;
899 [ - + ]: 1 : else if (collation == DEFAULT_COLLATION_OID)
900 : 0 : state = FDW_COLLATE_NONE;
901 : : else
902 : 1 : state = FDW_COLLATE_UNSAFE;
903 : : }
904 : 11 : break;
905 : 4 : case T_ArrayExpr:
906 : : {
907 : 4 : ArrayExpr *a = (ArrayExpr *) node;
908 : :
909 : : /*
910 : : * Recurse to input subexpressions.
911 : : */
912 [ - + ]: 4 : if (!foreign_expr_walker((Node *) a->elements,
913 : : glob_cxt, &inner_cxt, case_arg_cxt))
914 : 0 : return false;
915 : :
916 : : /*
917 : : * ArrayExpr must not introduce a collation not derived from
918 : : * an input foreign Var (same logic as for a function).
919 : : */
920 : 4 : collation = a->array_collid;
921 [ + - ]: 4 : if (collation == InvalidOid)
922 : 4 : state = FDW_COLLATE_NONE;
923 [ # # ]: 0 : else if (inner_cxt.state == FDW_COLLATE_SAFE &&
924 [ # # ]: 0 : collation == inner_cxt.collation)
925 : 0 : state = FDW_COLLATE_SAFE;
926 [ # # ]: 0 : else if (collation == DEFAULT_COLLATION_OID)
927 : 0 : state = FDW_COLLATE_NONE;
928 : : else
929 : 0 : state = FDW_COLLATE_UNSAFE;
930 : : }
931 : 4 : break;
932 : 1772 : case T_List:
933 : : {
934 : 1772 : List *l = (List *) node;
935 : : ListCell *lc;
936 : :
937 : : /*
938 : : * Recurse to component subexpressions.
939 : : */
940 [ + - + + : 5048 : foreach(lc, l)
+ + ]
941 : : {
942 [ + + ]: 3369 : if (!foreign_expr_walker((Node *) lfirst(lc),
943 : : glob_cxt, &inner_cxt, case_arg_cxt))
944 : 93 : return false;
945 : : }
946 : :
947 : : /*
948 : : * When processing a list, collation state just bubbles up
949 : : * from the list elements.
950 : : */
951 : 1679 : collation = inner_cxt.collation;
952 : 1679 : state = inner_cxt.state;
953 : :
954 : : /* Don't apply exprType() to the list. */
955 : 1679 : check_type = false;
956 : : }
957 : 1679 : break;
958 : 283 : case T_Aggref:
959 : : {
960 : 283 : Aggref *agg = (Aggref *) node;
961 : : ListCell *lc;
962 : :
963 : : /* Not safe to pushdown when not in grouping context */
964 [ + + - + ]: 283 : if (!IS_UPPER_REL(glob_cxt->foreignrel))
965 : 0 : return false;
966 : :
967 : : /* Only non-split aggregates are pushable. */
968 [ - + ]: 283 : if (agg->aggsplit != AGGSPLIT_SIMPLE)
969 : 0 : return false;
970 : :
971 : : /* As usual, it must be shippable. */
972 [ + + ]: 283 : if (!is_shippable(agg->aggfnoid, ProcedureRelationId, fpinfo))
973 : 4 : return false;
974 : :
975 : : /*
976 : : * Recurse to input args. aggdirectargs, aggorder and
977 : : * aggdistinct are all present in args, so no need to check
978 : : * their shippability explicitly.
979 : : */
980 [ + + + + : 494 : foreach(lc, agg->args)
+ + ]
981 : : {
982 : 227 : Node *n = (Node *) lfirst(lc);
983 : :
984 : : /* If TargetEntry, extract the expression from it */
985 [ + - ]: 227 : if (IsA(n, TargetEntry))
986 : : {
987 : 227 : TargetEntry *tle = (TargetEntry *) n;
988 : :
989 : 227 : n = (Node *) tle->expr;
990 : : }
991 : :
992 [ + + ]: 227 : if (!foreign_expr_walker(n,
993 : : glob_cxt, &inner_cxt, case_arg_cxt))
994 : 12 : return false;
995 : : }
996 : :
997 : : /*
998 : : * For aggorder elements, check whether the sort operator, if
999 : : * specified, is shippable or not.
1000 : : */
1001 [ + + ]: 267 : if (agg->aggorder)
1002 : : {
1003 [ + - + + : 70 : foreach(lc, agg->aggorder)
+ + ]
1004 : : {
1005 : 38 : SortGroupClause *srt = (SortGroupClause *) lfirst(lc);
1006 : : Oid sortcoltype;
1007 : : TypeCacheEntry *typentry;
1008 : : TargetEntry *tle;
1009 : :
1010 : 38 : tle = get_sortgroupref_tle(srt->tleSortGroupRef,
1011 : : agg->args);
1012 : 38 : sortcoltype = exprType((Node *) tle->expr);
1013 : 38 : typentry = lookup_type_cache(sortcoltype,
1014 : : TYPECACHE_LT_OPR | TYPECACHE_GT_OPR);
1015 : : /* Check shippability of non-default sort operator. */
1016 [ + + ]: 38 : if (srt->sortop != typentry->lt_opr &&
1017 [ + + ]: 14 : srt->sortop != typentry->gt_opr &&
1018 [ + + ]: 6 : !is_shippable(srt->sortop, OperatorRelationId,
1019 : : fpinfo))
1020 : 4 : return false;
1021 : : }
1022 : : }
1023 : :
1024 : : /* Check aggregate filter */
1025 [ + + ]: 263 : if (!foreign_expr_walker((Node *) agg->aggfilter,
1026 : : glob_cxt, &inner_cxt, case_arg_cxt))
1027 : 2 : return false;
1028 : :
1029 : : /*
1030 : : * If aggregate's input collation is not derived from a
1031 : : * foreign Var, it can't be sent to remote.
1032 : : */
1033 [ + + ]: 261 : if (agg->inputcollid == InvalidOid)
1034 : : /* OK, inputs are all noncollatable */ ;
1035 [ + - ]: 22 : else if (inner_cxt.state != FDW_COLLATE_SAFE ||
1036 [ - + ]: 22 : agg->inputcollid != inner_cxt.collation)
1037 : 0 : return false;
1038 : :
1039 : : /*
1040 : : * Detect whether node is introducing a collation not derived
1041 : : * from a foreign Var. (If so, we just mark it unsafe for now
1042 : : * rather than immediately returning false, since the parent
1043 : : * node might not care.)
1044 : : */
1045 : 261 : collation = agg->aggcollid;
1046 [ + + ]: 261 : if (collation == InvalidOid)
1047 : 260 : state = FDW_COLLATE_NONE;
1048 [ + - ]: 1 : else if (inner_cxt.state == FDW_COLLATE_SAFE &&
1049 [ + - ]: 1 : collation == inner_cxt.collation)
1050 : 1 : state = FDW_COLLATE_SAFE;
1051 [ # # ]: 0 : else if (collation == DEFAULT_COLLATION_OID)
1052 : 0 : state = FDW_COLLATE_NONE;
1053 : : else
1054 : 0 : state = FDW_COLLATE_UNSAFE;
1055 : : }
1056 : 261 : break;
1057 : 30 : default:
1058 : :
1059 : : /*
1060 : : * If it's anything else, assume it's unsafe. This list can be
1061 : : * expanded later, but don't forget to add deparse support below.
1062 : : */
1063 : 30 : return false;
1064 : : }
1065 : :
1066 : : /*
1067 : : * If result type of given expression is not shippable, it can't be sent
1068 : : * to remote because it might have incompatible semantics on remote side.
1069 : : */
1070 [ + + + + ]: 8978 : if (check_type && !is_shippable(exprType(node), TypeRelationId, fpinfo))
1071 : 25 : return false;
1072 : :
1073 : : /*
1074 : : * Now, merge my collation information into my parent's state.
1075 : : */
1076 [ + + ]: 8953 : if (state > outer_cxt->state)
1077 : : {
1078 : : /* Override previous parent state */
1079 : 492 : outer_cxt->collation = collation;
1080 : 492 : outer_cxt->state = state;
1081 : : }
1082 [ + + ]: 8461 : else if (state == outer_cxt->state)
1083 : : {
1084 : : /* Merge, or detect error if there's a collation conflict */
1085 [ + + + - ]: 8409 : switch (state)
1086 : : {
1087 : 8394 : case FDW_COLLATE_NONE:
1088 : : /* Nothing + nothing is still nothing */
1089 : 8394 : break;
1090 : 14 : case FDW_COLLATE_SAFE:
1091 [ - + ]: 14 : if (collation != outer_cxt->collation)
1092 : : {
1093 : : /*
1094 : : * Non-default collation always beats default.
1095 : : */
1096 [ # # ]: 0 : if (outer_cxt->collation == DEFAULT_COLLATION_OID)
1097 : : {
1098 : : /* Override previous parent state */
1099 : 0 : outer_cxt->collation = collation;
1100 : : }
1101 [ # # ]: 0 : else if (collation != DEFAULT_COLLATION_OID)
1102 : : {
1103 : : /*
1104 : : * Conflict; show state as indeterminate. We don't
1105 : : * want to "return false" right away, since parent
1106 : : * node might not care about collation.
1107 : : */
1108 : 0 : outer_cxt->state = FDW_COLLATE_UNSAFE;
1109 : : }
1110 : : }
1111 : 14 : break;
1112 : 1 : case FDW_COLLATE_UNSAFE:
1113 : : /* We're still conflicted ... */
1114 : 1 : break;
1115 : : }
1116 : : }
1117 : :
1118 : : /* It looks OK */
1119 : 8953 : return true;
1120 : : }
1121 : :
1122 : : /*
1123 : : * Returns true if given expr is something we'd have to send the value of
1124 : : * to the foreign server.
1125 : : *
1126 : : * This should return true when the expression is a shippable node that
1127 : : * deparseExpr would add to context->params_list. Note that we don't care
1128 : : * if the expression *contains* such a node, only whether one appears at top
1129 : : * level. We need this to detect cases where setrefs.c would recognize a
1130 : : * false match between an fdw_exprs item (which came from the params_list)
1131 : : * and an entry in fdw_scan_tlist (which we're considering putting the given
1132 : : * expression into).
1133 : : */
1134 : : bool
1135 : 279 : is_foreign_param(PlannerInfo *root,
1136 : : RelOptInfo *baserel,
1137 : : Expr *expr)
1138 : : {
1139 [ - + ]: 279 : if (expr == NULL)
1140 : 0 : return false;
1141 : :
1142 [ + + + ]: 279 : switch (nodeTag(expr))
1143 : : {
1144 : 76 : case T_Var:
1145 : : {
1146 : : /* It would have to be sent unless it's a foreign Var */
1147 : 76 : Var *var = (Var *) expr;
1148 : 76 : PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) (baserel->fdw_private);
1149 : : Relids relids;
1150 : :
1151 [ + + + - ]: 76 : if (IS_UPPER_REL(baserel))
1152 : 76 : relids = fpinfo->outerrel->relids;
1153 : : else
1154 : 0 : relids = baserel->relids;
1155 : :
1156 [ + - + - ]: 76 : if (bms_is_member(var->varno, relids) && var->varlevelsup == 0)
1157 : 76 : return false; /* foreign Var, so not a param */
1158 : : else
1159 : 0 : return true; /* it'd have to be a param */
1160 : : break;
1161 : : }
1162 : 4 : case T_Param:
1163 : : /* Params always have to be sent to the foreign server */
1164 : 4 : return true;
1165 : 199 : default:
1166 : 199 : break;
1167 : : }
1168 : 199 : return false;
1169 : : }
1170 : :
1171 : : /*
1172 : : * Returns true if it's safe to push down the sort expression described by
1173 : : * 'pathkey' to the foreign server.
1174 : : */
1175 : : bool
1176 : 779 : is_foreign_pathkey(PlannerInfo *root,
1177 : : RelOptInfo *baserel,
1178 : : PathKey *pathkey)
1179 : : {
1180 : 779 : EquivalenceClass *pathkey_ec = pathkey->pk_eclass;
1181 : 779 : PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) baserel->fdw_private;
1182 : :
1183 : : /*
1184 : : * is_foreign_expr would detect volatile expressions as well, but checking
1185 : : * ec_has_volatile here saves some cycles.
1186 : : */
1187 [ + + ]: 779 : if (pathkey_ec->ec_has_volatile)
1188 : 4 : return false;
1189 : :
1190 : : /* can't push down the sort if the pathkey's opfamily is not shippable */
1191 [ + + ]: 775 : if (!is_shippable(pathkey->pk_opfamily, OperatorFamilyRelationId, fpinfo))
1192 : 3 : return false;
1193 : :
1194 : : /* can push if a suitable EC member exists */
1195 : 772 : return (find_em_for_rel(root, pathkey_ec, baserel) != NULL);
1196 : : }
1197 : :
1198 : : /*
1199 : : * Convert type OID + typmod info into a type name we can ship to the remote
1200 : : * server. Someplace else had better have verified that this type name is
1201 : : * expected to be known on the remote end.
1202 : : *
1203 : : * This is almost just format_type_with_typemod(), except that if left to its
1204 : : * own devices, that function will make schema-qualification decisions based
1205 : : * on the local search_path, which is wrong. We must schema-qualify all
1206 : : * type names that are not in pg_catalog. We assume here that built-in types
1207 : : * are all in pg_catalog and need not be qualified; otherwise, qualify.
1208 : : */
1209 : : static char *
1210 : 576 : deparse_type_name(Oid type_oid, int32 typemod)
1211 : : {
1212 : 576 : uint16 flags = FORMAT_TYPE_TYPEMOD_GIVEN;
1213 : :
1214 [ - + ]: 576 : if (!is_builtin(type_oid))
1215 : 0 : flags |= FORMAT_TYPE_FORCE_QUALIFY;
1216 : :
1217 : 576 : return format_type_extended(type_oid, typemod, flags);
1218 : : }
1219 : :
1220 : : /*
1221 : : * Build the targetlist for given relation to be deparsed as SELECT clause.
1222 : : *
1223 : : * The output targetlist contains the columns that need to be fetched from the
1224 : : * foreign server for the given relation. If foreignrel is an upper relation,
1225 : : * then the output targetlist can also contain expressions to be evaluated on
1226 : : * foreign server.
1227 : : */
1228 : : List *
1229 : 816 : build_tlist_to_deparse(RelOptInfo *foreignrel)
1230 : : {
1231 : 816 : List *tlist = NIL;
1232 : 816 : PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) foreignrel->fdw_private;
1233 : : ListCell *lc;
1234 : :
1235 : : /*
1236 : : * For an upper relation, we have already built the target list while
1237 : : * checking shippability, so just return that.
1238 : : */
1239 [ + + + + ]: 816 : if (IS_UPPER_REL(foreignrel))
1240 : 169 : return fpinfo->grouped_tlist;
1241 : :
1242 : : /*
1243 : : * We require columns specified in foreignrel->reltarget->exprs and those
1244 : : * required for evaluating the local conditions.
1245 : : */
1246 : 647 : tlist = add_to_flat_tlist(tlist,
1247 : 647 : pull_var_clause((Node *) foreignrel->reltarget->exprs,
1248 : : PVC_RECURSE_PLACEHOLDERS));
1249 [ + + + + : 671 : foreach(lc, fpinfo->local_conds)
+ + ]
1250 : : {
1251 : 24 : RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
1252 : :
1253 : 24 : tlist = add_to_flat_tlist(tlist,
1254 : 24 : pull_var_clause((Node *) rinfo->clause,
1255 : : PVC_RECURSE_PLACEHOLDERS));
1256 : : }
1257 : :
1258 : 647 : return tlist;
1259 : : }
1260 : :
1261 : : /*
1262 : : * Deparse SELECT statement for given relation into buf.
1263 : : *
1264 : : * tlist contains the list of desired columns to be fetched from foreign server.
1265 : : * For a base relation fpinfo->attrs_used is used to construct SELECT clause,
1266 : : * hence the tlist is ignored for a base relation.
1267 : : *
1268 : : * remote_conds is the list of conditions to be deparsed into the WHERE clause
1269 : : * (or, in the case of upper relations, into the HAVING clause).
1270 : : *
1271 : : * If params_list is not NULL, it receives a list of Params and other-relation
1272 : : * Vars used in the clauses; these values must be transmitted to the remote
1273 : : * server as parameter values.
1274 : : *
1275 : : * If params_list is NULL, we're generating the query for EXPLAIN purposes,
1276 : : * so Params and other-relation Vars should be replaced by dummy values.
1277 : : *
1278 : : * pathkeys is the list of pathkeys to order the result by.
1279 : : *
1280 : : * is_subquery is the flag to indicate whether to deparse the specified
1281 : : * relation as a subquery.
1282 : : *
1283 : : * List of columns selected is returned in retrieved_attrs.
1284 : : */
1285 : : void
1286 : 2412 : deparseSelectStmtForRel(StringInfo buf, PlannerInfo *root, RelOptInfo *rel,
1287 : : List *tlist, List *remote_conds, List *pathkeys,
1288 : : bool has_final_sort, bool has_limit, bool is_subquery,
1289 : : List **retrieved_attrs, List **params_list)
1290 : : {
1291 : : deparse_expr_cxt context;
1292 : 2412 : PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) rel->fdw_private;
1293 : : List *quals;
1294 : :
1295 : : /*
1296 : : * We handle relations for foreign tables, joins between those and upper
1297 : : * relations.
1298 : : */
1299 : : Assert(IS_JOIN_REL(rel) || IS_SIMPLE_REL(rel) || IS_UPPER_REL(rel));
1300 : :
1301 : : /* Fill portions of context common to upper, join and base relation */
1302 : 2412 : context.buf = buf;
1303 : 2412 : context.root = root;
1304 : 2412 : context.foreignrel = rel;
1305 [ + + + + ]: 2412 : context.scanrel = IS_UPPER_REL(rel) ? fpinfo->outerrel : rel;
1306 : 2412 : context.params_list = params_list;
1307 : :
1308 : : /* Construct SELECT clause */
1309 : 2412 : deparseSelectSql(tlist, is_subquery, retrieved_attrs, &context);
1310 : :
1311 : : /*
1312 : : * For upper relations, the WHERE clause is built from the remote
1313 : : * conditions of the underlying scan relation; otherwise, we can use the
1314 : : * supplied list of remote conditions directly.
1315 : : */
1316 [ + + + + ]: 2412 : if (IS_UPPER_REL(rel))
1317 : 169 : {
1318 : : PgFdwRelationInfo *ofpinfo;
1319 : :
1320 : 169 : ofpinfo = (PgFdwRelationInfo *) fpinfo->outerrel->fdw_private;
1321 : 169 : quals = ofpinfo->remote_conds;
1322 : : }
1323 : : else
1324 : 2243 : quals = remote_conds;
1325 : :
1326 : : /* Construct FROM and WHERE clauses */
1327 : 2412 : deparseFromExpr(quals, &context);
1328 : :
1329 [ + + + + ]: 2412 : if (IS_UPPER_REL(rel))
1330 : : {
1331 : : /* Append GROUP BY clause */
1332 : 169 : appendGroupByClause(tlist, &context);
1333 : :
1334 : : /* Append HAVING clause */
1335 [ + + ]: 169 : if (remote_conds)
1336 : : {
1337 : 18 : appendStringInfoString(buf, " HAVING ");
1338 : 18 : appendConditions(remote_conds, &context);
1339 : : }
1340 : : }
1341 : :
1342 : : /* Add ORDER BY clause if we found any useful pathkeys */
1343 [ + + ]: 2412 : if (pathkeys)
1344 : 752 : appendOrderByClause(pathkeys, has_final_sort, &context);
1345 : :
1346 : : /* Add LIMIT clause if necessary */
1347 [ + + ]: 2412 : if (has_limit)
1348 : 147 : appendLimitClause(&context);
1349 : :
1350 : : /* Add any necessary FOR UPDATE/SHARE. */
1351 : 2412 : deparseLockingClause(&context);
1352 : 2412 : }
1353 : :
1354 : : /*
1355 : : * Construct a simple SELECT statement that retrieves desired columns
1356 : : * of the specified foreign table, and append it to "buf". The output
1357 : : * contains just "SELECT ... ".
1358 : : *
1359 : : * We also create an integer List of the columns being retrieved, which is
1360 : : * returned to *retrieved_attrs, unless we deparse the specified relation
1361 : : * as a subquery.
1362 : : *
1363 : : * tlist is the list of desired columns. is_subquery is the flag to
1364 : : * indicate whether to deparse the specified relation as a subquery.
1365 : : * Read prologue of deparseSelectStmtForRel() for details.
1366 : : */
1367 : : static void
1368 : 2412 : deparseSelectSql(List *tlist, bool is_subquery, List **retrieved_attrs,
1369 : : deparse_expr_cxt *context)
1370 : : {
1371 : 2412 : StringInfo buf = context->buf;
1372 : 2412 : RelOptInfo *foreignrel = context->foreignrel;
1373 : 2412 : PlannerInfo *root = context->root;
1374 : 2412 : PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) foreignrel->fdw_private;
1375 : :
1376 : : /*
1377 : : * Construct SELECT list
1378 : : */
1379 : 2412 : appendStringInfoString(buf, "SELECT ");
1380 : :
1381 [ + + ]: 2412 : if (is_subquery)
1382 : : {
1383 : : /*
1384 : : * For a relation that is deparsed as a subquery, emit expressions
1385 : : * specified in the relation's reltarget. Note that since this is for
1386 : : * the subquery, no need to care about *retrieved_attrs.
1387 : : */
1388 : 50 : deparseSubqueryTargetList(context);
1389 : : }
1390 [ + + + + : 2362 : else if (IS_JOIN_REL(foreignrel) || IS_UPPER_REL(foreignrel))
+ + + + ]
1391 : : {
1392 : : /*
1393 : : * For a join or upper relation the input tlist gives the list of
1394 : : * columns required to be fetched from the foreign server.
1395 : : */
1396 : 816 : deparseExplicitTargetList(tlist, false, retrieved_attrs, context);
1397 : : }
1398 : : else
1399 : : {
1400 : : /*
1401 : : * For a base relation fpinfo->attrs_used gives the list of columns
1402 : : * required to be fetched from the foreign server.
1403 : : */
1404 [ + - ]: 1546 : RangeTblEntry *rte = planner_rt_fetch(foreignrel->relid, root);
1405 : :
1406 : : /*
1407 : : * Core code already has some lock on each rel being planned, so we
1408 : : * can use NoLock here.
1409 : : */
1410 : 1546 : Relation rel = table_open(rte->relid, NoLock);
1411 : :
1412 : 1546 : deparseTargetList(buf, rte, foreignrel->relid, rel, false,
1413 : : fpinfo->attrs_used, false, retrieved_attrs);
1414 : 1546 : table_close(rel, NoLock);
1415 : : }
1416 : 2412 : }
1417 : :
1418 : : /*
1419 : : * Construct a FROM clause and, if needed, a WHERE clause, and append those to
1420 : : * "buf".
1421 : : *
1422 : : * quals is the list of clauses to be included in the WHERE clause.
1423 : : * (These may or may not include RestrictInfo decoration.)
1424 : : */
1425 : : static void
1426 : 2412 : deparseFromExpr(List *quals, deparse_expr_cxt *context)
1427 : : {
1428 : 2412 : StringInfo buf = context->buf;
1429 : 2412 : RelOptInfo *scanrel = context->scanrel;
1430 : 2412 : List *additional_conds = NIL;
1431 : :
1432 : : /* For upper relations, scanrel must be either a joinrel or a baserel */
1433 : : Assert(!IS_UPPER_REL(context->foreignrel) ||
1434 : : IS_JOIN_REL(scanrel) || IS_SIMPLE_REL(scanrel));
1435 : :
1436 : : /* Construct FROM clause */
1437 : 2412 : appendStringInfoString(buf, " FROM ");
1438 : 2412 : deparseFromExprForRel(buf, context->root, scanrel,
1439 : 2412 : (bms_membership(scanrel->relids) == BMS_MULTIPLE),
1440 : : (Index) 0, NULL, &additional_conds,
1441 : : context->params_list);
1442 : 2412 : appendWhereClause(quals, additional_conds, context);
1443 [ + + ]: 2412 : if (additional_conds != NIL)
1444 : 158 : list_free_deep(additional_conds);
1445 : 2412 : }
1446 : :
1447 : : /*
1448 : : * Emit a target list that retrieves the columns specified in attrs_used.
1449 : : * This is used for both SELECT and RETURNING targetlists; the is_returning
1450 : : * parameter is true only for a RETURNING targetlist.
1451 : : *
1452 : : * The tlist text is appended to buf, and we also create an integer List
1453 : : * of the columns being retrieved, which is returned to *retrieved_attrs.
1454 : : *
1455 : : * If qualify_col is true, add relation alias before the column name.
1456 : : */
1457 : : static void
1458 : 1944 : deparseTargetList(StringInfo buf,
1459 : : RangeTblEntry *rte,
1460 : : Index rtindex,
1461 : : Relation rel,
1462 : : bool is_returning,
1463 : : Bitmapset *attrs_used,
1464 : : bool qualify_col,
1465 : : List **retrieved_attrs)
1466 : : {
1467 : 1944 : TupleDesc tupdesc = RelationGetDescr(rel);
1468 : : bool have_wholerow;
1469 : : bool first;
1470 : : int i;
1471 : :
1472 : 1944 : *retrieved_attrs = NIL;
1473 : :
1474 : : /* If there's a whole-row reference, we'll need all the columns. */
1475 : 1944 : have_wholerow = bms_is_member(0 - FirstLowInvalidHeapAttributeNumber,
1476 : : attrs_used);
1477 : :
1478 : 1944 : first = true;
1479 [ + + ]: 13656 : for (i = 1; i <= tupdesc->natts; i++)
1480 : : {
1481 : : /* Ignore dropped attributes. */
1482 [ + + ]: 11712 : if (TupleDescCompactAttr(tupdesc, i - 1)->attisdropped)
1483 : 1040 : continue;
1484 : :
1485 [ + + + + ]: 17864 : if (have_wholerow ||
1486 : 7192 : bms_is_member(i - FirstLowInvalidHeapAttributeNumber,
1487 : : attrs_used))
1488 : : {
1489 [ + + ]: 6670 : if (!first)
1490 : 4855 : appendStringInfoString(buf, ", ");
1491 [ + + ]: 1815 : else if (is_returning)
1492 : 114 : appendStringInfoString(buf, " RETURNING ");
1493 : 6670 : first = false;
1494 : :
1495 : 6670 : deparseColumnRef(buf, rtindex, i, rte, qualify_col);
1496 : :
1497 : 6670 : *retrieved_attrs = lappend_int(*retrieved_attrs, i);
1498 : : }
1499 : : }
1500 : :
1501 : : /*
1502 : : * Add ctid if needed. We currently don't support retrieving any other
1503 : : * system columns.
1504 : : */
1505 [ + + ]: 1944 : if (bms_is_member(SelfItemPointerAttributeNumber - FirstLowInvalidHeapAttributeNumber,
1506 : : attrs_used))
1507 : : {
1508 [ + + ]: 314 : if (!first)
1509 : 236 : appendStringInfoString(buf, ", ");
1510 [ - + ]: 78 : else if (is_returning)
1511 : 0 : appendStringInfoString(buf, " RETURNING ");
1512 : 314 : first = false;
1513 : :
1514 [ - + ]: 314 : if (qualify_col)
1515 : 0 : ADD_REL_QUALIFIER(buf, rtindex);
1516 : 314 : appendStringInfoString(buf, "ctid");
1517 : :
1518 : 314 : *retrieved_attrs = lappend_int(*retrieved_attrs,
1519 : : SelfItemPointerAttributeNumber);
1520 : : }
1521 : :
1522 : : /* Don't generate bad syntax if no undropped columns */
1523 [ + + + + ]: 1944 : if (first && !is_returning)
1524 : 45 : appendStringInfoString(buf, "NULL");
1525 : 1944 : }
1526 : :
1527 : : /*
1528 : : * Deparse the appropriate locking clause (FOR UPDATE or FOR SHARE) for a
1529 : : * given relation (context->scanrel).
1530 : : */
1531 : : static void
1532 : 2412 : deparseLockingClause(deparse_expr_cxt *context)
1533 : : {
1534 : 2412 : StringInfo buf = context->buf;
1535 : 2412 : PlannerInfo *root = context->root;
1536 : 2412 : RelOptInfo *rel = context->scanrel;
1537 : 2412 : PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) rel->fdw_private;
1538 : 2412 : int relid = -1;
1539 : :
1540 [ + + ]: 6027 : while ((relid = bms_next_member(rel->relids, relid)) >= 0)
1541 : : {
1542 : : /*
1543 : : * Ignore relation if it appears in a lower subquery. Locking clause
1544 : : * for such a relation is included in the subquery if necessary.
1545 : : */
1546 [ + + ]: 3615 : if (bms_is_member(relid, fpinfo->lower_subquery_rels))
1547 : 80 : continue;
1548 : :
1549 : : /*
1550 : : * Add FOR UPDATE/SHARE if appropriate. We apply locking during the
1551 : : * initial row fetch, rather than later on as is done for local
1552 : : * tables. The extra roundtrips involved in trying to duplicate the
1553 : : * local semantics exactly don't seem worthwhile (see also comments
1554 : : * for RowMarkType).
1555 : : *
1556 : : * Note: because we actually run the query as a cursor, this assumes
1557 : : * that DECLARE CURSOR ... FOR UPDATE is supported, which it isn't
1558 : : * before 8.3.
1559 : : */
1560 [ + + ]: 3535 : if (bms_is_member(relid, root->all_result_relids) &&
1561 [ + + ]: 323 : (root->parse->commandType == CMD_UPDATE ||
1562 [ + + ]: 130 : root->parse->commandType == CMD_DELETE))
1563 : : {
1564 : : /* Relation is UPDATE/DELETE target, so use FOR UPDATE */
1565 : 321 : appendStringInfoString(buf, " FOR UPDATE");
1566 : :
1567 : : /* Add the relation alias if we are here for a join relation */
1568 [ + + - + ]: 321 : if (IS_JOIN_REL(rel))
1569 : 54 : appendStringInfo(buf, " OF %s%d", REL_ALIAS_PREFIX, relid);
1570 : : }
1571 : : else
1572 : : {
1573 : 3214 : PlanRowMark *rc = get_plan_rowmark(root->rowMarks, relid);
1574 : :
1575 [ + + ]: 3214 : if (rc)
1576 : : {
1577 : : /*
1578 : : * Relation is specified as a FOR UPDATE/SHARE target, so
1579 : : * handle that. (But we could also see LCS_NONE, meaning this
1580 : : * isn't a target relation after all.)
1581 : : *
1582 : : * For now, just ignore any [NO] KEY specification, since (a)
1583 : : * it's not clear what that means for a remote table that we
1584 : : * don't have complete information about, and (b) it wouldn't
1585 : : * work anyway on older remote servers. Likewise, we don't
1586 : : * worry about NOWAIT.
1587 : : */
1588 [ + + + - ]: 404 : switch (rc->strength)
1589 : : {
1590 : 240 : case LCS_NONE:
1591 : : /* No locking needed */
1592 : 240 : break;
1593 : 38 : case LCS_FORKEYSHARE:
1594 : : case LCS_FORSHARE:
1595 : 38 : appendStringInfoString(buf, " FOR SHARE");
1596 : 38 : break;
1597 : 126 : case LCS_FORNOKEYUPDATE:
1598 : : case LCS_FORUPDATE:
1599 : 126 : appendStringInfoString(buf, " FOR UPDATE");
1600 : 126 : break;
1601 : : }
1602 : :
1603 : : /* Add the relation alias if we are here for a join relation */
1604 [ + + ]: 404 : if (bms_membership(rel->relids) == BMS_MULTIPLE &&
1605 [ + + ]: 224 : rc->strength != LCS_NONE)
1606 : 110 : appendStringInfo(buf, " OF %s%d", REL_ALIAS_PREFIX, relid);
1607 : : }
1608 : : }
1609 : : }
1610 : 2412 : }
1611 : :
1612 : : /*
1613 : : * Deparse conditions from the provided list and append them to buf.
1614 : : *
1615 : : * The conditions in the list are assumed to be ANDed. This function is used to
1616 : : * deparse WHERE clauses, JOIN .. ON clauses and HAVING clauses.
1617 : : *
1618 : : * Depending on the caller, the list elements might be either RestrictInfos
1619 : : * or bare clauses.
1620 : : */
1621 : : static void
1622 : 1947 : appendConditions(List *exprs, deparse_expr_cxt *context)
1623 : : {
1624 : : int nestlevel;
1625 : : ListCell *lc;
1626 : 1947 : bool is_first = true;
1627 : 1947 : StringInfo buf = context->buf;
1628 : :
1629 : : /* Make sure any constants in the exprs are printed portably */
1630 : 1947 : nestlevel = set_transmission_modes();
1631 : :
1632 [ + - + + : 4465 : foreach(lc, exprs)
+ + ]
1633 : : {
1634 : 2518 : Expr *expr = (Expr *) lfirst(lc);
1635 : :
1636 : : /* Extract clause from RestrictInfo, if required */
1637 [ + + ]: 2518 : if (IsA(expr, RestrictInfo))
1638 : 2132 : expr = ((RestrictInfo *) expr)->clause;
1639 : :
1640 : : /* Connect expressions with "AND" and parenthesize each condition. */
1641 [ + + ]: 2518 : if (!is_first)
1642 : 571 : appendStringInfoString(buf, " AND ");
1643 : :
1644 : 2518 : appendStringInfoChar(buf, '(');
1645 : 2518 : deparseExpr(expr, context);
1646 : 2518 : appendStringInfoChar(buf, ')');
1647 : :
1648 : 2518 : is_first = false;
1649 : : }
1650 : :
1651 : 1947 : reset_transmission_modes(nestlevel);
1652 : 1947 : }
1653 : :
1654 : : /*
1655 : : * Append WHERE clause, containing conditions from exprs and additional_conds,
1656 : : * to context->buf.
1657 : : */
1658 : : static void
1659 : 2711 : appendWhereClause(List *exprs, List *additional_conds, deparse_expr_cxt *context)
1660 : : {
1661 : 2711 : StringInfo buf = context->buf;
1662 : 2711 : bool need_and = false;
1663 : : ListCell *lc;
1664 : :
1665 [ + + + + ]: 2711 : if (exprs != NIL || additional_conds != NIL)
1666 : 1281 : appendStringInfoString(buf, " WHERE ");
1667 : :
1668 : : /*
1669 : : * If there are some filters, append them.
1670 : : */
1671 [ + + ]: 2711 : if (exprs != NIL)
1672 : : {
1673 : 1178 : appendConditions(exprs, context);
1674 : 1178 : need_and = true;
1675 : : }
1676 : :
1677 : : /*
1678 : : * If there are some EXISTS conditions, coming from SEMI-JOINS, append
1679 : : * them.
1680 : : */
1681 [ + + + + : 2901 : foreach(lc, additional_conds)
+ + ]
1682 : : {
1683 [ + + ]: 190 : if (need_and)
1684 : 87 : appendStringInfoString(buf, " AND ");
1685 : 190 : appendStringInfoString(buf, (char *) lfirst(lc));
1686 : 190 : need_and = true;
1687 : : }
1688 : 2711 : }
1689 : :
1690 : : /* Output join name for given join type */
1691 : : const char *
1692 : 1111 : get_jointype_name(JoinType jointype)
1693 : : {
1694 [ + + - + : 1111 : switch (jointype)
+ - ]
1695 : : {
1696 : 743 : case JOIN_INNER:
1697 : 743 : return "INNER";
1698 : :
1699 : 216 : case JOIN_LEFT:
1700 : 216 : return "LEFT";
1701 : :
1702 : 0 : case JOIN_RIGHT:
1703 : 0 : return "RIGHT";
1704 : :
1705 : 108 : case JOIN_FULL:
1706 : 108 : return "FULL";
1707 : :
1708 : 44 : case JOIN_SEMI:
1709 : 44 : return "SEMI";
1710 : :
1711 : 0 : default:
1712 : : /* Shouldn't come here, but protect from buggy code. */
1713 [ # # ]: 0 : elog(ERROR, "unsupported join type %d", jointype);
1714 : : }
1715 : :
1716 : : /* Keep compiler happy */
1717 : : return NULL;
1718 : : }
1719 : :
1720 : : /*
1721 : : * Deparse given targetlist and append it to context->buf.
1722 : : *
1723 : : * tlist is list of TargetEntry's which in turn contain Var nodes.
1724 : : *
1725 : : * retrieved_attrs is the list of continuously increasing integers starting
1726 : : * from 1. It has same number of entries as tlist.
1727 : : *
1728 : : * This is used for both SELECT and RETURNING targetlists; the is_returning
1729 : : * parameter is true only for a RETURNING targetlist.
1730 : : */
1731 : : static void
1732 : 824 : deparseExplicitTargetList(List *tlist,
1733 : : bool is_returning,
1734 : : List **retrieved_attrs,
1735 : : deparse_expr_cxt *context)
1736 : : {
1737 : : ListCell *lc;
1738 : 824 : StringInfo buf = context->buf;
1739 : 824 : int i = 0;
1740 : :
1741 : 824 : *retrieved_attrs = NIL;
1742 : :
1743 [ + + + + : 4826 : foreach(lc, tlist)
+ + ]
1744 : : {
1745 : 4002 : TargetEntry *tle = lfirst_node(TargetEntry, lc);
1746 : :
1747 [ + + ]: 4002 : if (i > 0)
1748 : 3190 : appendStringInfoString(buf, ", ");
1749 [ + + ]: 812 : else if (is_returning)
1750 : 2 : appendStringInfoString(buf, " RETURNING ");
1751 : :
1752 : 4002 : deparseExpr((Expr *) tle->expr, context);
1753 : :
1754 : 4002 : *retrieved_attrs = lappend_int(*retrieved_attrs, i + 1);
1755 : 4002 : i++;
1756 : : }
1757 : :
1758 [ + + + + ]: 824 : if (i == 0 && !is_returning)
1759 : 6 : appendStringInfoString(buf, "NULL");
1760 : 824 : }
1761 : :
1762 : : /*
1763 : : * Emit expressions specified in the given relation's reltarget.
1764 : : *
1765 : : * This is used for deparsing the given relation as a subquery.
1766 : : */
1767 : : static void
1768 : 50 : deparseSubqueryTargetList(deparse_expr_cxt *context)
1769 : : {
1770 : 50 : StringInfo buf = context->buf;
1771 : 50 : RelOptInfo *foreignrel = context->foreignrel;
1772 : : bool first;
1773 : : ListCell *lc;
1774 : :
1775 : : /* Should only be called in these cases. */
1776 : : Assert(IS_SIMPLE_REL(foreignrel) || IS_JOIN_REL(foreignrel));
1777 : :
1778 : 50 : first = true;
1779 [ + + + + : 120 : foreach(lc, foreignrel->reltarget->exprs)
+ + ]
1780 : : {
1781 : 70 : Node *node = (Node *) lfirst(lc);
1782 : :
1783 [ + + ]: 70 : if (!first)
1784 : 24 : appendStringInfoString(buf, ", ");
1785 : 70 : first = false;
1786 : :
1787 : 70 : deparseExpr((Expr *) node, context);
1788 : : }
1789 : :
1790 : : /* Don't generate bad syntax if no expressions */
1791 [ + + ]: 50 : if (first)
1792 : 4 : appendStringInfoString(buf, "NULL");
1793 : 50 : }
1794 : :
1795 : : /*
1796 : : * Construct FROM clause for given relation
1797 : : *
1798 : : * The function constructs ... JOIN ... ON ... for join relation. For a base
1799 : : * relation it just returns schema-qualified tablename, with the appropriate
1800 : : * alias if so requested.
1801 : : *
1802 : : * 'ignore_rel' is either zero or the RT index of a target relation. In the
1803 : : * latter case the function constructs FROM clause of UPDATE or USING clause
1804 : : * of DELETE; it deparses the join relation as if the relation never contained
1805 : : * the target relation, and creates a List of conditions to be deparsed into
1806 : : * the top-level WHERE clause, which is returned to *ignore_conds.
1807 : : *
1808 : : * 'additional_conds' is a pointer to a list of strings to be appended to
1809 : : * the WHERE clause, coming from lower-level SEMI-JOINs.
1810 : : */
1811 : : static void
1812 : 4304 : deparseFromExprForRel(StringInfo buf, PlannerInfo *root, RelOptInfo *foreignrel,
1813 : : bool use_alias, Index ignore_rel, List **ignore_conds,
1814 : : List **additional_conds, List **params_list)
1815 : : {
1816 : 4304 : PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) foreignrel->fdw_private;
1817 : :
1818 [ + + + + ]: 4304 : if (IS_JOIN_REL(foreignrel))
1819 : 963 : {
1820 : : StringInfoData join_sql_o;
1821 : : StringInfoData join_sql_i;
1822 : 971 : RelOptInfo *outerrel = fpinfo->outerrel;
1823 : 971 : RelOptInfo *innerrel = fpinfo->innerrel;
1824 : 971 : bool outerrel_is_target = false;
1825 : 971 : bool innerrel_is_target = false;
1826 : 971 : List *additional_conds_i = NIL;
1827 : 971 : List *additional_conds_o = NIL;
1828 : :
1829 [ + + + - ]: 971 : if (ignore_rel > 0 && bms_is_member(ignore_rel, foreignrel->relids))
1830 : : {
1831 : : /*
1832 : : * If this is an inner join, add joinclauses to *ignore_conds and
1833 : : * set it to empty so that those can be deparsed into the WHERE
1834 : : * clause. Note that since the target relation can never be
1835 : : * within the nullable side of an outer join, those could safely
1836 : : * be pulled up into the WHERE clause (see foreign_join_ok()).
1837 : : * Note also that since the target relation is only inner-joined
1838 : : * to any other relation in the query, all conditions in the join
1839 : : * tree mentioning the target relation could be deparsed into the
1840 : : * WHERE clause by doing this recursively.
1841 : : */
1842 [ + + ]: 12 : if (fpinfo->jointype == JOIN_INNER)
1843 : : {
1844 : 20 : *ignore_conds = list_concat(*ignore_conds,
1845 : 10 : fpinfo->joinclauses);
1846 : 10 : fpinfo->joinclauses = NIL;
1847 : : }
1848 : :
1849 : : /*
1850 : : * Check if either of the input relations is the target relation.
1851 : : */
1852 [ + + ]: 12 : if (outerrel->relid == ignore_rel)
1853 : 8 : outerrel_is_target = true;
1854 [ - + ]: 4 : else if (innerrel->relid == ignore_rel)
1855 : 0 : innerrel_is_target = true;
1856 : : }
1857 : :
1858 : : /* Deparse outer relation if not the target relation. */
1859 [ + + ]: 971 : if (!outerrel_is_target)
1860 : : {
1861 : 963 : initStringInfo(&join_sql_o);
1862 : 963 : deparseRangeTblRef(&join_sql_o, root, outerrel,
1863 : 963 : fpinfo->make_outerrel_subquery,
1864 : : ignore_rel, ignore_conds, &additional_conds_o,
1865 : : params_list);
1866 : :
1867 : : /*
1868 : : * If inner relation is the target relation, skip deparsing it.
1869 : : * Note that since the join of the target relation with any other
1870 : : * relation in the query is an inner join and can never be within
1871 : : * the nullable side of an outer join, the join could be
1872 : : * interchanged with higher-level joins (cf. identity 1 on outer
1873 : : * join reordering shown in src/backend/optimizer/README), which
1874 : : * means it's safe to skip the target-relation deparsing here.
1875 : : */
1876 [ - + ]: 963 : if (innerrel_is_target)
1877 : : {
1878 : : Assert(fpinfo->jointype == JOIN_INNER);
1879 : : Assert(fpinfo->joinclauses == NIL);
1880 : 0 : appendBinaryStringInfo(buf, join_sql_o.data, join_sql_o.len);
1881 : : /* Pass EXISTS conditions to upper level */
1882 [ # # ]: 0 : if (additional_conds_o != NIL)
1883 : : {
1884 : : Assert(*additional_conds == NIL);
1885 : 0 : *additional_conds = additional_conds_o;
1886 : : }
1887 : 8 : return;
1888 : : }
1889 : : }
1890 : :
1891 : : /* Deparse inner relation if not the target relation. */
1892 [ + - ]: 971 : if (!innerrel_is_target)
1893 : : {
1894 : 971 : initStringInfo(&join_sql_i);
1895 : 971 : deparseRangeTblRef(&join_sql_i, root, innerrel,
1896 : 971 : fpinfo->make_innerrel_subquery,
1897 : : ignore_rel, ignore_conds, &additional_conds_i,
1898 : : params_list);
1899 : :
1900 : : /*
1901 : : * SEMI-JOIN is deparsed as the EXISTS subquery. It references
1902 : : * outer and inner relations, so it should be evaluated as the
1903 : : * condition in the upper-level WHERE clause. We deparse the
1904 : : * condition and pass it to upper level callers as an
1905 : : * additional_conds list. Upper level callers are responsible for
1906 : : * inserting conditions from the list where appropriate.
1907 : : */
1908 [ + + ]: 971 : if (fpinfo->jointype == JOIN_SEMI)
1909 : : {
1910 : : deparse_expr_cxt context;
1911 : : StringInfoData str;
1912 : :
1913 : : /* Construct deparsed condition from this SEMI-JOIN */
1914 : 190 : initStringInfo(&str);
1915 : 190 : appendStringInfo(&str, "EXISTS (SELECT NULL FROM %s",
1916 : : join_sql_i.data);
1917 : :
1918 : 190 : context.buf = &str;
1919 : 190 : context.foreignrel = foreignrel;
1920 : 190 : context.scanrel = foreignrel;
1921 : 190 : context.root = root;
1922 : 190 : context.params_list = params_list;
1923 : :
1924 : : /*
1925 : : * Append SEMI-JOIN clauses and EXISTS conditions from lower
1926 : : * levels to the current EXISTS subquery
1927 : : */
1928 : 190 : appendWhereClause(fpinfo->joinclauses, additional_conds_i, &context);
1929 : :
1930 : : /*
1931 : : * EXISTS conditions, coming from lower join levels, have just
1932 : : * been processed.
1933 : : */
1934 [ + + ]: 190 : if (additional_conds_i != NIL)
1935 : : {
1936 : 16 : list_free_deep(additional_conds_i);
1937 : 16 : additional_conds_i = NIL;
1938 : : }
1939 : :
1940 : : /* Close parentheses for EXISTS subquery */
1941 : 190 : appendStringInfoChar(&str, ')');
1942 : :
1943 : 190 : *additional_conds = lappend(*additional_conds, str.data);
1944 : : }
1945 : :
1946 : : /*
1947 : : * If outer relation is the target relation, skip deparsing it.
1948 : : * See the above note about safety.
1949 : : */
1950 [ + + ]: 971 : if (outerrel_is_target)
1951 : : {
1952 : : Assert(fpinfo->jointype == JOIN_INNER);
1953 : : Assert(fpinfo->joinclauses == NIL);
1954 : 8 : appendBinaryStringInfo(buf, join_sql_i.data, join_sql_i.len);
1955 : : /* Pass EXISTS conditions to the upper call */
1956 [ - + ]: 8 : if (additional_conds_i != NIL)
1957 : : {
1958 : : Assert(*additional_conds == NIL);
1959 : 0 : *additional_conds = additional_conds_i;
1960 : : }
1961 : 8 : return;
1962 : : }
1963 : : }
1964 : :
1965 : : /* Neither of the relations is the target relation. */
1966 : : Assert(!outerrel_is_target && !innerrel_is_target);
1967 : :
1968 : : /*
1969 : : * For semijoin FROM clause is deparsed as an outer relation. An inner
1970 : : * relation and join clauses are converted to EXISTS condition and
1971 : : * passed to the upper level.
1972 : : */
1973 [ + + ]: 963 : if (fpinfo->jointype == JOIN_SEMI)
1974 : : {
1975 : 190 : appendBinaryStringInfo(buf, join_sql_o.data, join_sql_o.len);
1976 : : }
1977 : : else
1978 : : {
1979 : : /*
1980 : : * For a join relation FROM clause, entry is deparsed as
1981 : : *
1982 : : * ((outer relation) <join type> (inner relation) ON
1983 : : * (joinclauses))
1984 : : */
1985 : 773 : appendStringInfo(buf, "(%s %s JOIN %s ON ", join_sql_o.data,
1986 : : get_jointype_name(fpinfo->jointype), join_sql_i.data);
1987 : :
1988 : : /* Append join clause; (TRUE) if no join clause */
1989 [ + + ]: 773 : if (fpinfo->joinclauses)
1990 : : {
1991 : : deparse_expr_cxt context;
1992 : :
1993 : 751 : context.buf = buf;
1994 : 751 : context.foreignrel = foreignrel;
1995 : 751 : context.scanrel = foreignrel;
1996 : 751 : context.root = root;
1997 : 751 : context.params_list = params_list;
1998 : :
1999 : 751 : appendStringInfoChar(buf, '(');
2000 : 751 : appendConditions(fpinfo->joinclauses, &context);
2001 : 751 : appendStringInfoChar(buf, ')');
2002 : : }
2003 : : else
2004 : 22 : appendStringInfoString(buf, "(TRUE)");
2005 : :
2006 : : /* End the FROM clause entry. */
2007 : 773 : appendStringInfoChar(buf, ')');
2008 : : }
2009 : :
2010 : : /*
2011 : : * Construct additional_conds to be passed to the upper caller from
2012 : : * current level additional_conds and additional_conds, coming from
2013 : : * inner and outer rels.
2014 : : */
2015 [ + + ]: 963 : if (additional_conds_o != NIL)
2016 : : {
2017 : 48 : *additional_conds = list_concat(*additional_conds,
2018 : : additional_conds_o);
2019 : 48 : list_free(additional_conds_o);
2020 : : }
2021 : :
2022 [ - + ]: 963 : if (additional_conds_i != NIL)
2023 : : {
2024 : 0 : *additional_conds = list_concat(*additional_conds,
2025 : : additional_conds_i);
2026 : 0 : list_free(additional_conds_i);
2027 : : }
2028 : : }
2029 : : else
2030 : : {
2031 [ + - ]: 3333 : RangeTblEntry *rte = planner_rt_fetch(foreignrel->relid, root);
2032 : :
2033 : : /*
2034 : : * Core code already has some lock on each rel being planned, so we
2035 : : * can use NoLock here.
2036 : : */
2037 : 3333 : Relation rel = table_open(rte->relid, NoLock);
2038 : :
2039 : 3333 : deparseRelation(buf, rel);
2040 : :
2041 : : /*
2042 : : * Add a unique alias to avoid any conflict in relation names due to
2043 : : * pulled up subqueries in the query being built for a pushed down
2044 : : * join.
2045 : : */
2046 [ + + ]: 3333 : if (use_alias)
2047 : 1612 : appendStringInfo(buf, " %s%d", REL_ALIAS_PREFIX, foreignrel->relid);
2048 : :
2049 : 3333 : table_close(rel, NoLock);
2050 : : }
2051 : : }
2052 : :
2053 : : /*
2054 : : * Append FROM clause entry for the given relation into buf.
2055 : : * Conditions from lower-level SEMI-JOINs are appended to additional_conds
2056 : : * and should be added to upper level WHERE clause.
2057 : : */
2058 : : static void
2059 : 1934 : deparseRangeTblRef(StringInfo buf, PlannerInfo *root, RelOptInfo *foreignrel,
2060 : : bool make_subquery, Index ignore_rel, List **ignore_conds,
2061 : : List **additional_conds, List **params_list)
2062 : : {
2063 : 1934 : PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) foreignrel->fdw_private;
2064 : :
2065 : : /* Should only be called in these cases. */
2066 : : Assert(IS_SIMPLE_REL(foreignrel) || IS_JOIN_REL(foreignrel));
2067 : :
2068 : : Assert(fpinfo->local_conds == NIL);
2069 : :
2070 : : /* If make_subquery is true, deparse the relation as a subquery. */
2071 [ + + ]: 1934 : if (make_subquery)
2072 : : {
2073 : : List *retrieved_attrs;
2074 : : int ncols;
2075 : :
2076 : : /*
2077 : : * The given relation shouldn't contain the target relation, because
2078 : : * this should only happen for input relations for a full join, and
2079 : : * such relations can never contain an UPDATE/DELETE target.
2080 : : */
2081 : : Assert(ignore_rel == 0 ||
2082 : : !bms_is_member(ignore_rel, foreignrel->relids));
2083 : :
2084 : : /* Deparse the subquery representing the relation. */
2085 : 50 : appendStringInfoChar(buf, '(');
2086 : 50 : deparseSelectStmtForRel(buf, root, foreignrel, NIL,
2087 : : fpinfo->remote_conds, NIL,
2088 : : false, false, true,
2089 : : &retrieved_attrs, params_list);
2090 : 50 : appendStringInfoChar(buf, ')');
2091 : :
2092 : : /* Append the relation alias. */
2093 : 50 : appendStringInfo(buf, " %s%d", SUBQUERY_REL_ALIAS_PREFIX,
2094 : : fpinfo->relation_index);
2095 : :
2096 : : /*
2097 : : * Append the column aliases if needed. Note that the subquery emits
2098 : : * expressions specified in the relation's reltarget (see
2099 : : * deparseSubqueryTargetList).
2100 : : */
2101 : 50 : ncols = list_length(foreignrel->reltarget->exprs);
2102 [ + + ]: 50 : if (ncols > 0)
2103 : : {
2104 : : int i;
2105 : :
2106 : 46 : appendStringInfoChar(buf, '(');
2107 [ + + ]: 116 : for (i = 1; i <= ncols; i++)
2108 : : {
2109 [ + + ]: 70 : if (i > 1)
2110 : 24 : appendStringInfoString(buf, ", ");
2111 : :
2112 : 70 : appendStringInfo(buf, "%s%d", SUBQUERY_COL_ALIAS_PREFIX, i);
2113 : : }
2114 : 46 : appendStringInfoChar(buf, ')');
2115 : : }
2116 : : }
2117 : : else
2118 : 1884 : deparseFromExprForRel(buf, root, foreignrel, true, ignore_rel,
2119 : : ignore_conds, additional_conds,
2120 : : params_list);
2121 : 1934 : }
2122 : :
2123 : : /*
2124 : : * deparse remote INSERT statement
2125 : : *
2126 : : * The statement text is appended to buf, and we also create an integer List
2127 : : * of the columns being retrieved by WITH CHECK OPTION or RETURNING (if any),
2128 : : * which is returned to *retrieved_attrs.
2129 : : *
2130 : : * This also stores end position of the VALUES clause, so that we can rebuild
2131 : : * an INSERT for a batch of rows later.
2132 : : */
2133 : : void
2134 : 146 : deparseInsertSql(StringInfo buf, RangeTblEntry *rte,
2135 : : Index rtindex, Relation rel,
2136 : : List *targetAttrs, bool doNothing,
2137 : : List *withCheckOptionList, List *returningList,
2138 : : List **retrieved_attrs, int *values_end_len)
2139 : : {
2140 : 146 : TupleDesc tupdesc = RelationGetDescr(rel);
2141 : : AttrNumber pindex;
2142 : : bool first;
2143 : : ListCell *lc;
2144 : :
2145 : 146 : appendStringInfoString(buf, "INSERT INTO ");
2146 : 146 : deparseRelation(buf, rel);
2147 : :
2148 [ + + ]: 146 : if (targetAttrs)
2149 : : {
2150 : 145 : appendStringInfoChar(buf, '(');
2151 : :
2152 : 145 : first = true;
2153 [ + - + + : 540 : foreach(lc, targetAttrs)
+ + ]
2154 : : {
2155 : 395 : int attnum = lfirst_int(lc);
2156 : :
2157 [ + + ]: 395 : if (!first)
2158 : 250 : appendStringInfoString(buf, ", ");
2159 : 395 : first = false;
2160 : :
2161 : 395 : deparseColumnRef(buf, rtindex, attnum, rte, false);
2162 : : }
2163 : :
2164 : 145 : appendStringInfoString(buf, ") VALUES (");
2165 : :
2166 : 145 : pindex = 1;
2167 : 145 : first = true;
2168 [ + - + + : 540 : foreach(lc, targetAttrs)
+ + ]
2169 : : {
2170 : 395 : int attnum = lfirst_int(lc);
2171 : 395 : CompactAttribute *attr = TupleDescCompactAttr(tupdesc, attnum - 1);
2172 : :
2173 [ + + ]: 395 : if (!first)
2174 : 250 : appendStringInfoString(buf, ", ");
2175 : 395 : first = false;
2176 : :
2177 [ + + ]: 395 : if (attr->attgenerated)
2178 : 10 : appendStringInfoString(buf, "DEFAULT");
2179 : : else
2180 : : {
2181 : 385 : appendStringInfo(buf, "$%d", pindex);
2182 : 385 : pindex++;
2183 : : }
2184 : : }
2185 : :
2186 : 145 : appendStringInfoChar(buf, ')');
2187 : : }
2188 : : else
2189 : 1 : appendStringInfoString(buf, " DEFAULT VALUES");
2190 : 146 : *values_end_len = buf->len;
2191 : :
2192 [ + + ]: 146 : if (doNothing)
2193 : 3 : appendStringInfoString(buf, " ON CONFLICT DO NOTHING");
2194 : :
2195 : 146 : deparseReturningList(buf, rte, rtindex, rel,
2196 [ + + + + ]: 146 : rel->trigdesc && rel->trigdesc->trig_insert_after_row,
2197 : 146 : withCheckOptionList, returningList, retrieved_attrs);
2198 : 146 : }
2199 : :
2200 : : /*
2201 : : * rebuild remote INSERT statement
2202 : : *
2203 : : * Provided a number of rows in a batch, builds INSERT statement with the
2204 : : * right number of parameters.
2205 : : */
2206 : : void
2207 : 26 : rebuildInsertSql(StringInfo buf, Relation rel,
2208 : : char *orig_query, List *target_attrs,
2209 : : int values_end_len, int num_params,
2210 : : int num_rows)
2211 : : {
2212 : 26 : TupleDesc tupdesc = RelationGetDescr(rel);
2213 : : int i;
2214 : : int pindex;
2215 : : bool first;
2216 : : ListCell *lc;
2217 : :
2218 : : /* Make sure the values_end_len is sensible */
2219 : : Assert((values_end_len > 0) && (values_end_len <= strlen(orig_query)));
2220 : :
2221 : : /* Copy up to the end of the first record from the original query */
2222 : 26 : appendBinaryStringInfo(buf, orig_query, values_end_len);
2223 : :
2224 : : /*
2225 : : * Add records to VALUES clause (we already have parameters for the first
2226 : : * row, so start at the right offset).
2227 : : */
2228 : 26 : pindex = num_params + 1;
2229 [ + + ]: 103 : for (i = 0; i < num_rows; i++)
2230 : : {
2231 : 77 : appendStringInfoString(buf, ", (");
2232 : :
2233 : 77 : first = true;
2234 [ + - + + : 226 : foreach(lc, target_attrs)
+ + ]
2235 : : {
2236 : 149 : int attnum = lfirst_int(lc);
2237 : 149 : CompactAttribute *attr = TupleDescCompactAttr(tupdesc, attnum - 1);
2238 : :
2239 [ + + ]: 149 : if (!first)
2240 : 72 : appendStringInfoString(buf, ", ");
2241 : 149 : first = false;
2242 : :
2243 [ + + ]: 149 : if (attr->attgenerated)
2244 : 2 : appendStringInfoString(buf, "DEFAULT");
2245 : : else
2246 : : {
2247 : 147 : appendStringInfo(buf, "$%d", pindex);
2248 : 147 : pindex++;
2249 : : }
2250 : : }
2251 : :
2252 : 77 : appendStringInfoChar(buf, ')');
2253 : : }
2254 : :
2255 : : /* Copy stuff after VALUES clause from the original query */
2256 : 26 : appendStringInfoString(buf, orig_query + values_end_len);
2257 : 26 : }
2258 : :
2259 : : /*
2260 : : * deparse remote UPDATE statement
2261 : : *
2262 : : * The statement text is appended to buf, and we also create an integer List
2263 : : * of the columns being retrieved by WITH CHECK OPTION or RETURNING (if any),
2264 : : * which is returned to *retrieved_attrs.
2265 : : */
2266 : : void
2267 : 61 : deparseUpdateSql(StringInfo buf, RangeTblEntry *rte,
2268 : : Index rtindex, Relation rel,
2269 : : List *targetAttrs,
2270 : : List *withCheckOptionList, List *returningList,
2271 : : List **retrieved_attrs)
2272 : : {
2273 : 61 : TupleDesc tupdesc = RelationGetDescr(rel);
2274 : : AttrNumber pindex;
2275 : : bool first;
2276 : : ListCell *lc;
2277 : :
2278 : 61 : appendStringInfoString(buf, "UPDATE ");
2279 : 61 : deparseRelation(buf, rel);
2280 : 61 : appendStringInfoString(buf, " SET ");
2281 : :
2282 : 61 : pindex = 2; /* ctid is always the first param */
2283 : 61 : first = true;
2284 [ + - + + : 149 : foreach(lc, targetAttrs)
+ + ]
2285 : : {
2286 : 88 : int attnum = lfirst_int(lc);
2287 : 88 : CompactAttribute *attr = TupleDescCompactAttr(tupdesc, attnum - 1);
2288 : :
2289 [ + + ]: 88 : if (!first)
2290 : 27 : appendStringInfoString(buf, ", ");
2291 : 88 : first = false;
2292 : :
2293 : 88 : deparseColumnRef(buf, rtindex, attnum, rte, false);
2294 [ + + ]: 88 : if (attr->attgenerated)
2295 : 4 : appendStringInfoString(buf, " = DEFAULT");
2296 : : else
2297 : : {
2298 : 84 : appendStringInfo(buf, " = $%d", pindex);
2299 : 84 : pindex++;
2300 : : }
2301 : : }
2302 : 61 : appendStringInfoString(buf, " WHERE ctid = $1");
2303 : :
2304 : 61 : deparseReturningList(buf, rte, rtindex, rel,
2305 [ + + + + ]: 61 : rel->trigdesc && rel->trigdesc->trig_update_after_row,
2306 : 61 : withCheckOptionList, returningList, retrieved_attrs);
2307 : 61 : }
2308 : :
2309 : : /*
2310 : : * deparse remote UPDATE statement
2311 : : *
2312 : : * 'buf' is the output buffer to append the statement to
2313 : : * 'rtindex' is the RT index of the associated target relation
2314 : : * 'rel' is the relation descriptor for the target relation
2315 : : * 'foreignrel' is the RelOptInfo for the target relation or the join relation
2316 : : * containing all base relations in the query
2317 : : * 'targetlist' is the tlist of the underlying foreign-scan plan node
2318 : : * (note that this only contains new-value expressions and junk attrs)
2319 : : * 'targetAttrs' is the target columns of the UPDATE
2320 : : * 'remote_conds' is the qual clauses that must be evaluated remotely
2321 : : * '*params_list' is an output list of exprs that will become remote Params
2322 : : * 'returningList' is the RETURNING targetlist
2323 : : * '*retrieved_attrs' is an output list of integers of columns being retrieved
2324 : : * by RETURNING (if any)
2325 : : */
2326 : : void
2327 : 48 : deparseDirectUpdateSql(StringInfo buf, PlannerInfo *root,
2328 : : Index rtindex, Relation rel,
2329 : : RelOptInfo *foreignrel,
2330 : : List *targetlist,
2331 : : List *targetAttrs,
2332 : : List *remote_conds,
2333 : : List **params_list,
2334 : : List *returningList,
2335 : : List **retrieved_attrs)
2336 : : {
2337 : : deparse_expr_cxt context;
2338 : : int nestlevel;
2339 : : bool first;
2340 [ + - ]: 48 : RangeTblEntry *rte = planner_rt_fetch(rtindex, root);
2341 : : ListCell *lc,
2342 : : *lc2;
2343 : 48 : List *additional_conds = NIL;
2344 : :
2345 : : /* Set up context struct for recursion */
2346 : 48 : context.root = root;
2347 : 48 : context.foreignrel = foreignrel;
2348 : 48 : context.scanrel = foreignrel;
2349 : 48 : context.buf = buf;
2350 : 48 : context.params_list = params_list;
2351 : :
2352 : 48 : appendStringInfoString(buf, "UPDATE ");
2353 : 48 : deparseRelation(buf, rel);
2354 [ + + ]: 48 : if (foreignrel->reloptkind == RELOPT_JOINREL)
2355 : 4 : appendStringInfo(buf, " %s%d", REL_ALIAS_PREFIX, rtindex);
2356 : 48 : appendStringInfoString(buf, " SET ");
2357 : :
2358 : : /* Make sure any constants in the exprs are printed portably */
2359 : 48 : nestlevel = set_transmission_modes();
2360 : :
2361 : 48 : first = true;
2362 [ + - + - : 106 : forboth(lc, targetlist, lc2, targetAttrs)
+ - + + +
- + + +
+ ]
2363 : : {
2364 : 58 : TargetEntry *tle = lfirst_node(TargetEntry, lc);
2365 : 58 : int attnum = lfirst_int(lc2);
2366 : :
2367 : : /* update's new-value expressions shouldn't be resjunk */
2368 : : Assert(!tle->resjunk);
2369 : :
2370 [ + + ]: 58 : if (!first)
2371 : 10 : appendStringInfoString(buf, ", ");
2372 : 58 : first = false;
2373 : :
2374 : 58 : deparseColumnRef(buf, rtindex, attnum, rte, false);
2375 : 58 : appendStringInfoString(buf, " = ");
2376 : 58 : deparseExpr((Expr *) tle->expr, &context);
2377 : : }
2378 : :
2379 : 48 : reset_transmission_modes(nestlevel);
2380 : :
2381 [ + + ]: 48 : if (foreignrel->reloptkind == RELOPT_JOINREL)
2382 : : {
2383 : 4 : List *ignore_conds = NIL;
2384 : :
2385 : :
2386 : 4 : appendStringInfoString(buf, " FROM ");
2387 : 4 : deparseFromExprForRel(buf, root, foreignrel, true, rtindex,
2388 : : &ignore_conds, &additional_conds, params_list);
2389 : 4 : remote_conds = list_concat(remote_conds, ignore_conds);
2390 : : }
2391 : :
2392 : 48 : appendWhereClause(remote_conds, additional_conds, &context);
2393 : :
2394 [ - + ]: 48 : if (additional_conds != NIL)
2395 : 0 : list_free_deep(additional_conds);
2396 : :
2397 [ + + ]: 48 : if (foreignrel->reloptkind == RELOPT_JOINREL)
2398 : 4 : deparseExplicitTargetList(returningList, true, retrieved_attrs,
2399 : : &context);
2400 : : else
2401 : 44 : deparseReturningList(buf, rte, rtindex, rel, false,
2402 : : NIL, returningList, retrieved_attrs);
2403 : 48 : }
2404 : :
2405 : : /*
2406 : : * deparse remote DELETE statement
2407 : : *
2408 : : * The statement text is appended to buf, and we also create an integer List
2409 : : * of the columns being retrieved by RETURNING (if any), which is returned
2410 : : * to *retrieved_attrs.
2411 : : */
2412 : : void
2413 : 22 : deparseDeleteSql(StringInfo buf, RangeTblEntry *rte,
2414 : : Index rtindex, Relation rel,
2415 : : List *returningList,
2416 : : List **retrieved_attrs)
2417 : : {
2418 : 22 : appendStringInfoString(buf, "DELETE FROM ");
2419 : 22 : deparseRelation(buf, rel);
2420 : 22 : appendStringInfoString(buf, " WHERE ctid = $1");
2421 : :
2422 : 22 : deparseReturningList(buf, rte, rtindex, rel,
2423 [ + + + + ]: 22 : rel->trigdesc && rel->trigdesc->trig_delete_after_row,
2424 : 22 : NIL, returningList, retrieved_attrs);
2425 : 22 : }
2426 : :
2427 : : /*
2428 : : * deparse remote DELETE statement
2429 : : *
2430 : : * 'buf' is the output buffer to append the statement to
2431 : : * 'rtindex' is the RT index of the associated target relation
2432 : : * 'rel' is the relation descriptor for the target relation
2433 : : * 'foreignrel' is the RelOptInfo for the target relation or the join relation
2434 : : * containing all base relations in the query
2435 : : * 'remote_conds' is the qual clauses that must be evaluated remotely
2436 : : * '*params_list' is an output list of exprs that will become remote Params
2437 : : * 'returningList' is the RETURNING targetlist
2438 : : * '*retrieved_attrs' is an output list of integers of columns being retrieved
2439 : : * by RETURNING (if any)
2440 : : */
2441 : : void
2442 : 61 : deparseDirectDeleteSql(StringInfo buf, PlannerInfo *root,
2443 : : Index rtindex, Relation rel,
2444 : : RelOptInfo *foreignrel,
2445 : : List *remote_conds,
2446 : : List **params_list,
2447 : : List *returningList,
2448 : : List **retrieved_attrs)
2449 : : {
2450 : : deparse_expr_cxt context;
2451 : 61 : List *additional_conds = NIL;
2452 : :
2453 : : /* Set up context struct for recursion */
2454 : 61 : context.root = root;
2455 : 61 : context.foreignrel = foreignrel;
2456 : 61 : context.scanrel = foreignrel;
2457 : 61 : context.buf = buf;
2458 : 61 : context.params_list = params_list;
2459 : :
2460 : 61 : appendStringInfoString(buf, "DELETE FROM ");
2461 : 61 : deparseRelation(buf, rel);
2462 [ + + ]: 61 : if (foreignrel->reloptkind == RELOPT_JOINREL)
2463 : 4 : appendStringInfo(buf, " %s%d", REL_ALIAS_PREFIX, rtindex);
2464 : :
2465 [ + + ]: 61 : if (foreignrel->reloptkind == RELOPT_JOINREL)
2466 : : {
2467 : 4 : List *ignore_conds = NIL;
2468 : :
2469 : 4 : appendStringInfoString(buf, " USING ");
2470 : 4 : deparseFromExprForRel(buf, root, foreignrel, true, rtindex,
2471 : : &ignore_conds, &additional_conds, params_list);
2472 : 4 : remote_conds = list_concat(remote_conds, ignore_conds);
2473 : : }
2474 : :
2475 : 61 : appendWhereClause(remote_conds, additional_conds, &context);
2476 : :
2477 [ - + ]: 61 : if (additional_conds != NIL)
2478 : 0 : list_free_deep(additional_conds);
2479 : :
2480 [ + + ]: 61 : if (foreignrel->reloptkind == RELOPT_JOINREL)
2481 : 4 : deparseExplicitTargetList(returningList, true, retrieved_attrs,
2482 : : &context);
2483 : : else
2484 [ + - ]: 57 : deparseReturningList(buf, planner_rt_fetch(rtindex, root),
2485 : : rtindex, rel, false,
2486 : : NIL, returningList, retrieved_attrs);
2487 : 61 : }
2488 : :
2489 : : /*
2490 : : * Add a RETURNING clause, if needed, to an INSERT/UPDATE/DELETE.
2491 : : */
2492 : : static void
2493 : 330 : deparseReturningList(StringInfo buf, RangeTblEntry *rte,
2494 : : Index rtindex, Relation rel,
2495 : : bool trig_after_row,
2496 : : List *withCheckOptionList,
2497 : : List *returningList,
2498 : : List **retrieved_attrs)
2499 : : {
2500 : 330 : Bitmapset *attrs_used = NULL;
2501 : :
2502 [ + + ]: 330 : if (trig_after_row)
2503 : : {
2504 : : /* whole-row reference acquires all non-system columns */
2505 : 24 : attrs_used =
2506 : 24 : bms_make_singleton(0 - FirstLowInvalidHeapAttributeNumber);
2507 : : }
2508 : :
2509 [ + + ]: 330 : if (withCheckOptionList != NIL)
2510 : : {
2511 : : /*
2512 : : * We need the attrs, non-system and system, mentioned in the local
2513 : : * query's WITH CHECK OPTION list.
2514 : : *
2515 : : * Note: we do this to ensure that WCO constraints will be evaluated
2516 : : * on the data actually inserted/updated on the remote side, which
2517 : : * might differ from the data supplied by the core code, for example
2518 : : * as a result of remote triggers.
2519 : : */
2520 : 19 : pull_varattnos((Node *) withCheckOptionList, rtindex,
2521 : : &attrs_used);
2522 : : }
2523 : :
2524 [ + + ]: 330 : if (returningList != NIL)
2525 : : {
2526 : : /*
2527 : : * We need the attrs, non-system and system, mentioned in the local
2528 : : * query's RETURNING list.
2529 : : */
2530 : 78 : pull_varattnos((Node *) returningList, rtindex,
2531 : : &attrs_used);
2532 : : }
2533 : :
2534 [ + + ]: 330 : if (attrs_used != NULL)
2535 : 120 : deparseTargetList(buf, rte, rtindex, rel, true, attrs_used, false,
2536 : : retrieved_attrs);
2537 : : else
2538 : 210 : *retrieved_attrs = NIL;
2539 : 330 : }
2540 : :
2541 : : /*
2542 : : * Construct SELECT statement to acquire size in blocks of given relation.
2543 : : *
2544 : : * Note: we use local definition of block size, not remote definition.
2545 : : * This is perhaps debatable.
2546 : : *
2547 : : * Note: pg_relation_size() exists in 8.1 and later.
2548 : : */
2549 : : void
2550 : 52 : deparseAnalyzeSizeSql(StringInfo buf, Relation rel)
2551 : : {
2552 : : StringInfoData relname;
2553 : :
2554 : : /* We'll need the remote relation name as a literal. */
2555 : 52 : initStringInfo(&relname);
2556 : 52 : deparseRelation(&relname, rel);
2557 : :
2558 : 52 : appendStringInfoString(buf, "SELECT pg_catalog.pg_relation_size(");
2559 : 52 : deparseStringLiteral(buf, relname.data);
2560 : 52 : appendStringInfo(buf, "::pg_catalog.regclass) / %d", BLCKSZ);
2561 : 52 : }
2562 : :
2563 : : /*
2564 : : * Construct SELECT statement to acquire the number of pages, the number of
2565 : : * rows, and the relkind of a relation.
2566 : : *
2567 : : * Note: we just return the remote server's reltuples value, which might
2568 : : * be off a good deal, but it doesn't seem worth working harder. See
2569 : : * comments in postgresAcquireSampleRowsFunc.
2570 : : */
2571 : : void
2572 : 57 : deparseAnalyzeInfoSql(StringInfo buf, Relation rel)
2573 : : {
2574 : : StringInfoData relname;
2575 : :
2576 : : /* We'll need the remote relation name as a literal. */
2577 : 57 : initStringInfo(&relname);
2578 : 57 : deparseRelation(&relname, rel);
2579 : :
2580 : 57 : appendStringInfoString(buf, "SELECT relpages, reltuples, relkind FROM pg_catalog.pg_class WHERE oid = ");
2581 : 57 : deparseStringLiteral(buf, relname.data);
2582 : 57 : appendStringInfoString(buf, "::pg_catalog.regclass");
2583 : 57 : }
2584 : :
2585 : : /*
2586 : : * Construct SELECT statement to acquire sample rows of given relation.
2587 : : *
2588 : : * SELECT command is appended to buf, and list of columns retrieved
2589 : : * is returned to *retrieved_attrs.
2590 : : *
2591 : : * We only support sampling methods we can decide based on server version.
2592 : : * Allowing custom TSM modules (like tsm_system_rows) might be useful, but it
2593 : : * would require detecting which extensions are installed, to allow automatic
2594 : : * fall-back. Moreover, the methods may use different parameters like number
2595 : : * of rows (and not sampling rate). So we leave this for future improvements.
2596 : : *
2597 : : * Using random() to sample rows on the remote server has the advantage that
2598 : : * this works on all PostgreSQL versions (unlike TABLESAMPLE), and that it
2599 : : * does the sampling on the remote side (without transferring everything and
2600 : : * then discarding most rows).
2601 : : *
2602 : : * The disadvantage is that we still have to read all rows and evaluate the
2603 : : * random(), while TABLESAMPLE (at least with the "system" method) may skip.
2604 : : * It's not that different from the "bernoulli" method, though.
2605 : : *
2606 : : * We could also do "ORDER BY random() LIMIT x", which would always pick
2607 : : * the expected number of rows, but it requires sorting so it may be much
2608 : : * more expensive (particularly on large tables, which is what the
2609 : : * remote sampling is meant to improve).
2610 : : */
2611 : : void
2612 : 52 : deparseAnalyzeSql(StringInfo buf, Relation rel,
2613 : : PgFdwSamplingMethod sample_method, double sample_frac,
2614 : : List **retrieved_attrs)
2615 : : {
2616 : 52 : Oid relid = RelationGetRelid(rel);
2617 : 52 : TupleDesc tupdesc = RelationGetDescr(rel);
2618 : : int i;
2619 : : char *colname;
2620 : : List *options;
2621 : : ListCell *lc;
2622 : 52 : bool first = true;
2623 : :
2624 : 52 : *retrieved_attrs = NIL;
2625 : :
2626 : 52 : appendStringInfoString(buf, "SELECT ");
2627 [ + + ]: 221 : for (i = 0; i < tupdesc->natts; i++)
2628 : : {
2629 : : /* Ignore dropped columns. */
2630 [ + + ]: 169 : if (TupleDescAttr(tupdesc, i)->attisdropped)
2631 : 3 : continue;
2632 : :
2633 [ + + ]: 166 : if (!first)
2634 : 114 : appendStringInfoString(buf, ", ");
2635 : 166 : first = false;
2636 : :
2637 : : /* Use attribute name or column_name option. */
2638 : 166 : colname = NameStr(TupleDescAttr(tupdesc, i)->attname);
2639 : 166 : options = GetForeignColumnOptions(relid, i + 1);
2640 : :
2641 [ + + + - : 166 : foreach(lc, options)
+ + ]
2642 : : {
2643 : 7 : DefElem *def = (DefElem *) lfirst(lc);
2644 : :
2645 [ + - ]: 7 : if (strcmp(def->defname, "column_name") == 0)
2646 : : {
2647 : 7 : colname = defGetString(def);
2648 : 7 : break;
2649 : : }
2650 : : }
2651 : :
2652 : 166 : appendStringInfoString(buf, quote_identifier(colname));
2653 : :
2654 : 166 : *retrieved_attrs = lappend_int(*retrieved_attrs, i + 1);
2655 : : }
2656 : :
2657 : : /* Don't generate bad syntax for zero-column relation. */
2658 [ - + ]: 52 : if (first)
2659 : 0 : appendStringInfoString(buf, "NULL");
2660 : :
2661 : : /*
2662 : : * Construct FROM clause, and perhaps WHERE clause too, depending on the
2663 : : * selected sampling method.
2664 : : */
2665 : 52 : appendStringInfoString(buf, " FROM ");
2666 : 52 : deparseRelation(buf, rel);
2667 : :
2668 [ + - - - : 52 : switch (sample_method)
- - ]
2669 : : {
2670 : 52 : case ANALYZE_SAMPLE_OFF:
2671 : : /* nothing to do here */
2672 : 52 : break;
2673 : :
2674 : 0 : case ANALYZE_SAMPLE_RANDOM:
2675 : 0 : appendStringInfo(buf, " WHERE pg_catalog.random() < %f", sample_frac);
2676 : 0 : break;
2677 : :
2678 : 0 : case ANALYZE_SAMPLE_SYSTEM:
2679 : 0 : appendStringInfo(buf, " TABLESAMPLE SYSTEM(%f)", (100.0 * sample_frac));
2680 : 0 : break;
2681 : :
2682 : 0 : case ANALYZE_SAMPLE_BERNOULLI:
2683 : 0 : appendStringInfo(buf, " TABLESAMPLE BERNOULLI(%f)", (100.0 * sample_frac));
2684 : 0 : break;
2685 : :
2686 : 0 : case ANALYZE_SAMPLE_AUTO:
2687 : : /* should have been resolved into actual method */
2688 [ # # ]: 0 : elog(ERROR, "unexpected sampling method");
2689 : : break;
2690 : : }
2691 : 52 : }
2692 : :
2693 : : /*
2694 : : * Construct a simple "TRUNCATE rel" statement
2695 : : */
2696 : : void
2697 : 12 : deparseTruncateSql(StringInfo buf,
2698 : : List *rels,
2699 : : DropBehavior behavior,
2700 : : bool restart_seqs)
2701 : : {
2702 : : ListCell *cell;
2703 : :
2704 : 12 : appendStringInfoString(buf, "TRUNCATE ");
2705 : :
2706 [ + - + + : 26 : foreach(cell, rels)
+ + ]
2707 : : {
2708 : 14 : Relation rel = lfirst(cell);
2709 : :
2710 [ + + ]: 14 : if (cell != list_head(rels))
2711 : 2 : appendStringInfoString(buf, ", ");
2712 : :
2713 : 14 : deparseRelation(buf, rel);
2714 : : }
2715 : :
2716 [ - + ]: 12 : appendStringInfo(buf, " %s IDENTITY",
2717 : : restart_seqs ? "RESTART" : "CONTINUE");
2718 : :
2719 [ + + ]: 12 : if (behavior == DROP_RESTRICT)
2720 : 10 : appendStringInfoString(buf, " RESTRICT");
2721 [ + - ]: 2 : else if (behavior == DROP_CASCADE)
2722 : 2 : appendStringInfoString(buf, " CASCADE");
2723 : 12 : }
2724 : :
2725 : : /*
2726 : : * Construct name to use for given column, and emit it into buf.
2727 : : * If it has a column_name FDW option, use that instead of attribute name.
2728 : : *
2729 : : * If qualify_col is true, qualify column name with the alias of relation.
2730 : : */
2731 : : static void
2732 : 15559 : deparseColumnRef(StringInfo buf, int varno, int varattno, RangeTblEntry *rte,
2733 : : bool qualify_col)
2734 : : {
2735 : : /* We support fetching the remote side's CTID and OID. */
2736 [ + + ]: 15559 : if (varattno == SelfItemPointerAttributeNumber)
2737 : : {
2738 [ + + ]: 60 : if (qualify_col)
2739 : 58 : ADD_REL_QUALIFIER(buf, varno);
2740 : 60 : appendStringInfoString(buf, "ctid");
2741 : : }
2742 [ - + ]: 15499 : else if (varattno < 0)
2743 : : {
2744 : : /*
2745 : : * All other system attributes are fetched as 0, except for table OID,
2746 : : * which is fetched as the local table OID. However, we must be
2747 : : * careful; the table could be beneath an outer join, in which case it
2748 : : * must go to NULL whenever the rest of the row does.
2749 : : */
2750 : 0 : Oid fetchval = 0;
2751 : :
2752 [ # # ]: 0 : if (varattno == TableOidAttributeNumber)
2753 : 0 : fetchval = rte->relid;
2754 : :
2755 [ # # ]: 0 : if (qualify_col)
2756 : : {
2757 : 0 : appendStringInfoString(buf, "CASE WHEN (");
2758 : 0 : ADD_REL_QUALIFIER(buf, varno);
2759 : 0 : appendStringInfo(buf, "*)::text IS NOT NULL THEN %u END", fetchval);
2760 : : }
2761 : : else
2762 : 0 : appendStringInfo(buf, "%u", fetchval);
2763 : : }
2764 [ + + ]: 15499 : else if (varattno == 0)
2765 : : {
2766 : : /* Whole row reference */
2767 : : Relation rel;
2768 : : Bitmapset *attrs_used;
2769 : :
2770 : : /* Required only to be passed down to deparseTargetList(). */
2771 : : List *retrieved_attrs;
2772 : :
2773 : : /*
2774 : : * The lock on the relation will be held by upper callers, so it's
2775 : : * fine to open it with no lock here.
2776 : : */
2777 : 278 : rel = table_open(rte->relid, NoLock);
2778 : :
2779 : : /*
2780 : : * The local name of the foreign table can not be recognized by the
2781 : : * foreign server and the table it references on foreign server might
2782 : : * have different column ordering or different columns than those
2783 : : * declared locally. Hence we have to deparse whole-row reference as
2784 : : * ROW(columns referenced locally). Construct this by deparsing a
2785 : : * "whole row" attribute.
2786 : : */
2787 : 278 : attrs_used = bms_add_member(NULL,
2788 : : 0 - FirstLowInvalidHeapAttributeNumber);
2789 : :
2790 : : /*
2791 : : * In case the whole-row reference is under an outer join then it has
2792 : : * to go NULL whenever the rest of the row goes NULL. Deparsing a join
2793 : : * query would always involve multiple relations, thus qualify_col
2794 : : * would be true.
2795 : : */
2796 [ + + ]: 278 : if (qualify_col)
2797 : : {
2798 : 274 : appendStringInfoString(buf, "CASE WHEN (");
2799 : 274 : ADD_REL_QUALIFIER(buf, varno);
2800 : 274 : appendStringInfoString(buf, "*)::text IS NOT NULL THEN ");
2801 : : }
2802 : :
2803 : 278 : appendStringInfoString(buf, "ROW(");
2804 : 278 : deparseTargetList(buf, rte, varno, rel, false, attrs_used, qualify_col,
2805 : : &retrieved_attrs);
2806 : 278 : appendStringInfoChar(buf, ')');
2807 : :
2808 : : /* Complete the CASE WHEN statement started above. */
2809 [ + + ]: 278 : if (qualify_col)
2810 : 274 : appendStringInfoString(buf, " END");
2811 : :
2812 : 278 : table_close(rel, NoLock);
2813 : 278 : bms_free(attrs_used);
2814 : : }
2815 : : else
2816 : : {
2817 : 15221 : char *colname = NULL;
2818 : : List *options;
2819 : : ListCell *lc;
2820 : :
2821 : : /* varno must not be any of OUTER_VAR, INNER_VAR and INDEX_VAR. */
2822 : : Assert(!IS_SPECIAL_VARNO(varno));
2823 : :
2824 : : /*
2825 : : * If it's a column of a foreign table, and it has the column_name FDW
2826 : : * option, use that value.
2827 : : */
2828 : 15221 : options = GetForeignColumnOptions(rte->relid, varattno);
2829 [ + + + - : 15221 : foreach(lc, options)
+ + ]
2830 : : {
2831 : 3470 : DefElem *def = (DefElem *) lfirst(lc);
2832 : :
2833 [ + - ]: 3470 : if (strcmp(def->defname, "column_name") == 0)
2834 : : {
2835 : 3470 : colname = defGetString(def);
2836 : 3470 : break;
2837 : : }
2838 : : }
2839 : :
2840 : : /*
2841 : : * If it's a column of a regular table or it doesn't have column_name
2842 : : * FDW option, use attribute name.
2843 : : */
2844 [ + + ]: 15221 : if (colname == NULL)
2845 : 11751 : colname = get_attname(rte->relid, varattno, false);
2846 : :
2847 [ + + ]: 15221 : if (qualify_col)
2848 : 7776 : ADD_REL_QUALIFIER(buf, varno);
2849 : :
2850 : 15221 : appendStringInfoString(buf, quote_identifier(colname));
2851 : : }
2852 : 15559 : }
2853 : :
2854 : : /*
2855 : : * Append remote name of specified foreign table to buf.
2856 : : * Use value of table_name FDW option (if any) instead of relation's name.
2857 : : * Similarly, schema_name FDW option overrides schema name.
2858 : : */
2859 : : static void
2860 : 3846 : deparseRelation(StringInfo buf, Relation rel)
2861 : : {
2862 : : ForeignTable *table;
2863 : 3846 : const char *nspname = NULL;
2864 : 3846 : const char *relname = NULL;
2865 : : ListCell *lc;
2866 : :
2867 : : /* obtain additional catalog information. */
2868 : 3846 : table = GetForeignTable(RelationGetRelid(rel));
2869 : :
2870 : : /*
2871 : : * Use value of FDW options if any, instead of the name of object itself.
2872 : : */
2873 [ + - + + : 12483 : foreach(lc, table->options)
+ + ]
2874 : : {
2875 : 8637 : DefElem *def = (DefElem *) lfirst(lc);
2876 : :
2877 [ + + ]: 8637 : if (strcmp(def->defname, "schema_name") == 0)
2878 : 2591 : nspname = defGetString(def);
2879 [ + + ]: 6046 : else if (strcmp(def->defname, "table_name") == 0)
2880 : 3846 : relname = defGetString(def);
2881 : : }
2882 : :
2883 : : /*
2884 : : * Note: we could skip printing the schema name if it's pg_catalog, but
2885 : : * that doesn't seem worth the trouble.
2886 : : */
2887 [ + + ]: 3846 : if (nspname == NULL)
2888 : 1255 : nspname = get_namespace_name(RelationGetNamespace(rel));
2889 [ - + ]: 3846 : if (relname == NULL)
2890 : 0 : relname = RelationGetRelationName(rel);
2891 : :
2892 : 3846 : appendStringInfo(buf, "%s.%s",
2893 : : quote_identifier(nspname), quote_identifier(relname));
2894 : 3846 : }
2895 : :
2896 : : /*
2897 : : * Append a SQL string literal representing "val" to buf.
2898 : : */
2899 : : void
2900 : 415 : deparseStringLiteral(StringInfo buf, const char *val)
2901 : : {
2902 : : const char *valptr;
2903 : :
2904 : : /*
2905 : : * Rather than making assumptions about the remote server's value of
2906 : : * standard_conforming_strings, always use E'foo' syntax if there are any
2907 : : * backslashes. This will fail on remote servers before 8.1, but those
2908 : : * are long out of support.
2909 : : */
2910 [ + + ]: 415 : if (strchr(val, '\\') != NULL)
2911 : 2 : appendStringInfoChar(buf, ESCAPE_STRING_SYNTAX);
2912 : 415 : appendStringInfoChar(buf, '\'');
2913 [ + + ]: 4086 : for (valptr = val; *valptr; valptr++)
2914 : : {
2915 : 3671 : char ch = *valptr;
2916 : :
2917 [ + + + + ]: 3671 : if (SQL_STR_DOUBLE(ch, true))
2918 : 4 : appendStringInfoChar(buf, ch);
2919 : 3671 : appendStringInfoChar(buf, ch);
2920 : : }
2921 : 415 : appendStringInfoChar(buf, '\'');
2922 : 415 : }
2923 : :
2924 : : /*
2925 : : * Deparse given expression into context->buf.
2926 : : *
2927 : : * This function must support all the same node types that foreign_expr_walker
2928 : : * accepts.
2929 : : *
2930 : : * Note: unlike ruleutils.c, we just use a simple hard-wired parenthesization
2931 : : * scheme: anything more complex than a Var, Const, function call or cast
2932 : : * should be self-parenthesized.
2933 : : */
2934 : : static void
2935 : 12662 : deparseExpr(Expr *node, deparse_expr_cxt *context)
2936 : : {
2937 [ - + ]: 12662 : if (node == NULL)
2938 : 0 : return;
2939 : :
2940 [ + + + + : 12662 : switch (nodeTag(node))
+ + + + +
+ + + + +
+ - ]
2941 : : {
2942 : 8727 : case T_Var:
2943 : 8727 : deparseVar((Var *) node, context);
2944 : 8727 : break;
2945 : 587 : case T_Const:
2946 : 587 : deparseConst((Const *) node, context, 0);
2947 : 587 : break;
2948 : 46 : case T_Param:
2949 : 46 : deparseParam((Param *) node, context);
2950 : 46 : break;
2951 : 1 : case T_SubscriptingRef:
2952 : 1 : deparseSubscriptingRef((SubscriptingRef *) node, context);
2953 : 1 : break;
2954 : 60 : case T_FuncExpr:
2955 : 60 : deparseFuncExpr((FuncExpr *) node, context);
2956 : 60 : break;
2957 : 2835 : case T_OpExpr:
2958 : 2835 : deparseOpExpr((OpExpr *) node, context);
2959 : 2835 : break;
2960 : 1 : case T_DistinctExpr:
2961 : 1 : deparseDistinctExpr((DistinctExpr *) node, context);
2962 : 1 : break;
2963 : 9 : case T_ScalarArrayOpExpr:
2964 : 9 : deparseScalarArrayOpExpr((ScalarArrayOpExpr *) node, context);
2965 : 9 : break;
2966 : 36 : case T_RelabelType:
2967 : 36 : deparseRelabelType((RelabelType *) node, context);
2968 : 36 : break;
2969 : 5 : case T_ArrayCoerceExpr:
2970 : 5 : deparseArrayCoerceExpr((ArrayCoerceExpr *) node, context);
2971 : 5 : break;
2972 : 38 : case T_BoolExpr:
2973 : 38 : deparseBoolExpr((BoolExpr *) node, context);
2974 : 38 : break;
2975 : 26 : case T_NullTest:
2976 : 26 : deparseNullTest((NullTest *) node, context);
2977 : 26 : break;
2978 : 21 : case T_CaseExpr:
2979 : 21 : deparseCaseExpr((CaseExpr *) node, context);
2980 : 21 : break;
2981 : 4 : case T_ArrayExpr:
2982 : 4 : deparseArrayExpr((ArrayExpr *) node, context);
2983 : 4 : break;
2984 : 266 : case T_Aggref:
2985 : 266 : deparseAggref((Aggref *) node, context);
2986 : 266 : break;
2987 : 0 : default:
2988 [ # # ]: 0 : elog(ERROR, "unsupported expression type for deparse: %d",
2989 : : (int) nodeTag(node));
2990 : : break;
2991 : : }
2992 : : }
2993 : :
2994 : : /*
2995 : : * Deparse given Var node into context->buf.
2996 : : *
2997 : : * If the Var belongs to the foreign relation, just print its remote name.
2998 : : * Otherwise, it's effectively a Param (and will in fact be a Param at
2999 : : * run time). Handle it the same way we handle plain Params --- see
3000 : : * deparseParam for comments.
3001 : : */
3002 : : static void
3003 : 8727 : deparseVar(Var *node, deparse_expr_cxt *context)
3004 : : {
3005 : 8727 : Relids relids = context->scanrel->relids;
3006 : : int relno;
3007 : : int colno;
3008 : :
3009 : : /* Qualify columns when multiple relations are involved. */
3010 : 8727 : bool qualify_col = (bms_membership(relids) == BMS_MULTIPLE);
3011 : :
3012 : : /*
3013 : : * If the Var belongs to the foreign relation that is deparsed as a
3014 : : * subquery, use the relation and column alias to the Var provided by the
3015 : : * subquery, instead of the remote name.
3016 : : */
3017 [ + + ]: 8727 : if (is_subquery_var(node, context->scanrel, &relno, &colno))
3018 : : {
3019 : 152 : appendStringInfo(context->buf, "%s%d.%s%d",
3020 : : SUBQUERY_REL_ALIAS_PREFIX, relno,
3021 : : SUBQUERY_COL_ALIAS_PREFIX, colno);
3022 : 152 : return;
3023 : : }
3024 : :
3025 [ + + + - ]: 8575 : if (bms_is_member(node->varno, relids) && node->varlevelsup == 0)
3026 : 8348 : deparseColumnRef(context->buf, node->varno, node->varattno,
3027 [ + - ]: 8348 : planner_rt_fetch(node->varno, context->root),
3028 : : qualify_col);
3029 : : else
3030 : : {
3031 : : /* Treat like a Param */
3032 [ + + ]: 227 : if (context->params_list)
3033 : : {
3034 : 13 : int pindex = 0;
3035 : : ListCell *lc;
3036 : :
3037 : : /* find its index in params_list */
3038 [ - + - - : 13 : foreach(lc, *context->params_list)
- + ]
3039 : : {
3040 : 0 : pindex++;
3041 [ # # ]: 0 : if (equal(node, (Node *) lfirst(lc)))
3042 : 0 : break;
3043 : : }
3044 [ + - ]: 13 : if (lc == NULL)
3045 : : {
3046 : : /* not in list, so add it */
3047 : 13 : pindex++;
3048 : 13 : *context->params_list = lappend(*context->params_list, node);
3049 : : }
3050 : :
3051 : 13 : printRemoteParam(pindex, node->vartype, node->vartypmod, context);
3052 : : }
3053 : : else
3054 : : {
3055 : 214 : printRemotePlaceholder(node->vartype, node->vartypmod, context);
3056 : : }
3057 : : }
3058 : : }
3059 : :
3060 : : /*
3061 : : * Deparse given constant value into context->buf.
3062 : : *
3063 : : * This function has to be kept in sync with ruleutils.c's get_const_expr.
3064 : : *
3065 : : * As in that function, showtype can be -1 to never show "::typename"
3066 : : * decoration, +1 to always show it, or 0 to show it only if the constant
3067 : : * wouldn't be assumed to be the right type by default.
3068 : : *
3069 : : * In addition, this code allows showtype to be -2 to indicate that we should
3070 : : * not show "::typename" decoration if the constant is printed as an untyped
3071 : : * literal or NULL (while in other cases, behaving as for showtype == 0).
3072 : : */
3073 : : static void
3074 : 1899 : deparseConst(Const *node, deparse_expr_cxt *context, int showtype)
3075 : : {
3076 : 1899 : StringInfo buf = context->buf;
3077 : : Oid typoutput;
3078 : : bool typIsVarlena;
3079 : : char *extval;
3080 : 1899 : bool isfloat = false;
3081 : 1899 : bool isstring = false;
3082 : : bool needlabel;
3083 : :
3084 [ + + ]: 1899 : if (node->constisnull)
3085 : : {
3086 : 19 : appendStringInfoString(buf, "NULL");
3087 [ + - ]: 19 : if (showtype >= 0)
3088 : 19 : appendStringInfo(buf, "::%s",
3089 : : deparse_type_name(node->consttype,
3090 : : node->consttypmod));
3091 : 19 : return;
3092 : : }
3093 : :
3094 : 1880 : getTypeOutputInfo(node->consttype,
3095 : : &typoutput, &typIsVarlena);
3096 : 1880 : extval = OidOutputFunctionCall(typoutput, node->constvalue);
3097 : :
3098 [ + - + + ]: 1880 : switch (node->consttype)
3099 : : {
3100 : 1780 : case INT2OID:
3101 : : case INT4OID:
3102 : : case INT8OID:
3103 : : case OIDOID:
3104 : : case FLOAT4OID:
3105 : : case FLOAT8OID:
3106 : : case NUMERICOID:
3107 : : {
3108 : : /*
3109 : : * No need to quote unless it's a special value such as 'NaN'.
3110 : : * See comments in get_const_expr().
3111 : : */
3112 [ + - ]: 1780 : if (strspn(extval, "0123456789+-eE.") == strlen(extval))
3113 : : {
3114 [ + - + + ]: 1780 : if (extval[0] == '+' || extval[0] == '-')
3115 : 1 : appendStringInfo(buf, "(%s)", extval);
3116 : : else
3117 : 1779 : appendStringInfoString(buf, extval);
3118 [ + + ]: 1780 : if (strcspn(extval, "eE.") != strlen(extval))
3119 : 2 : isfloat = true; /* it looks like a float */
3120 : : }
3121 : : else
3122 : 0 : appendStringInfo(buf, "'%s'", extval);
3123 : : }
3124 : 1780 : break;
3125 : 0 : case BITOID:
3126 : : case VARBITOID:
3127 : 0 : appendStringInfo(buf, "B'%s'", extval);
3128 : 0 : break;
3129 : 2 : case BOOLOID:
3130 [ + - ]: 2 : if (strcmp(extval, "t") == 0)
3131 : 2 : appendStringInfoString(buf, "true");
3132 : : else
3133 : 0 : appendStringInfoString(buf, "false");
3134 : 2 : break;
3135 : 98 : default:
3136 : 98 : deparseStringLiteral(buf, extval);
3137 : 98 : isstring = true;
3138 : 98 : break;
3139 : : }
3140 : :
3141 : 1880 : pfree(extval);
3142 : :
3143 [ - + ]: 1880 : if (showtype == -1)
3144 : 0 : return; /* never print type label */
3145 : :
3146 : : /*
3147 : : * For showtype == 0, append ::typename unless the constant will be
3148 : : * implicitly typed as the right type when it is read in.
3149 : : *
3150 : : * XXX this code has to be kept in sync with the behavior of the parser,
3151 : : * especially make_const.
3152 : : */
3153 [ + + + ]: 1880 : switch (node->consttype)
3154 : : {
3155 : 1537 : case BOOLOID:
3156 : : case INT4OID:
3157 : : case UNKNOWNOID:
3158 : 1537 : needlabel = false;
3159 : 1537 : break;
3160 : 21 : case NUMERICOID:
3161 [ + + - + ]: 21 : needlabel = !isfloat || (node->consttypmod >= 0);
3162 : 21 : break;
3163 : 322 : default:
3164 [ + + ]: 322 : if (showtype == -2)
3165 : : {
3166 : : /* label unless we printed it as an untyped string */
3167 : 57 : needlabel = !isstring;
3168 : : }
3169 : : else
3170 : 265 : needlabel = true;
3171 : 322 : break;
3172 : : }
3173 [ + + - + ]: 1880 : if (needlabel || showtype > 0)
3174 : 284 : appendStringInfo(buf, "::%s",
3175 : : deparse_type_name(node->consttype,
3176 : : node->consttypmod));
3177 : : }
3178 : :
3179 : : /*
3180 : : * Deparse given Param node.
3181 : : *
3182 : : * If we're generating the query "for real", add the Param to
3183 : : * context->params_list if it's not already present, and then use its index
3184 : : * in that list as the remote parameter number. During EXPLAIN, there's
3185 : : * no need to identify a parameter number.
3186 : : */
3187 : : static void
3188 : 46 : deparseParam(Param *node, deparse_expr_cxt *context)
3189 : : {
3190 [ + + ]: 46 : if (context->params_list)
3191 : : {
3192 : 26 : int pindex = 0;
3193 : : ListCell *lc;
3194 : :
3195 : : /* find its index in params_list */
3196 [ + + + + : 28 : foreach(lc, *context->params_list)
+ + ]
3197 : : {
3198 : 4 : pindex++;
3199 [ + + ]: 4 : if (equal(node, (Node *) lfirst(lc)))
3200 : 2 : break;
3201 : : }
3202 [ + + ]: 26 : if (lc == NULL)
3203 : : {
3204 : : /* not in list, so add it */
3205 : 24 : pindex++;
3206 : 24 : *context->params_list = lappend(*context->params_list, node);
3207 : : }
3208 : :
3209 : 26 : printRemoteParam(pindex, node->paramtype, node->paramtypmod, context);
3210 : : }
3211 : : else
3212 : : {
3213 : 20 : printRemotePlaceholder(node->paramtype, node->paramtypmod, context);
3214 : : }
3215 : 46 : }
3216 : :
3217 : : /*
3218 : : * Deparse a container subscript expression.
3219 : : */
3220 : : static void
3221 : 1 : deparseSubscriptingRef(SubscriptingRef *node, deparse_expr_cxt *context)
3222 : : {
3223 : 1 : StringInfo buf = context->buf;
3224 : : ListCell *lowlist_item;
3225 : : ListCell *uplist_item;
3226 : :
3227 : : /* Always parenthesize the expression. */
3228 : 1 : appendStringInfoChar(buf, '(');
3229 : :
3230 : : /*
3231 : : * Deparse referenced array expression first. If that expression includes
3232 : : * a cast, we have to parenthesize to prevent the array subscript from
3233 : : * being taken as typename decoration. We can avoid that in the typical
3234 : : * case of subscripting a Var, but otherwise do it.
3235 : : */
3236 [ - + ]: 1 : if (IsA(node->refexpr, Var))
3237 : 0 : deparseExpr(node->refexpr, context);
3238 : : else
3239 : : {
3240 : 1 : appendStringInfoChar(buf, '(');
3241 : 1 : deparseExpr(node->refexpr, context);
3242 : 1 : appendStringInfoChar(buf, ')');
3243 : : }
3244 : :
3245 : : /* Deparse subscript expressions. */
3246 : 1 : lowlist_item = list_head(node->reflowerindexpr); /* could be NULL */
3247 [ + - + + : 2 : foreach(uplist_item, node->refupperindexpr)
+ + ]
3248 : : {
3249 : 1 : appendStringInfoChar(buf, '[');
3250 [ - + ]: 1 : if (lowlist_item)
3251 : : {
3252 : 0 : deparseExpr(lfirst(lowlist_item), context);
3253 : 0 : appendStringInfoChar(buf, ':');
3254 : 0 : lowlist_item = lnext(node->reflowerindexpr, lowlist_item);
3255 : : }
3256 : 1 : deparseExpr(lfirst(uplist_item), context);
3257 : 1 : appendStringInfoChar(buf, ']');
3258 : : }
3259 : :
3260 : 1 : appendStringInfoChar(buf, ')');
3261 : 1 : }
3262 : :
3263 : : /*
3264 : : * Deparse a function call.
3265 : : */
3266 : : static void
3267 : 60 : deparseFuncExpr(FuncExpr *node, deparse_expr_cxt *context)
3268 : : {
3269 : 60 : StringInfo buf = context->buf;
3270 : : bool use_variadic;
3271 : : bool first;
3272 : : ListCell *arg;
3273 : :
3274 : : /*
3275 : : * If the function call came from an implicit coercion, then just show the
3276 : : * first argument.
3277 : : */
3278 [ + + ]: 60 : if (node->funcformat == COERCE_IMPLICIT_CAST)
3279 : : {
3280 : 21 : deparseExpr((Expr *) linitial(node->args), context);
3281 : 21 : return;
3282 : : }
3283 : :
3284 : : /*
3285 : : * If the function call came from a cast, then show the first argument
3286 : : * plus an explicit cast operation.
3287 : : */
3288 [ - + ]: 39 : if (node->funcformat == COERCE_EXPLICIT_CAST)
3289 : : {
3290 : 0 : Oid rettype = node->funcresulttype;
3291 : : int32 coercedTypmod;
3292 : :
3293 : : /* Get the typmod if this is a length-coercion function */
3294 : 0 : (void) exprIsLengthCoercion((Node *) node, &coercedTypmod);
3295 : :
3296 : 0 : deparseExpr((Expr *) linitial(node->args), context);
3297 : 0 : appendStringInfo(buf, "::%s",
3298 : : deparse_type_name(rettype, coercedTypmod));
3299 : 0 : return;
3300 : : }
3301 : :
3302 : : /* Check if need to print VARIADIC (cf. ruleutils.c) */
3303 : 39 : use_variadic = node->funcvariadic;
3304 : :
3305 : : /*
3306 : : * Normal function: display as proname(args).
3307 : : */
3308 : 39 : appendFunctionName(node->funcid, context);
3309 : 39 : appendStringInfoChar(buf, '(');
3310 : :
3311 : : /* ... and all the arguments */
3312 : 39 : first = true;
3313 [ + - + + : 84 : foreach(arg, node->args)
+ + ]
3314 : : {
3315 [ + + ]: 45 : if (!first)
3316 : 6 : appendStringInfoString(buf, ", ");
3317 [ - + - - ]: 45 : if (use_variadic && lnext(node->args, arg) == NULL)
3318 : 0 : appendStringInfoString(buf, "VARIADIC ");
3319 : 45 : deparseExpr((Expr *) lfirst(arg), context);
3320 : 45 : first = false;
3321 : : }
3322 : 39 : appendStringInfoChar(buf, ')');
3323 : : }
3324 : :
3325 : : /*
3326 : : * Deparse given operator expression. To avoid problems around
3327 : : * priority of operations, we always parenthesize the arguments.
3328 : : */
3329 : : static void
3330 : 2835 : deparseOpExpr(OpExpr *node, deparse_expr_cxt *context)
3331 : : {
3332 : 2835 : StringInfo buf = context->buf;
3333 : : HeapTuple tuple;
3334 : : Form_pg_operator form;
3335 : : Expr *right;
3336 : 2835 : bool canSuppressRightConstCast = false;
3337 : : char oprkind;
3338 : :
3339 : : /* Retrieve information about the operator from system catalog. */
3340 : 2835 : tuple = SearchSysCache1(OPEROID, ObjectIdGetDatum(node->opno));
3341 [ - + ]: 2835 : if (!HeapTupleIsValid(tuple))
3342 [ # # ]: 0 : elog(ERROR, "cache lookup failed for operator %u", node->opno);
3343 : 2835 : form = (Form_pg_operator) GETSTRUCT(tuple);
3344 : 2835 : oprkind = form->oprkind;
3345 : :
3346 : : /* Sanity check. */
3347 : : Assert((oprkind == 'l' && list_length(node->args) == 1) ||
3348 : : (oprkind == 'b' && list_length(node->args) == 2));
3349 : :
3350 : 2835 : right = llast(node->args);
3351 : :
3352 : : /* Always parenthesize the expression. */
3353 : 2835 : appendStringInfoChar(buf, '(');
3354 : :
3355 : : /* Deparse left operand, if any. */
3356 [ + + ]: 2835 : if (oprkind == 'b')
3357 : : {
3358 : 2832 : Expr *left = linitial(node->args);
3359 : 2832 : Oid leftType = exprType((Node *) left);
3360 : 2832 : Oid rightType = exprType((Node *) right);
3361 : 2832 : bool canSuppressLeftConstCast = false;
3362 : :
3363 : : /*
3364 : : * When considering a binary operator, if one operand is a Const that
3365 : : * can be printed as a bare string literal or NULL (i.e., it will look
3366 : : * like type UNKNOWN to the remote parser), the Const normally
3367 : : * receives an explicit cast to the operator's input type. However,
3368 : : * in Const-to-Var comparisons where both operands are of the same
3369 : : * type, we prefer to suppress the explicit cast, leaving the Const's
3370 : : * type resolution up to the remote parser. The remote's resolution
3371 : : * heuristic will assume that an unknown input type being compared to
3372 : : * a known input type is of that known type as well.
3373 : : *
3374 : : * This hack allows some cases to succeed where a remote column is
3375 : : * declared with a different type in the local (foreign) table. By
3376 : : * emitting "foreigncol = 'foo'" not "foreigncol = 'foo'::text" or the
3377 : : * like, we allow the remote parser to pick an "=" operator that's
3378 : : * compatible with whatever type the remote column really is, such as
3379 : : * an enum.
3380 : : *
3381 : : * We allow cast suppression to happen only when the other operand is
3382 : : * a plain foreign Var. Although the remote's unknown-type heuristic
3383 : : * would apply to other cases just as well, we would be taking a
3384 : : * bigger risk that the inferred type is something unexpected. With
3385 : : * this restriction, if anything goes wrong it's the user's fault for
3386 : : * not declaring the local column with the same type as the remote
3387 : : * column.
3388 : : */
3389 [ + + ]: 2832 : if (leftType == rightType)
3390 : : {
3391 [ + + ]: 2816 : if (IsA(left, Const))
3392 : 3 : canSuppressLeftConstCast = isPlainForeignVar(right, context);
3393 [ + + ]: 2813 : else if (IsA(right, Const))
3394 : 1567 : canSuppressRightConstCast = isPlainForeignVar(left, context);
3395 : : }
3396 : :
3397 [ + + ]: 2832 : if (canSuppressLeftConstCast)
3398 : 2 : deparseConst((Const *) left, context, -2);
3399 : : else
3400 : 2830 : deparseExpr(left, context);
3401 : :
3402 : 2832 : appendStringInfoChar(buf, ' ');
3403 : : }
3404 : :
3405 : : /* Deparse operator name. */
3406 : 2835 : deparseOperatorName(buf, form);
3407 : :
3408 : : /* Deparse right operand. */
3409 : 2835 : appendStringInfoChar(buf, ' ');
3410 : :
3411 [ + + ]: 2835 : if (canSuppressRightConstCast)
3412 : 1310 : deparseConst((Const *) right, context, -2);
3413 : : else
3414 : 1525 : deparseExpr(right, context);
3415 : :
3416 : 2835 : appendStringInfoChar(buf, ')');
3417 : :
3418 : 2835 : ReleaseSysCache(tuple);
3419 : 2835 : }
3420 : :
3421 : : /*
3422 : : * Will "node" deparse as a plain foreign Var?
3423 : : */
3424 : : static bool
3425 : 1570 : isPlainForeignVar(Expr *node, deparse_expr_cxt *context)
3426 : : {
3427 : : /*
3428 : : * We allow the foreign Var to have an implicit RelabelType, mainly so
3429 : : * that this'll work with varchar columns. Note that deparseRelabelType
3430 : : * will not print such a cast, so we're not breaking the restriction that
3431 : : * the expression print as a plain Var. We won't risk it for an implicit
3432 : : * cast that requires a function, nor for non-implicit RelabelType; such
3433 : : * cases seem too likely to involve semantics changes compared to what
3434 : : * would happen on the remote side.
3435 : : */
3436 [ + + ]: 1570 : if (IsA(node, RelabelType) &&
3437 [ + - ]: 5 : ((RelabelType *) node)->relabelformat == COERCE_IMPLICIT_CAST)
3438 : 5 : node = ((RelabelType *) node)->arg;
3439 : :
3440 [ + + ]: 1570 : if (IsA(node, Var))
3441 : : {
3442 : : /*
3443 : : * The Var must be one that'll deparse as a foreign column reference
3444 : : * (cf. deparseVar).
3445 : : */
3446 : 1312 : Var *var = (Var *) node;
3447 : 1312 : Relids relids = context->scanrel->relids;
3448 : :
3449 [ + - + - ]: 1312 : if (bms_is_member(var->varno, relids) && var->varlevelsup == 0)
3450 : 1312 : return true;
3451 : : }
3452 : :
3453 : 258 : return false;
3454 : : }
3455 : :
3456 : : /*
3457 : : * Print the name of an operator.
3458 : : */
3459 : : static void
3460 : 2852 : deparseOperatorName(StringInfo buf, Form_pg_operator opform)
3461 : : {
3462 : : char *opname;
3463 : :
3464 : : /* opname is not a SQL identifier, so we should not quote it. */
3465 : 2852 : opname = NameStr(opform->oprname);
3466 : :
3467 : : /* Print schema name only if it's not pg_catalog */
3468 [ + + ]: 2852 : if (opform->oprnamespace != PG_CATALOG_NAMESPACE)
3469 : : {
3470 : : const char *opnspname;
3471 : :
3472 : 13 : opnspname = get_namespace_name(opform->oprnamespace);
3473 : : /* Print fully qualified operator name. */
3474 : 13 : appendStringInfo(buf, "OPERATOR(%s.%s)",
3475 : : quote_identifier(opnspname), opname);
3476 : : }
3477 : : else
3478 : : {
3479 : : /* Just print operator name. */
3480 : 2839 : appendStringInfoString(buf, opname);
3481 : : }
3482 : 2852 : }
3483 : :
3484 : : /*
3485 : : * Deparse IS DISTINCT FROM.
3486 : : */
3487 : : static void
3488 : 1 : deparseDistinctExpr(DistinctExpr *node, deparse_expr_cxt *context)
3489 : : {
3490 : 1 : StringInfo buf = context->buf;
3491 : :
3492 : : Assert(list_length(node->args) == 2);
3493 : :
3494 : 1 : appendStringInfoChar(buf, '(');
3495 : 1 : deparseExpr(linitial(node->args), context);
3496 : 1 : appendStringInfoString(buf, " IS DISTINCT FROM ");
3497 : 1 : deparseExpr(lsecond(node->args), context);
3498 : 1 : appendStringInfoChar(buf, ')');
3499 : 1 : }
3500 : :
3501 : : /*
3502 : : * Deparse given ScalarArrayOpExpr expression. To avoid problems
3503 : : * around priority of operations, we always parenthesize the arguments.
3504 : : */
3505 : : static void
3506 : 9 : deparseScalarArrayOpExpr(ScalarArrayOpExpr *node, deparse_expr_cxt *context)
3507 : : {
3508 : 9 : StringInfo buf = context->buf;
3509 : : HeapTuple tuple;
3510 : : Form_pg_operator form;
3511 : : Expr *arg1;
3512 : : Expr *arg2;
3513 : :
3514 : : /* Retrieve information about the operator from system catalog. */
3515 : 9 : tuple = SearchSysCache1(OPEROID, ObjectIdGetDatum(node->opno));
3516 [ - + ]: 9 : if (!HeapTupleIsValid(tuple))
3517 [ # # ]: 0 : elog(ERROR, "cache lookup failed for operator %u", node->opno);
3518 : 9 : form = (Form_pg_operator) GETSTRUCT(tuple);
3519 : :
3520 : : /* Sanity check. */
3521 : : Assert(list_length(node->args) == 2);
3522 : :
3523 : : /* Always parenthesize the expression. */
3524 : 9 : appendStringInfoChar(buf, '(');
3525 : :
3526 : : /* Deparse left operand. */
3527 : 9 : arg1 = linitial(node->args);
3528 : 9 : deparseExpr(arg1, context);
3529 : 9 : appendStringInfoChar(buf, ' ');
3530 : :
3531 : : /* Deparse operator name plus decoration. */
3532 : 9 : deparseOperatorName(buf, form);
3533 [ + - ]: 9 : appendStringInfo(buf, " %s (", node->useOr ? "ANY" : "ALL");
3534 : :
3535 : : /* Deparse right operand. */
3536 : 9 : arg2 = lsecond(node->args);
3537 : 9 : deparseExpr(arg2, context);
3538 : :
3539 : 9 : appendStringInfoChar(buf, ')');
3540 : :
3541 : : /* Always parenthesize the expression. */
3542 : 9 : appendStringInfoChar(buf, ')');
3543 : :
3544 : 9 : ReleaseSysCache(tuple);
3545 : 9 : }
3546 : :
3547 : : /*
3548 : : * Deparse a RelabelType (binary-compatible cast) node.
3549 : : */
3550 : : static void
3551 : 36 : deparseRelabelType(RelabelType *node, deparse_expr_cxt *context)
3552 : : {
3553 : 36 : deparseExpr(node->arg, context);
3554 [ - + ]: 36 : if (node->relabelformat != COERCE_IMPLICIT_CAST)
3555 : 0 : appendStringInfo(context->buf, "::%s",
3556 : : deparse_type_name(node->resulttype,
3557 : : node->resulttypmod));
3558 : 36 : }
3559 : :
3560 : : /*
3561 : : * Deparse an ArrayCoerceExpr (array-type conversion) node.
3562 : : */
3563 : : static void
3564 : 5 : deparseArrayCoerceExpr(ArrayCoerceExpr *node, deparse_expr_cxt *context)
3565 : : {
3566 : 5 : deparseExpr(node->arg, context);
3567 : :
3568 : : /*
3569 : : * No difference how to deparse explicit cast, but if we omit implicit
3570 : : * cast in the query, it'll be more user-friendly
3571 : : */
3572 [ - + ]: 5 : if (node->coerceformat != COERCE_IMPLICIT_CAST)
3573 : 0 : appendStringInfo(context->buf, "::%s",
3574 : : deparse_type_name(node->resulttype,
3575 : : node->resulttypmod));
3576 : 5 : }
3577 : :
3578 : : /*
3579 : : * Deparse a BoolExpr node.
3580 : : */
3581 : : static void
3582 : 38 : deparseBoolExpr(BoolExpr *node, deparse_expr_cxt *context)
3583 : : {
3584 : 38 : StringInfo buf = context->buf;
3585 : 38 : const char *op = NULL; /* keep compiler quiet */
3586 : : bool first;
3587 : : ListCell *lc;
3588 : :
3589 [ + + - - ]: 38 : switch (node->boolop)
3590 : : {
3591 : 18 : case AND_EXPR:
3592 : 18 : op = "AND";
3593 : 18 : break;
3594 : 20 : case OR_EXPR:
3595 : 20 : op = "OR";
3596 : 20 : break;
3597 : 0 : case NOT_EXPR:
3598 : 0 : appendStringInfoString(buf, "(NOT ");
3599 : 0 : deparseExpr(linitial(node->args), context);
3600 : 0 : appendStringInfoChar(buf, ')');
3601 : 0 : return;
3602 : : }
3603 : :
3604 : 38 : appendStringInfoChar(buf, '(');
3605 : 38 : first = true;
3606 [ + - + + : 114 : foreach(lc, node->args)
+ + ]
3607 : : {
3608 [ + + ]: 76 : if (!first)
3609 : 38 : appendStringInfo(buf, " %s ", op);
3610 : 76 : deparseExpr((Expr *) lfirst(lc), context);
3611 : 76 : first = false;
3612 : : }
3613 : 38 : appendStringInfoChar(buf, ')');
3614 : : }
3615 : :
3616 : : /*
3617 : : * Deparse IS [NOT] NULL expression.
3618 : : */
3619 : : static void
3620 : 26 : deparseNullTest(NullTest *node, deparse_expr_cxt *context)
3621 : : {
3622 : 26 : StringInfo buf = context->buf;
3623 : :
3624 : 26 : appendStringInfoChar(buf, '(');
3625 : 26 : deparseExpr(node->arg, context);
3626 : :
3627 : : /*
3628 : : * For scalar inputs, we prefer to print as IS [NOT] NULL, which is
3629 : : * shorter and traditional. If it's a rowtype input but we're applying a
3630 : : * scalar test, must print IS [NOT] DISTINCT FROM NULL to be semantically
3631 : : * correct.
3632 : : */
3633 [ + - + - ]: 26 : if (node->argisrow || !type_is_rowtype(exprType((Node *) node->arg)))
3634 : : {
3635 [ + + ]: 26 : if (node->nulltesttype == IS_NULL)
3636 : 19 : appendStringInfoString(buf, " IS NULL)");
3637 : : else
3638 : 7 : appendStringInfoString(buf, " IS NOT NULL)");
3639 : : }
3640 : : else
3641 : : {
3642 [ # # ]: 0 : if (node->nulltesttype == IS_NULL)
3643 : 0 : appendStringInfoString(buf, " IS NOT DISTINCT FROM NULL)");
3644 : : else
3645 : 0 : appendStringInfoString(buf, " IS DISTINCT FROM NULL)");
3646 : : }
3647 : 26 : }
3648 : :
3649 : : /*
3650 : : * Deparse CASE expression
3651 : : */
3652 : : static void
3653 : 21 : deparseCaseExpr(CaseExpr *node, deparse_expr_cxt *context)
3654 : : {
3655 : 21 : StringInfo buf = context->buf;
3656 : : ListCell *lc;
3657 : :
3658 : 21 : appendStringInfoString(buf, "(CASE");
3659 : :
3660 : : /* If this is a CASE arg WHEN then emit the arg expression */
3661 [ + + ]: 21 : if (node->arg != NULL)
3662 : : {
3663 : 9 : appendStringInfoChar(buf, ' ');
3664 : 9 : deparseExpr(node->arg, context);
3665 : : }
3666 : :
3667 : : /* Add each condition/result of the CASE clause */
3668 [ + - + + : 49 : foreach(lc, node->args)
+ + ]
3669 : : {
3670 : 28 : CaseWhen *whenclause = (CaseWhen *) lfirst(lc);
3671 : :
3672 : : /* WHEN */
3673 : 28 : appendStringInfoString(buf, " WHEN ");
3674 [ + + ]: 28 : if (node->arg == NULL) /* CASE WHEN */
3675 : 12 : deparseExpr(whenclause->expr, context);
3676 : : else /* CASE arg WHEN */
3677 : : {
3678 : : /* Ignore the CaseTestExpr and equality operator. */
3679 : 16 : deparseExpr(lsecond(castNode(OpExpr, whenclause->expr)->args),
3680 : : context);
3681 : : }
3682 : :
3683 : : /* THEN */
3684 : 28 : appendStringInfoString(buf, " THEN ");
3685 : 28 : deparseExpr(whenclause->result, context);
3686 : : }
3687 : :
3688 : : /* add ELSE if present */
3689 [ + - ]: 21 : if (node->defresult != NULL)
3690 : : {
3691 : 21 : appendStringInfoString(buf, " ELSE ");
3692 : 21 : deparseExpr(node->defresult, context);
3693 : : }
3694 : :
3695 : : /* append END */
3696 : 21 : appendStringInfoString(buf, " END)");
3697 : 21 : }
3698 : :
3699 : : /*
3700 : : * Deparse ARRAY[...] construct.
3701 : : */
3702 : : static void
3703 : 4 : deparseArrayExpr(ArrayExpr *node, deparse_expr_cxt *context)
3704 : : {
3705 : 4 : StringInfo buf = context->buf;
3706 : 4 : bool first = true;
3707 : : ListCell *lc;
3708 : :
3709 : 4 : appendStringInfoString(buf, "ARRAY[");
3710 [ + - + + : 12 : foreach(lc, node->elements)
+ + ]
3711 : : {
3712 [ + + ]: 8 : if (!first)
3713 : 4 : appendStringInfoString(buf, ", ");
3714 : 8 : deparseExpr(lfirst(lc), context);
3715 : 8 : first = false;
3716 : : }
3717 : 4 : appendStringInfoChar(buf, ']');
3718 : :
3719 : : /* If the array is empty, we need an explicit cast to the array type. */
3720 [ - + ]: 4 : if (node->elements == NIL)
3721 : 0 : appendStringInfo(buf, "::%s",
3722 : : deparse_type_name(node->array_typeid, -1));
3723 : 4 : }
3724 : :
3725 : : /*
3726 : : * Deparse an Aggref node.
3727 : : */
3728 : : static void
3729 : 266 : deparseAggref(Aggref *node, deparse_expr_cxt *context)
3730 : : {
3731 : 266 : StringInfo buf = context->buf;
3732 : : bool use_variadic;
3733 : :
3734 : : /* Only basic, non-split aggregation accepted. */
3735 : : Assert(node->aggsplit == AGGSPLIT_SIMPLE);
3736 : :
3737 : : /* Check if need to print VARIADIC (cf. ruleutils.c) */
3738 : 266 : use_variadic = node->aggvariadic;
3739 : :
3740 : : /* Find aggregate name from aggfnoid which is a pg_proc entry */
3741 : 266 : appendFunctionName(node->aggfnoid, context);
3742 : 266 : appendStringInfoChar(buf, '(');
3743 : :
3744 : : /* Add DISTINCT */
3745 [ + + ]: 266 : appendStringInfoString(buf, (node->aggdistinct != NIL) ? "DISTINCT " : "");
3746 : :
3747 [ + + ]: 266 : if (AGGKIND_IS_ORDERED_SET(node->aggkind))
3748 : : {
3749 : : /* Add WITHIN GROUP (ORDER BY ..) */
3750 : : ListCell *arg;
3751 : 8 : bool first = true;
3752 : :
3753 : : Assert(!node->aggvariadic);
3754 : : Assert(node->aggorder != NIL);
3755 : :
3756 [ + - + + : 18 : foreach(arg, node->aggdirectargs)
+ + ]
3757 : : {
3758 [ + + ]: 10 : if (!first)
3759 : 2 : appendStringInfoString(buf, ", ");
3760 : 10 : first = false;
3761 : :
3762 : 10 : deparseExpr((Expr *) lfirst(arg), context);
3763 : : }
3764 : :
3765 : 8 : appendStringInfoString(buf, ") WITHIN GROUP (ORDER BY ");
3766 : 8 : appendAggOrderBy(node->aggorder, node->args, context);
3767 : : }
3768 : : else
3769 : : {
3770 : : /* aggstar can be set only in zero-argument aggregates */
3771 [ + + ]: 258 : if (node->aggstar)
3772 : 79 : appendStringInfoChar(buf, '*');
3773 : : else
3774 : : {
3775 : : ListCell *arg;
3776 : 179 : bool first = true;
3777 : :
3778 : : /* Add all the arguments */
3779 [ + - + + : 362 : foreach(arg, node->args)
+ + ]
3780 : : {
3781 : 183 : TargetEntry *tle = (TargetEntry *) lfirst(arg);
3782 : 183 : Node *n = (Node *) tle->expr;
3783 : :
3784 [ + + ]: 183 : if (tle->resjunk)
3785 : 4 : continue;
3786 : :
3787 [ - + ]: 179 : if (!first)
3788 : 0 : appendStringInfoString(buf, ", ");
3789 : 179 : first = false;
3790 : :
3791 : : /* Add VARIADIC */
3792 [ + + + - ]: 179 : if (use_variadic && lnext(node->args, arg) == NULL)
3793 : 2 : appendStringInfoString(buf, "VARIADIC ");
3794 : :
3795 : 179 : deparseExpr((Expr *) n, context);
3796 : : }
3797 : : }
3798 : :
3799 : : /* Add ORDER BY */
3800 [ + + ]: 258 : if (node->aggorder != NIL)
3801 : : {
3802 : 22 : appendStringInfoString(buf, " ORDER BY ");
3803 : 22 : appendAggOrderBy(node->aggorder, node->args, context);
3804 : : }
3805 : : }
3806 : :
3807 : : /* Add FILTER (WHERE ..) */
3808 [ + + ]: 266 : if (node->aggfilter != NULL)
3809 : : {
3810 : 12 : appendStringInfoString(buf, ") FILTER (WHERE ");
3811 : 12 : deparseExpr((Expr *) node->aggfilter, context);
3812 : : }
3813 : :
3814 : 266 : appendStringInfoChar(buf, ')');
3815 : 266 : }
3816 : :
3817 : : /*
3818 : : * Append ORDER BY within aggregate function.
3819 : : */
3820 : : static void
3821 : 30 : appendAggOrderBy(List *orderList, List *targetList, deparse_expr_cxt *context)
3822 : : {
3823 : 30 : StringInfo buf = context->buf;
3824 : : ListCell *lc;
3825 : 30 : bool first = true;
3826 : :
3827 [ + - + + : 62 : foreach(lc, orderList)
+ + ]
3828 : : {
3829 : 32 : SortGroupClause *srt = (SortGroupClause *) lfirst(lc);
3830 : : Node *sortexpr;
3831 : :
3832 [ + + ]: 32 : if (!first)
3833 : 2 : appendStringInfoString(buf, ", ");
3834 : 32 : first = false;
3835 : :
3836 : : /* Deparse the sort expression proper. */
3837 : 32 : sortexpr = deparseSortGroupClause(srt->tleSortGroupRef, targetList,
3838 : : false, context);
3839 : : /* Add decoration as needed. */
3840 : 32 : appendOrderBySuffix(srt->sortop, exprType(sortexpr), srt->nulls_first,
3841 : : context);
3842 : : }
3843 : 30 : }
3844 : :
3845 : : /*
3846 : : * Append the ASC, DESC, USING <OPERATOR> and NULLS FIRST / NULLS LAST parts
3847 : : * of an ORDER BY clause.
3848 : : */
3849 : : static void
3850 : 911 : appendOrderBySuffix(Oid sortop, Oid sortcoltype, bool nulls_first,
3851 : : deparse_expr_cxt *context)
3852 : : {
3853 : 911 : StringInfo buf = context->buf;
3854 : : TypeCacheEntry *typentry;
3855 : :
3856 : : /* See whether operator is default < or > for sort expr's datatype. */
3857 : 911 : typentry = lookup_type_cache(sortcoltype,
3858 : : TYPECACHE_LT_OPR | TYPECACHE_GT_OPR);
3859 : :
3860 [ + + ]: 911 : if (sortop == typentry->lt_opr)
3861 : 888 : appendStringInfoString(buf, " ASC");
3862 [ + + ]: 23 : else if (sortop == typentry->gt_opr)
3863 : 15 : appendStringInfoString(buf, " DESC");
3864 : : else
3865 : : {
3866 : : HeapTuple opertup;
3867 : : Form_pg_operator operform;
3868 : :
3869 : 8 : appendStringInfoString(buf, " USING ");
3870 : :
3871 : : /* Append operator name. */
3872 : 8 : opertup = SearchSysCache1(OPEROID, ObjectIdGetDatum(sortop));
3873 [ - + ]: 8 : if (!HeapTupleIsValid(opertup))
3874 [ # # ]: 0 : elog(ERROR, "cache lookup failed for operator %u", sortop);
3875 : 8 : operform = (Form_pg_operator) GETSTRUCT(opertup);
3876 : 8 : deparseOperatorName(buf, operform);
3877 : 8 : ReleaseSysCache(opertup);
3878 : : }
3879 : :
3880 [ + + ]: 911 : if (nulls_first)
3881 : 11 : appendStringInfoString(buf, " NULLS FIRST");
3882 : : else
3883 : 900 : appendStringInfoString(buf, " NULLS LAST");
3884 : 911 : }
3885 : :
3886 : : /*
3887 : : * Print the representation of a parameter to be sent to the remote side.
3888 : : *
3889 : : * Note: we always label the Param's type explicitly rather than relying on
3890 : : * transmitting a numeric type OID in PQsendQueryParams(). This allows us to
3891 : : * avoid assuming that types have the same OIDs on the remote side as they
3892 : : * do locally --- they need only have the same names.
3893 : : */
3894 : : static void
3895 : 39 : printRemoteParam(int paramindex, Oid paramtype, int32 paramtypmod,
3896 : : deparse_expr_cxt *context)
3897 : : {
3898 : 39 : StringInfo buf = context->buf;
3899 : 39 : char *ptypename = deparse_type_name(paramtype, paramtypmod);
3900 : :
3901 : 39 : appendStringInfo(buf, "$%d::%s", paramindex, ptypename);
3902 : 39 : }
3903 : :
3904 : : /*
3905 : : * Print the representation of a placeholder for a parameter that will be
3906 : : * sent to the remote side at execution time.
3907 : : *
3908 : : * This is used when we're just trying to EXPLAIN the remote query.
3909 : : * We don't have the actual value of the runtime parameter yet, and we don't
3910 : : * want the remote planner to generate a plan that depends on such a value
3911 : : * anyway. Thus, we can't do something simple like "$1::paramtype".
3912 : : * Instead, we emit "((SELECT null::paramtype)::paramtype)".
3913 : : * In all extant versions of Postgres, the planner will see that as an unknown
3914 : : * constant value, which is what we want. This might need adjustment if we
3915 : : * ever make the planner flatten scalar subqueries. Note: the reason for the
3916 : : * apparently useless outer cast is to ensure that the representation as a
3917 : : * whole will be parsed as an a_expr and not a select_with_parens; the latter
3918 : : * would do the wrong thing in the context "x = ANY(...)".
3919 : : */
3920 : : static void
3921 : 234 : printRemotePlaceholder(Oid paramtype, int32 paramtypmod,
3922 : : deparse_expr_cxt *context)
3923 : : {
3924 : 234 : StringInfo buf = context->buf;
3925 : 234 : char *ptypename = deparse_type_name(paramtype, paramtypmod);
3926 : :
3927 : 234 : appendStringInfo(buf, "((SELECT null::%s)::%s)", ptypename, ptypename);
3928 : 234 : }
3929 : :
3930 : : /*
3931 : : * Deparse GROUP BY clause.
3932 : : */
3933 : : static void
3934 : 169 : appendGroupByClause(List *tlist, deparse_expr_cxt *context)
3935 : : {
3936 : 169 : StringInfo buf = context->buf;
3937 : 169 : Query *query = context->root->parse;
3938 : : ListCell *lc;
3939 : 169 : bool first = true;
3940 : :
3941 : : /* Nothing to be done, if there's no GROUP BY clause in the query. */
3942 [ + + ]: 169 : if (!query->groupClause)
3943 : 68 : return;
3944 : :
3945 : 101 : appendStringInfoString(buf, " GROUP BY ");
3946 : :
3947 : : /*
3948 : : * Queries with grouping sets are not pushed down, so we don't expect
3949 : : * grouping sets here.
3950 : : */
3951 : : Assert(!query->groupingSets);
3952 : :
3953 : : /*
3954 : : * We intentionally print query->groupClause not processed_groupClause,
3955 : : * leaving it to the remote planner to get rid of any redundant GROUP BY
3956 : : * items again. This is necessary in case processed_groupClause reduced
3957 : : * to empty, and in any case the redundancy situation on the remote might
3958 : : * be different than what we think here.
3959 : : */
3960 [ + - + + : 214 : foreach(lc, query->groupClause)
+ + ]
3961 : : {
3962 : 113 : SortGroupClause *grp = (SortGroupClause *) lfirst(lc);
3963 : :
3964 [ + + ]: 113 : if (!first)
3965 : 12 : appendStringInfoString(buf, ", ");
3966 : 113 : first = false;
3967 : :
3968 : 113 : deparseSortGroupClause(grp->tleSortGroupRef, tlist, true, context);
3969 : : }
3970 : : }
3971 : :
3972 : : /*
3973 : : * Deparse ORDER BY clause defined by the given pathkeys.
3974 : : *
3975 : : * The clause should use Vars from context->scanrel if !has_final_sort,
3976 : : * or from context->foreignrel's targetlist if has_final_sort.
3977 : : *
3978 : : * We find a suitable pathkey expression (some earlier step
3979 : : * should have verified that there is one) and deparse it.
3980 : : */
3981 : : static void
3982 : 752 : appendOrderByClause(List *pathkeys, bool has_final_sort,
3983 : : deparse_expr_cxt *context)
3984 : : {
3985 : : ListCell *lcell;
3986 : : int nestlevel;
3987 : 752 : StringInfo buf = context->buf;
3988 : 752 : bool gotone = false;
3989 : :
3990 : : /* Make sure any constants in the exprs are printed portably */
3991 : 752 : nestlevel = set_transmission_modes();
3992 : :
3993 [ + - + + : 1637 : foreach(lcell, pathkeys)
+ + ]
3994 : : {
3995 : 885 : PathKey *pathkey = lfirst(lcell);
3996 : : EquivalenceMember *em;
3997 : : Expr *em_expr;
3998 : : Oid oprid;
3999 : :
4000 [ + + ]: 885 : if (has_final_sort)
4001 : : {
4002 : : /*
4003 : : * By construction, context->foreignrel is the input relation to
4004 : : * the final sort.
4005 : : */
4006 : 207 : em = find_em_for_rel_target(context->root,
4007 : : pathkey->pk_eclass,
4008 : : context->foreignrel);
4009 : : }
4010 : : else
4011 : 678 : em = find_em_for_rel(context->root,
4012 : : pathkey->pk_eclass,
4013 : : context->scanrel);
4014 : :
4015 : : /*
4016 : : * We don't expect any error here; it would mean that shippability
4017 : : * wasn't verified earlier. For the same reason, we don't recheck
4018 : : * shippability of the sort operator.
4019 : : */
4020 [ - + ]: 885 : if (em == NULL)
4021 [ # # ]: 0 : elog(ERROR, "could not find pathkey item to sort");
4022 : :
4023 : 885 : em_expr = em->em_expr;
4024 : :
4025 : : /*
4026 : : * If the member is a Const expression then we needn't add it to the
4027 : : * ORDER BY clause. This can happen in UNION ALL queries where the
4028 : : * union child targetlist has a Const. Adding these would be
4029 : : * wasteful, but also, for INT columns, an integer literal would be
4030 : : * seen as an ordinal column position rather than a value to sort by.
4031 : : * deparseConst() does have code to handle this, but it seems less
4032 : : * effort on all accounts just to skip these for ORDER BY clauses.
4033 : : */
4034 [ + + ]: 885 : if (IsA(em_expr, Const))
4035 : 6 : continue;
4036 : :
4037 [ + + ]: 879 : if (!gotone)
4038 : : {
4039 : 749 : appendStringInfoString(buf, " ORDER BY ");
4040 : 749 : gotone = true;
4041 : : }
4042 : : else
4043 : 130 : appendStringInfoString(buf, ", ");
4044 : :
4045 : : /*
4046 : : * Lookup the operator corresponding to the compare type in the
4047 : : * opclass. The datatype used by the opfamily is not necessarily the
4048 : : * same as the expression type (for array types for example).
4049 : : */
4050 : 879 : oprid = get_opfamily_member_for_cmptype(pathkey->pk_opfamily,
4051 : : em->em_datatype,
4052 : : em->em_datatype,
4053 : : pathkey->pk_cmptype);
4054 [ - + ]: 879 : if (!OidIsValid(oprid))
4055 [ # # ]: 0 : elog(ERROR, "missing operator %d(%u,%u) in opfamily %u",
4056 : : pathkey->pk_cmptype, em->em_datatype, em->em_datatype,
4057 : : pathkey->pk_opfamily);
4058 : :
4059 : 879 : deparseExpr(em_expr, context);
4060 : :
4061 : : /*
4062 : : * Here we need to use the expression's actual type to discover
4063 : : * whether the desired operator will be the default or not.
4064 : : */
4065 : 879 : appendOrderBySuffix(oprid, exprType((Node *) em_expr),
4066 : 879 : pathkey->pk_nulls_first, context);
4067 : :
4068 : : }
4069 : 752 : reset_transmission_modes(nestlevel);
4070 : 752 : }
4071 : :
4072 : : /*
4073 : : * Deparse LIMIT/OFFSET clause.
4074 : : */
4075 : : static void
4076 : 147 : appendLimitClause(deparse_expr_cxt *context)
4077 : : {
4078 : 147 : PlannerInfo *root = context->root;
4079 : 147 : StringInfo buf = context->buf;
4080 : : int nestlevel;
4081 : :
4082 : : /* Make sure any constants in the exprs are printed portably */
4083 : 147 : nestlevel = set_transmission_modes();
4084 : :
4085 [ + - ]: 147 : if (root->parse->limitCount)
4086 : : {
4087 : 147 : appendStringInfoString(buf, " LIMIT ");
4088 : 147 : deparseExpr((Expr *) root->parse->limitCount, context);
4089 : : }
4090 [ + + ]: 147 : if (root->parse->limitOffset)
4091 : : {
4092 : 75 : appendStringInfoString(buf, " OFFSET ");
4093 : 75 : deparseExpr((Expr *) root->parse->limitOffset, context);
4094 : : }
4095 : :
4096 : 147 : reset_transmission_modes(nestlevel);
4097 : 147 : }
4098 : :
4099 : : /*
4100 : : * appendFunctionName
4101 : : * Deparses function name from given function oid.
4102 : : */
4103 : : static void
4104 : 305 : appendFunctionName(Oid funcid, deparse_expr_cxt *context)
4105 : : {
4106 : 305 : StringInfo buf = context->buf;
4107 : : HeapTuple proctup;
4108 : : Form_pg_proc procform;
4109 : : const char *proname;
4110 : :
4111 : 305 : proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
4112 [ - + ]: 305 : if (!HeapTupleIsValid(proctup))
4113 [ # # ]: 0 : elog(ERROR, "cache lookup failed for function %u", funcid);
4114 : 305 : procform = (Form_pg_proc) GETSTRUCT(proctup);
4115 : :
4116 : : /* Print schema name only if it's not pg_catalog */
4117 [ + + ]: 305 : if (procform->pronamespace != PG_CATALOG_NAMESPACE)
4118 : : {
4119 : : const char *schemaname;
4120 : :
4121 : 6 : schemaname = get_namespace_name(procform->pronamespace);
4122 : 6 : appendStringInfo(buf, "%s.", quote_identifier(schemaname));
4123 : : }
4124 : :
4125 : : /* Always print the function name */
4126 : 305 : proname = NameStr(procform->proname);
4127 : 305 : appendStringInfoString(buf, quote_identifier(proname));
4128 : :
4129 : 305 : ReleaseSysCache(proctup);
4130 : 305 : }
4131 : :
4132 : : /*
4133 : : * Appends a sort or group clause.
4134 : : *
4135 : : * Like get_rule_sortgroupclause(), returns the expression tree, so caller
4136 : : * need not find it again.
4137 : : */
4138 : : static Node *
4139 : 145 : deparseSortGroupClause(Index ref, List *tlist, bool force_colno,
4140 : : deparse_expr_cxt *context)
4141 : : {
4142 : 145 : StringInfo buf = context->buf;
4143 : : TargetEntry *tle;
4144 : : Expr *expr;
4145 : :
4146 : 145 : tle = get_sortgroupref_tle(ref, tlist);
4147 : 145 : expr = tle->expr;
4148 : :
4149 [ + + ]: 145 : if (force_colno)
4150 : : {
4151 : : /* Use column-number form when requested by caller. */
4152 : : Assert(!tle->resjunk);
4153 : 113 : appendStringInfo(buf, "%d", tle->resno);
4154 : : }
4155 [ + - - + ]: 32 : else if (expr && IsA(expr, Const))
4156 : : {
4157 : : /*
4158 : : * Force a typecast here so that we don't emit something like "GROUP
4159 : : * BY 2", which will be misconstrued as a column position rather than
4160 : : * a constant.
4161 : : */
4162 : 0 : deparseConst((Const *) expr, context, 1);
4163 : : }
4164 [ + - + + ]: 32 : else if (!expr || IsA(expr, Var))
4165 : 18 : deparseExpr(expr, context);
4166 : : else
4167 : : {
4168 : : /* Always parenthesize the expression. */
4169 : 14 : appendStringInfoChar(buf, '(');
4170 : 14 : deparseExpr(expr, context);
4171 : 14 : appendStringInfoChar(buf, ')');
4172 : : }
4173 : :
4174 : 145 : return (Node *) expr;
4175 : : }
4176 : :
4177 : :
4178 : : /*
4179 : : * Returns true if given Var is deparsed as a subquery output column, in
4180 : : * which case, *relno and *colno are set to the IDs for the relation and
4181 : : * column alias to the Var provided by the subquery.
4182 : : */
4183 : : static bool
4184 : 8741 : is_subquery_var(Var *node, RelOptInfo *foreignrel, int *relno, int *colno)
4185 : : {
4186 : 8741 : PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) foreignrel->fdw_private;
4187 : 8741 : RelOptInfo *outerrel = fpinfo->outerrel;
4188 : 8741 : RelOptInfo *innerrel = fpinfo->innerrel;
4189 : :
4190 : : /* Should only be called in these cases. */
4191 : : Assert(IS_SIMPLE_REL(foreignrel) || IS_JOIN_REL(foreignrel));
4192 : :
4193 : : /*
4194 : : * If the given relation isn't a join relation, it doesn't have any lower
4195 : : * subqueries, so the Var isn't a subquery output column.
4196 : : */
4197 [ + + + + ]: 8741 : if (!IS_JOIN_REL(foreignrel))
4198 : 2125 : return false;
4199 : :
4200 : : /*
4201 : : * If the Var doesn't belong to any lower subqueries, it isn't a subquery
4202 : : * output column.
4203 : : */
4204 [ + + ]: 6616 : if (!bms_is_member(node->varno, fpinfo->lower_subquery_rels))
4205 : 6450 : return false;
4206 : :
4207 [ + + ]: 166 : if (bms_is_member(node->varno, outerrel->relids))
4208 : : {
4209 : : /*
4210 : : * If outer relation is deparsed as a subquery, the Var is an output
4211 : : * column of the subquery; get the IDs for the relation/column alias.
4212 : : */
4213 [ + + ]: 56 : if (fpinfo->make_outerrel_subquery)
4214 : : {
4215 : 42 : get_relation_column_alias_ids(node, outerrel, relno, colno);
4216 : 42 : return true;
4217 : : }
4218 : :
4219 : : /* Otherwise, recurse into the outer relation. */
4220 : 14 : return is_subquery_var(node, outerrel, relno, colno);
4221 : : }
4222 : : else
4223 : : {
4224 : : Assert(bms_is_member(node->varno, innerrel->relids));
4225 : :
4226 : : /*
4227 : : * If inner relation is deparsed as a subquery, the Var is an output
4228 : : * column of the subquery; get the IDs for the relation/column alias.
4229 : : */
4230 [ + - ]: 110 : if (fpinfo->make_innerrel_subquery)
4231 : : {
4232 : 110 : get_relation_column_alias_ids(node, innerrel, relno, colno);
4233 : 110 : return true;
4234 : : }
4235 : :
4236 : : /* Otherwise, recurse into the inner relation. */
4237 : 0 : return is_subquery_var(node, innerrel, relno, colno);
4238 : : }
4239 : : }
4240 : :
4241 : : /*
4242 : : * Get the IDs for the relation and column alias to given Var belonging to
4243 : : * given relation, which are returned into *relno and *colno.
4244 : : */
4245 : : static void
4246 : 152 : get_relation_column_alias_ids(Var *node, RelOptInfo *foreignrel,
4247 : : int *relno, int *colno)
4248 : : {
4249 : 152 : PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) foreignrel->fdw_private;
4250 : : int i;
4251 : : ListCell *lc;
4252 : :
4253 : : /* Get the relation alias ID */
4254 : 152 : *relno = fpinfo->relation_index;
4255 : :
4256 : : /* Get the column alias ID */
4257 : 152 : i = 1;
4258 [ + - + - : 188 : foreach(lc, foreignrel->reltarget->exprs)
+ - ]
4259 : : {
4260 : 188 : Var *tlvar = (Var *) lfirst(lc);
4261 : :
4262 : : /*
4263 : : * Match reltarget entries only on varno/varattno. Ideally there
4264 : : * would be some cross-check on varnullingrels, but it's unclear what
4265 : : * to do exactly; we don't have enough context to know what that value
4266 : : * should be.
4267 : : */
4268 [ + - ]: 188 : if (IsA(tlvar, Var) &&
4269 [ + + ]: 188 : tlvar->varno == node->varno &&
4270 [ + + ]: 180 : tlvar->varattno == node->varattno)
4271 : : {
4272 : 152 : *colno = i;
4273 : 152 : return;
4274 : : }
4275 : 36 : i++;
4276 : : }
4277 : :
4278 : : /* Shouldn't get here */
4279 [ # # ]: 0 : elog(ERROR, "unexpected expression in subquery output");
4280 : : }
|