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