PostgreSQL Source Code  git master
tidpath.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * tidpath.c
4  * Routines to determine which TID conditions are usable for scanning
5  * a given relation, and create TidPaths and TidRangePaths accordingly.
6  *
7  * For TidPaths, we look for WHERE conditions of the form
8  * "CTID = pseudoconstant", which can be implemented by just fetching
9  * the tuple directly via heap_fetch(). We can also handle OR'd conditions
10  * such as (CTID = const1) OR (CTID = const2), as well as ScalarArrayOpExpr
11  * conditions of the form CTID = ANY(pseudoconstant_array). In particular
12  * this allows
13  * WHERE ctid IN (tid1, tid2, ...)
14  *
15  * As with indexscans, our definition of "pseudoconstant" is pretty liberal:
16  * we allow anything that doesn't involve a volatile function or a Var of
17  * the relation under consideration. Vars belonging to other relations of
18  * the query are allowed, giving rise to parameterized TID scans.
19  *
20  * We also support "WHERE CURRENT OF cursor" conditions (CurrentOfExpr),
21  * which amount to "CTID = run-time-determined-TID". These could in
22  * theory be translated to a simple comparison of CTID to the result of
23  * a function, but in practice it works better to keep the special node
24  * representation all the way through to execution.
25  *
26  * Additionally, TidRangePaths may be created for conditions of the form
27  * "CTID relop pseudoconstant", where relop is one of >,>=,<,<=, and
28  * AND-clauses composed of such conditions.
29  *
30  * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
31  * Portions Copyright (c) 1994, Regents of the University of California
32  *
33  *
34  * IDENTIFICATION
35  * src/backend/optimizer/path/tidpath.c
36  *
37  *-------------------------------------------------------------------------
38  */
39 #include "postgres.h"
40 
41 #include "access/sysattr.h"
42 #include "catalog/pg_operator.h"
43 #include "catalog/pg_type.h"
44 #include "nodes/nodeFuncs.h"
45 #include "optimizer/clauses.h"
46 #include "optimizer/optimizer.h"
47 #include "optimizer/pathnode.h"
48 #include "optimizer/paths.h"
49 #include "optimizer/restrictinfo.h"
50 
51 
52 /*
53  * Does this Var represent the CTID column of the specified baserel?
54  */
55 static inline bool
57 {
58  /* The vartype check is strictly paranoia */
60  var->vartype == TIDOID &&
61  var->varno == rel->relid &&
62  var->varnullingrels == NULL &&
63  var->varlevelsup == 0)
64  return true;
65  return false;
66 }
67 
68 /*
69  * Check to see if a RestrictInfo is of the form
70  * CTID OP pseudoconstant
71  * or
72  * pseudoconstant OP CTID
73  * where OP is a binary operation, the CTID Var belongs to relation "rel",
74  * and nothing on the other side of the clause does.
75  */
76 static bool
78 {
79  OpExpr *node;
80  Node *arg1,
81  *arg2,
82  *other;
83  Relids other_relids;
84 
85  /* Must be an OpExpr */
86  if (!is_opclause(rinfo->clause))
87  return false;
88  node = (OpExpr *) rinfo->clause;
89 
90  /* OpExpr must have two arguments */
91  if (list_length(node->args) != 2)
92  return false;
93  arg1 = linitial(node->args);
94  arg2 = lsecond(node->args);
95 
96  /* Look for CTID as either argument */
97  other = NULL;
98  other_relids = NULL;
99  if (arg1 && IsA(arg1, Var) &&
100  IsCTIDVar((Var *) arg1, rel))
101  {
102  other = arg2;
103  other_relids = rinfo->right_relids;
104  }
105  if (!other && arg2 && IsA(arg2, Var) &&
106  IsCTIDVar((Var *) arg2, rel))
107  {
108  other = arg1;
109  other_relids = rinfo->left_relids;
110  }
111  if (!other)
112  return false;
113 
114  /* The other argument must be a pseudoconstant */
115  if (bms_is_member(rel->relid, other_relids) ||
117  return false;
118 
119  return true; /* success */
120 }
121 
122 /*
123  * Check to see if a RestrictInfo is of the form
124  * CTID = pseudoconstant
125  * or
126  * pseudoconstant = CTID
127  * where the CTID Var belongs to relation "rel", and nothing on the
128  * other side of the clause does.
129  */
130 static bool
132 {
133  if (!IsBinaryTidClause(rinfo, rel))
134  return false;
135 
136  if (((OpExpr *) rinfo->clause)->opno == TIDEqualOperator)
137  return true;
138 
139  return false;
140 }
141 
142 /*
143  * Check to see if a RestrictInfo is of the form
144  * CTID OP pseudoconstant
145  * or
146  * pseudoconstant OP CTID
147  * where OP is a range operator such as <, <=, >, or >=, the CTID Var belongs
148  * to relation "rel", and nothing on the other side of the clause does.
149  */
150 static bool
152 {
153  Oid opno;
154 
155  if (!IsBinaryTidClause(rinfo, rel))
156  return false;
157  opno = ((OpExpr *) rinfo->clause)->opno;
158 
159  if (opno == TIDLessOperator || opno == TIDLessEqOperator ||
160  opno == TIDGreaterOperator || opno == TIDGreaterEqOperator)
161  return true;
162 
163  return false;
164 }
165 
166 /*
167  * Check to see if a RestrictInfo is of the form
168  * CTID = ANY (pseudoconstant_array)
169  * where the CTID Var belongs to relation "rel", and nothing on the
170  * other side of the clause does.
171  */
172 static bool
174 {
175  ScalarArrayOpExpr *node;
176  Node *arg1,
177  *arg2;
178 
179  /* Must be a ScalarArrayOpExpr */
180  if (!(rinfo->clause && IsA(rinfo->clause, ScalarArrayOpExpr)))
181  return false;
182  node = (ScalarArrayOpExpr *) rinfo->clause;
183 
184  /* Operator must be tideq */
185  if (node->opno != TIDEqualOperator)
186  return false;
187  if (!node->useOr)
188  return false;
189  Assert(list_length(node->args) == 2);
190  arg1 = linitial(node->args);
191  arg2 = lsecond(node->args);
192 
193  /* CTID must be first argument */
194  if (arg1 && IsA(arg1, Var) &&
195  IsCTIDVar((Var *) arg1, rel))
196  {
197  /* The other argument must be a pseudoconstant */
198  if (bms_is_member(rel->relid, pull_varnos(root, arg2)) ||
200  return false;
201 
202  return true; /* success */
203  }
204 
205  return false;
206 }
207 
208 /*
209  * Check to see if a RestrictInfo is a CurrentOfExpr referencing "rel".
210  */
211 static bool
213 {
214  CurrentOfExpr *node;
215 
216  /* Must be a CurrentOfExpr */
217  if (!(rinfo->clause && IsA(rinfo->clause, CurrentOfExpr)))
218  return false;
219  node = (CurrentOfExpr *) rinfo->clause;
220 
221  /* If it references this rel, we're good */
222  if (node->cvarno == rel->relid)
223  return true;
224 
225  return false;
226 }
227 
228 /*
229  * Extract a set of CTID conditions from the given RestrictInfo
230  *
231  * Returns a List of CTID qual RestrictInfos for the specified rel (with
232  * implicit OR semantics across the list), or NIL if there are no usable
233  * conditions.
234  *
235  * This function considers only base cases; AND/OR combination is handled
236  * below. Therefore the returned List never has more than one element.
237  * (Using a List may seem a bit weird, but it simplifies the caller.)
238  */
239 static List *
241 {
242  /*
243  * We may ignore pseudoconstant clauses (they can't contain Vars, so could
244  * not match anyway).
245  */
246  if (rinfo->pseudoconstant)
247  return NIL;
248 
249  /*
250  * If clause must wait till after some lower-security-level restriction
251  * clause, reject it.
252  */
253  if (!restriction_is_securely_promotable(rinfo, rel))
254  return NIL;
255 
256  /*
257  * Check all base cases. If we get a match, return the clause.
258  */
259  if (IsTidEqualClause(rinfo, rel) ||
260  IsTidEqualAnyClause(root, rinfo, rel) ||
261  IsCurrentOfClause(rinfo, rel))
262  return list_make1(rinfo);
263 
264  return NIL;
265 }
266 
267 /*
268  * Extract a set of CTID conditions from implicit-AND List of RestrictInfos
269  *
270  * Returns a List of CTID qual RestrictInfos for the specified rel (with
271  * implicit OR semantics across the list), or NIL if there are no usable
272  * equality conditions.
273  *
274  * This function is just concerned with handling AND/OR recursion.
275  */
276 static List *
278 {
279  List *rlst = NIL;
280  ListCell *l;
281 
282  foreach(l, rlist)
283  {
285 
286  if (restriction_is_or_clause(rinfo))
287  {
288  ListCell *j;
289 
290  /*
291  * We must be able to extract a CTID condition from every
292  * sub-clause of an OR, or we can't use it.
293  */
294  foreach(j, ((BoolExpr *) rinfo->orclause)->args)
295  {
296  Node *orarg = (Node *) lfirst(j);
297  List *sublist;
298 
299  /* OR arguments should be ANDs or sub-RestrictInfos */
300  if (is_andclause(orarg))
301  {
302  List *andargs = ((BoolExpr *) orarg)->args;
303 
304  /* Recurse in case there are sub-ORs */
305  sublist = TidQualFromRestrictInfoList(root, andargs, rel);
306  }
307  else
308  {
309  RestrictInfo *ri = castNode(RestrictInfo, orarg);
310 
312  sublist = TidQualFromRestrictInfo(root, ri, rel);
313  }
314 
315  /*
316  * If nothing found in this arm, we can't do anything with
317  * this OR clause.
318  */
319  if (sublist == NIL)
320  {
321  rlst = NIL; /* forget anything we had */
322  break; /* out of loop over OR args */
323  }
324 
325  /*
326  * OK, continue constructing implicitly-OR'ed result list.
327  */
328  rlst = list_concat(rlst, sublist);
329  }
330  }
331  else
332  {
333  /* Not an OR clause, so handle base cases */
334  rlst = TidQualFromRestrictInfo(root, rinfo, rel);
335  }
336 
337  /*
338  * Stop as soon as we find any usable CTID condition. In theory we
339  * could get CTID equality conditions from different AND'ed clauses,
340  * in which case we could try to pick the most efficient one. In
341  * practice, such usage seems very unlikely, so we don't bother; we
342  * just exit as soon as we find the first candidate.
343  */
344  if (rlst)
345  break;
346  }
347 
348  return rlst;
349 }
350 
351 /*
352  * Extract a set of CTID range conditions from implicit-AND List of RestrictInfos
353  *
354  * Returns a List of CTID range qual RestrictInfos for the specified rel
355  * (with implicit AND semantics across the list), or NIL if there are no
356  * usable range conditions or if the rel's table AM does not support TID range
357  * scans.
358  */
359 static List *
361 {
362  List *rlst = NIL;
363  ListCell *l;
364 
365  if ((rel->amflags & AMFLAG_HAS_TID_RANGE) == 0)
366  return NIL;
367 
368  foreach(l, rlist)
369  {
371 
372  if (IsTidRangeClause(rinfo, rel))
373  rlst = lappend(rlst, rinfo);
374  }
375 
376  return rlst;
377 }
378 
379 /*
380  * Given a list of join clauses involving our rel, create a parameterized
381  * TidPath for each one that is a suitable TidEqual clause.
382  *
383  * In principle we could combine clauses that reference the same outer rels,
384  * but it doesn't seem like such cases would arise often enough to be worth
385  * troubling over.
386  */
387 static void
389 {
390  ListCell *l;
391 
392  foreach(l, clauses)
393  {
395  List *tidquals;
396  Relids required_outer;
397 
398  /*
399  * Validate whether each clause is actually usable; we must check this
400  * even when examining clauses generated from an EquivalenceClass,
401  * since they might not satisfy the restriction on not having Vars of
402  * our rel on the other side, or somebody might've built an operator
403  * class that accepts type "tid" but has other operators in it.
404  *
405  * We currently consider only TidEqual join clauses. In principle we
406  * might find a suitable ScalarArrayOpExpr in the rel's joininfo list,
407  * but it seems unlikely to be worth expending the cycles to check.
408  * And we definitely won't find a CurrentOfExpr here. Hence, we don't
409  * use TidQualFromRestrictInfo; but this must match that function
410  * otherwise.
411  */
412  if (rinfo->pseudoconstant ||
413  !restriction_is_securely_promotable(rinfo, rel) ||
414  !IsTidEqualClause(rinfo, rel))
415  continue;
416 
417  /*
418  * Check if clause can be moved to this rel; this is probably
419  * redundant when considering EC-derived clauses, but we must check it
420  * for "loose" join clauses.
421  */
422  if (!join_clause_is_movable_to(rinfo, rel))
423  continue;
424 
425  /* OK, make list of clauses for this path */
426  tidquals = list_make1(rinfo);
427 
428  /* Compute required outer rels for this path */
429  required_outer = bms_union(rinfo->required_relids, rel->lateral_relids);
430  required_outer = bms_del_member(required_outer, rel->relid);
431 
432  add_path(rel, (Path *) create_tidscan_path(root, rel, tidquals,
433  required_outer));
434  }
435 }
436 
437 /*
438  * Test whether an EquivalenceClass member matches our rel's CTID Var.
439  *
440  * This is a callback for use by generate_implied_equalities_for_column.
441  */
442 static bool
445  void *arg)
446 {
447  if (em->em_expr && IsA(em->em_expr, Var) &&
448  IsCTIDVar((Var *) em->em_expr, rel))
449  return true;
450  return false;
451 }
452 
453 /*
454  * create_tidscan_paths
455  * Create paths corresponding to direct TID scans of the given rel.
456  *
457  * Candidate paths are added to the rel's pathlist (using add_path).
458  */
459 void
461 {
462  List *tidquals;
463  List *tidrangequals;
464 
465  /*
466  * If any suitable quals exist in the rel's baserestrict list, generate a
467  * plain (unparameterized) TidPath with them.
468  */
469  tidquals = TidQualFromRestrictInfoList(root, rel->baserestrictinfo, rel);
470 
471  if (tidquals != NIL)
472  {
473  /*
474  * This path uses no join clauses, but it could still have required
475  * parameterization due to LATERAL refs in its tlist.
476  */
477  Relids required_outer = rel->lateral_relids;
478 
479  add_path(rel, (Path *) create_tidscan_path(root, rel, tidquals,
480  required_outer));
481  }
482 
483  /*
484  * If there are range quals in the baserestrict list, generate a
485  * TidRangePath.
486  */
488  rel);
489 
490  if (tidrangequals != NIL)
491  {
492  /*
493  * This path uses no join clauses, but it could still have required
494  * parameterization due to LATERAL refs in its tlist.
495  */
496  Relids required_outer = rel->lateral_relids;
497 
498  add_path(rel, (Path *) create_tidrangescan_path(root, rel,
499  tidrangequals,
500  required_outer));
501  }
502 
503  /*
504  * Try to generate parameterized TidPaths using equality clauses extracted
505  * from EquivalenceClasses. (This is important since simple "t1.ctid =
506  * t2.ctid" clauses will turn into ECs.)
507  */
508  if (rel->has_eclass_joins)
509  {
510  List *clauses;
511 
512  /* Generate clauses, skipping any that join to lateral_referencers */
514  rel,
516  NULL,
517  rel->lateral_referencers);
518 
519  /* Generate a path for each usable join clause */
520  BuildParameterizedTidPaths(root, rel, clauses);
521  }
522 
523  /*
524  * Also consider parameterized TidPaths using "loose" join quals. Quals
525  * of the form "t1.ctid = t2.ctid" would turn into these if they are outer
526  * join quals, for example.
527  */
528  BuildParameterizedTidPaths(root, rel, rel->joininfo);
529 }
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:460
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:211
Bitmapset * bms_del_member(Bitmapset *a, int x)
Definition: bitmapset.c:793
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:483
List * generate_implied_equalities_for_column(PlannerInfo *root, RelOptInfo *rel, ec_matches_callback_type callback, void *callback_arg, Relids prohibited_rels)
Definition: equivclass.c:2884
int j
Definition: isn.c:74
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
Assert(fmt[strlen(fmt) - 1] !='\n')
List * lappend(List *list, void *datum)
Definition: list.c:338
List * list_concat(List *list1, const List *list2)
Definition: list.c:560
static bool is_andclause(const void *clause)
Definition: nodeFuncs.h:105
static bool is_opclause(const void *clause)
Definition: nodeFuncs.h:74
#define IsA(nodeptr, _type_)
Definition: nodes.h:179
#define castNode(_type_, nodeptr)
Definition: nodes.h:197
TidPath * create_tidscan_path(PlannerInfo *root, RelOptInfo *rel, List *tidquals, Relids required_outer)
Definition: pathnode.c:1181
void add_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:422
TidRangePath * create_tidrangescan_path(PlannerInfo *root, RelOptInfo *rel, List *tidrangequals, Relids required_outer)
Definition: pathnode.c:1210
#define AMFLAG_HAS_TID_RANGE
Definition: pathnodes.h:808
void * arg
#define lfirst(lc)
Definition: pg_list.h:172
#define lfirst_node(type, lc)
Definition: pg_list.h:176
static int list_length(const List *l)
Definition: pg_list.h:152
#define NIL
Definition: pg_list.h:68
#define list_make1(x1)
Definition: pg_list.h:212
#define linitial(l)
Definition: pg_list.h:178
#define lsecond(l)
Definition: pg_list.h:183
unsigned int Oid
Definition: postgres_ext.h:31
bool restriction_is_or_clause(RestrictInfo *restrictinfo)
Definition: restrictinfo.c:416
bool restriction_is_securely_promotable(RestrictInfo *restrictinfo, RelOptInfo *rel)
Definition: restrictinfo.c:431
bool join_clause_is_movable_to(RestrictInfo *rinfo, RelOptInfo *baserel)
Definition: restrictinfo.c:584
Definition: pg_list.h:54
Definition: nodes.h:129
List * args
Definition: primnodes.h:763
List * baserestrictinfo
Definition: pathnodes.h:964
uint32 amflags
Definition: pathnodes.h:937
List * joininfo
Definition: pathnodes.h:970
Index relid
Definition: pathnodes.h:903
Relids lateral_relids
Definition: pathnodes.h:898
Relids lateral_referencers
Definition: pathnodes.h:921
bool has_eclass_joins
Definition: pathnodes.h:972
Relids required_relids
Definition: pathnodes.h:2560
Expr * clause
Definition: pathnodes.h:2529
Definition: primnodes.h:226
AttrNumber varattno
Definition: primnodes.h:238
int varno
Definition: primnodes.h:233
Index varlevelsup
Definition: primnodes.h:258
#define SelfItemPointerAttributeNumber
Definition: sysattr.h:21
static bool IsBinaryTidClause(RestrictInfo *rinfo, RelOptInfo *rel)
Definition: tidpath.c:77
static void BuildParameterizedTidPaths(PlannerInfo *root, RelOptInfo *rel, List *clauses)
Definition: tidpath.c:388
static bool IsTidEqualClause(RestrictInfo *rinfo, RelOptInfo *rel)
Definition: tidpath.c:131
static List * TidQualFromRestrictInfoList(PlannerInfo *root, List *rlist, RelOptInfo *rel)
Definition: tidpath.c:277
static List * TidRangeQualFromRestrictInfoList(List *rlist, RelOptInfo *rel)
Definition: tidpath.c:360
static bool IsCTIDVar(Var *var, RelOptInfo *rel)
Definition: tidpath.c:56
static bool IsCurrentOfClause(RestrictInfo *rinfo, RelOptInfo *rel)
Definition: tidpath.c:212
static List * TidQualFromRestrictInfo(PlannerInfo *root, RestrictInfo *rinfo, RelOptInfo *rel)
Definition: tidpath.c:240
static bool ec_member_matches_ctid(PlannerInfo *root, RelOptInfo *rel, EquivalenceClass *ec, EquivalenceMember *em, void *arg)
Definition: tidpath.c:443
static bool IsTidRangeClause(RestrictInfo *rinfo, RelOptInfo *rel)
Definition: tidpath.c:151
void create_tidscan_paths(PlannerInfo *root, RelOptInfo *rel)
Definition: tidpath.c:460
static bool IsTidEqualAnyClause(PlannerInfo *root, RestrictInfo *rinfo, RelOptInfo *rel)
Definition: tidpath.c:173
Relids pull_varnos(PlannerInfo *root, Node *node)
Definition: var.c:108