Debian Codegraph @

src/backend/optimizer/util/plancat.c

raw
1 /*-------------------------------------------------------------------------
2 *
3 * plancat.c
4 * routines for accessing the system catalogs
5 *
6 *
7 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
9 *
10 *
11 * IDENTIFICATION
12 * src/backend/optimizer/util/plancat.c
13 *
14 *-------------------------------------------------------------------------
15 */
16 #include "postgres.h"
17
18 #include <math.h>
19
20 #include "access/genam.h"
21 #include "access/htup_details.h"
22 #include "access/nbtree.h"
23 #include "access/sysattr.h"
24 #include "access/table.h"
25 #include "access/tableam.h"
26 #include "access/transam.h"
27 #include "access/xlog.h"
28 #include "catalog/catalog.h"
29 #include "catalog/heap.h"
30 #include "catalog/pg_am.h"
31 #include "catalog/pg_proc.h"
32 #include "catalog/pg_statistic_ext.h"
33 #include "catalog/pg_statistic_ext_data.h"
34 #include "foreign/fdwapi.h"
35 #include "miscadmin.h"
36 #include "nodes/makefuncs.h"
37 #include "nodes/nodeFuncs.h"
38 #include "nodes/supportnodes.h"
39 #include "optimizer/cost.h"
40 #include "optimizer/optimizer.h"
41 #include "optimizer/plancat.h"
42 #include "parser/parse_relation.h"
43 #include "parser/parsetree.h"
44 #include "partitioning/partdesc.h"
45 #include "rewrite/rewriteHandler.h"
46 #include "rewrite/rewriteManip.h"
47 #include "statistics/statistics.h"
48 #include "storage/bufmgr.h"
49 #include "tcop/tcopprot.h"
50 #include "utils/builtins.h"
51 #include "utils/lsyscache.h"
52 #include "utils/partcache.h"
53 #include "utils/rel.h"
54 #include "utils/snapmgr.h"
55 #include "utils/syscache.h"
56
57 /* GUC parameter */
58 int constraint_exclusion = CONSTRAINT_EXCLUSION_PARTITION;
59
60 /* Hook for plugins to get control in get_relation_info() */
61 get_relation_info_hook_type get_relation_info_hook = NULL;
62
63
64 static void get_relation_foreign_keys(PlannerInfo *root, RelOptInfo *rel,
65 Relation relation, bool inhparent);
66 static bool infer_collation_opclass_match(InferenceElem *elem, Relation idxRel,
67 List *idxExprs);
68 static List *get_relation_constraints(PlannerInfo *root,
69 Oid relationObjectId, RelOptInfo *rel,
70 bool include_noinherit,
71 bool include_notnull,
72 bool include_partition);
73 static List *build_index_tlist(PlannerInfo *root, IndexOptInfo *index,
74 Relation heapRelation);
75 static List *get_relation_statistics(RelOptInfo *rel, Relation relation);
76 static void set_relation_partition_info(PlannerInfo *root, RelOptInfo *rel,
77 Relation relation);
78 static PartitionScheme find_partition_scheme(PlannerInfo *root,
79 Relation relation);
80 static void set_baserel_partition_key_exprs(Relation relation,
81 RelOptInfo *rel);
82 static void set_baserel_partition_constraint(Relation relation,
83 RelOptInfo *rel);
84
85
86 /*
87 * get_relation_info -
88 * Retrieves catalog information for a given relation.
89 *
90 * Given the Oid of the relation, return the following info into fields
91 * of the RelOptInfo struct:
92 *
93 * min_attr lowest valid AttrNumber
94 * max_attr highest valid AttrNumber
95 * indexlist list of IndexOptInfos for relation's indexes
96 * statlist list of StatisticExtInfo for relation's statistic objects
97 * serverid if it's a foreign table, the server OID
98 * fdwroutine if it's a foreign table, the FDW function pointers
99 * pages number of pages
100 * tuples number of tuples
101 * rel_parallel_workers user-defined number of parallel workers
102 *
103 * Also, add information about the relation's foreign keys to root->fkey_list.
104 *
105 * Also, initialize the attr_needed[] and attr_widths[] arrays. In most
106 * cases these are left as zeroes, but sometimes we need to compute attr
107 * widths here, and we may as well cache the results for costsize.c.
108 *
109 * If inhparent is true, all we need to do is set up the attr arrays:
110 * the RelOptInfo actually represents the appendrel formed by an inheritance
111 * tree, and so the parent rel's physical size and index information isn't
112 * important for it, however, for partitioned tables, we do populate the
113 * indexlist as the planner uses unique indexes as unique proofs for certain
114 * optimizations.
115 */
116 void
117 get_relation_info(PlannerInfo *root, Oid relationObjectId, bool inhparent,
118 RelOptInfo *rel)
119 {
120 Index varno = rel->relid;
121 Relation relation;
122 bool hasindex;
123 List *indexinfos = NIL;
124
125 /*
126 * We need not lock the relation since it was already locked, either by
127 * the rewriter or when expand_inherited_rtentry() added it to the query's
128 * rangetable.
129 */
130 relation = table_open(relationObjectId, NoLock);
131
132 /*
133 * Relations without a table AM can be used in a query only if they are of
134 * special-cased relkinds. This check prevents us from crashing later if,
135 * for example, a view's ON SELECT rule has gone missing. Note that
136 * table_open() already rejected indexes and composite types; spell the
137 * error the same way it does.
138 */
139 if (!relation->rd_tableam)
140 {
141 if (!(relation->rd_rel->relkind == RELKIND_FOREIGN_TABLE ||
142 relation->rd_rel->relkind == RELKIND_PARTITIONED_TABLE))
143 ereport(ERROR,
144 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
145 errmsg("cannot open relation \"%s\"",
146 RelationGetRelationName(relation)),
147 errdetail_relkind_not_supported(relation->rd_rel->relkind)));
148 }
149
150 /* Temporary and unlogged relations are inaccessible during recovery. */
151 if (!RelationIsPermanent(relation) && RecoveryInProgress())
152 ereport(ERROR,
153 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
154 errmsg("cannot access temporary or unlogged relations during recovery")));
155
156 rel->min_attr = FirstLowInvalidHeapAttributeNumber + 1;
157 rel->max_attr = RelationGetNumberOfAttributes(relation);
158 rel->reltablespace = RelationGetForm(relation)->reltablespace;
159
160 Assert(rel->max_attr >= rel->min_attr);
161 rel->attr_needed = (Relids *)
162 palloc0((rel->max_attr - rel->min_attr + 1) * sizeof(Relids));
163 rel->attr_widths = (int32 *)
164 palloc0((rel->max_attr - rel->min_attr + 1) * sizeof(int32));
165
166 /*
167 * Record which columns are defined as NOT NULL. We leave this
168 * unpopulated for non-partitioned inheritance parent relations as it's
169 * ambiguous as to what it means. Some child tables may have a NOT NULL
170 * constraint for a column while others may not. We could work harder and
171 * build a unioned set of all child relations notnullattnums, but there's
172 * currently no need. The RelOptInfo corresponding to the !inh
173 * RangeTblEntry does get populated.
174 */
175 if (!inhparent || relation->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
176 {
177 for (int i = 0; i < relation->rd_att->natts; i++)
178 {
179 CompactAttribute *attr = TupleDescCompactAttr(relation->rd_att, i);
180
181 Assert(attr->attnullability != ATTNULLABLE_UNKNOWN);
182
183 if (attr->attnullability == ATTNULLABLE_VALID)
184 {
185 rel->notnullattnums = bms_add_member(rel->notnullattnums,
186 i + 1);
187
188 /*
189 * Per RemoveAttributeById(), dropped columns will have their
190 * attnotnull unset, so we needn't check for dropped columns
191 * in the above condition.
192 */
193 Assert(!attr->attisdropped);
194 }
195 }
196 }
197
198 /*
199 * Estimate relation size --- unless it's an inheritance parent, in which
200 * case the size we want is not the rel's own size but the size of its
201 * inheritance tree. That will be computed in set_append_rel_size().
202 */
203 if (!inhparent)
204 estimate_rel_size(relation, rel->attr_widths - rel->min_attr,
205 &rel->pages, &rel->tuples, &rel->allvisfrac);
206
207 /* Retrieve the parallel_workers reloption, or -1 if not set. */
208 rel->rel_parallel_workers = RelationGetParallelWorkers(relation, -1);
209
210 /*
211 * Make list of indexes. Ignore indexes on system catalogs if told to.
212 * Don't bother with indexes from traditional inheritance parents. For
213 * partitioned tables, we need a list of at least unique indexes as these
214 * serve as unique proofs for certain planner optimizations. However,
215 * let's not discriminate here and just record all partitioned indexes
216 * whether they're unique indexes or not.
217 */
218 if ((inhparent && relation->rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
219 || (IgnoreSystemIndexes && IsSystemRelation(relation)))
220 hasindex = false;
221 else
222 hasindex = relation->rd_rel->relhasindex;
223
224 if (hasindex)
225 {
226 List *indexoidlist;
227 LOCKMODE lmode;
228 ListCell *l;
229
230 indexoidlist = RelationGetIndexList(relation);
231
232 /*
233 * For each index, we get the same type of lock that the executor will
234 * need, and do not release it. This saves a couple of trips to the
235 * shared lock manager while not creating any real loss of
236 * concurrency, because no schema changes could be happening on the
237 * index while we hold lock on the parent rel, and no lock type used
238 * for queries blocks any other kind of index operation.
239 */
240 lmode = root->simple_rte_array[varno]->rellockmode;
241
242 foreach(l, indexoidlist)
243 {
244 Oid indexoid = lfirst_oid(l);
245 Relation indexRelation;
246 Form_pg_index index;
247 IndexAmRoutine *amroutine = NULL;
248 IndexOptInfo *info;
249 int ncolumns,
250 nkeycolumns;
251 int i;
252
253 /*
254 * Extract info from the relation descriptor for the index.
255 */
256 indexRelation = index_open(indexoid, lmode);
257 index = indexRelation->rd_index;
258
259 /*
260 * Ignore invalid indexes, since they can't safely be used for
261 * queries. Note that this is OK because the data structure we
262 * are constructing is only used by the planner --- the executor
263 * still needs to insert into "invalid" indexes, if they're marked
264 * indisready.
265 */
266 if (!index->indisvalid)
267 {
268 index_close(indexRelation, NoLock);
269 continue;
270 }
271
272 /*
273 * If the index is valid, but cannot yet be used, ignore it; but
274 * mark the plan we are generating as transient. See
275 * src/backend/access/heap/README.HOT for discussion.
276 */
277 if (index->indcheckxmin &&
278 !TransactionIdPrecedes(HeapTupleHeaderGetXmin(indexRelation->rd_indextuple->t_data),
279 TransactionXmin))
280 {
281 root->glob->transientPlan = true;
282 index_close(indexRelation, NoLock);
283 continue;
284 }
285
286 info = makeNode(IndexOptInfo);
287
288 info->indexoid = index->indexrelid;
289 info->reltablespace =
290 RelationGetForm(indexRelation)->reltablespace;
291 info->rel = rel;
292 info->ncolumns = ncolumns = index->indnatts;
293 info->nkeycolumns = nkeycolumns = index->indnkeyatts;
294
295 info->indexkeys = (int *) palloc(sizeof(int) * ncolumns);
296 info->indexcollations = (Oid *) palloc(sizeof(Oid) * nkeycolumns);
297 info->opfamily = (Oid *) palloc(sizeof(Oid) * nkeycolumns);
298 info->opcintype = (Oid *) palloc(sizeof(Oid) * nkeycolumns);
299 info->canreturn = (bool *) palloc(sizeof(bool) * ncolumns);
300
301 for (i = 0; i < ncolumns; i++)
302 {
303 info->indexkeys[i] = index->indkey.values[i];
304 info->canreturn[i] = index_can_return(indexRelation, i + 1);
305 }
306
307 for (i = 0; i < nkeycolumns; i++)
308 {
309 info->opfamily[i] = indexRelation->rd_opfamily[i];
310 info->opcintype[i] = indexRelation->rd_opcintype[i];
311 info->indexcollations[i] = indexRelation->rd_indcollation[i];
312 }
313
314 info->relam = indexRelation->rd_rel->relam;
315
316 /*
317 * We don't have an AM for partitioned indexes, so we'll just
318 * NULLify the AM related fields for those.
319 */
320 if (indexRelation->rd_rel->relkind != RELKIND_PARTITIONED_INDEX)
321 {
322 /* We copy just the fields we need, not all of rd_indam */
323 amroutine = indexRelation->rd_indam;
324 info->amcanorderbyop = amroutine->amcanorderbyop;
325 info->amoptionalkey = amroutine->amoptionalkey;
326 info->amsearcharray = amroutine->amsearcharray;
327 info->amsearchnulls = amroutine->amsearchnulls;
328 info->amcanparallel = amroutine->amcanparallel;
329 info->amhasgettuple = (amroutine->amgettuple != NULL);
330 info->amhasgetbitmap = amroutine->amgetbitmap != NULL &&
331 relation->rd_tableam->scan_bitmap_next_tuple != NULL;
332 info->amcanmarkpos = (amroutine->ammarkpos != NULL &&
333 amroutine->amrestrpos != NULL);
334 info->amcostestimate = amroutine->amcostestimate;
335 Assert(info->amcostestimate != NULL);
336
337 /* Fetch index opclass options */
338 info->opclassoptions = RelationGetIndexAttOptions(indexRelation, true);
339
340 /*
341 * Fetch the ordering information for the index, if any.
342 */
343 if (info->relam == BTREE_AM_OID)
344 {
345 /*
346 * If it's a btree index, we can use its opfamily OIDs
347 * directly as the sort ordering opfamily OIDs.
348 */
349 Assert(amroutine->amcanorder);
350
351 info->sortopfamily = info->opfamily;
352 info->reverse_sort = (bool *) palloc(sizeof(bool) * nkeycolumns);
353 info->nulls_first = (bool *) palloc(sizeof(bool) * nkeycolumns);
354
355 for (i = 0; i < nkeycolumns; i++)
356 {
357 int16 opt = indexRelation->rd_indoption[i];
358
359 info->reverse_sort[i] = (opt & INDOPTION_DESC) != 0;
360 info->nulls_first[i] = (opt & INDOPTION_NULLS_FIRST) != 0;
361 }
362 }
363 else if (amroutine->amcanorder)
364 {
365 /*
366 * Otherwise, identify the corresponding btree opfamilies
367 * by trying to map this index's "<" operators into btree.
368 * Since "<" uniquely defines the behavior of a sort
369 * order, this is a sufficient test.
370 *
371 * XXX This method is rather slow and complicated. It'd
372 * be better to have a way to explicitly declare the
373 * corresponding btree opfamily for each opfamily of the
374 * other index type.
375 */
376 info->sortopfamily = (Oid *) palloc(sizeof(Oid) * nkeycolumns);
377 info->reverse_sort = (bool *) palloc(sizeof(bool) * nkeycolumns);
378 info->nulls_first = (bool *) palloc(sizeof(bool) * nkeycolumns);
379
380 for (i = 0; i < nkeycolumns; i++)
381 {
382 int16 opt = indexRelation->rd_indoption[i];
383 Oid ltopr;
384 Oid opfamily;
385 Oid opcintype;
386 CompareType cmptype;
387
388 info->reverse_sort[i] = (opt & INDOPTION_DESC) != 0;
389 info->nulls_first[i] = (opt & INDOPTION_NULLS_FIRST) != 0;
390
391 ltopr = get_opfamily_member_for_cmptype(info->opfamily[i],
392 info->opcintype[i],
393 info->opcintype[i],
394 COMPARE_LT);
395 if (OidIsValid(ltopr) &&
396 get_ordering_op_properties(ltopr,
397 &opfamily,
398 &opcintype,
399 &cmptype) &&
400 opcintype == info->opcintype[i] &&
401 cmptype == COMPARE_LT)
402 {
403 /* Successful mapping */
404 info->sortopfamily[i] = opfamily;
405 }
406 else
407 {
408 /* Fail ... quietly treat index as unordered */
409 info->sortopfamily = NULL;
410 info->reverse_sort = NULL;
411 info->nulls_first = NULL;
412 break;
413 }
414 }
415 }
416 else
417 {
418 info->sortopfamily = NULL;
419 info->reverse_sort = NULL;
420 info->nulls_first = NULL;
421 }
422 }
423 else
424 {
425 info->amcanorderbyop = false;
426 info->amoptionalkey = false;
427 info->amsearcharray = false;
428 info->amsearchnulls = false;
429 info->amcanparallel = false;
430 info->amhasgettuple = false;
431 info->amhasgetbitmap = false;
432 info->amcanmarkpos = false;
433 info->amcostestimate = NULL;
434
435 info->sortopfamily = NULL;
436 info->reverse_sort = NULL;
437 info->nulls_first = NULL;
438 }
439
440 /*
441 * Fetch the index expressions and predicate, if any. We must
442 * modify the copies we obtain from the relcache to have the
443 * correct varno for the parent relation, so that they match up
444 * correctly against qual clauses.
445 */
446 info->indexprs = RelationGetIndexExpressions(indexRelation);
447 info->indpred = RelationGetIndexPredicate(indexRelation);
448 if (info->indexprs && varno != 1)
449 ChangeVarNodes((Node *) info->indexprs, 1, varno, 0);
450 if (info->indpred && varno != 1)
451 ChangeVarNodes((Node *) info->indpred, 1, varno, 0);
452
453 /* Build targetlist using the completed indexprs data */
454 info->indextlist = build_index_tlist(root, info, relation);
455
456 info->indrestrictinfo = NIL; /* set later, in indxpath.c */
457 info->predOK = false; /* set later, in indxpath.c */
458 info->unique = index->indisunique;
459 info->nullsnotdistinct = index->indnullsnotdistinct;
460 info->immediate = index->indimmediate;
461 info->hypothetical = false;
462
463 /*
464 * Estimate the index size. If it's not a partial index, we lock
465 * the number-of-tuples estimate to equal the parent table; if it
466 * is partial then we have to use the same methods as we would for
467 * a table, except we can be sure that the index is not larger
468 * than the table. We must ignore partitioned indexes here as
469 * there are not physical indexes.
470 */
471 if (indexRelation->rd_rel->relkind != RELKIND_PARTITIONED_INDEX)
472 {
473 if (info->indpred == NIL)
474 {
475 info->pages = RelationGetNumberOfBlocks(indexRelation);
476 info->tuples = rel->tuples;
477 }
478 else
479 {
480 double allvisfrac; /* dummy */
481
482 estimate_rel_size(indexRelation, NULL,
483 &info->pages, &info->tuples, &allvisfrac);
484 if (info->tuples > rel->tuples)
485 info->tuples = rel->tuples;
486 }
487
488 /*
489 * Get tree height while we have the index open
490 */
491 if (amroutine->amgettreeheight)
492 {
493 info->tree_height = amroutine->amgettreeheight(indexRelation);
494 }
495 else
496 {
497 /* For other index types, just set it to "unknown" for now */
498 info->tree_height = -1;
499 }
500 }
501 else
502 {
503 /* Zero these out for partitioned indexes */
504 info->pages = 0;
505 info->tuples = 0.0;
506 info->tree_height = -1;
507 }
508
509 index_close(indexRelation, NoLock);
510
511 /*
512 * We've historically used lcons() here. It'd make more sense to
513 * use lappend(), but that causes the planner to change behavior
514 * in cases where two indexes seem equally attractive. For now,
515 * stick with lcons() --- few tables should have so many indexes
516 * that the O(N^2) behavior of lcons() is really a problem.
517 */
518 indexinfos = lcons(info, indexinfos);
519 }
520
521 list_free(indexoidlist);
522 }
523
524 rel->indexlist = indexinfos;
525
526 rel->statlist = get_relation_statistics(rel, relation);
527
528 /* Grab foreign-table info using the relcache, while we have it */
529 if (relation->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
530 {
531 /* Check if the access to foreign tables is restricted */
532 if (unlikely((restrict_nonsystem_relation_kind & RESTRICT_RELKIND_FOREIGN_TABLE) != 0))
533 {
534 /* there must not be built-in foreign tables */
535 Assert(RelationGetRelid(relation) >= FirstNormalObjectId);
536
537 ereport(ERROR,
538 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
539 errmsg("access to non-system foreign table is restricted")));
540 }
541
542 rel->serverid = GetForeignServerIdByRelId(RelationGetRelid(relation));
543 rel->fdwroutine = GetFdwRoutineForRelation(relation, true);
544 }
545 else
546 {
547 rel->serverid = InvalidOid;
548 rel->fdwroutine = NULL;
549 }
550
551 /* Collect info about relation's foreign keys, if relevant */
552 get_relation_foreign_keys(root, rel, relation, inhparent);
553
554 /* Collect info about functions implemented by the rel's table AM. */
555 if (relation->rd_tableam &&
556 relation->rd_tableam->scan_set_tidrange != NULL &&
557 relation->rd_tableam->scan_getnextslot_tidrange != NULL)
558 rel->amflags |= AMFLAG_HAS_TID_RANGE;
559
560 /*
561 * Collect info about relation's partitioning scheme, if any. Only
562 * inheritance parents may be partitioned.
563 */
564 if (inhparent && relation->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
565 set_relation_partition_info(root, rel, relation);
566
567 table_close(relation, NoLock);
568
569 /*
570 * Allow a plugin to editorialize on the info we obtained from the
571 * catalogs. Actions might include altering the assumed relation size,
572 * removing an index, or adding a hypothetical index to the indexlist.
573 */
574 if (get_relation_info_hook)
575 (*get_relation_info_hook) (root, relationObjectId, inhparent, rel);
576 }
577
578 /*
579 * get_relation_foreign_keys -
580 * Retrieves foreign key information for a given relation.
581 *
582 * ForeignKeyOptInfos for relevant foreign keys are created and added to
583 * root->fkey_list. We do this now while we have the relcache entry open.
584 * We could sometimes avoid making useless ForeignKeyOptInfos if we waited
585 * until all RelOptInfos have been built, but the cost of re-opening the
586 * relcache entries would probably exceed any savings.
587 */
588 static void
589 get_relation_foreign_keys(PlannerInfo *root, RelOptInfo *rel,
590 Relation relation, bool inhparent)
591 {
592 List *rtable = root->parse->rtable;
593 List *cachedfkeys;
594 ListCell *lc;
595
596 /*
597 * If it's not a baserel, we don't care about its FKs. Also, if the query
598 * references only a single relation, we can skip the lookup since no FKs
599 * could satisfy the requirements below.
600 */
601 if (rel->reloptkind != RELOPT_BASEREL ||
602 list_length(rtable) < 2)
603 return;
604
605 /*
606 * If it's the parent of an inheritance tree, ignore its FKs. We could
607 * make useful FK-based deductions if we found that all members of the
608 * inheritance tree have equivalent FK constraints, but detecting that
609 * would require code that hasn't been written.
610 */
611 if (inhparent)
612 return;
613
614 /*
615 * Extract data about relation's FKs from the relcache. Note that this
616 * list belongs to the relcache and might disappear in a cache flush, so
617 * we must not do any further catalog access within this function.
618 */
619 cachedfkeys = RelationGetFKeyList(relation);
620
621 /*
622 * Figure out which FKs are of interest for this query, and create
623 * ForeignKeyOptInfos for them. We want only FKs that reference some
624 * other RTE of the current query. In queries containing self-joins,
625 * there might be more than one other RTE for a referenced table, and we
626 * should make a ForeignKeyOptInfo for each occurrence.
627 *
628 * Ideally, we would ignore RTEs that correspond to non-baserels, but it's
629 * too hard to identify those here, so we might end up making some useless
630 * ForeignKeyOptInfos. If so, match_foreign_keys_to_quals() will remove
631 * them again.
632 */
633 foreach(lc, cachedfkeys)
634 {
635 ForeignKeyCacheInfo *cachedfk = (ForeignKeyCacheInfo *) lfirst(lc);
636 Index rti;
637 ListCell *lc2;
638
639 /* conrelid should always be that of the table we're considering */
640 Assert(cachedfk->conrelid == RelationGetRelid(relation));
641
642 /* skip constraints currently not enforced */
643 if (!cachedfk->conenforced)
644 continue;
645
646 /* Scan to find other RTEs matching confrelid */
647 rti = 0;
648 foreach(lc2, rtable)
649 {
650 RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc2);
651 ForeignKeyOptInfo *info;
652
653 rti++;
654 /* Ignore if not the correct table */
655 if (rte->rtekind != RTE_RELATION ||
656 rte->relid != cachedfk->confrelid)
657 continue;
658 /* Ignore if it's an inheritance parent; doesn't really match */
659 if (rte->inh)
660 continue;
661 /* Ignore self-referential FKs; we only care about joins */
662 if (rti == rel->relid)
663 continue;
664
665 /* OK, let's make an entry */
666 info = makeNode(ForeignKeyOptInfo);
667 info->con_relid = rel->relid;
668 info->ref_relid = rti;
669 info->nkeys = cachedfk->nkeys;
670 memcpy(info->conkey, cachedfk->conkey, sizeof(info->conkey));
671 memcpy(info->confkey, cachedfk->confkey, sizeof(info->confkey));
672 memcpy(info->conpfeqop, cachedfk->conpfeqop, sizeof(info->conpfeqop));
673 /* zero out fields to be filled by match_foreign_keys_to_quals */
674 info->nmatched_ec = 0;
675 info->nconst_ec = 0;
676 info->nmatched_rcols = 0;
677 info->nmatched_ri = 0;
678 memset(info->eclass, 0, sizeof(info->eclass));
679 memset(info->fk_eclass_member, 0, sizeof(info->fk_eclass_member));
680 memset(info->rinfos, 0, sizeof(info->rinfos));
681
682 root->fkey_list = lappend(root->fkey_list, info);
683 }
684 }
685 }
686
687 /*
688 * infer_arbiter_indexes -
689 * Determine the unique indexes used to arbitrate speculative insertion.
690 *
691 * Uses user-supplied inference clause expressions and predicate to match a
692 * unique index from those defined and ready on the heap relation (target).
693 * An exact match is required on columns/expressions (although they can appear
694 * in any order). However, the predicate given by the user need only restrict
695 * insertion to a subset of some part of the table covered by some particular
696 * unique index (in particular, a partial unique index) in order to be
697 * inferred.
698 *
699 * The implementation does not consider which B-Tree operator class any
700 * particular available unique index attribute uses, unless one was specified
701 * in the inference specification. The same is true of collations. In
702 * particular, there is no system dependency on the default operator class for
703 * the purposes of inference. If no opclass (or collation) is specified, then
704 * all matching indexes (that may or may not match the default in terms of
705 * each attribute opclass/collation) are used for inference.
706 *
707 * Note: during index CONCURRENTLY operations, different transactions may
708 * reference different sets of arbiter indexes. This can lead to false unique
709 * constraint violations that wouldn't occur during normal operations. For
710 * more information, see insert.sgml.
711 */
712 List *
713 infer_arbiter_indexes(PlannerInfo *root)
714 {
715 OnConflictExpr *onconflict = root->parse->onConflict;
716
717 /* Iteration state */
718 Index varno;
719 RangeTblEntry *rte;
720 Relation relation;
721 Oid indexOidFromConstraint = InvalidOid;
722 List *indexList;
723 ListCell *l;
724
725 /* Normalized inference attributes and inference expressions: */
726 Bitmapset *inferAttrs = NULL;
727 List *inferElems = NIL;
728
729 /* Results */
730 List *results = NIL;
731
732 /*
733 * Quickly return NIL for ON CONFLICT DO NOTHING without an inference
734 * specification or named constraint. ON CONFLICT DO UPDATE statements
735 * must always provide one or the other (but parser ought to have caught
736 * that already).
737 */
738 if (onconflict->arbiterElems == NIL &&
739 onconflict->constraint == InvalidOid)
740 return NIL;
741
742 /*
743 * We need not lock the relation since it was already locked, either by
744 * the rewriter or when expand_inherited_rtentry() added it to the query's
745 * rangetable.
746 */
747 varno = root->parse->resultRelation;
748 rte = rt_fetch(varno, root->parse->rtable);
749
750 relation = table_open(rte->relid, NoLock);
751
752 /*
753 * Build normalized/BMS representation of plain indexed attributes, as
754 * well as a separate list of expression items. This simplifies matching
755 * the cataloged definition of indexes.
756 */
757 foreach(l, onconflict->arbiterElems)
758 {
759 InferenceElem *elem = (InferenceElem *) lfirst(l);
760 Var *var;
761 int attno;
762
763 if (!IsA(elem->expr, Var))
764 {
765 /* If not a plain Var, just shove it in inferElems for now */
766 inferElems = lappend(inferElems, elem->expr);
767 continue;
768 }
769
770 var = (Var *) elem->expr;
771 attno = var->varattno;
772
773 if (attno == 0)
774 ereport(ERROR,
775 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
776 errmsg("whole row unique index inference specifications are not supported")));
777
778 inferAttrs = bms_add_member(inferAttrs,
779 attno - FirstLowInvalidHeapAttributeNumber);
780 }
781
782 /*
783 * Lookup named constraint's index. This is not immediately returned
784 * because some additional sanity checks are required.
785 */
786 if (onconflict->constraint != InvalidOid)
787 {
788 indexOidFromConstraint = get_constraint_index(onconflict->constraint);
789
790 if (indexOidFromConstraint == InvalidOid)
791 ereport(ERROR,
792 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
793 errmsg("constraint in ON CONFLICT clause has no associated index")));
794 }
795
796 /*
797 * Using that representation, iterate through the list of indexes on the
798 * target relation to try and find a match
799 */
800 indexList = RelationGetIndexList(relation);
801
802 foreach(l, indexList)
803 {
804 Oid indexoid = lfirst_oid(l);
805 Relation idxRel;
806 Form_pg_index idxForm;
807 Bitmapset *indexedAttrs;
808 List *idxExprs;
809 List *predExprs;
810 AttrNumber natt;
811 ListCell *el;
812
813 /*
814 * Extract info from the relation descriptor for the index. Obtain
815 * the same lock type that the executor will ultimately use.
816 *
817 * Let executor complain about !indimmediate case directly, because
818 * enforcement needs to occur there anyway when an inference clause is
819 * omitted.
820 */
821 idxRel = index_open(indexoid, rte->rellockmode);
822 idxForm = idxRel->rd_index;
823
824 if (!idxForm->indisvalid)
825 goto next;
826
827 /*
828 * Note that we do not perform a check against indcheckxmin (like e.g.
829 * get_relation_info()) here to eliminate candidates, because
830 * uniqueness checking only cares about the most recently committed
831 * tuple versions.
832 */
833
834 /*
835 * Look for match on "ON constraint_name" variant, which may not be
836 * unique constraint. This can only be a constraint name.
837 */
838 if (indexOidFromConstraint == idxForm->indexrelid)
839 {
840 if (idxForm->indisexclusion && onconflict->action == ONCONFLICT_UPDATE)
841 ereport(ERROR,
842 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
843 errmsg("ON CONFLICT DO UPDATE not supported with exclusion constraints")));
844
845 results = lappend_oid(results, idxForm->indexrelid);
846 list_free(indexList);
847 index_close(idxRel, NoLock);
848 table_close(relation, NoLock);
849 return results;
850 }
851 else if (indexOidFromConstraint != InvalidOid)
852 {
853 /* No point in further work for index in named constraint case */
854 goto next;
855 }
856
857 /*
858 * Only considering conventional inference at this point (not named
859 * constraints), so index under consideration can be immediately
860 * skipped if it's not unique
861 */
862 if (!idxForm->indisunique)
863 goto next;
864
865 /*
866 * So-called unique constraints with WITHOUT OVERLAPS are really
867 * exclusion constraints, so skip those too.
868 */
869 if (idxForm->indisexclusion)
870 goto next;
871
872 /* Build BMS representation of plain (non expression) index attrs */
873 indexedAttrs = NULL;
874 for (natt = 0; natt < idxForm->indnkeyatts; natt++)
875 {
876 int attno = idxRel->rd_index->indkey.values[natt];
877
878 if (attno != 0)
879 indexedAttrs = bms_add_member(indexedAttrs,
880 attno - FirstLowInvalidHeapAttributeNumber);
881 }
882
883 /* Non-expression attributes (if any) must match */
884 if (!bms_equal(indexedAttrs, inferAttrs))
885 goto next;
886
887 /* Expression attributes (if any) must match */
888 idxExprs = RelationGetIndexExpressions(idxRel);
889 if (idxExprs && varno != 1)
890 ChangeVarNodes((Node *) idxExprs, 1, varno, 0);
891
892 foreach(el, onconflict->arbiterElems)
893 {
894 InferenceElem *elem = (InferenceElem *) lfirst(el);
895
896 /*
897 * Ensure that collation/opclass aspects of inference expression
898 * element match. Even though this loop is primarily concerned
899 * with matching expressions, it is a convenient point to check
900 * this for both expressions and ordinary (non-expression)
901 * attributes appearing as inference elements.
902 */
903 if (!infer_collation_opclass_match(elem, idxRel, idxExprs))
904 goto next;
905
906 /*
907 * Plain Vars don't factor into count of expression elements, and
908 * the question of whether or not they satisfy the index
909 * definition has already been considered (they must).
910 */
911 if (IsA(elem->expr, Var))
912 continue;
913
914 /*
915 * Might as well avoid redundant check in the rare cases where
916 * infer_collation_opclass_match() is required to do real work.
917 * Otherwise, check that element expression appears in cataloged
918 * index definition.
919 */
920 if (elem->infercollid != InvalidOid ||
921 elem->inferopclass != InvalidOid ||
922 list_member(idxExprs, elem->expr))
923 continue;
924
925 goto next;
926 }
927
928 /*
929 * Now that all inference elements were matched, ensure that the
930 * expression elements from inference clause are not missing any
931 * cataloged expressions. This does the right thing when unique
932 * indexes redundantly repeat the same attribute, or if attributes
933 * redundantly appear multiple times within an inference clause.
934 */
935 if (list_difference(idxExprs, inferElems) != NIL)
936 goto next;
937
938 /*
939 * If it's a partial index, its predicate must be implied by the ON
940 * CONFLICT's WHERE clause.
941 */
942 predExprs = RelationGetIndexPredicate(idxRel);
943 if (predExprs && varno != 1)
944 ChangeVarNodes((Node *) predExprs, 1, varno, 0);
945
946 if (!predicate_implied_by(predExprs, (List *) onconflict->arbiterWhere, false))
947 goto next;
948
949 results = lappend_oid(results, idxForm->indexrelid);
950 next:
951 index_close(idxRel, NoLock);
952 }
953
954 list_free(indexList);
955 table_close(relation, NoLock);
956
957 if (results == NIL)
958 ereport(ERROR,
959 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
960 errmsg("there is no unique or exclusion constraint matching the ON CONFLICT specification")));
961
962 return results;
963 }
964
965 /*
966 * infer_collation_opclass_match - ensure infer element opclass/collation match
967 *
968 * Given unique index inference element from inference specification, if
969 * collation was specified, or if opclass was specified, verify that there is
970 * at least one matching indexed attribute (occasionally, there may be more).
971 * Skip this in the common case where inference specification does not include
972 * collation or opclass (instead matching everything, regardless of cataloged
973 * collation/opclass of indexed attribute).
974 *
975 * At least historically, Postgres has not offered collations or opclasses
976 * with alternative-to-default notions of equality, so these additional
977 * criteria should only be required infrequently.
978 *
979 * Don't give up immediately when an inference element matches some attribute
980 * cataloged as indexed but not matching additional opclass/collation
981 * criteria. This is done so that the implementation is as forgiving as
982 * possible of redundancy within cataloged index attributes (or, less
983 * usefully, within inference specification elements). If collations actually
984 * differ between apparently redundantly indexed attributes (redundant within
985 * or across indexes), then there really is no redundancy as such.
986 *
987 * Note that if an inference element specifies an opclass and a collation at
988 * once, both must match in at least one particular attribute within index
989 * catalog definition in order for that inference element to be considered
990 * inferred/satisfied.
991 */
992 static bool
993 infer_collation_opclass_match(InferenceElem *elem, Relation idxRel,
994 List *idxExprs)
995 {
996 AttrNumber natt;
997 Oid inferopfamily = InvalidOid; /* OID of opclass opfamily */
998 Oid inferopcinputtype = InvalidOid; /* OID of opclass input type */
999 int nplain = 0; /* # plain attrs observed */
1000
1001 /*
1002 * If inference specification element lacks collation/opclass, then no
1003 * need to check for exact match.
1004 */
1005 if (elem->infercollid == InvalidOid && elem->inferopclass == InvalidOid)
1006 return true;
1007
1008 /*
1009 * Lookup opfamily and input type, for matching indexes
1010 */
1011 if (elem->inferopclass)
1012 {
1013 inferopfamily = get_opclass_family(elem->inferopclass);
1014 inferopcinputtype = get_opclass_input_type(elem->inferopclass);
1015 }
1016
1017 for (natt = 1; natt <= idxRel->rd_att->natts; natt++)
1018 {
1019 Oid opfamily = idxRel->rd_opfamily[natt - 1];
1020 Oid opcinputtype = idxRel->rd_opcintype[natt - 1];
1021 Oid collation = idxRel->rd_indcollation[natt - 1];
1022 int attno = idxRel->rd_index->indkey.values[natt - 1];
1023
1024 if (attno != 0)
1025 nplain++;
1026
1027 if (elem->inferopclass != InvalidOid &&
1028 (inferopfamily != opfamily || inferopcinputtype != opcinputtype))
1029 {
1030 /* Attribute needed to match opclass, but didn't */
1031 continue;
1032 }
1033
1034 if (elem->infercollid != InvalidOid &&
1035 elem->infercollid != collation)
1036 {
1037 /* Attribute needed to match collation, but didn't */
1038 continue;
1039 }
1040
1041 /* If one matching index att found, good enough -- return true */
1042 if (IsA(elem->expr, Var))
1043 {
1044 if (((Var *) elem->expr)->varattno == attno)
1045 return true;
1046 }
1047 else if (attno == 0)
1048 {
1049 Node *nattExpr = list_nth(idxExprs, (natt - 1) - nplain);
1050
1051 /*
1052 * Note that unlike routines like match_index_to_operand() we
1053 * don't need to care about RelabelType. Neither the index
1054 * definition nor the inference clause should contain them.
1055 */
1056 if (equal(elem->expr, nattExpr))
1057 return true;
1058 }
1059 }
1060
1061 return false;
1062 }
1063
1064 /*
1065 * estimate_rel_size - estimate # pages and # tuples in a table or index
1066 *
1067 * We also estimate the fraction of the pages that are marked all-visible in
1068 * the visibility map, for use in estimation of index-only scans.
1069 *
1070 * If attr_widths isn't NULL, it points to the zero-index entry of the
1071 * relation's attr_widths[] cache; we fill this in if we have need to compute
1072 * the attribute widths for estimation purposes.
1073 */
1074 void
1075 estimate_rel_size(Relation rel, int32 *attr_widths,
1076 BlockNumber *pages, double *tuples, double *allvisfrac)
1077 {
1078 BlockNumber curpages;
1079 BlockNumber relpages;
1080 double reltuples;
1081 BlockNumber relallvisible;
1082 double density;
1083
1084 if (RELKIND_HAS_TABLE_AM(rel->rd_rel->relkind))
1085 {
1086 table_relation_estimate_size(rel, attr_widths, pages, tuples,
1087 allvisfrac);
1088 }
1089 else if (rel->rd_rel->relkind == RELKIND_INDEX)
1090 {
1091 /*
1092 * XXX: It'd probably be good to move this into a callback, individual
1093 * index types e.g. know if they have a metapage.
1094 */
1095
1096 /* it has storage, ok to call the smgr */
1097 curpages = RelationGetNumberOfBlocks(rel);
1098
1099 /* report estimated # pages */
1100 *pages = curpages;
1101 /* quick exit if rel is clearly empty */
1102 if (curpages == 0)
1103 {
1104 *tuples = 0;
1105 *allvisfrac = 0;
1106 return;
1107 }
1108
1109 /* coerce values in pg_class to more desirable types */
1110 relpages = (BlockNumber) rel->rd_rel->relpages;
1111 reltuples = (double) rel->rd_rel->reltuples;
1112 relallvisible = (BlockNumber) rel->rd_rel->relallvisible;
1113
1114 /*
1115 * Discount the metapage while estimating the number of tuples. This
1116 * is a kluge because it assumes more than it ought to about index
1117 * structure. Currently it's OK for btree, hash, and GIN indexes but
1118 * suspect for GiST indexes.
1119 */
1120 if (relpages > 0)
1121 {
1122 curpages--;
1123 relpages--;
1124 }
1125
1126 /* estimate number of tuples from previous tuple density */
1127 if (reltuples >= 0 && relpages > 0)
1128 density = reltuples / (double) relpages;
1129 else
1130 {
1131 /*
1132 * If we have no data because the relation was never vacuumed,
1133 * estimate tuple width from attribute datatypes. We assume here
1134 * that the pages are completely full, which is OK for tables
1135 * (since they've presumably not been VACUUMed yet) but is
1136 * probably an overestimate for indexes. Fortunately
1137 * get_relation_info() can clamp the overestimate to the parent
1138 * table's size.
1139 *
1140 * Note: this code intentionally disregards alignment
1141 * considerations, because (a) that would be gilding the lily
1142 * considering how crude the estimate is, and (b) it creates
1143 * platform dependencies in the default plans which are kind of a
1144 * headache for regression testing.
1145 *
1146 * XXX: Should this logic be more index specific?
1147 */
1148 int32 tuple_width;
1149
1150 tuple_width = get_rel_data_width(rel, attr_widths);
1151 tuple_width += MAXALIGN(SizeofHeapTupleHeader);
1152 tuple_width += sizeof(ItemIdData);
1153 /* note: integer division is intentional here */
1154 density = (BLCKSZ - SizeOfPageHeaderData) / tuple_width;
1155 }
1156 *tuples = rint(density * (double) curpages);
1157
1158 /*
1159 * We use relallvisible as-is, rather than scaling it up like we do
1160 * for the pages and tuples counts, on the theory that any pages added
1161 * since the last VACUUM are most likely not marked all-visible. But
1162 * costsize.c wants it converted to a fraction.
1163 */
1164 if (relallvisible == 0 || curpages <= 0)
1165 *allvisfrac = 0;
1166 else if ((double) relallvisible >= curpages)
1167 *allvisfrac = 1;
1168 else
1169 *allvisfrac = (double) relallvisible / curpages;
1170 }
1171 else
1172 {
1173 /*
1174 * Just use whatever's in pg_class. This covers foreign tables,
1175 * sequences, and also relkinds without storage (shouldn't get here?);
1176 * see initializations in AddNewRelationTuple(). Note that FDW must
1177 * cope if reltuples is -1!
1178 */
1179 *pages = rel->rd_rel->relpages;
1180 *tuples = rel->rd_rel->reltuples;
1181 *allvisfrac = 0;
1182 }
1183 }
1184
1185
1186 /*
1187 * get_rel_data_width
1188 *
1189 * Estimate the average width of (the data part of) the relation's tuples.
1190 *
1191 * If attr_widths isn't NULL, it points to the zero-index entry of the
1192 * relation's attr_widths[] cache; use and update that cache as appropriate.
1193 *
1194 * Currently we ignore dropped columns. Ideally those should be included
1195 * in the result, but we haven't got any way to get info about them; and
1196 * since they might be mostly NULLs, treating them as zero-width is not
1197 * necessarily the wrong thing anyway.
1198 */
1199 int32
1200 get_rel_data_width(Relation rel, int32 *attr_widths)
1201 {
1202 int64 tuple_width = 0;
1203 int i;
1204
1205 for (i = 1; i <= RelationGetNumberOfAttributes(rel); i++)
1206 {
1207 Form_pg_attribute att = TupleDescAttr(rel->rd_att, i - 1);
1208 int32 item_width;
1209
1210 if (att->attisdropped)
1211 continue;
1212
1213 /* use previously cached data, if any */
1214 if (attr_widths != NULL && attr_widths[i] > 0)
1215 {
1216 tuple_width += attr_widths[i];
1217 continue;
1218 }
1219
1220 /* This should match set_rel_width() in costsize.c */
1221 item_width = get_attavgwidth(RelationGetRelid(rel), i);
1222 if (item_width <= 0)
1223 {
1224 item_width = get_typavgwidth(att->atttypid, att->atttypmod);
1225 Assert(item_width > 0);
1226 }
1227 if (attr_widths != NULL)
1228 attr_widths[i] = item_width;
1229 tuple_width += item_width;
1230 }
1231
1232 return clamp_width_est(tuple_width);
1233 }
1234
1235 /*
1236 * get_relation_data_width
1237 *
1238 * External API for get_rel_data_width: same behavior except we have to
1239 * open the relcache entry.
1240 */
1241 int32
1242 get_relation_data_width(Oid relid, int32 *attr_widths)
1243 {
1244 int32 result;
1245 Relation relation;
1246
1247 /* As above, assume relation is already locked */
1248 relation = table_open(relid, NoLock);
1249
1250 result = get_rel_data_width(relation, attr_widths);
1251
1252 table_close(relation, NoLock);
1253
1254 return result;
1255 }
1256
1257
1258 /*
1259 * get_relation_constraints
1260 *
1261 * Retrieve the applicable constraint expressions of the given relation.
1262 * Only constraints that have been validated are considered.
1263 *
1264 * Returns a List (possibly empty) of constraint expressions. Each one
1265 * has been canonicalized, and its Vars are changed to have the varno
1266 * indicated by rel->relid. This allows the expressions to be easily
1267 * compared to expressions taken from WHERE.
1268 *
1269 * If include_noinherit is true, it's okay to include constraints that
1270 * are marked NO INHERIT.
1271 *
1272 * If include_notnull is true, "col IS NOT NULL" expressions are generated
1273 * and added to the result for each column that's marked attnotnull.
1274 *
1275 * If include_partition is true, and the relation is a partition,
1276 * also include the partitioning constraints.
1277 *
1278 * Note: at present this is invoked at most once per relation per planner
1279 * run, and in many cases it won't be invoked at all, so there seems no
1280 * point in caching the data in RelOptInfo.
1281 */
1282 static List *
1283 get_relation_constraints(PlannerInfo *root,
1284 Oid relationObjectId, RelOptInfo *rel,
1285 bool include_noinherit,
1286 bool include_notnull,
1287 bool include_partition)
1288 {
1289 List *result = NIL;
1290 Index varno = rel->relid;
1291 Relation relation;
1292 TupleConstr *constr;
1293
1294 /*
1295 * We assume the relation has already been safely locked.
1296 */
1297 relation = table_open(relationObjectId, NoLock);
1298
1299 constr = relation->rd_att->constr;
1300 if (constr != NULL)
1301 {
1302 int num_check = constr->num_check;
1303 int i;
1304
1305 for (i = 0; i < num_check; i++)
1306 {
1307 Node *cexpr;
1308
1309 /*
1310 * If this constraint hasn't been fully validated yet, we must
1311 * ignore it here.
1312 */
1313 if (!constr->check[i].ccvalid)
1314 continue;
1315
1316 /*
1317 * NOT ENFORCED constraints are always marked as invalid, which
1318 * should have been ignored.
1319 */
1320 Assert(constr->check[i].ccenforced);
1321
1322 /*
1323 * Also ignore if NO INHERIT and we weren't told that that's safe.
1324 */
1325 if (constr->check[i].ccnoinherit && !include_noinherit)
1326 continue;
1327
1328 cexpr = stringToNode(constr->check[i].ccbin);
1329
1330 /*
1331 * Run each expression through const-simplification and
1332 * canonicalization. This is not just an optimization, but is
1333 * necessary, because we will be comparing it to
1334 * similarly-processed qual clauses, and may fail to detect valid
1335 * matches without this. This must match the processing done to
1336 * qual clauses in preprocess_expression()! (We can skip the
1337 * stuff involving subqueries, however, since we don't allow any
1338 * in check constraints.)
1339 */
1340 cexpr = eval_const_expressions(root, cexpr);
1341
1342 cexpr = (Node *) canonicalize_qual((Expr *) cexpr, true);
1343
1344 /* Fix Vars to have the desired varno */
1345 if (varno != 1)
1346 ChangeVarNodes(cexpr, 1, varno, 0);
1347
1348 /*
1349 * Finally, convert to implicit-AND format (that is, a List) and
1350 * append the resulting item(s) to our output list.
1351 */
1352 result = list_concat(result,
1353 make_ands_implicit((Expr *) cexpr));
1354 }
1355
1356 /* Add NOT NULL constraints in expression form, if requested */
1357 if (include_notnull && constr->has_not_null)
1358 {
1359 int natts = relation->rd_att->natts;
1360
1361 for (i = 1; i <= natts; i++)
1362 {
1363 CompactAttribute *att = TupleDescCompactAttr(relation->rd_att, i - 1);
1364
1365 if (att->attnullability == ATTNULLABLE_VALID && !att->attisdropped)
1366 {
1367 Form_pg_attribute wholeatt = TupleDescAttr(relation->rd_att, i - 1);
1368 NullTest *ntest = makeNode(NullTest);
1369
1370 ntest->arg = (Expr *) makeVar(varno,
1371 i,
1372 wholeatt->atttypid,
1373 wholeatt->atttypmod,
1374 wholeatt->attcollation,
1375 0);
1376 ntest->nulltesttype = IS_NOT_NULL;
1377
1378 /*
1379 * argisrow=false is correct even for a composite column,
1380 * because attnotnull does not represent a SQL-spec IS NOT
1381 * NULL test in such a case, just IS DISTINCT FROM NULL.
1382 */
1383 ntest->argisrow = false;
1384 ntest->location = -1;
1385 result = lappend(result, ntest);
1386 }
1387 }
1388 }
1389 }
1390
1391 /*
1392 * Add partitioning constraints, if requested.
1393 */
1394 if (include_partition && relation->rd_rel->relispartition)
1395 {
1396 /* make sure rel->partition_qual is set */
1397 set_baserel_partition_constraint(relation, rel);
1398 result = list_concat(result, rel->partition_qual);
1399 }
1400
1401 /*
1402 * Expand virtual generated columns in the constraint expressions.
1403 */
1404 if (result)
1405 result = (List *) expand_generated_columns_in_expr((Node *) result,
1406 relation,
1407 varno);
1408
1409 table_close(relation, NoLock);
1410
1411 return result;
1412 }
1413
1414 /*
1415 * Try loading data for the statistics object.
1416 *
1417 * We don't know if the data (specified by statOid and inh value) exist.
1418 * The result is stored in stainfos list.
1419 */
1420 static void
1421 get_relation_statistics_worker(List **stainfos, RelOptInfo *rel,
1422 Oid statOid, bool inh,
1423 Bitmapset *keys, List *exprs)
1424 {
1425 Form_pg_statistic_ext_data dataForm;
1426 HeapTuple dtup;
1427
1428 dtup = SearchSysCache2(STATEXTDATASTXOID,
1429 ObjectIdGetDatum(statOid), BoolGetDatum(inh));
1430 if (!HeapTupleIsValid(dtup))
1431 return;
1432
1433 dataForm = (Form_pg_statistic_ext_data) GETSTRUCT(dtup);
1434
1435 /* add one StatisticExtInfo for each kind built */
1436 if (statext_is_kind_built(dtup, STATS_EXT_NDISTINCT))
1437 {
1438 StatisticExtInfo *info = makeNode(StatisticExtInfo);
1439
1440 info->statOid = statOid;
1441 info->inherit = dataForm->stxdinherit;
1442 info->rel = rel;
1443 info->kind = STATS_EXT_NDISTINCT;
1444 info->keys = bms_copy(keys);
1445 info->exprs = exprs;
1446
1447 *stainfos = lappend(*stainfos, info);
1448 }
1449
1450 if (statext_is_kind_built(dtup, STATS_EXT_DEPENDENCIES))
1451 {
1452 StatisticExtInfo *info = makeNode(StatisticExtInfo);
1453
1454 info->statOid = statOid;
1455 info->inherit = dataForm->stxdinherit;
1456 info->rel = rel;
1457 info->kind = STATS_EXT_DEPENDENCIES;
1458 info->keys = bms_copy(keys);
1459 info->exprs = exprs;
1460
1461 *stainfos = lappend(*stainfos, info);
1462 }
1463
1464 if (statext_is_kind_built(dtup, STATS_EXT_MCV))
1465 {
1466 StatisticExtInfo *info = makeNode(StatisticExtInfo);
1467
1468 info->statOid = statOid;
1469 info->inherit = dataForm->stxdinherit;
1470 info->rel = rel;
1471 info->kind = STATS_EXT_MCV;
1472 info->keys = bms_copy(keys);
1473 info->exprs = exprs;
1474
1475 *stainfos = lappend(*stainfos, info);
1476 }
1477
1478 if (statext_is_kind_built(dtup, STATS_EXT_EXPRESSIONS))
1479 {
1480 StatisticExtInfo *info = makeNode(StatisticExtInfo);
1481
1482 info->statOid = statOid;
1483 info->inherit = dataForm->stxdinherit;
1484 info->rel = rel;
1485 info->kind = STATS_EXT_EXPRESSIONS;
1486 info->keys = bms_copy(keys);
1487 info->exprs = exprs;
1488
1489 *stainfos = lappend(*stainfos, info);
1490 }
1491
1492 ReleaseSysCache(dtup);
1493 }
1494
1495 /*
1496 * get_relation_statistics
1497 * Retrieve extended statistics defined on the table.
1498 *
1499 * Returns a List (possibly empty) of StatisticExtInfo objects describing
1500 * the statistics. Note that this doesn't load the actual statistics data,
1501 * just the identifying metadata. Only stats actually built are considered.
1502 */
1503 static List *
1504 get_relation_statistics(RelOptInfo *rel, Relation relation)
1505 {
1506 Index varno = rel->relid;
1507 List *statoidlist;
1508 List *stainfos = NIL;
1509 ListCell *l;
1510
1511 statoidlist = RelationGetStatExtList(relation);
1512
1513 foreach(l, statoidlist)
1514 {
1515 Oid statOid = lfirst_oid(l);
1516 Form_pg_statistic_ext staForm;
1517 HeapTuple htup;
1518 Bitmapset *keys = NULL;
1519 List *exprs = NIL;
1520 int i;
1521
1522 htup = SearchSysCache1(STATEXTOID, ObjectIdGetDatum(statOid));
1523 if (!HeapTupleIsValid(htup))
1524 elog(ERROR, "cache lookup failed for statistics object %u", statOid);
1525 staForm = (Form_pg_statistic_ext) GETSTRUCT(htup);
1526
1527 /*
1528 * First, build the array of columns covered. This is ultimately
1529 * wasted if no stats within the object have actually been built, but
1530 * it doesn't seem worth troubling over that case.
1531 */
1532 for (i = 0; i < staForm->stxkeys.dim1; i++)
1533 keys = bms_add_member(keys, staForm->stxkeys.values[i]);
1534
1535 /*
1536 * Preprocess expressions (if any). We read the expressions, run them
1537 * through eval_const_expressions, and fix the varnos.
1538 *
1539 * XXX We don't know yet if there are any data for this stats object,
1540 * with either stxdinherit value. But it's reasonable to assume there
1541 * is at least one of those, possibly both. So it's better to process
1542 * keys and expressions here.
1543 */
1544 {
1545 bool isnull;
1546 Datum datum;
1547
1548 /* decode expression (if any) */
1549 datum = SysCacheGetAttr(STATEXTOID, htup,
1550 Anum_pg_statistic_ext_stxexprs, &isnull);
1551
1552 if (!isnull)
1553 {
1554 char *exprsString;
1555
1556 exprsString = TextDatumGetCString(datum);
1557 exprs = (List *) stringToNode(exprsString);
1558 pfree(exprsString);
1559
1560 /*
1561 * Run the expressions through eval_const_expressions. This is
1562 * not just an optimization, but is necessary, because the
1563 * planner will be comparing them to similarly-processed qual
1564 * clauses, and may fail to detect valid matches without this.
1565 * We must not use canonicalize_qual, however, since these
1566 * aren't qual expressions.
1567 */
1568 exprs = (List *) eval_const_expressions(NULL, (Node *) exprs);
1569
1570 /* May as well fix opfuncids too */
1571 fix_opfuncids((Node *) exprs);
1572
1573 /*
1574 * Modify the copies we obtain from the relcache to have the
1575 * correct varno for the parent relation, so that they match
1576 * up correctly against qual clauses.
1577 */
1578 if (varno != 1)
1579 ChangeVarNodes((Node *) exprs, 1, varno, 0);
1580 }
1581 }
1582
1583 /* extract statistics for possible values of stxdinherit flag */
1584
1585 get_relation_statistics_worker(&stainfos, rel, statOid, true, keys, exprs);
1586
1587 get_relation_statistics_worker(&stainfos, rel, statOid, false, keys, exprs);
1588
1589 ReleaseSysCache(htup);
1590 bms_free(keys);
1591 }
1592
1593 list_free(statoidlist);
1594
1595 return stainfos;
1596 }
1597
1598 /*
1599 * relation_excluded_by_constraints
1600 *
1601 * Detect whether the relation need not be scanned because it has either
1602 * self-inconsistent restrictions, or restrictions inconsistent with the
1603 * relation's applicable constraints.
1604 *
1605 * Note: this examines only rel->relid, rel->reloptkind, and
1606 * rel->baserestrictinfo; therefore it can be called before filling in
1607 * other fields of the RelOptInfo.
1608 */
1609 bool
1610 relation_excluded_by_constraints(PlannerInfo *root,
1611 RelOptInfo *rel, RangeTblEntry *rte)
1612 {
1613 bool include_noinherit;
1614 bool include_notnull;
1615 bool include_partition = false;
1616 List *safe_restrictions;
1617 List *constraint_pred;
1618 List *safe_constraints;
1619 ListCell *lc;
1620
1621 /* As of now, constraint exclusion works only with simple relations. */
1622 Assert(IS_SIMPLE_REL(rel));
1623
1624 /*
1625 * If there are no base restriction clauses, we have no hope of proving
1626 * anything below, so fall out quickly.
1627 */
1628 if (rel->baserestrictinfo == NIL)
1629 return false;
1630
1631 /*
1632 * Regardless of the setting of constraint_exclusion, detect
1633 * constant-FALSE-or-NULL restriction clauses. Although const-folding
1634 * will reduce "anything AND FALSE" to just "FALSE", the baserestrictinfo
1635 * list can still have other members besides the FALSE constant, due to
1636 * qual pushdown and other mechanisms; so check them all. This doesn't
1637 * fire very often, but it seems cheap enough to be worth doing anyway.
1638 * (Without this, we'd miss some optimizations that 9.5 and earlier found
1639 * via much more roundabout methods.)
1640 */
1641 foreach(lc, rel->baserestrictinfo)
1642 {
1643 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1644 Expr *clause = rinfo->clause;
1645
1646 if (clause && IsA(clause, Const) &&
1647 (((Const *) clause)->constisnull ||
1648 !DatumGetBool(((Const *) clause)->constvalue)))
1649 return true;
1650 }
1651
1652 /*
1653 * Skip further tests, depending on constraint_exclusion.
1654 */
1655 switch (constraint_exclusion)
1656 {
1657 case CONSTRAINT_EXCLUSION_OFF:
1658 /* In 'off' mode, never make any further tests */
1659 return false;
1660
1661 case CONSTRAINT_EXCLUSION_PARTITION:
1662
1663 /*
1664 * When constraint_exclusion is set to 'partition' we only handle
1665 * appendrel members. Partition pruning has already been applied,
1666 * so there is no need to consider the rel's partition constraints
1667 * here.
1668 */
1669 if (rel->reloptkind == RELOPT_OTHER_MEMBER_REL)
1670 break; /* appendrel member, so process it */
1671 return false;
1672
1673 case CONSTRAINT_EXCLUSION_ON:
1674
1675 /*
1676 * In 'on' mode, always apply constraint exclusion. If we are
1677 * considering a baserel that is a partition (i.e., it was
1678 * directly named rather than expanded from a parent table), then
1679 * its partition constraints haven't been considered yet, so
1680 * include them in the processing here.
1681 */
1682 if (rel->reloptkind == RELOPT_BASEREL)
1683 include_partition = true;
1684 break; /* always try to exclude */
1685 }
1686
1687 /*
1688 * Check for self-contradictory restriction clauses. We dare not make
1689 * deductions with non-immutable functions, but any immutable clauses that
1690 * are self-contradictory allow us to conclude the scan is unnecessary.
1691 *
1692 * Note: strip off RestrictInfo because predicate_refuted_by() isn't
1693 * expecting to see any in its predicate argument.
1694 */
1695 safe_restrictions = NIL;
1696 foreach(lc, rel->baserestrictinfo)
1697 {
1698 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1699
1700 if (!contain_mutable_functions((Node *) rinfo->clause))
1701 safe_restrictions = lappend(safe_restrictions, rinfo->clause);
1702 }
1703
1704 /*
1705 * We can use weak refutation here, since we're comparing restriction
1706 * clauses with restriction clauses.
1707 */
1708 if (predicate_refuted_by(safe_restrictions, safe_restrictions, true))
1709 return true;
1710
1711 /*
1712 * Only plain relations have constraints, so stop here for other rtekinds.
1713 */
1714 if (rte->rtekind != RTE_RELATION)
1715 return false;
1716
1717 /*
1718 * If we are scanning just this table, we can use NO INHERIT constraints,
1719 * but not if we're scanning its children too. (Note that partitioned
1720 * tables should never have NO INHERIT constraints; but it's not necessary
1721 * for us to assume that here.)
1722 */
1723 include_noinherit = !rte->inh;
1724
1725 /*
1726 * Currently, attnotnull constraints must be treated as NO INHERIT unless
1727 * this is a partitioned table. In future we might track their
1728 * inheritance status more accurately, allowing this to be refined.
1729 *
1730 * XXX do we need/want to change this?
1731 */
1732 include_notnull = (!rte->inh || rte->relkind == RELKIND_PARTITIONED_TABLE);
1733
1734 /*
1735 * Fetch the appropriate set of constraint expressions.
1736 */
1737 constraint_pred = get_relation_constraints(root, rte->relid, rel,
1738 include_noinherit,
1739 include_notnull,
1740 include_partition);
1741
1742 /*
1743 * We do not currently enforce that CHECK constraints contain only
1744 * immutable functions, so it's necessary to check here. We daren't draw
1745 * conclusions from plan-time evaluation of non-immutable functions. Since
1746 * they're ANDed, we can just ignore any mutable constraints in the list,
1747 * and reason about the rest.
1748 */
1749 safe_constraints = NIL;
1750 foreach(lc, constraint_pred)
1751 {
1752 Node *pred = (Node *) lfirst(lc);
1753
1754 if (!contain_mutable_functions(pred))
1755 safe_constraints = lappend(safe_constraints, pred);
1756 }
1757
1758 /*
1759 * The constraints are effectively ANDed together, so we can just try to
1760 * refute the entire collection at once. This may allow us to make proofs
1761 * that would fail if we took them individually.
1762 *
1763 * Note: we use rel->baserestrictinfo, not safe_restrictions as might seem
1764 * an obvious optimization. Some of the clauses might be OR clauses that
1765 * have volatile and nonvolatile subclauses, and it's OK to make
1766 * deductions with the nonvolatile parts.
1767 *
1768 * We need strong refutation because we have to prove that the constraints
1769 * would yield false, not just NULL.
1770 */
1771 if (predicate_refuted_by(safe_constraints, rel->baserestrictinfo, false))
1772 return true;
1773
1774 return false;
1775 }
1776
1777
1778 /*
1779 * build_physical_tlist
1780 *
1781 * Build a targetlist consisting of exactly the relation's user attributes,
1782 * in order. The executor can special-case such tlists to avoid a projection
1783 * step at runtime, so we use such tlists preferentially for scan nodes.
1784 *
1785 * Exception: if there are any dropped or missing columns, we punt and return
1786 * NIL. Ideally we would like to handle these cases too. However this
1787 * creates problems for ExecTypeFromTL, which may be asked to build a tupdesc
1788 * for a tlist that includes vars of no-longer-existent types. In theory we
1789 * could dig out the required info from the pg_attribute entries of the
1790 * relation, but that data is not readily available to ExecTypeFromTL.
1791 * For now, we don't apply the physical-tlist optimization when there are
1792 * dropped cols.
1793 *
1794 * We also support building a "physical" tlist for subqueries, functions,
1795 * values lists, table expressions, and CTEs, since the same optimization can
1796 * occur in SubqueryScan, FunctionScan, ValuesScan, CteScan, TableFunc,
1797 * NamedTuplestoreScan, and WorkTableScan nodes.
1798 */
1799 List *
1800 build_physical_tlist(PlannerInfo *root, RelOptInfo *rel)
1801 {
1802 List *tlist = NIL;
1803 Index varno = rel->relid;
1804 RangeTblEntry *rte = planner_rt_fetch(varno, root);
1805 Relation relation;
1806 Query *subquery;
1807 Var *var;
1808 ListCell *l;
1809 int attrno,
1810 numattrs;
1811 List *colvars;
1812
1813 switch (rte->rtekind)
1814 {
1815 case RTE_RELATION:
1816 /* Assume we already have adequate lock */
1817 relation = table_open(rte->relid, NoLock);
1818
1819 numattrs = RelationGetNumberOfAttributes(relation);
1820 for (attrno = 1; attrno <= numattrs; attrno++)
1821 {
1822 Form_pg_attribute att_tup = TupleDescAttr(relation->rd_att,
1823 attrno - 1);
1824
1825 if (att_tup->attisdropped || att_tup->atthasmissing)
1826 {
1827 /* found a dropped or missing col, so punt */
1828 tlist = NIL;
1829 break;
1830 }
1831
1832 var = makeVar(varno,
1833 attrno,
1834 att_tup->atttypid,
1835 att_tup->atttypmod,
1836 att_tup->attcollation,
1837 0);
1838
1839 tlist = lappend(tlist,
1840 makeTargetEntry((Expr *) var,
1841 attrno,
1842 NULL,
1843 false));
1844 }
1845
1846 table_close(relation, NoLock);
1847 break;
1848
1849 case RTE_SUBQUERY:
1850 subquery = rte->subquery;
1851 foreach(l, subquery->targetList)
1852 {
1853 TargetEntry *tle = (TargetEntry *) lfirst(l);
1854
1855 /*
1856 * A resjunk column of the subquery can be reflected as
1857 * resjunk in the physical tlist; we need not punt.
1858 */
1859 var = makeVarFromTargetEntry(varno, tle);
1860
1861 tlist = lappend(tlist,
1862 makeTargetEntry((Expr *) var,
1863 tle->resno,
1864 NULL,
1865 tle->resjunk));
1866 }
1867 break;
1868
1869 case RTE_FUNCTION:
1870 case RTE_TABLEFUNC:
1871 case RTE_VALUES:
1872 case RTE_CTE:
1873 case RTE_NAMEDTUPLESTORE:
1874 case RTE_RESULT:
1875 /* Not all of these can have dropped cols, but share code anyway */
1876 expandRTE(rte, varno, 0, VAR_RETURNING_DEFAULT, -1,
1877 true /* include dropped */ , NULL, &colvars);
1878 foreach(l, colvars)
1879 {
1880 var = (Var *) lfirst(l);
1881
1882 /*
1883 * A non-Var in expandRTE's output means a dropped column;
1884 * must punt.
1885 */
1886 if (!IsA(var, Var))
1887 {
1888 tlist = NIL;
1889 break;
1890 }
1891
1892 tlist = lappend(tlist,
1893 makeTargetEntry((Expr *) var,
1894 var->varattno,
1895 NULL,
1896 false));
1897 }
1898 break;
1899
1900 default:
1901 /* caller error */
1902 elog(ERROR, "unsupported RTE kind %d in build_physical_tlist",
1903 (int) rte->rtekind);
1904 break;
1905 }
1906
1907 return tlist;
1908 }
1909
1910 /*
1911 * build_index_tlist
1912 *
1913 * Build a targetlist representing the columns of the specified index.
1914 * Each column is represented by a Var for the corresponding base-relation
1915 * column, or an expression in base-relation Vars, as appropriate.
1916 *
1917 * There are never any dropped columns in indexes, so unlike
1918 * build_physical_tlist, we need no failure case.
1919 */
1920 static List *
1921 build_index_tlist(PlannerInfo *root, IndexOptInfo *index,
1922 Relation heapRelation)
1923 {
1924 List *tlist = NIL;
1925 Index varno = index->rel->relid;
1926 ListCell *indexpr_item;
1927 int i;
1928
1929 indexpr_item = list_head(index->indexprs);
1930 for (i = 0; i < index->ncolumns; i++)
1931 {
1932 int indexkey = index->indexkeys[i];
1933 Expr *indexvar;
1934
1935 if (indexkey != 0)
1936 {
1937 /* simple column */
1938 const FormData_pg_attribute *att_tup;
1939
1940 if (indexkey < 0)
1941 att_tup = SystemAttributeDefinition(indexkey);
1942 else
1943 att_tup = TupleDescAttr(heapRelation->rd_att, indexkey - 1);
1944
1945 indexvar = (Expr *) makeVar(varno,
1946 indexkey,
1947 att_tup->atttypid,
1948 att_tup->atttypmod,
1949 att_tup->attcollation,
1950 0);
1951 }
1952 else
1953 {
1954 /* expression column */
1955 if (indexpr_item == NULL)
1956 elog(ERROR, "wrong number of index expressions");
1957 indexvar = (Expr *) lfirst(indexpr_item);
1958 indexpr_item = lnext(index->indexprs, indexpr_item);
1959 }
1960
1961 tlist = lappend(tlist,
1962 makeTargetEntry(indexvar,
1963 i + 1,
1964 NULL,
1965 false));
1966 }
1967 if (indexpr_item != NULL)
1968 elog(ERROR, "wrong number of index expressions");
1969
1970 return tlist;
1971 }
1972
1973 /*
1974 * restriction_selectivity
1975 *
1976 * Returns the selectivity of a specified restriction operator clause.
1977 * This code executes registered procedures stored in the
1978 * operator relation, by calling the function manager.
1979 *
1980 * See clause_selectivity() for the meaning of the additional parameters.
1981 */
1982 Selectivity
1983 restriction_selectivity(PlannerInfo *root,
1984 Oid operatorid,
1985 List *args,
1986 Oid inputcollid,
1987 int varRelid)
1988 {
1989 RegProcedure oprrest = get_oprrest(operatorid);
1990 float8 result;
1991
1992 /*
1993 * if the oprrest procedure is missing for whatever reason, use a
1994 * selectivity of 0.5
1995 */
1996 if (!oprrest)
1997 return (Selectivity) 0.5;
1998
1999 result = DatumGetFloat8(OidFunctionCall4Coll(oprrest,
2000 inputcollid,
2001 PointerGetDatum(root),
2002 ObjectIdGetDatum(operatorid),
2003 PointerGetDatum(args),
2004 Int32GetDatum(varRelid)));
2005
2006 if (result < 0.0 || result > 1.0)
2007 elog(ERROR, "invalid restriction selectivity: %f", result);
2008
2009 return (Selectivity) result;
2010 }
2011
2012 /*
2013 * join_selectivity
2014 *
2015 * Returns the selectivity of a specified join operator clause.
2016 * This code executes registered procedures stored in the
2017 * operator relation, by calling the function manager.
2018 *
2019 * See clause_selectivity() for the meaning of the additional parameters.
2020 */
2021 Selectivity
2022 join_selectivity(PlannerInfo *root,
2023 Oid operatorid,
2024 List *args,
2025 Oid inputcollid,
2026 JoinType jointype,
2027 SpecialJoinInfo *sjinfo)
2028 {
2029 RegProcedure oprjoin = get_oprjoin(operatorid);
2030 float8 result;
2031
2032 /*
2033 * if the oprjoin procedure is missing for whatever reason, use a
2034 * selectivity of 0.5
2035 */
2036 if (!oprjoin)
2037 return (Selectivity) 0.5;
2038
2039 result = DatumGetFloat8(OidFunctionCall5Coll(oprjoin,
2040 inputcollid,
2041 PointerGetDatum(root),
2042 ObjectIdGetDatum(operatorid),
2043 PointerGetDatum(args),
2044 Int16GetDatum(jointype),
2045 PointerGetDatum(sjinfo)));
2046
2047 if (result < 0.0 || result > 1.0)
2048 elog(ERROR, "invalid join selectivity: %f", result);
2049
2050 return (Selectivity) result;
2051 }
2052
2053 /*
2054 * function_selectivity
2055 *
2056 * Returns the selectivity of a specified boolean function clause.
2057 * This code executes registered procedures stored in the
2058 * pg_proc relation, by calling the function manager.
2059 *
2060 * See clause_selectivity() for the meaning of the additional parameters.
2061 */
2062 Selectivity
2063 function_selectivity(PlannerInfo *root,
2064 Oid funcid,
2065 List *args,
2066 Oid inputcollid,
2067 bool is_join,
2068 int varRelid,
2069 JoinType jointype,
2070 SpecialJoinInfo *sjinfo)
2071 {
2072 RegProcedure prosupport = get_func_support(funcid);
2073 SupportRequestSelectivity req;
2074 SupportRequestSelectivity *sresult;
2075
2076 /*
2077 * If no support function is provided, use our historical default
2078 * estimate, 0.3333333. This seems a pretty unprincipled choice, but
2079 * Postgres has been using that estimate for function calls since 1992.
2080 * The hoariness of this behavior suggests that we should not be in too
2081 * much hurry to use another value.
2082 */
2083 if (!prosupport)
2084 return (Selectivity) 0.3333333;
2085
2086 req.type = T_SupportRequestSelectivity;
2087 req.root = root;
2088 req.funcid = funcid;
2089 req.args = args;
2090 req.inputcollid = inputcollid;
2091 req.is_join = is_join;
2092 req.varRelid = varRelid;
2093 req.jointype = jointype;
2094 req.sjinfo = sjinfo;
2095 req.selectivity = -1; /* to catch failure to set the value */
2096
2097 sresult = (SupportRequestSelectivity *)
2098 DatumGetPointer(OidFunctionCall1(prosupport,
2099 PointerGetDatum(&req)));
2100
2101 /* If support function fails, use default */
2102 if (sresult != &req)
2103 return (Selectivity) 0.3333333;
2104
2105 if (req.selectivity < 0.0 || req.selectivity > 1.0)
2106 elog(ERROR, "invalid function selectivity: %f", req.selectivity);
2107
2108 return (Selectivity) req.selectivity;
2109 }
2110
2111 /*
2112 * add_function_cost
2113 *
2114 * Get an estimate of the execution cost of a function, and *add* it to
2115 * the contents of *cost. The estimate may include both one-time and
2116 * per-tuple components, since QualCost does.
2117 *
2118 * The funcid must always be supplied. If it is being called as the
2119 * implementation of a specific parsetree node (FuncExpr, OpExpr,
2120 * WindowFunc, etc), pass that as "node", else pass NULL.
2121 *
2122 * In some usages root might be NULL, too.
2123 */
2124 void
2125 add_function_cost(PlannerInfo *root, Oid funcid, Node *node,
2126 QualCost *cost)
2127 {
2128 HeapTuple proctup;
2129 Form_pg_proc procform;
2130
2131 proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
2132 if (!HeapTupleIsValid(proctup))
2133 elog(ERROR, "cache lookup failed for function %u", funcid);
2134 procform = (Form_pg_proc) GETSTRUCT(proctup);
2135
2136 if (OidIsValid(procform->prosupport))
2137 {
2138 SupportRequestCost req;
2139 SupportRequestCost *sresult;
2140
2141 req.type = T_SupportRequestCost;
2142 req.root = root;
2143 req.funcid = funcid;
2144 req.node = node;
2145
2146 /* Initialize cost fields so that support function doesn't have to */
2147 req.startup = 0;
2148 req.per_tuple = 0;
2149
2150 sresult = (SupportRequestCost *)
2151 DatumGetPointer(OidFunctionCall1(procform->prosupport,
2152 PointerGetDatum(&req)));
2153
2154 if (sresult == &req)
2155 {
2156 /* Success, so accumulate support function's estimate into *cost */
2157 cost->startup += req.startup;
2158 cost->per_tuple += req.per_tuple;
2159 ReleaseSysCache(proctup);
2160 return;
2161 }
2162 }
2163
2164 /* No support function, or it failed, so rely on procost */
2165 cost->per_tuple += procform->procost * cpu_operator_cost;
2166
2167 ReleaseSysCache(proctup);
2168 }
2169
2170 /*
2171 * get_function_rows
2172 *
2173 * Get an estimate of the number of rows returned by a set-returning function.
2174 *
2175 * The funcid must always be supplied. In current usage, the calling node
2176 * will always be supplied, and will be either a FuncExpr or OpExpr.
2177 * But it's a good idea to not fail if it's NULL.
2178 *
2179 * In some usages root might be NULL, too.
2180 *
2181 * Note: this returns the unfiltered result of the support function, if any.
2182 * It's usually a good idea to apply clamp_row_est() to the result, but we
2183 * leave it to the caller to do so.
2184 */
2185 double
2186 get_function_rows(PlannerInfo *root, Oid funcid, Node *node)
2187 {
2188 HeapTuple proctup;
2189 Form_pg_proc procform;
2190 double result;
2191
2192 proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
2193 if (!HeapTupleIsValid(proctup))
2194 elog(ERROR, "cache lookup failed for function %u", funcid);
2195 procform = (Form_pg_proc) GETSTRUCT(proctup);
2196
2197 Assert(procform->proretset); /* else caller error */
2198
2199 if (OidIsValid(procform->prosupport))
2200 {
2201 SupportRequestRows req;
2202 SupportRequestRows *sresult;
2203
2204 req.type = T_SupportRequestRows;
2205 req.root = root;
2206 req.funcid = funcid;
2207 req.node = node;
2208
2209 req.rows = 0; /* just for sanity */
2210
2211 sresult = (SupportRequestRows *)
2212 DatumGetPointer(OidFunctionCall1(procform->prosupport,
2213 PointerGetDatum(&req)));
2214
2215 if (sresult == &req)
2216 {
2217 /* Success */
2218 ReleaseSysCache(proctup);
2219 return req.rows;
2220 }
2221 }
2222
2223 /* No support function, or it failed, so rely on prorows */
2224 result = procform->prorows;
2225
2226 ReleaseSysCache(proctup);
2227
2228 return result;
2229 }
2230
2231 /*
2232 * has_unique_index
2233 *
2234 * Detect whether there is a unique index on the specified attribute
2235 * of the specified relation, thus allowing us to conclude that all
2236 * the (non-null) values of the attribute are distinct.
2237 *
2238 * This function does not check the index's indimmediate property, which
2239 * means that uniqueness may transiently fail to hold intra-transaction.
2240 * That's appropriate when we are making statistical estimates, but beware
2241 * of using this for any correctness proofs.
2242 */
2243 bool
2244 has_unique_index(RelOptInfo *rel, AttrNumber attno)
2245 {
2246 ListCell *ilist;
2247
2248 foreach(ilist, rel->indexlist)
2249 {
2250 IndexOptInfo *index = (IndexOptInfo *) lfirst(ilist);
2251
2252 /*
2253 * Note: ignore partial indexes, since they don't allow us to conclude
2254 * that all attr values are distinct, *unless* they are marked predOK
2255 * which means we know the index's predicate is satisfied by the
2256 * query. We don't take any interest in expressional indexes either.
2257 * Also, a multicolumn unique index doesn't allow us to conclude that
2258 * just the specified attr is unique.
2259 */
2260 if (index->unique &&
2261 index->nkeycolumns == 1 &&
2262 index->indexkeys[0] == attno &&
2263 (index->indpred == NIL || index->predOK))
2264 return true;
2265 }
2266 return false;
2267 }
2268
2269
2270 /*
2271 * has_row_triggers
2272 *
2273 * Detect whether the specified relation has any row-level triggers for event.
2274 */
2275 bool
2276 has_row_triggers(PlannerInfo *root, Index rti, CmdType event)
2277 {
2278 RangeTblEntry *rte = planner_rt_fetch(rti, root);
2279 Relation relation;
2280 TriggerDesc *trigDesc;
2281 bool result = false;
2282
2283 /* Assume we already have adequate lock */
2284 relation = table_open(rte->relid, NoLock);
2285
2286 trigDesc = relation->trigdesc;
2287 switch (event)
2288 {
2289 case CMD_INSERT:
2290 if (trigDesc &&
2291 (trigDesc->trig_insert_after_row ||
2292 trigDesc->trig_insert_before_row))
2293 result = true;
2294 break;
2295 case CMD_UPDATE:
2296 if (trigDesc &&
2297 (trigDesc->trig_update_after_row ||
2298 trigDesc->trig_update_before_row))
2299 result = true;
2300 break;
2301 case CMD_DELETE:
2302 if (trigDesc &&
2303 (trigDesc->trig_delete_after_row ||
2304 trigDesc->trig_delete_before_row))
2305 result = true;
2306 break;
2307 /* There is no separate event for MERGE, only INSERT/UPDATE/DELETE */
2308 case CMD_MERGE:
2309 result = false;
2310 break;
2311 default:
2312 elog(ERROR, "unrecognized CmdType: %d", (int) event);
2313 break;
2314 }
2315
2316 table_close(relation, NoLock);
2317 return result;
2318 }
2319
2320 /*
2321 * has_transition_tables
2322 *
2323 * Detect whether the specified relation has any transition tables for event.
2324 */
2325 bool
2326 has_transition_tables(PlannerInfo *root, Index rti, CmdType event)
2327 {
2328 RangeTblEntry *rte = planner_rt_fetch(rti, root);
2329 Relation relation;
2330 TriggerDesc *trigDesc;
2331 bool result = false;
2332
2333 Assert(rte->rtekind == RTE_RELATION);
2334
2335 /* Currently foreign tables cannot have transition tables */
2336 if (rte->relkind == RELKIND_FOREIGN_TABLE)
2337 return result;
2338
2339 /* Assume we already have adequate lock */
2340 relation = table_open(rte->relid, NoLock);
2341
2342 trigDesc = relation->trigdesc;
2343 switch (event)
2344 {
2345 case CMD_INSERT:
2346 if (trigDesc &&
2347 trigDesc->trig_insert_new_table)
2348 result = true;
2349 break;
2350 case CMD_UPDATE:
2351 if (trigDesc &&
2352 (trigDesc->trig_update_old_table ||
2353 trigDesc->trig_update_new_table))
2354 result = true;
2355 break;
2356 case CMD_DELETE:
2357 if (trigDesc &&
2358 trigDesc->trig_delete_old_table)
2359 result = true;
2360 break;
2361 /* There is no separate event for MERGE, only INSERT/UPDATE/DELETE */
2362 case CMD_MERGE:
2363 result = false;
2364 break;
2365 default:
2366 elog(ERROR, "unrecognized CmdType: %d", (int) event);
2367 break;
2368 }
2369
2370 table_close(relation, NoLock);
2371 return result;
2372 }
2373
2374 /*
2375 * has_stored_generated_columns
2376 *
2377 * Does table identified by RTI have any STORED GENERATED columns?
2378 */
2379 bool
2380 has_stored_generated_columns(PlannerInfo *root, Index rti)
2381 {
2382 RangeTblEntry *rte = planner_rt_fetch(rti, root);
2383 Relation relation;
2384 TupleDesc tupdesc;
2385 bool result = false;
2386
2387 /* Assume we already have adequate lock */
2388 relation = table_open(rte->relid, NoLock);
2389
2390 tupdesc = RelationGetDescr(relation);
2391 result = tupdesc->constr && tupdesc->constr->has_generated_stored;
2392
2393 table_close(relation, NoLock);
2394
2395 return result;
2396 }
2397
2398 /*
2399 * get_dependent_generated_columns
2400 *
2401 * Get the column numbers of any STORED GENERATED columns of the relation
2402 * that depend on any column listed in target_cols. Both the input and
2403 * result bitmapsets contain column numbers offset by
2404 * FirstLowInvalidHeapAttributeNumber.
2405 */
2406 Bitmapset *
2407 get_dependent_generated_columns(PlannerInfo *root, Index rti,
2408 Bitmapset *target_cols)
2409 {
2410 Bitmapset *dependentCols = NULL;
2411 RangeTblEntry *rte = planner_rt_fetch(rti, root);
2412 Relation relation;
2413 TupleDesc tupdesc;
2414 TupleConstr *constr;
2415
2416 /* Assume we already have adequate lock */
2417 relation = table_open(rte->relid, NoLock);
2418
2419 tupdesc = RelationGetDescr(relation);
2420 constr = tupdesc->constr;
2421
2422 if (constr && constr->has_generated_stored)
2423 {
2424 for (int i = 0; i < constr->num_defval; i++)
2425 {
2426 AttrDefault *defval = &constr->defval[i];
2427 Node *expr;
2428 Bitmapset *attrs_used = NULL;
2429
2430 /* skip if not generated column */
2431 if (!TupleDescAttr(tupdesc, defval->adnum - 1)->attgenerated)
2432 continue;
2433
2434 /* identify columns this generated column depends on */
2435 expr = stringToNode(defval->adbin);
2436 pull_varattnos(expr, 1, &attrs_used);
2437
2438 if (bms_overlap(target_cols, attrs_used))
2439 dependentCols = bms_add_member(dependentCols,
2440 defval->adnum - FirstLowInvalidHeapAttributeNumber);
2441 }
2442 }
2443
2444 table_close(relation, NoLock);
2445
2446 return dependentCols;
2447 }
2448
2449 /*
2450 * set_relation_partition_info
2451 *
2452 * Set partitioning scheme and related information for a partitioned table.
2453 */
2454 static void
2455 set_relation_partition_info(PlannerInfo *root, RelOptInfo *rel,
2456 Relation relation)
2457 {
2458 PartitionDesc partdesc;
2459
2460 /*
2461 * Create the PartitionDirectory infrastructure if we didn't already.
2462 */
2463 if (root->glob->partition_directory == NULL)
2464 {
2465 root->glob->partition_directory =
2466 CreatePartitionDirectory(CurrentMemoryContext, true);
2467 }
2468
2469 partdesc = PartitionDirectoryLookup(root->glob->partition_directory,
2470 relation);
2471 rel->part_scheme = find_partition_scheme(root, relation);
2472 Assert(partdesc != NULL && rel->part_scheme != NULL);
2473 rel->boundinfo = partdesc->boundinfo;
2474 rel->nparts = partdesc->nparts;
2475 set_baserel_partition_key_exprs(relation, rel);
2476 set_baserel_partition_constraint(relation, rel);
2477 }
2478
2479 /*
2480 * find_partition_scheme
2481 *
2482 * Find or create a PartitionScheme for this Relation.
2483 */
2484 static PartitionScheme
2485 find_partition_scheme(PlannerInfo *root, Relation relation)
2486 {
2487 PartitionKey partkey = RelationGetPartitionKey(relation);
2488 ListCell *lc;
2489 int partnatts,
2490 i;
2491 PartitionScheme part_scheme;
2492
2493 /* A partitioned table should have a partition key. */
2494 Assert(partkey != NULL);
2495
2496 partnatts = partkey->partnatts;
2497
2498 /* Search for a matching partition scheme and return if found one. */
2499 foreach(lc, root->part_schemes)
2500 {
2501 part_scheme = lfirst(lc);
2502
2503 /* Match partitioning strategy and number of keys. */
2504 if (partkey->strategy != part_scheme->strategy ||
2505 partnatts != part_scheme->partnatts)
2506 continue;
2507
2508 /* Match partition key type properties. */
2509 if (memcmp(partkey->partopfamily, part_scheme->partopfamily,
2510 sizeof(Oid) * partnatts) != 0 ||
2511 memcmp(partkey->partopcintype, part_scheme->partopcintype,
2512 sizeof(Oid) * partnatts) != 0 ||
2513 memcmp(partkey->partcollation, part_scheme->partcollation,
2514 sizeof(Oid) * partnatts) != 0)
2515 continue;
2516
2517 /*
2518 * Length and byval information should match when partopcintype
2519 * matches.
2520 */
2521 Assert(memcmp(partkey->parttyplen, part_scheme->parttyplen,
2522 sizeof(int16) * partnatts) == 0);
2523 Assert(memcmp(partkey->parttypbyval, part_scheme->parttypbyval,
2524 sizeof(bool) * partnatts) == 0);
2525
2526 /*
2527 * If partopfamily and partopcintype matched, must have the same
2528 * partition comparison functions. Note that we cannot reliably
2529 * Assert the equality of function structs themselves for they might
2530 * be different across PartitionKey's, so just Assert for the function
2531 * OIDs.
2532 */
2533 #ifdef USE_ASSERT_CHECKING
2534 for (i = 0; i < partkey->partnatts; i++)
2535 Assert(partkey->partsupfunc[i].fn_oid ==
2536 part_scheme->partsupfunc[i].fn_oid);
2537 #endif
2538
2539 /* Found matching partition scheme. */
2540 return part_scheme;
2541 }
2542
2543 /*
2544 * Did not find matching partition scheme. Create one copying relevant
2545 * information from the relcache. We need to copy the contents of the
2546 * array since the relcache entry may not survive after we have closed the
2547 * relation.
2548 */
2549 part_scheme = (PartitionScheme) palloc0(sizeof(PartitionSchemeData));
2550 part_scheme->strategy = partkey->strategy;
2551 part_scheme->partnatts = partkey->partnatts;
2552
2553 part_scheme->partopfamily = (Oid *) palloc(sizeof(Oid) * partnatts);
2554 memcpy(part_scheme->partopfamily, partkey->partopfamily,
2555 sizeof(Oid) * partnatts);
2556
2557 part_scheme->partopcintype = (Oid *) palloc(sizeof(Oid) * partnatts);
2558 memcpy(part_scheme->partopcintype, partkey->partopcintype,
2559 sizeof(Oid) * partnatts);
2560
2561 part_scheme->partcollation = (Oid *) palloc(sizeof(Oid) * partnatts);
2562 memcpy(part_scheme->partcollation, partkey->partcollation,
2563 sizeof(Oid) * partnatts);
2564
2565 part_scheme->parttyplen = (int16 *) palloc(sizeof(int16) * partnatts);
2566 memcpy(part_scheme->parttyplen, partkey->parttyplen,
2567 sizeof(int16) * partnatts);
2568
2569 part_scheme->parttypbyval = (bool *) palloc(sizeof(bool) * partnatts);
2570 memcpy(part_scheme->parttypbyval, partkey->parttypbyval,
2571 sizeof(bool) * partnatts);
2572
2573 part_scheme->partsupfunc = (FmgrInfo *)
2574 palloc(sizeof(FmgrInfo) * partnatts);
2575 for (i = 0; i < partnatts; i++)
2576 fmgr_info_copy(&part_scheme->partsupfunc[i], &partkey->partsupfunc[i],
2577 CurrentMemoryContext);
2578
2579 /* Add the partitioning scheme to PlannerInfo. */
2580 root->part_schemes = lappend(root->part_schemes, part_scheme);
2581
2582 return part_scheme;
2583 }
2584
2585 /*
2586 * set_baserel_partition_key_exprs
2587 *
2588 * Builds partition key expressions for the given base relation and fills
2589 * rel->partexprs.
2590 */
2591 static void
2592 set_baserel_partition_key_exprs(Relation relation,
2593 RelOptInfo *rel)
2594 {
2595 PartitionKey partkey = RelationGetPartitionKey(relation);
2596 int partnatts;
2597 int cnt;
2598 List **partexprs;
2599 ListCell *lc;
2600 Index varno = rel->relid;
2601
2602 Assert(IS_SIMPLE_REL(rel) && rel->relid > 0);
2603
2604 /* A partitioned table should have a partition key. */
2605 Assert(partkey != NULL);
2606
2607 partnatts = partkey->partnatts;
2608 partexprs = (List **) palloc(sizeof(List *) * partnatts);
2609 lc = list_head(partkey->partexprs);
2610
2611 for (cnt = 0; cnt < partnatts; cnt++)
2612 {
2613 Expr *partexpr;
2614 AttrNumber attno = partkey->partattrs[cnt];
2615
2616 if (attno != InvalidAttrNumber)
2617 {
2618 /* Single column partition key is stored as a Var node. */
2619 Assert(attno > 0);
2620
2621 partexpr = (Expr *) makeVar(varno, attno,
2622 partkey->parttypid[cnt],
2623 partkey->parttypmod[cnt],
2624 partkey->parttypcoll[cnt], 0);
2625 }
2626 else
2627 {
2628 if (lc == NULL)
2629 elog(ERROR, "wrong number of partition key expressions");
2630
2631 /* Re-stamp the expression with given varno. */
2632 partexpr = (Expr *) copyObject(lfirst(lc));
2633 ChangeVarNodes((Node *) partexpr, 1, varno, 0);
2634 lc = lnext(partkey->partexprs, lc);
2635 }
2636
2637 /* Base relations have a single expression per key. */
2638 partexprs[cnt] = list_make1(partexpr);
2639 }
2640
2641 rel->partexprs = partexprs;
2642
2643 /*
2644 * A base relation does not have nullable partition key expressions, since
2645 * no outer join is involved. We still allocate an array of empty
2646 * expression lists to keep partition key expression handling code simple.
2647 * See build_joinrel_partition_info() and match_expr_to_partition_keys().
2648 */
2649 rel->nullable_partexprs = (List **) palloc0(sizeof(List *) * partnatts);
2650 }
2651
2652 /*
2653 * set_baserel_partition_constraint
2654 *
2655 * Builds the partition constraint for the given base relation and sets it
2656 * in the given RelOptInfo. All Var nodes are restamped with the relid of the
2657 * given relation.
2658 */
2659 static void
2660 set_baserel_partition_constraint(Relation relation, RelOptInfo *rel)
2661 {
2662 List *partconstr;
2663
2664 if (rel->partition_qual) /* already done */
2665 return;
2666
2667 /*
2668 * Run the partition quals through const-simplification similar to check
2669 * constraints. We skip canonicalize_qual, though, because partition
2670 * quals should be in canonical form already; also, since the qual is in
2671 * implicit-AND format, we'd have to explicitly convert it to explicit-AND
2672 * format and back again.
2673 */
2674 partconstr = RelationGetPartitionQual(relation);
2675 if (partconstr)
2676 {
2677 partconstr = (List *) expression_planner((Expr *) partconstr);
2678 if (rel->relid != 1)
2679 ChangeVarNodes((Node *) partconstr, 1, rel->relid, 0);
2680 rel->partition_qual = partconstr;
2681 }
2682 }