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 472832 : get_relation_info(PlannerInfo *root, Oid relationObjectId, bool inhparent,
123 : RelOptInfo *rel)
124 : {
125 472832 : Index varno = rel->relid;
126 : Relation relation;
127 : bool hasindex;
128 472832 : 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 472832 : 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 472832 : if (!relation->rd_tableam)
145 : {
146 19462 : if (!(relation->rd_rel->relkind == RELKIND_FOREIGN_TABLE ||
147 16986 : 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 472832 : 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 472832 : rel->min_attr = FirstLowInvalidHeapAttributeNumber + 1;
162 472832 : rel->max_attr = RelationGetNumberOfAttributes(relation);
163 472832 : rel->reltablespace = RelationGetForm(relation)->reltablespace;
164 :
165 : Assert(rel->max_attr >= rel->min_attr);
166 472832 : rel->attr_needed = (Relids *)
167 472832 : palloc0((rel->max_attr - rel->min_attr + 1) * sizeof(Relids));
168 472832 : rel->attr_widths = (int32 *)
169 472832 : 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 472832 : if (!inhparent || relation->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
181 434216 : 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 472832 : if (!inhparent)
189 417270 : estimate_rel_size(relation, rel->attr_widths - rel->min_attr,
190 417270 : &rel->pages, &rel->tuples, &rel->allvisfrac);
191 :
192 : /* Retrieve the parallel_workers reloption, or -1 if not set. */
193 472832 : 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 472832 : if ((inhparent && relation->rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
204 434216 : || (IgnoreSystemIndexes && IsSystemRelation(relation)))
205 38616 : hasindex = false;
206 : else
207 434216 : hasindex = relation->rd_rel->relhasindex;
208 :
209 472832 : if (hasindex)
210 : {
211 : List *indexoidlist;
212 : LOCKMODE lmode;
213 : ListCell *l;
214 :
215 351844 : 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 351844 : lmode = root->simple_rte_array[varno]->rellockmode;
226 :
227 1099666 : foreach(l, indexoidlist)
228 : {
229 747822 : Oid indexoid = lfirst_oid(l);
230 : Relation indexRelation;
231 : Form_pg_index index;
232 747822 : 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 747822 : indexRelation = index_open(indexoid, lmode);
242 747822 : 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 747822 : 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 747800 : 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 747506 : info = makeNode(IndexOptInfo);
272 :
273 747506 : info->indexoid = index->indexrelid;
274 747506 : info->reltablespace =
275 747506 : RelationGetForm(indexRelation)->reltablespace;
276 747506 : info->rel = rel;
277 747506 : info->ncolumns = ncolumns = index->indnatts;
278 747506 : info->nkeycolumns = nkeycolumns = index->indnkeyatts;
279 :
280 747506 : info->indexkeys = (int *) palloc(sizeof(int) * ncolumns);
281 747506 : info->indexcollations = (Oid *) palloc(sizeof(Oid) * nkeycolumns);
282 747506 : info->opfamily = (Oid *) palloc(sizeof(Oid) * nkeycolumns);
283 747506 : info->opcintype = (Oid *) palloc(sizeof(Oid) * nkeycolumns);
284 747506 : info->canreturn = (bool *) palloc(sizeof(bool) * ncolumns);
285 :
286 2155644 : for (i = 0; i < ncolumns; i++)
287 : {
288 1408138 : info->indexkeys[i] = index->indkey.values[i];
289 1408138 : info->canreturn[i] = index_can_return(indexRelation, i + 1);
290 : }
291 :
292 2155226 : for (i = 0; i < nkeycolumns; i++)
293 : {
294 1407720 : info->opfamily[i] = indexRelation->rd_opfamily[i];
295 1407720 : info->opcintype[i] = indexRelation->rd_opcintype[i];
296 1407720 : info->indexcollations[i] = indexRelation->rd_indcollation[i];
297 : }
298 :
299 747506 : 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 747506 : if (indexRelation->rd_rel->relkind != RELKIND_PARTITIONED_INDEX)
306 : {
307 : /* We copy just the fields we need, not all of rd_indam */
308 740830 : amroutine = indexRelation->rd_indam;
309 740830 : info->amcanorderbyop = amroutine->amcanorderbyop;
310 740830 : info->amoptionalkey = amroutine->amoptionalkey;
311 740830 : info->amsearcharray = amroutine->amsearcharray;
312 740830 : info->amsearchnulls = amroutine->amsearchnulls;
313 740830 : info->amcanparallel = amroutine->amcanparallel;
314 740830 : info->amhasgettuple = (amroutine->amgettuple != NULL);
315 1481660 : info->amhasgetbitmap = amroutine->amgetbitmap != NULL &&
316 740830 : relation->rd_tableam->scan_bitmap_next_tuple != NULL;
317 1459706 : info->amcanmarkpos = (amroutine->ammarkpos != NULL &&
318 718876 : amroutine->amrestrpos != NULL);
319 740830 : info->amcostestimate = amroutine->amcostestimate;
320 : Assert(info->amcostestimate != NULL);
321 :
322 : /* Fetch index opclass options */
323 740830 : info->opclassoptions = RelationGetIndexAttOptions(indexRelation, true);
324 :
325 : /*
326 : * Fetch the ordering information for the index, if any.
327 : */
328 740830 : 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 718876 : info->sortopfamily = info->opfamily;
337 718876 : info->reverse_sort = (bool *) palloc(sizeof(bool) * nkeycolumns);
338 718876 : info->nulls_first = (bool *) palloc(sizeof(bool) * nkeycolumns);
339 :
340 1837240 : for (i = 0; i < nkeycolumns; i++)
341 : {
342 1118364 : int16 opt = indexRelation->rd_indoption[i];
343 :
344 1118364 : info->reverse_sort[i] = (opt & INDOPTION_DESC) != 0;
345 1118364 : info->nulls_first[i] = (opt & INDOPTION_NULLS_FIRST) != 0;
346 : }
347 : }
348 21954 : 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 21954 : info->sortopfamily = NULL;
404 21954 : info->reverse_sort = NULL;
405 21954 : info->nulls_first = NULL;
406 : }
407 : }
408 : else
409 : {
410 6676 : info->amcanorderbyop = false;
411 6676 : info->amoptionalkey = false;
412 6676 : info->amsearcharray = false;
413 6676 : info->amsearchnulls = false;
414 6676 : info->amcanparallel = false;
415 6676 : info->amhasgettuple = false;
416 6676 : info->amhasgetbitmap = false;
417 6676 : info->amcanmarkpos = false;
418 6676 : info->amcostestimate = NULL;
419 :
420 6676 : info->sortopfamily = NULL;
421 6676 : info->reverse_sort = NULL;
422 6676 : 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 747506 : info->indexprs = RelationGetIndexExpressions(indexRelation);
432 747506 : info->indpred = RelationGetIndexPredicate(indexRelation);
433 747506 : if (info->indexprs && varno != 1)
434 1938 : ChangeVarNodes((Node *) info->indexprs, 1, varno, 0);
435 747506 : if (info->indpred && varno != 1)
436 126 : ChangeVarNodes((Node *) info->indpred, 1, varno, 0);
437 :
438 : /* Build targetlist using the completed indexprs data */
439 747506 : info->indextlist = build_index_tlist(root, info, relation);
440 :
441 747506 : info->indrestrictinfo = NIL; /* set later, in indxpath.c */
442 747506 : info->predOK = false; /* set later, in indxpath.c */
443 747506 : info->unique = index->indisunique;
444 747506 : info->nullsnotdistinct = index->indnullsnotdistinct;
445 747506 : info->immediate = index->indimmediate;
446 747506 : 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 747506 : if (indexRelation->rd_rel->relkind != RELKIND_PARTITIONED_INDEX)
457 : {
458 740830 : if (info->indpred == NIL)
459 : {
460 739846 : info->pages = RelationGetNumberOfBlocks(indexRelation);
461 739846 : 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 740830 : if (amroutine->amgettreeheight)
477 : {
478 718876 : info->tree_height = amroutine->amgettreeheight(indexRelation);
479 : }
480 : else
481 : {
482 : /* For other index types, just set it to "unknown" for now */
483 21954 : info->tree_height = -1;
484 : }
485 : }
486 : else
487 : {
488 : /* Zero these out for partitioned indexes */
489 6676 : info->pages = 0;
490 6676 : info->tuples = 0.0;
491 6676 : info->tree_height = -1;
492 : }
493 :
494 747506 : 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 747506 : indexinfos = lcons(info, indexinfos);
504 : }
505 :
506 351844 : list_free(indexoidlist);
507 : }
508 :
509 472832 : rel->indexlist = indexinfos;
510 :
511 472832 : rel->statlist = get_relation_statistics(rel, relation);
512 :
513 : /* Grab foreign-table info using the relcache, while we have it */
514 472832 : if (relation->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
515 : {
516 : /* Check if the access to foreign tables is restricted */
517 2476 : 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 2472 : rel->serverid = GetForeignServerIdByRelId(RelationGetRelid(relation));
528 2472 : rel->fdwroutine = GetFdwRoutineForRelation(relation, true);
529 : }
530 : else
531 : {
532 470356 : rel->serverid = InvalidOid;
533 470356 : rel->fdwroutine = NULL;
534 : }
535 :
536 : /* Collect info about relation's foreign keys, if relevant */
537 472814 : get_relation_foreign_keys(root, rel, relation, inhparent);
538 :
539 : /* Collect info about functions implemented by the rel's table AM. */
540 472814 : if (relation->rd_tableam &&
541 453370 : relation->rd_tableam->scan_set_tidrange != NULL &&
542 453370 : relation->rd_tableam->scan_getnextslot_tidrange != NULL)
543 453370 : 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 472814 : if (inhparent && relation->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
550 16946 : set_relation_partition_info(root, rel, relation);
551 :
552 472814 : 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 472814 : if (get_relation_info_hook)
560 0 : (*get_relation_info_hook) (root, relationObjectId, inhparent, rel);
561 472814 : }
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 472814 : get_relation_foreign_keys(PlannerInfo *root, RelOptInfo *rel,
575 : Relation relation, bool inhparent)
576 : {
577 472814 : 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 904026 : if (rel->reloptkind != RELOPT_BASEREL ||
587 431212 : list_length(rtable) < 2)
588 248118 : 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 224696 : if (inhparent)
597 5548 : 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 219148 : 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 221824 : foreach(lc, cachedfkeys)
619 : {
620 2676 : 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 2676 : if (!cachedfk->conenforced)
629 18 : continue;
630 :
631 : /* Scan to find other RTEs matching confrelid */
632 2658 : rti = 0;
633 11724 : foreach(lc2, rtable)
634 : {
635 9066 : RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc2);
636 : ForeignKeyOptInfo *info;
637 :
638 9066 : rti++;
639 : /* Ignore if not the correct table */
640 9066 : if (rte->rtekind != RTE_RELATION ||
641 5658 : rte->relid != cachedfk->confrelid)
642 6824 : continue;
643 : /* Ignore if it's an inheritance parent; doesn't really match */
644 2242 : if (rte->inh)
645 280 : continue;
646 : /* Ignore self-referential FKs; we only care about joins */
647 1962 : if (rti == rel->relid)
648 132 : continue;
649 :
650 : /* OK, let's make an entry */
651 1830 : info = makeNode(ForeignKeyOptInfo);
652 1830 : info->con_relid = rel->relid;
653 1830 : info->ref_relid = rti;
654 1830 : info->nkeys = cachedfk->nkeys;
655 1830 : memcpy(info->conkey, cachedfk->conkey, sizeof(info->conkey));
656 1830 : memcpy(info->confkey, cachedfk->confkey, sizeof(info->confkey));
657 1830 : memcpy(info->conpfeqop, cachedfk->conpfeqop, sizeof(info->conpfeqop));
658 : /* zero out fields to be filled by match_foreign_keys_to_quals */
659 1830 : info->nmatched_ec = 0;
660 1830 : info->nconst_ec = 0;
661 1830 : info->nmatched_rcols = 0;
662 1830 : info->nmatched_ri = 0;
663 1830 : memset(info->eclass, 0, sizeof(info->eclass));
664 1830 : memset(info->fk_eclass_member, 0, sizeof(info->fk_eclass_member));
665 1830 : memset(info->rinfos, 0, sizeof(info->rinfos));
666 :
667 1830 : 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 508840 : get_relation_notnullatts(PlannerInfo *root, Relation relation)
681 : {
682 508840 : Oid relid = RelationGetRelid(relation);
683 : NotnullHashEntry *hentry;
684 : bool found;
685 508840 : Relids notnullattnums = NULL;
686 :
687 : /* bail out if the relation has no not-null constraints */
688 508840 : if (relation->rd_att->constr == NULL ||
689 341586 : !relation->rd_att->constr->has_not_null)
690 205988 : return;
691 :
692 : /* create the hash table if it hasn't been created yet */
693 336264 : if (root->glob->rel_notnullatts_hash == NULL)
694 : {
695 : HTAB *hashtab;
696 : HASHCTL hash_ctl;
697 :
698 176220 : hash_ctl.keysize = sizeof(Oid);
699 176220 : hash_ctl.entrysize = sizeof(NotnullHashEntry);
700 176220 : hash_ctl.hcxt = CurrentMemoryContext;
701 :
702 176220 : hashtab = hash_create("Relation NOT NULL attnums",
703 : 64L, /* arbitrary initial size */
704 : &hash_ctl,
705 : HASH_ELEM | HASH_BLOBS | HASH_CONTEXT);
706 :
707 176220 : 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 336264 : 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 336264 : if (found)
721 33412 : return;
722 :
723 : /* collect the column not-null constraint information for this relation */
724 4491304 : for (int i = 0; i < relation->rd_att->natts; i++)
725 : {
726 4188452 : CompactAttribute *attr = TupleDescCompactAttr(relation->rd_att, i);
727 :
728 : Assert(attr->attnullability != ATTNULLABLE_UNKNOWN);
729 :
730 4188452 : if (attr->attnullability == ATTNULLABLE_VALID)
731 : {
732 3544580 : 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 302852 : 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 448614 : find_relation_notnullatts(PlannerInfo *root, Oid relid)
754 : {
755 : NotnullHashEntry *hentry;
756 : bool found;
757 :
758 448614 : if (root->glob->rel_notnullatts_hash == NULL)
759 116876 : return NULL;
760 :
761 331738 : hentry = (NotnullHashEntry *) hash_search(root->glob->rel_notnullatts_hash,
762 : &relid,
763 : HASH_FIND,
764 : &found);
765 331738 : if (!found)
766 5006 : return NULL;
767 :
768 326732 : 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 1812 : infer_arbiter_indexes(PlannerInfo *root)
793 : {
794 1812 : OnConflictExpr *onconflict = root->parse->onConflict;
795 :
796 : /* Iteration state */
797 : Index varno;
798 : RangeTblEntry *rte;
799 : Relation relation;
800 1812 : Oid indexOidFromConstraint = InvalidOid;
801 : List *indexList;
802 : ListCell *l;
803 :
804 : /* Normalized inference attributes and inference expressions: */
805 1812 : Bitmapset *inferAttrs = NULL;
806 1812 : List *inferElems = NIL;
807 :
808 : /* Results */
809 1812 : 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 1812 : 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 1596 : varno = root->parse->resultRelation;
827 1596 : rte = rt_fetch(varno, root->parse->rtable);
828 :
829 1596 : 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 3454 : foreach(l, onconflict->arbiterElems)
837 : {
838 1858 : InferenceElem *elem = (InferenceElem *) lfirst(l);
839 : Var *var;
840 : int attno;
841 :
842 1858 : 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 1684 : var = (Var *) elem->expr;
850 1684 : attno = var->varattno;
851 :
852 1684 : 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 1684 : 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 1596 : 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 1596 : indexList = RelationGetIndexList(relation);
880 :
881 3428 : foreach(l, indexList)
882 : {
883 2024 : 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 2024 : idxRel = index_open(indexoid, rte->rellockmode);
901 2024 : idxForm = idxRel->rd_index;
902 :
903 2024 : 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 2018 : 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 1826 : 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 1808 : 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 1804 : if (idxForm->indisexclusion)
949 144 : goto next;
950 :
951 : /* Build BMS representation of plain (non expression) index attrs */
952 1660 : indexedAttrs = NULL;
953 3884 : for (natt = 0; natt < idxForm->indnkeyatts; natt++)
954 : {
955 2224 : int attno = idxRel->rd_index->indkey.values[natt];
956 :
957 2224 : if (attno != 0)
958 1912 : indexedAttrs = bms_add_member(indexedAttrs,
959 : attno - FirstLowInvalidHeapAttributeNumber);
960 : }
961 :
962 : /* Non-expression attributes (if any) must match */
963 1660 : if (!bms_equal(indexedAttrs, inferAttrs))
964 378 : goto next;
965 :
966 : /* Expression attributes (if any) must match */
967 1282 : idxExprs = RelationGetIndexExpressions(idxRel);
968 1282 : if (idxExprs && varno != 1)
969 6 : ChangeVarNodes((Node *) idxExprs, 1, varno, 0);
970 :
971 2924 : foreach(el, onconflict->arbiterElems)
972 : {
973 1690 : 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 1690 : 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 1654 : if (IsA(elem->expr, Var))
991 1480 : 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 1234 : 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 1180 : predExprs = RelationGetIndexPredicate(idxRel);
1022 1180 : if (predExprs && varno != 1)
1023 6 : ChangeVarNodes((Node *) predExprs, 1, varno, 0);
1024 :
1025 1180 : if (!predicate_implied_by(predExprs, (List *) onconflict->arbiterWhere, false))
1026 36 : goto next;
1027 :
1028 1144 : results = lappend_oid(results, idxForm->indexrelid);
1029 1832 : next:
1030 1832 : index_close(idxRel, NoLock);
1031 : }
1032 :
1033 1404 : list_free(indexList);
1034 1404 : table_close(relation, NoLock);
1035 :
1036 1404 : 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 1090 : 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 1690 : infer_collation_opclass_match(InferenceElem *elem, Relation idxRel,
1073 : List *idxExprs)
1074 : {
1075 : AttrNumber natt;
1076 1690 : Oid inferopfamily = InvalidOid; /* OID of opclass opfamily */
1077 1690 : Oid inferopcinputtype = InvalidOid; /* OID of opclass input type */
1078 1690 : 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 1690 : if (elem->infercollid == InvalidOid && elem->inferopclass == InvalidOid)
1085 1576 : 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 452312 : 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 452312 : if (RELKIND_HAS_TABLE_AM(rel->rd_rel->relkind))
1164 : {
1165 448812 : table_relation_estimate_size(rel, attr_widths, pages, tuples,
1166 : allvisfrac);
1167 : }
1168 3500 : 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 2516 : *pages = rel->rd_rel->relpages;
1259 2516 : *tuples = rel->rd_rel->reltuples;
1260 2516 : *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 151122 : get_rel_data_width(Relation rel, int32 *attr_widths)
1280 : {
1281 151122 : int64 tuple_width = 0;
1282 : int i;
1283 :
1284 818144 : for (i = 1; i <= RelationGetNumberOfAttributes(rel); i++)
1285 : {
1286 667022 : Form_pg_attribute att = TupleDescAttr(rel->rd_att, i - 1);
1287 : int32 item_width;
1288 :
1289 667022 : if (att->attisdropped)
1290 2660 : continue;
1291 :
1292 : /* use previously cached data, if any */
1293 664362 : if (attr_widths != NULL && attr_widths[i] > 0)
1294 : {
1295 5776 : tuple_width += attr_widths[i];
1296 5776 : continue;
1297 : }
1298 :
1299 : /* This should match set_rel_width() in costsize.c */
1300 658586 : item_width = get_attavgwidth(RelationGetRelid(rel), i);
1301 658586 : if (item_width <= 0)
1302 : {
1303 656738 : item_width = get_typavgwidth(att->atttypid, att->atttypmod);
1304 : Assert(item_width > 0);
1305 : }
1306 658586 : if (attr_widths != NULL)
1307 580320 : attr_widths[i] = item_width;
1308 658586 : tuple_width += item_width;
1309 : }
1310 :
1311 151122 : 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 2482 : 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 2482 : relation = table_open(relid, NoLock);
1328 :
1329 2482 : result = get_rel_data_width(relation, attr_widths);
1330 :
1331 2482 : table_close(relation, NoLock);
1332 :
1333 2482 : 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 21002 : get_relation_constraints(PlannerInfo *root,
1363 : Oid relationObjectId, RelOptInfo *rel,
1364 : bool include_noinherit,
1365 : bool include_notnull,
1366 : bool include_partition)
1367 : {
1368 21002 : List *result = NIL;
1369 21002 : Index varno = rel->relid;
1370 : Relation relation;
1371 : TupleConstr *constr;
1372 :
1373 : /*
1374 : * We assume the relation has already been safely locked.
1375 : */
1376 21002 : relation = table_open(relationObjectId, NoLock);
1377 :
1378 21002 : constr = relation->rd_att->constr;
1379 21002 : if (constr != NULL)
1380 : {
1381 8008 : int num_check = constr->num_check;
1382 : int i;
1383 :
1384 8564 : 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 8008 : if (include_notnull && constr->has_not_null)
1437 : {
1438 7570 : int natts = relation->rd_att->natts;
1439 :
1440 30764 : for (i = 1; i <= natts; i++)
1441 : {
1442 23194 : CompactAttribute *att = TupleDescCompactAttr(relation->rd_att, i - 1);
1443 :
1444 23194 : if (att->attnullability == ATTNULLABLE_VALID && !att->attisdropped)
1445 : {
1446 9348 : Form_pg_attribute wholeatt = TupleDescAttr(relation->rd_att, i - 1);
1447 9348 : NullTest *ntest = makeNode(NullTest);
1448 :
1449 9348 : ntest->arg = (Expr *) makeVar(varno,
1450 : i,
1451 : wholeatt->atttypid,
1452 : wholeatt->atttypmod,
1453 : wholeatt->attcollation,
1454 : 0);
1455 9348 : 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 9348 : ntest->argisrow = false;
1463 9348 : ntest->location = -1;
1464 9348 : result = lappend(result, ntest);
1465 : }
1466 : }
1467 : }
1468 : }
1469 :
1470 : /*
1471 : * Add partitioning constraints, if requested.
1472 : */
1473 21002 : 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 21002 : table_close(relation, NoLock);
1481 :
1482 21002 : 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 3844 : 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 3844 : dtup = SearchSysCache2(STATEXTDATASTXOID,
1500 : ObjectIdGetDatum(statOid), BoolGetDatum(inh));
1501 3844 : if (!HeapTupleIsValid(dtup))
1502 1924 : return;
1503 :
1504 1920 : dataForm = (Form_pg_statistic_ext_data) GETSTRUCT(dtup);
1505 :
1506 : /* add one StatisticExtInfo for each kind built */
1507 1920 : 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 1920 : 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 1920 : if (statext_is_kind_built(dtup, STATS_EXT_MCV))
1536 : {
1537 816 : StatisticExtInfo *info = makeNode(StatisticExtInfo);
1538 :
1539 816 : info->statOid = statOid;
1540 816 : info->inherit = dataForm->stxdinherit;
1541 816 : info->rel = rel;
1542 816 : info->kind = STATS_EXT_MCV;
1543 816 : info->keys = bms_copy(keys);
1544 816 : info->exprs = exprs;
1545 :
1546 816 : *stainfos = lappend(*stainfos, info);
1547 : }
1548 :
1549 1920 : 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 1920 : 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 472832 : get_relation_statistics(RelOptInfo *rel, Relation relation)
1576 : {
1577 472832 : Index varno = rel->relid;
1578 : List *statoidlist;
1579 472832 : List *stainfos = NIL;
1580 : ListCell *l;
1581 :
1582 472832 : statoidlist = RelationGetStatExtList(relation);
1583 :
1584 474754 : foreach(l, statoidlist)
1585 : {
1586 1922 : Oid statOid = lfirst_oid(l);
1587 : Form_pg_statistic_ext staForm;
1588 : HeapTuple htup;
1589 1922 : Bitmapset *keys = NULL;
1590 1922 : List *exprs = NIL;
1591 : int i;
1592 :
1593 1922 : htup = SearchSysCache1(STATEXTOID, ObjectIdGetDatum(statOid));
1594 1922 : if (!HeapTupleIsValid(htup))
1595 0 : elog(ERROR, "cache lookup failed for statistics object %u", statOid);
1596 1922 : 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 5446 : for (i = 0; i < staForm->stxkeys.dim1; i++)
1604 3524 : 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 1922 : datum = SysCacheGetAttr(STATEXTOID, htup,
1621 : Anum_pg_statistic_ext_stxexprs, &isnull);
1622 :
1623 1922 : 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 1922 : get_relation_statistics_worker(&stainfos, rel, statOid, true, keys, exprs);
1657 :
1658 1922 : get_relation_statistics_worker(&stainfos, rel, statOid, false, keys, exprs);
1659 :
1660 1922 : ReleaseSysCache(htup);
1661 1922 : bms_free(keys);
1662 : }
1663 :
1664 472832 : list_free(statoidlist);
1665 :
1666 472832 : 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 512134 : relation_excluded_by_constraints(PlannerInfo *root,
1682 : RelOptInfo *rel, RangeTblEntry *rte)
1683 : {
1684 : bool include_noinherit;
1685 : bool include_notnull;
1686 512134 : 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 512134 : if (rel->baserestrictinfo == NIL)
1700 227386 : 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 711684 : foreach(lc, rel->baserestrictinfo)
1713 : {
1714 427420 : RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1715 427420 : Expr *clause = rinfo->clause;
1716 :
1717 427420 : if (clause && IsA(clause, Const) &&
1718 484 : (((Const *) clause)->constisnull ||
1719 478 : !DatumGetBool(((Const *) clause)->constvalue)))
1720 484 : return true;
1721 : }
1722 :
1723 : /*
1724 : * Skip further tests, depending on constraint_exclusion.
1725 : */
1726 284264 : 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 284090 : 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 284090 : if (rel->reloptkind == RELOPT_OTHER_MEMBER_REL)
1741 21240 : break; /* appendrel member, so process it */
1742 262850 : 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 21360 : safe_restrictions = NIL;
1767 50314 : foreach(lc, rel->baserestrictinfo)
1768 : {
1769 28954 : RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1770 :
1771 28954 : if (!contain_mutable_functions((Node *) rinfo->clause))
1772 27708 : 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 21360 : 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 21288 : 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 21002 : 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 21002 : include_notnull = (!rte->inh || rte->relkind == RELKIND_PARTITIONED_TABLE);
1804 :
1805 : /*
1806 : * Fetch the appropriate set of constraint expressions.
1807 : */
1808 21002 : 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 21002 : safe_constraints = NIL;
1821 31054 : foreach(lc, constraint_pred)
1822 : {
1823 10052 : Node *pred = (Node *) lfirst(lc);
1824 :
1825 10052 : if (!contain_mutable_functions(pred))
1826 10052 : 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 21002 : if (predicate_refuted_by(safe_constraints, rel->baserestrictinfo, false))
1843 168 : return true;
1844 :
1845 20834 : 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 185826 : build_physical_tlist(PlannerInfo *root, RelOptInfo *rel)
1872 : {
1873 185826 : List *tlist = NIL;
1874 185826 : Index varno = rel->relid;
1875 185826 : 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 185826 : switch (rte->rtekind)
1885 : {
1886 159218 : case RTE_RELATION:
1887 : /* Assume we already have adequate lock */
1888 159218 : relation = table_open(rte->relid, NoLock);
1889 :
1890 159218 : numattrs = RelationGetNumberOfAttributes(relation);
1891 2865394 : for (attrno = 1; attrno <= numattrs; attrno++)
1892 : {
1893 2706314 : Form_pg_attribute att_tup = TupleDescAttr(relation->rd_att,
1894 : attrno - 1);
1895 :
1896 2706314 : 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 2706176 : var = makeVar(varno,
1904 : attrno,
1905 : att_tup->atttypid,
1906 : att_tup->atttypmod,
1907 : att_tup->attcollation,
1908 : 0);
1909 :
1910 2706176 : tlist = lappend(tlist,
1911 2706176 : makeTargetEntry((Expr *) var,
1912 : attrno,
1913 : NULL,
1914 : false));
1915 : }
1916 :
1917 159218 : table_close(relation, NoLock);
1918 159218 : break;
1919 :
1920 2210 : case RTE_SUBQUERY:
1921 2210 : subquery = rte->subquery;
1922 8454 : foreach(l, subquery->targetList)
1923 : {
1924 6244 : 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 6244 : var = makeVarFromTargetEntry(varno, tle);
1931 :
1932 6244 : tlist = lappend(tlist,
1933 6244 : makeTargetEntry((Expr *) var,
1934 6244 : tle->resno,
1935 : NULL,
1936 6244 : tle->resjunk));
1937 : }
1938 2210 : break;
1939 :
1940 24398 : 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 24398 : expandRTE(rte, varno, 0, VAR_RETURNING_DEFAULT, -1,
1948 : true /* include dropped */ , NULL, &colvars);
1949 123376 : foreach(l, colvars)
1950 : {
1951 98978 : var = (Var *) lfirst(l);
1952 :
1953 : /*
1954 : * A non-Var in expandRTE's output means a dropped column;
1955 : * must punt.
1956 : */
1957 98978 : if (!IsA(var, Var))
1958 : {
1959 0 : tlist = NIL;
1960 0 : break;
1961 : }
1962 :
1963 98978 : tlist = lappend(tlist,
1964 98978 : makeTargetEntry((Expr *) var,
1965 98978 : var->varattno,
1966 : NULL,
1967 : false));
1968 : }
1969 24398 : 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 185826 : 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 747506 : build_index_tlist(PlannerInfo *root, IndexOptInfo *index,
1993 : Relation heapRelation)
1994 : {
1995 747506 : List *tlist = NIL;
1996 747506 : Index varno = index->rel->relid;
1997 : ListCell *indexpr_item;
1998 : int i;
1999 :
2000 747506 : indexpr_item = list_head(index->indexprs);
2001 2155644 : for (i = 0; i < index->ncolumns; i++)
2002 : {
2003 1408138 : int indexkey = index->indexkeys[i];
2004 : Expr *indexvar;
2005 :
2006 1408138 : if (indexkey != 0)
2007 : {
2008 : /* simple column */
2009 : const FormData_pg_attribute *att_tup;
2010 :
2011 1405122 : if (indexkey < 0)
2012 0 : att_tup = SystemAttributeDefinition(indexkey);
2013 : else
2014 1405122 : att_tup = TupleDescAttr(heapRelation->rd_att, indexkey - 1);
2015 :
2016 1405122 : indexvar = (Expr *) makeVar(varno,
2017 : indexkey,
2018 1405122 : att_tup->atttypid,
2019 1405122 : att_tup->atttypmod,
2020 1405122 : 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 1408138 : tlist = lappend(tlist,
2033 1408138 : makeTargetEntry(indexvar,
2034 1408138 : i + 1,
2035 : NULL,
2036 : false));
2037 : }
2038 747506 : if (indexpr_item != NULL)
2039 0 : elog(ERROR, "wrong number of index expressions");
2040 :
2041 747506 : 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 701344 : restriction_selectivity(PlannerInfo *root,
2055 : Oid operatorid,
2056 : List *args,
2057 : Oid inputcollid,
2058 : int varRelid)
2059 : {
2060 701344 : 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 701344 : if (!oprrest)
2068 160 : return (Selectivity) 0.5;
2069 :
2070 701184 : result = DatumGetFloat8(OidFunctionCall4Coll(oprrest,
2071 : inputcollid,
2072 : PointerGetDatum(root),
2073 : ObjectIdGetDatum(operatorid),
2074 : PointerGetDatum(args),
2075 : Int32GetDatum(varRelid)));
2076 :
2077 701154 : if (result < 0.0 || result > 1.0)
2078 0 : elog(ERROR, "invalid restriction selectivity: %f", result);
2079 :
2080 701154 : 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 237194 : join_selectivity(PlannerInfo *root,
2094 : Oid operatorid,
2095 : List *args,
2096 : Oid inputcollid,
2097 : JoinType jointype,
2098 : SpecialJoinInfo *sjinfo)
2099 : {
2100 237194 : 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 237194 : if (!oprjoin)
2108 146 : return (Selectivity) 0.5;
2109 :
2110 237048 : result = DatumGetFloat8(OidFunctionCall5Coll(oprjoin,
2111 : inputcollid,
2112 : PointerGetDatum(root),
2113 : ObjectIdGetDatum(operatorid),
2114 : PointerGetDatum(args),
2115 : Int16GetDatum(jointype),
2116 : PointerGetDatum(sjinfo)));
2117 :
2118 237048 : if (result < 0.0 || result > 1.0)
2119 0 : elog(ERROR, "invalid join selectivity: %f", result);
2120 :
2121 237048 : 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 12122 : 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 12122 : 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 12122 : if (!prosupport)
2155 12092 : 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 1150774 : add_function_cost(PlannerInfo *root, Oid funcid, Node *node,
2197 : QualCost *cost)
2198 : {
2199 : HeapTuple proctup;
2200 : Form_pg_proc procform;
2201 :
2202 1150774 : proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
2203 1150774 : if (!HeapTupleIsValid(proctup))
2204 0 : elog(ERROR, "cache lookup failed for function %u", funcid);
2205 1150774 : procform = (Form_pg_proc) GETSTRUCT(proctup);
2206 :
2207 1150774 : if (OidIsValid(procform->prosupport))
2208 : {
2209 : SupportRequestCost req;
2210 : SupportRequestCost *sresult;
2211 :
2212 36010 : req.type = T_SupportRequestCost;
2213 36010 : req.root = root;
2214 36010 : req.funcid = funcid;
2215 36010 : req.node = node;
2216 :
2217 : /* Initialize cost fields so that support function doesn't have to */
2218 36010 : req.startup = 0;
2219 36010 : req.per_tuple = 0;
2220 :
2221 : sresult = (SupportRequestCost *)
2222 36010 : DatumGetPointer(OidFunctionCall1(procform->prosupport,
2223 : PointerGetDatum(&req)));
2224 :
2225 36010 : 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 1150756 : cost->per_tuple += procform->procost * cpu_operator_cost;
2237 :
2238 1150756 : 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 56980 : get_function_rows(PlannerInfo *root, Oid funcid, Node *node)
2258 : {
2259 : HeapTuple proctup;
2260 : Form_pg_proc procform;
2261 : double result;
2262 :
2263 56980 : proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
2264 56980 : if (!HeapTupleIsValid(proctup))
2265 0 : elog(ERROR, "cache lookup failed for function %u", funcid);
2266 56980 : procform = (Form_pg_proc) GETSTRUCT(proctup);
2267 :
2268 : Assert(procform->proretset); /* else caller error */
2269 :
2270 56980 : if (OidIsValid(procform->prosupport))
2271 : {
2272 : SupportRequestRows req;
2273 : SupportRequestRows *sresult;
2274 :
2275 21926 : req.type = T_SupportRequestRows;
2276 21926 : req.root = root;
2277 21926 : req.funcid = funcid;
2278 21926 : req.node = node;
2279 :
2280 21926 : req.rows = 0; /* just for sanity */
2281 :
2282 : sresult = (SupportRequestRows *)
2283 21926 : DatumGetPointer(OidFunctionCall1(procform->prosupport,
2284 : PointerGetDatum(&req)));
2285 :
2286 21926 : if (sresult == &req)
2287 : {
2288 : /* Success */
2289 17786 : ReleaseSysCache(proctup);
2290 17786 : return req.rows;
2291 : }
2292 : }
2293 :
2294 : /* No support function, or it failed, so rely on prorows */
2295 39194 : result = procform->prorows;
2296 :
2297 39194 : ReleaseSysCache(proctup);
2298 :
2299 39194 : 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 2138876 : has_unique_index(RelOptInfo *rel, AttrNumber attno)
2316 : {
2317 : ListCell *ilist;
2318 :
2319 5434632 : foreach(ilist, rel->indexlist)
2320 : {
2321 3992374 : 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 3992374 : if (index->unique &&
2332 2718002 : index->nkeycolumns == 1 &&
2333 1491558 : index->indexkeys[0] == attno &&
2334 696654 : (index->indpred == NIL || index->predOK))
2335 696618 : return true;
2336 : }
2337 1442258 : 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 498 : has_row_triggers(PlannerInfo *root, Index rti, CmdType event)
2348 : {
2349 498 : RangeTblEntry *rte = planner_rt_fetch(rti, root);
2350 : Relation relation;
2351 : TriggerDesc *trigDesc;
2352 498 : bool result = false;
2353 :
2354 : /* Assume we already have adequate lock */
2355 498 : relation = table_open(rte->relid, NoLock);
2356 :
2357 498 : trigDesc = relation->trigdesc;
2358 498 : switch (event)
2359 : {
2360 162 : case CMD_INSERT:
2361 162 : if (trigDesc &&
2362 26 : (trigDesc->trig_insert_after_row ||
2363 14 : trigDesc->trig_insert_before_row))
2364 26 : result = true;
2365 162 : break;
2366 182 : case CMD_UPDATE:
2367 182 : if (trigDesc &&
2368 48 : (trigDesc->trig_update_after_row ||
2369 28 : trigDesc->trig_update_before_row))
2370 36 : result = true;
2371 182 : break;
2372 154 : case CMD_DELETE:
2373 154 : if (trigDesc &&
2374 30 : (trigDesc->trig_delete_after_row ||
2375 18 : trigDesc->trig_delete_before_row))
2376 16 : result = true;
2377 154 : 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 498 : table_close(relation, NoLock);
2388 498 : return result;
2389 : }
2390 :
2391 : /*
2392 : * has_stored_generated_columns
2393 : *
2394 : * Does table identified by RTI have any STORED GENERATED columns?
2395 : */
2396 : bool
2397 420 : has_stored_generated_columns(PlannerInfo *root, Index rti)
2398 : {
2399 420 : RangeTblEntry *rte = planner_rt_fetch(rti, root);
2400 : Relation relation;
2401 : TupleDesc tupdesc;
2402 420 : bool result = false;
2403 :
2404 : /* Assume we already have adequate lock */
2405 420 : relation = table_open(rte->relid, NoLock);
2406 :
2407 420 : tupdesc = RelationGetDescr(relation);
2408 420 : result = tupdesc->constr && tupdesc->constr->has_generated_stored;
2409 :
2410 420 : table_close(relation, NoLock);
2411 :
2412 420 : return result;
2413 : }
2414 :
2415 : /*
2416 : * get_dependent_generated_columns
2417 : *
2418 : * Get the column numbers of any STORED GENERATED columns of the relation
2419 : * that depend on any column listed in target_cols. Both the input and
2420 : * result bitmapsets contain column numbers offset by
2421 : * FirstLowInvalidHeapAttributeNumber.
2422 : */
2423 : Bitmapset *
2424 82 : get_dependent_generated_columns(PlannerInfo *root, Index rti,
2425 : Bitmapset *target_cols)
2426 : {
2427 82 : Bitmapset *dependentCols = NULL;
2428 82 : RangeTblEntry *rte = planner_rt_fetch(rti, root);
2429 : Relation relation;
2430 : TupleDesc tupdesc;
2431 : TupleConstr *constr;
2432 :
2433 : /* Assume we already have adequate lock */
2434 82 : relation = table_open(rte->relid, NoLock);
2435 :
2436 82 : tupdesc = RelationGetDescr(relation);
2437 82 : constr = tupdesc->constr;
2438 :
2439 82 : if (constr && constr->has_generated_stored)
2440 : {
2441 12 : for (int i = 0; i < constr->num_defval; i++)
2442 : {
2443 8 : AttrDefault *defval = &constr->defval[i];
2444 : Node *expr;
2445 8 : Bitmapset *attrs_used = NULL;
2446 :
2447 : /* skip if not generated column */
2448 8 : if (!TupleDescAttr(tupdesc, defval->adnum - 1)->attgenerated)
2449 0 : continue;
2450 :
2451 : /* identify columns this generated column depends on */
2452 8 : expr = stringToNode(defval->adbin);
2453 8 : pull_varattnos(expr, 1, &attrs_used);
2454 :
2455 8 : if (bms_overlap(target_cols, attrs_used))
2456 8 : dependentCols = bms_add_member(dependentCols,
2457 8 : defval->adnum - FirstLowInvalidHeapAttributeNumber);
2458 : }
2459 : }
2460 :
2461 82 : table_close(relation, NoLock);
2462 :
2463 82 : return dependentCols;
2464 : }
2465 :
2466 : /*
2467 : * set_relation_partition_info
2468 : *
2469 : * Set partitioning scheme and related information for a partitioned table.
2470 : */
2471 : static void
2472 16946 : set_relation_partition_info(PlannerInfo *root, RelOptInfo *rel,
2473 : Relation relation)
2474 : {
2475 : PartitionDesc partdesc;
2476 :
2477 : /*
2478 : * Create the PartitionDirectory infrastructure if we didn't already.
2479 : */
2480 16946 : if (root->glob->partition_directory == NULL)
2481 : {
2482 11556 : root->glob->partition_directory =
2483 11556 : CreatePartitionDirectory(CurrentMemoryContext, true);
2484 : }
2485 :
2486 16946 : partdesc = PartitionDirectoryLookup(root->glob->partition_directory,
2487 : relation);
2488 16946 : rel->part_scheme = find_partition_scheme(root, relation);
2489 : Assert(partdesc != NULL && rel->part_scheme != NULL);
2490 16946 : rel->boundinfo = partdesc->boundinfo;
2491 16946 : rel->nparts = partdesc->nparts;
2492 16946 : set_baserel_partition_key_exprs(relation, rel);
2493 16946 : set_baserel_partition_constraint(relation, rel);
2494 16946 : }
2495 :
2496 : /*
2497 : * find_partition_scheme
2498 : *
2499 : * Find or create a PartitionScheme for this Relation.
2500 : */
2501 : static PartitionScheme
2502 16946 : find_partition_scheme(PlannerInfo *root, Relation relation)
2503 : {
2504 16946 : PartitionKey partkey = RelationGetPartitionKey(relation);
2505 : ListCell *lc;
2506 : int partnatts,
2507 : i;
2508 : PartitionScheme part_scheme;
2509 :
2510 : /* A partitioned table should have a partition key. */
2511 : Assert(partkey != NULL);
2512 :
2513 16946 : partnatts = partkey->partnatts;
2514 :
2515 : /* Search for a matching partition scheme and return if found one. */
2516 18836 : foreach(lc, root->part_schemes)
2517 : {
2518 5982 : part_scheme = lfirst(lc);
2519 :
2520 : /* Match partitioning strategy and number of keys. */
2521 5982 : if (partkey->strategy != part_scheme->strategy ||
2522 4992 : partnatts != part_scheme->partnatts)
2523 1440 : continue;
2524 :
2525 : /* Match partition key type properties. */
2526 4542 : if (memcmp(partkey->partopfamily, part_scheme->partopfamily,
2527 4092 : sizeof(Oid) * partnatts) != 0 ||
2528 4092 : memcmp(partkey->partopcintype, part_scheme->partopcintype,
2529 4092 : sizeof(Oid) * partnatts) != 0 ||
2530 4092 : memcmp(partkey->partcollation, part_scheme->partcollation,
2531 : sizeof(Oid) * partnatts) != 0)
2532 450 : continue;
2533 :
2534 : /*
2535 : * Length and byval information should match when partopcintype
2536 : * matches.
2537 : */
2538 : Assert(memcmp(partkey->parttyplen, part_scheme->parttyplen,
2539 : sizeof(int16) * partnatts) == 0);
2540 : Assert(memcmp(partkey->parttypbyval, part_scheme->parttypbyval,
2541 : sizeof(bool) * partnatts) == 0);
2542 :
2543 : /*
2544 : * If partopfamily and partopcintype matched, must have the same
2545 : * partition comparison functions. Note that we cannot reliably
2546 : * Assert the equality of function structs themselves for they might
2547 : * be different across PartitionKey's, so just Assert for the function
2548 : * OIDs.
2549 : */
2550 : #ifdef USE_ASSERT_CHECKING
2551 : for (i = 0; i < partkey->partnatts; i++)
2552 : Assert(partkey->partsupfunc[i].fn_oid ==
2553 : part_scheme->partsupfunc[i].fn_oid);
2554 : #endif
2555 :
2556 : /* Found matching partition scheme. */
2557 4092 : return part_scheme;
2558 : }
2559 :
2560 : /*
2561 : * Did not find matching partition scheme. Create one copying relevant
2562 : * information from the relcache. We need to copy the contents of the
2563 : * array since the relcache entry may not survive after we have closed the
2564 : * relation.
2565 : */
2566 12854 : part_scheme = (PartitionScheme) palloc0(sizeof(PartitionSchemeData));
2567 12854 : part_scheme->strategy = partkey->strategy;
2568 12854 : part_scheme->partnatts = partkey->partnatts;
2569 :
2570 12854 : part_scheme->partopfamily = (Oid *) palloc(sizeof(Oid) * partnatts);
2571 12854 : memcpy(part_scheme->partopfamily, partkey->partopfamily,
2572 : sizeof(Oid) * partnatts);
2573 :
2574 12854 : part_scheme->partopcintype = (Oid *) palloc(sizeof(Oid) * partnatts);
2575 12854 : memcpy(part_scheme->partopcintype, partkey->partopcintype,
2576 : sizeof(Oid) * partnatts);
2577 :
2578 12854 : part_scheme->partcollation = (Oid *) palloc(sizeof(Oid) * partnatts);
2579 12854 : memcpy(part_scheme->partcollation, partkey->partcollation,
2580 : sizeof(Oid) * partnatts);
2581 :
2582 12854 : part_scheme->parttyplen = (int16 *) palloc(sizeof(int16) * partnatts);
2583 12854 : memcpy(part_scheme->parttyplen, partkey->parttyplen,
2584 : sizeof(int16) * partnatts);
2585 :
2586 12854 : part_scheme->parttypbyval = (bool *) palloc(sizeof(bool) * partnatts);
2587 12854 : memcpy(part_scheme->parttypbyval, partkey->parttypbyval,
2588 : sizeof(bool) * partnatts);
2589 :
2590 12854 : part_scheme->partsupfunc = (FmgrInfo *)
2591 12854 : palloc(sizeof(FmgrInfo) * partnatts);
2592 27556 : for (i = 0; i < partnatts; i++)
2593 14702 : fmgr_info_copy(&part_scheme->partsupfunc[i], &partkey->partsupfunc[i],
2594 : CurrentMemoryContext);
2595 :
2596 : /* Add the partitioning scheme to PlannerInfo. */
2597 12854 : root->part_schemes = lappend(root->part_schemes, part_scheme);
2598 :
2599 12854 : return part_scheme;
2600 : }
2601 :
2602 : /*
2603 : * set_baserel_partition_key_exprs
2604 : *
2605 : * Builds partition key expressions for the given base relation and fills
2606 : * rel->partexprs.
2607 : */
2608 : static void
2609 16946 : set_baserel_partition_key_exprs(Relation relation,
2610 : RelOptInfo *rel)
2611 : {
2612 16946 : PartitionKey partkey = RelationGetPartitionKey(relation);
2613 : int partnatts;
2614 : int cnt;
2615 : List **partexprs;
2616 : ListCell *lc;
2617 16946 : Index varno = rel->relid;
2618 :
2619 : Assert(IS_SIMPLE_REL(rel) && rel->relid > 0);
2620 :
2621 : /* A partitioned table should have a partition key. */
2622 : Assert(partkey != NULL);
2623 :
2624 16946 : partnatts = partkey->partnatts;
2625 16946 : partexprs = (List **) palloc(sizeof(List *) * partnatts);
2626 16946 : lc = list_head(partkey->partexprs);
2627 :
2628 35770 : for (cnt = 0; cnt < partnatts; cnt++)
2629 : {
2630 : Expr *partexpr;
2631 18824 : AttrNumber attno = partkey->partattrs[cnt];
2632 :
2633 18824 : if (attno != InvalidAttrNumber)
2634 : {
2635 : /* Single column partition key is stored as a Var node. */
2636 : Assert(attno > 0);
2637 :
2638 17894 : partexpr = (Expr *) makeVar(varno, attno,
2639 17894 : partkey->parttypid[cnt],
2640 17894 : partkey->parttypmod[cnt],
2641 17894 : partkey->parttypcoll[cnt], 0);
2642 : }
2643 : else
2644 : {
2645 930 : if (lc == NULL)
2646 0 : elog(ERROR, "wrong number of partition key expressions");
2647 :
2648 : /* Re-stamp the expression with given varno. */
2649 930 : partexpr = (Expr *) copyObject(lfirst(lc));
2650 930 : ChangeVarNodes((Node *) partexpr, 1, varno, 0);
2651 930 : lc = lnext(partkey->partexprs, lc);
2652 : }
2653 :
2654 : /* Base relations have a single expression per key. */
2655 18824 : partexprs[cnt] = list_make1(partexpr);
2656 : }
2657 :
2658 16946 : rel->partexprs = partexprs;
2659 :
2660 : /*
2661 : * A base relation does not have nullable partition key expressions, since
2662 : * no outer join is involved. We still allocate an array of empty
2663 : * expression lists to keep partition key expression handling code simple.
2664 : * See build_joinrel_partition_info() and match_expr_to_partition_keys().
2665 : */
2666 16946 : rel->nullable_partexprs = (List **) palloc0(sizeof(List *) * partnatts);
2667 16946 : }
2668 :
2669 : /*
2670 : * set_baserel_partition_constraint
2671 : *
2672 : * Builds the partition constraint for the given base relation and sets it
2673 : * in the given RelOptInfo. All Var nodes are restamped with the relid of the
2674 : * given relation.
2675 : */
2676 : static void
2677 16958 : set_baserel_partition_constraint(Relation relation, RelOptInfo *rel)
2678 : {
2679 : List *partconstr;
2680 :
2681 16958 : if (rel->partition_qual) /* already done */
2682 0 : return;
2683 :
2684 : /*
2685 : * Run the partition quals through const-simplification similar to check
2686 : * constraints. We skip canonicalize_qual, though, because partition
2687 : * quals should be in canonical form already; also, since the qual is in
2688 : * implicit-AND format, we'd have to explicitly convert it to explicit-AND
2689 : * format and back again.
2690 : */
2691 16958 : partconstr = RelationGetPartitionQual(relation);
2692 16958 : if (partconstr)
2693 : {
2694 3458 : partconstr = (List *) expression_planner((Expr *) partconstr);
2695 3458 : if (rel->relid != 1)
2696 3376 : ChangeVarNodes((Node *) partconstr, 1, rel->relid, 0);
2697 3458 : rel->partition_qual = partconstr;
2698 : }
2699 : }
|