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