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