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