1 /*-------------------------------------------------------------------------
2  *
3  * plancat.c
4  *	   routines for accessing the system catalogs
5  *
6  *
7  * Portions Copyright (c) 1996-2016, 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/heapam.h"
22 #include "access/htup_details.h"
23 #include "access/nbtree.h"
24 #include "access/sysattr.h"
25 #include "access/transam.h"
26 #include "access/xlog.h"
27 #include "catalog/catalog.h"
28 #include "catalog/dependency.h"
29 #include "catalog/heap.h"
30 #include "catalog/pg_am.h"
31 #include "foreign/fdwapi.h"
32 #include "miscadmin.h"
33 #include "nodes/makefuncs.h"
34 #include "optimizer/clauses.h"
35 #include "optimizer/cost.h"
36 #include "optimizer/plancat.h"
37 #include "optimizer/predtest.h"
38 #include "optimizer/prep.h"
39 #include "parser/parse_relation.h"
40 #include "parser/parsetree.h"
41 #include "rewrite/rewriteManip.h"
42 #include "storage/bufmgr.h"
43 #include "utils/lsyscache.h"
44 #include "utils/rel.h"
45 #include "utils/snapmgr.h"
46 
47 
48 /* GUC parameter */
49 int			constraint_exclusion = CONSTRAINT_EXCLUSION_PARTITION;
50 
51 /* Hook for plugins to get control in get_relation_info() */
52 get_relation_info_hook_type get_relation_info_hook = NULL;
53 
54 
55 static void get_relation_foreign_keys(PlannerInfo *root, RelOptInfo *rel,
56 						  Relation relation, bool inhparent);
57 static bool infer_collation_opclass_match(InferenceElem *elem, Relation idxRel,
58 							  List *idxExprs);
59 static int32 get_rel_data_width(Relation rel, int32 *attr_widths);
60 static List *get_relation_constraints(PlannerInfo *root,
61 						 Oid relationObjectId, RelOptInfo *rel,
62 						 bool include_notnull);
63 static List *build_index_tlist(PlannerInfo *root, IndexOptInfo *index,
64 				  Relation heapRelation);
65 
66 
67 /*
68  * get_relation_info -
69  *	  Retrieves catalog information for a given relation.
70  *
71  * Given the Oid of the relation, return the following info into fields
72  * of the RelOptInfo struct:
73  *
74  *	min_attr	lowest valid AttrNumber
75  *	max_attr	highest valid AttrNumber
76  *	indexlist	list of IndexOptInfos for relation's indexes
77  *	serverid	if it's a foreign table, the server OID
78  *	fdwroutine	if it's a foreign table, the FDW function pointers
79  *	pages		number of pages
80  *	tuples		number of tuples
81  *	rel_parallel_workers user-defined number of parallel workers
82  *
83  * Also, add information about the relation's foreign keys to root->fkey_list.
84  *
85  * Also, initialize the attr_needed[] and attr_widths[] arrays.  In most
86  * cases these are left as zeroes, but sometimes we need to compute attr
87  * widths here, and we may as well cache the results for costsize.c.
88  *
89  * If inhparent is true, all we need to do is set up the attr arrays:
90  * the RelOptInfo actually represents the appendrel formed by an inheritance
91  * tree, and so the parent rel's physical size and index information isn't
92  * important for it.
93  */
94 void
get_relation_info(PlannerInfo * root,Oid relationObjectId,bool inhparent,RelOptInfo * rel)95 get_relation_info(PlannerInfo *root, Oid relationObjectId, bool inhparent,
96 				  RelOptInfo *rel)
97 {
98 	Index		varno = rel->relid;
99 	Relation	relation;
100 	bool		hasindex;
101 	List	   *indexinfos = NIL;
102 
103 	/*
104 	 * We need not lock the relation since it was already locked, either by
105 	 * the rewriter or when expand_inherited_rtentry() added it to the query's
106 	 * rangetable.
107 	 */
108 	relation = heap_open(relationObjectId, NoLock);
109 
110 	/* Temporary and unlogged relations are inaccessible during recovery. */
111 	if (!RelationNeedsWAL(relation) && RecoveryInProgress())
112 		ereport(ERROR,
113 				(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
114 				 errmsg("cannot access temporary or unlogged relations during recovery")));
115 
116 	rel->min_attr = FirstLowInvalidHeapAttributeNumber + 1;
117 	rel->max_attr = RelationGetNumberOfAttributes(relation);
118 	rel->reltablespace = RelationGetForm(relation)->reltablespace;
119 
120 	Assert(rel->max_attr >= rel->min_attr);
121 	rel->attr_needed = (Relids *)
122 		palloc0((rel->max_attr - rel->min_attr + 1) * sizeof(Relids));
123 	rel->attr_widths = (int32 *)
124 		palloc0((rel->max_attr - rel->min_attr + 1) * sizeof(int32));
125 
126 	/*
127 	 * Estimate relation size --- unless it's an inheritance parent, in which
128 	 * case the size will be computed later in set_append_rel_pathlist, and we
129 	 * must leave it zero for now to avoid bollixing the total_table_pages
130 	 * calculation.
131 	 */
132 	if (!inhparent)
133 		estimate_rel_size(relation, rel->attr_widths - rel->min_attr,
134 						  &rel->pages, &rel->tuples, &rel->allvisfrac);
135 
136 	/* Retrieve the parallel_workers reloption, or -1 if not set. */
137 	rel->rel_parallel_workers = RelationGetParallelWorkers(relation, -1);
138 
139 	/*
140 	 * Make list of indexes.  Ignore indexes on system catalogs if told to.
141 	 * Don't bother with indexes for an inheritance parent, either.
142 	 */
143 	if (inhparent ||
144 		(IgnoreSystemIndexes && IsSystemRelation(relation)))
145 		hasindex = false;
146 	else
147 		hasindex = relation->rd_rel->relhasindex;
148 
149 	if (hasindex)
150 	{
151 		List	   *indexoidlist;
152 		ListCell   *l;
153 		LOCKMODE	lmode;
154 
155 		indexoidlist = RelationGetIndexList(relation);
156 
157 		/*
158 		 * For each index, we get the same type of lock that the executor will
159 		 * need, and do not release it.  This saves a couple of trips to the
160 		 * shared lock manager while not creating any real loss of
161 		 * concurrency, because no schema changes could be happening on the
162 		 * index while we hold lock on the parent rel, and neither lock type
163 		 * blocks any other kind of index operation.
164 		 */
165 		if (rel->relid == root->parse->resultRelation)
166 			lmode = RowExclusiveLock;
167 		else
168 			lmode = AccessShareLock;
169 
170 		foreach(l, indexoidlist)
171 		{
172 			Oid			indexoid = lfirst_oid(l);
173 			Relation	indexRelation;
174 			Form_pg_index index;
175 			IndexAmRoutine *amroutine;
176 			IndexOptInfo *info;
177 			int			ncolumns;
178 			int			i;
179 
180 			/*
181 			 * Extract info from the relation descriptor for the index.
182 			 */
183 			indexRelation = index_open(indexoid, lmode);
184 			index = indexRelation->rd_index;
185 
186 			/*
187 			 * Ignore invalid indexes, since they can't safely be used for
188 			 * queries.  Note that this is OK because the data structure we
189 			 * are constructing is only used by the planner --- the executor
190 			 * still needs to insert into "invalid" indexes, if they're marked
191 			 * IndexIsReady.
192 			 */
193 			if (!IndexIsValid(index))
194 			{
195 				index_close(indexRelation, NoLock);
196 				continue;
197 			}
198 
199 			/*
200 			 * If the index is valid, but cannot yet be used, ignore it; but
201 			 * mark the plan we are generating as transient. See
202 			 * src/backend/access/heap/README.HOT for discussion.
203 			 */
204 			if (index->indcheckxmin &&
205 				!TransactionIdPrecedes(HeapTupleHeaderGetXmin(indexRelation->rd_indextuple->t_data),
206 									   TransactionXmin))
207 			{
208 				root->glob->transientPlan = true;
209 				index_close(indexRelation, NoLock);
210 				continue;
211 			}
212 
213 			info = makeNode(IndexOptInfo);
214 
215 			info->indexoid = index->indexrelid;
216 			info->reltablespace =
217 				RelationGetForm(indexRelation)->reltablespace;
218 			info->rel = rel;
219 			info->ncolumns = ncolumns = index->indnatts;
220 			info->indexkeys = (int *) palloc(sizeof(int) * ncolumns);
221 			info->indexcollations = (Oid *) palloc(sizeof(Oid) * ncolumns);
222 			info->opfamily = (Oid *) palloc(sizeof(Oid) * ncolumns);
223 			info->opcintype = (Oid *) palloc(sizeof(Oid) * ncolumns);
224 			info->canreturn = (bool *) palloc(sizeof(bool) * ncolumns);
225 
226 			for (i = 0; i < ncolumns; i++)
227 			{
228 				info->indexkeys[i] = index->indkey.values[i];
229 				info->indexcollations[i] = indexRelation->rd_indcollation[i];
230 				info->opfamily[i] = indexRelation->rd_opfamily[i];
231 				info->opcintype[i] = indexRelation->rd_opcintype[i];
232 				info->canreturn[i] = index_can_return(indexRelation, i + 1);
233 			}
234 
235 			info->relam = indexRelation->rd_rel->relam;
236 
237 			/* We copy just the fields we need, not all of rd_amroutine */
238 			amroutine = indexRelation->rd_amroutine;
239 			info->amcanorderbyop = amroutine->amcanorderbyop;
240 			info->amoptionalkey = amroutine->amoptionalkey;
241 			info->amsearcharray = amroutine->amsearcharray;
242 			info->amsearchnulls = amroutine->amsearchnulls;
243 			info->amhasgettuple = (amroutine->amgettuple != NULL);
244 			info->amhasgetbitmap = (amroutine->amgetbitmap != NULL);
245 			info->amcanmarkpos = (amroutine->ammarkpos != NULL &&
246 								  amroutine->amrestrpos != NULL);
247 			info->amcostestimate = amroutine->amcostestimate;
248 			Assert(info->amcostestimate != NULL);
249 
250 			/*
251 			 * Fetch the ordering information for the index, if any.
252 			 */
253 			if (info->relam == BTREE_AM_OID)
254 			{
255 				/*
256 				 * If it's a btree index, we can use its opfamily OIDs
257 				 * directly as the sort ordering opfamily OIDs.
258 				 */
259 				Assert(amroutine->amcanorder);
260 
261 				info->sortopfamily = info->opfamily;
262 				info->reverse_sort = (bool *) palloc(sizeof(bool) * ncolumns);
263 				info->nulls_first = (bool *) palloc(sizeof(bool) * ncolumns);
264 
265 				for (i = 0; i < ncolumns; i++)
266 				{
267 					int16		opt = indexRelation->rd_indoption[i];
268 
269 					info->reverse_sort[i] = (opt & INDOPTION_DESC) != 0;
270 					info->nulls_first[i] = (opt & INDOPTION_NULLS_FIRST) != 0;
271 				}
272 			}
273 			else if (amroutine->amcanorder)
274 			{
275 				/*
276 				 * Otherwise, identify the corresponding btree opfamilies by
277 				 * trying to map this index's "<" operators into btree.  Since
278 				 * "<" uniquely defines the behavior of a sort order, this is
279 				 * a sufficient test.
280 				 *
281 				 * XXX This method is rather slow and also requires the
282 				 * undesirable assumption that the other index AM numbers its
283 				 * strategies the same as btree.  It'd be better to have a way
284 				 * to explicitly declare the corresponding btree opfamily for
285 				 * each opfamily of the other index type.  But given the lack
286 				 * of current or foreseeable amcanorder index types, it's not
287 				 * worth expending more effort on now.
288 				 */
289 				info->sortopfamily = (Oid *) palloc(sizeof(Oid) * ncolumns);
290 				info->reverse_sort = (bool *) palloc(sizeof(bool) * ncolumns);
291 				info->nulls_first = (bool *) palloc(sizeof(bool) * ncolumns);
292 
293 				for (i = 0; i < ncolumns; i++)
294 				{
295 					int16		opt = indexRelation->rd_indoption[i];
296 					Oid			ltopr;
297 					Oid			btopfamily;
298 					Oid			btopcintype;
299 					int16		btstrategy;
300 
301 					info->reverse_sort[i] = (opt & INDOPTION_DESC) != 0;
302 					info->nulls_first[i] = (opt & INDOPTION_NULLS_FIRST) != 0;
303 
304 					ltopr = get_opfamily_member(info->opfamily[i],
305 												info->opcintype[i],
306 												info->opcintype[i],
307 												BTLessStrategyNumber);
308 					if (OidIsValid(ltopr) &&
309 						get_ordering_op_properties(ltopr,
310 												   &btopfamily,
311 												   &btopcintype,
312 												   &btstrategy) &&
313 						btopcintype == info->opcintype[i] &&
314 						btstrategy == BTLessStrategyNumber)
315 					{
316 						/* Successful mapping */
317 						info->sortopfamily[i] = btopfamily;
318 					}
319 					else
320 					{
321 						/* Fail ... quietly treat index as unordered */
322 						info->sortopfamily = NULL;
323 						info->reverse_sort = NULL;
324 						info->nulls_first = NULL;
325 						break;
326 					}
327 				}
328 			}
329 			else
330 			{
331 				info->sortopfamily = NULL;
332 				info->reverse_sort = NULL;
333 				info->nulls_first = NULL;
334 			}
335 
336 			/*
337 			 * Fetch the index expressions and predicate, if any.  We must
338 			 * modify the copies we obtain from the relcache to have the
339 			 * correct varno for the parent relation, so that they match up
340 			 * correctly against qual clauses.
341 			 */
342 			info->indexprs = RelationGetIndexExpressions(indexRelation);
343 			info->indpred = RelationGetIndexPredicate(indexRelation);
344 			if (info->indexprs && varno != 1)
345 				ChangeVarNodes((Node *) info->indexprs, 1, varno, 0);
346 			if (info->indpred && varno != 1)
347 				ChangeVarNodes((Node *) info->indpred, 1, varno, 0);
348 
349 			/* Build targetlist using the completed indexprs data */
350 			info->indextlist = build_index_tlist(root, info, relation);
351 
352 			info->indrestrictinfo = NIL;		/* set later, in indxpath.c */
353 			info->predOK = false;		/* set later, in indxpath.c */
354 			info->unique = index->indisunique;
355 			info->immediate = index->indimmediate;
356 			info->hypothetical = false;
357 
358 			/*
359 			 * Estimate the index size.  If it's not a partial index, we lock
360 			 * the number-of-tuples estimate to equal the parent table; if it
361 			 * is partial then we have to use the same methods as we would for
362 			 * a table, except we can be sure that the index is not larger
363 			 * than the table.
364 			 */
365 			if (info->indpred == NIL)
366 			{
367 				info->pages = RelationGetNumberOfBlocks(indexRelation);
368 				info->tuples = rel->tuples;
369 			}
370 			else
371 			{
372 				double		allvisfrac; /* dummy */
373 
374 				estimate_rel_size(indexRelation, NULL,
375 								  &info->pages, &info->tuples, &allvisfrac);
376 				if (info->tuples > rel->tuples)
377 					info->tuples = rel->tuples;
378 			}
379 
380 			if (info->relam == BTREE_AM_OID)
381 			{
382 				/* For btrees, get tree height while we have the index open */
383 				info->tree_height = _bt_getrootheight(indexRelation);
384 			}
385 			else
386 			{
387 				/* For other index types, just set it to "unknown" for now */
388 				info->tree_height = -1;
389 			}
390 
391 			index_close(indexRelation, NoLock);
392 
393 			indexinfos = lcons(info, indexinfos);
394 		}
395 
396 		list_free(indexoidlist);
397 	}
398 
399 	rel->indexlist = indexinfos;
400 
401 	/* Grab foreign-table info using the relcache, while we have it */
402 	if (relation->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
403 	{
404 		rel->serverid = GetForeignServerIdByRelId(RelationGetRelid(relation));
405 		rel->fdwroutine = GetFdwRoutineForRelation(relation, true);
406 	}
407 	else
408 	{
409 		rel->serverid = InvalidOid;
410 		rel->fdwroutine = NULL;
411 	}
412 
413 	/* Collect info about relation's foreign keys, if relevant */
414 	get_relation_foreign_keys(root, rel, relation, inhparent);
415 
416 	heap_close(relation, NoLock);
417 
418 	/*
419 	 * Allow a plugin to editorialize on the info we obtained from the
420 	 * catalogs.  Actions might include altering the assumed relation size,
421 	 * removing an index, or adding a hypothetical index to the indexlist.
422 	 */
423 	if (get_relation_info_hook)
424 		(*get_relation_info_hook) (root, relationObjectId, inhparent, rel);
425 }
426 
427 /*
428  * get_relation_foreign_keys -
429  *	  Retrieves foreign key information for a given relation.
430  *
431  * ForeignKeyOptInfos for relevant foreign keys are created and added to
432  * root->fkey_list.  We do this now while we have the relcache entry open.
433  * We could sometimes avoid making useless ForeignKeyOptInfos if we waited
434  * until all RelOptInfos have been built, but the cost of re-opening the
435  * relcache entries would probably exceed any savings.
436  */
437 static void
get_relation_foreign_keys(PlannerInfo * root,RelOptInfo * rel,Relation relation,bool inhparent)438 get_relation_foreign_keys(PlannerInfo *root, RelOptInfo *rel,
439 						  Relation relation, bool inhparent)
440 {
441 	List	   *rtable = root->parse->rtable;
442 	List	   *cachedfkeys;
443 	ListCell   *lc;
444 
445 	/*
446 	 * If it's not a baserel, we don't care about its FKs.  Also, if the query
447 	 * references only a single relation, we can skip the lookup since no FKs
448 	 * could satisfy the requirements below.
449 	 */
450 	if (rel->reloptkind != RELOPT_BASEREL ||
451 		list_length(rtable) < 2)
452 		return;
453 
454 	/*
455 	 * If it's the parent of an inheritance tree, ignore its FKs.  We could
456 	 * make useful FK-based deductions if we found that all members of the
457 	 * inheritance tree have equivalent FK constraints, but detecting that
458 	 * would require code that hasn't been written.
459 	 */
460 	if (inhparent)
461 		return;
462 
463 	/*
464 	 * Extract data about relation's FKs from the relcache.  Note that this
465 	 * list belongs to the relcache and might disappear in a cache flush, so
466 	 * we must not do any further catalog access within this function.
467 	 */
468 	cachedfkeys = RelationGetFKeyList(relation);
469 
470 	/*
471 	 * Figure out which FKs are of interest for this query, and create
472 	 * ForeignKeyOptInfos for them.  We want only FKs that reference some
473 	 * other RTE of the current query.  In queries containing self-joins,
474 	 * there might be more than one other RTE for a referenced table, and we
475 	 * should make a ForeignKeyOptInfo for each occurrence.
476 	 *
477 	 * Ideally, we would ignore RTEs that correspond to non-baserels, but it's
478 	 * too hard to identify those here, so we might end up making some useless
479 	 * ForeignKeyOptInfos.  If so, match_foreign_keys_to_quals() will remove
480 	 * them again.
481 	 */
482 	foreach(lc, cachedfkeys)
483 	{
484 		ForeignKeyCacheInfo *cachedfk = (ForeignKeyCacheInfo *) lfirst(lc);
485 		Index		rti;
486 		ListCell   *lc2;
487 
488 		/* conrelid should always be that of the table we're considering */
489 		Assert(cachedfk->conrelid == RelationGetRelid(relation));
490 
491 		/* Scan to find other RTEs matching confrelid */
492 		rti = 0;
493 		foreach(lc2, rtable)
494 		{
495 			RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc2);
496 			ForeignKeyOptInfo *info;
497 
498 			rti++;
499 			/* Ignore if not the correct table */
500 			if (rte->rtekind != RTE_RELATION ||
501 				rte->relid != cachedfk->confrelid)
502 				continue;
503 			/* Ignore if it's an inheritance parent; doesn't really match */
504 			if (rte->inh)
505 				continue;
506 			/* Ignore self-referential FKs; we only care about joins */
507 			if (rti == rel->relid)
508 				continue;
509 
510 			/* OK, let's make an entry */
511 			info = makeNode(ForeignKeyOptInfo);
512 			info->con_relid = rel->relid;
513 			info->ref_relid = rti;
514 			info->nkeys = cachedfk->nkeys;
515 			memcpy(info->conkey, cachedfk->conkey, sizeof(info->conkey));
516 			memcpy(info->confkey, cachedfk->confkey, sizeof(info->confkey));
517 			memcpy(info->conpfeqop, cachedfk->conpfeqop, sizeof(info->conpfeqop));
518 			/* zero out fields to be filled by match_foreign_keys_to_quals */
519 			info->nmatched_ec = 0;
520 			info->nmatched_rcols = 0;
521 			info->nmatched_ri = 0;
522 			memset(info->eclass, 0, sizeof(info->eclass));
523 			memset(info->rinfos, 0, sizeof(info->rinfos));
524 
525 			root->fkey_list = lappend(root->fkey_list, info);
526 		}
527 	}
528 }
529 
530 /*
531  * infer_arbiter_indexes -
532  *	  Determine the unique indexes used to arbitrate speculative insertion.
533  *
534  * Uses user-supplied inference clause expressions and predicate to match a
535  * unique index from those defined and ready on the heap relation (target).
536  * An exact match is required on columns/expressions (although they can appear
537  * in any order).  However, the predicate given by the user need only restrict
538  * insertion to a subset of some part of the table covered by some particular
539  * unique index (in particular, a partial unique index) in order to be
540  * inferred.
541  *
542  * The implementation does not consider which B-Tree operator class any
543  * particular available unique index attribute uses, unless one was specified
544  * in the inference specification. The same is true of collations.  In
545  * particular, there is no system dependency on the default operator class for
546  * the purposes of inference.  If no opclass (or collation) is specified, then
547  * all matching indexes (that may or may not match the default in terms of
548  * each attribute opclass/collation) are used for inference.
549  */
550 List *
infer_arbiter_indexes(PlannerInfo * root)551 infer_arbiter_indexes(PlannerInfo *root)
552 {
553 	OnConflictExpr *onconflict = root->parse->onConflict;
554 
555 	/* Iteration state */
556 	Relation	relation;
557 	Oid			relationObjectId;
558 	Oid			indexOidFromConstraint = InvalidOid;
559 	List	   *indexList;
560 	ListCell   *l;
561 
562 	/* Normalized inference attributes and inference expressions: */
563 	Bitmapset  *inferAttrs = NULL;
564 	List	   *inferElems = NIL;
565 
566 	/* Results */
567 	List	   *results = NIL;
568 
569 	/*
570 	 * Quickly return NIL for ON CONFLICT DO NOTHING without an inference
571 	 * specification or named constraint.  ON CONFLICT DO UPDATE statements
572 	 * must always provide one or the other (but parser ought to have caught
573 	 * that already).
574 	 */
575 	if (onconflict->arbiterElems == NIL &&
576 		onconflict->constraint == InvalidOid)
577 		return NIL;
578 
579 	/*
580 	 * We need not lock the relation since it was already locked, either by
581 	 * the rewriter or when expand_inherited_rtentry() added it to the query's
582 	 * rangetable.
583 	 */
584 	relationObjectId = rt_fetch(root->parse->resultRelation,
585 								root->parse->rtable)->relid;
586 
587 	relation = heap_open(relationObjectId, NoLock);
588 
589 	/*
590 	 * Build normalized/BMS representation of plain indexed attributes, as
591 	 * well as a separate list of expression items.  This simplifies matching
592 	 * the cataloged definition of indexes.
593 	 */
594 	foreach(l, onconflict->arbiterElems)
595 	{
596 		InferenceElem *elem = (InferenceElem *) lfirst(l);
597 		Var		   *var;
598 		int			attno;
599 
600 		if (!IsA(elem->expr, Var))
601 		{
602 			/* If not a plain Var, just shove it in inferElems for now */
603 			inferElems = lappend(inferElems, elem->expr);
604 			continue;
605 		}
606 
607 		var = (Var *) elem->expr;
608 		attno = var->varattno;
609 
610 		if (attno == 0)
611 			ereport(ERROR,
612 					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
613 					 errmsg("whole row unique index inference specifications are not supported")));
614 
615 		inferAttrs = bms_add_member(inferAttrs,
616 								 attno - FirstLowInvalidHeapAttributeNumber);
617 	}
618 
619 	/*
620 	 * Lookup named constraint's index.  This is not immediately returned
621 	 * because some additional sanity checks are required.
622 	 */
623 	if (onconflict->constraint != InvalidOid)
624 	{
625 		indexOidFromConstraint = get_constraint_index(onconflict->constraint);
626 
627 		if (indexOidFromConstraint == InvalidOid)
628 			ereport(ERROR,
629 					(errcode(ERRCODE_WRONG_OBJECT_TYPE),
630 					 errmsg("constraint in ON CONFLICT clause has no associated index")));
631 	}
632 
633 	/*
634 	 * Using that representation, iterate through the list of indexes on the
635 	 * target relation to try and find a match
636 	 */
637 	indexList = RelationGetIndexList(relation);
638 
639 	foreach(l, indexList)
640 	{
641 		Oid			indexoid = lfirst_oid(l);
642 		Relation	idxRel;
643 		Form_pg_index idxForm;
644 		Bitmapset  *indexedAttrs;
645 		List	   *idxExprs;
646 		List	   *predExprs;
647 		AttrNumber	natt;
648 		ListCell   *el;
649 
650 		/*
651 		 * Extract info from the relation descriptor for the index.  We know
652 		 * that this is a target, so get lock type it is known will ultimately
653 		 * be required by the executor.
654 		 *
655 		 * Let executor complain about !indimmediate case directly, because
656 		 * enforcement needs to occur there anyway when an inference clause is
657 		 * omitted.
658 		 */
659 		idxRel = index_open(indexoid, RowExclusiveLock);
660 		idxForm = idxRel->rd_index;
661 
662 		if (!IndexIsValid(idxForm))
663 			goto next;
664 
665 		/*
666 		 * Note that we do not perform a check against indcheckxmin (like e.g.
667 		 * get_relation_info()) here to eliminate candidates, because
668 		 * uniqueness checking only cares about the most recently committed
669 		 * tuple versions.
670 		 */
671 
672 		/*
673 		 * Look for match on "ON constraint_name" variant, which may not be
674 		 * unique constraint.  This can only be a constraint name.
675 		 */
676 		if (indexOidFromConstraint == idxForm->indexrelid)
677 		{
678 			if (!idxForm->indisunique && onconflict->action == ONCONFLICT_UPDATE)
679 				ereport(ERROR,
680 						(errcode(ERRCODE_WRONG_OBJECT_TYPE),
681 						 errmsg("ON CONFLICT DO UPDATE not supported with exclusion constraints")));
682 
683 			results = lappend_oid(results, idxForm->indexrelid);
684 			list_free(indexList);
685 			index_close(idxRel, NoLock);
686 			heap_close(relation, NoLock);
687 			return results;
688 		}
689 		else if (indexOidFromConstraint != InvalidOid)
690 		{
691 			/* No point in further work for index in named constraint case */
692 			goto next;
693 		}
694 
695 		/*
696 		 * Only considering conventional inference at this point (not named
697 		 * constraints), so index under consideration can be immediately
698 		 * skipped if it's not unique
699 		 */
700 		if (!idxForm->indisunique)
701 			goto next;
702 
703 		/* Build BMS representation of plain (non expression) index attrs */
704 		indexedAttrs = NULL;
705 		for (natt = 0; natt < idxForm->indnatts; natt++)
706 		{
707 			int			attno = idxRel->rd_index->indkey.values[natt];
708 
709 			if (attno != 0)
710 				indexedAttrs = bms_add_member(indexedAttrs,
711 								 attno - FirstLowInvalidHeapAttributeNumber);
712 		}
713 
714 		/* Non-expression attributes (if any) must match */
715 		if (!bms_equal(indexedAttrs, inferAttrs))
716 			goto next;
717 
718 		/* Expression attributes (if any) must match */
719 		idxExprs = RelationGetIndexExpressions(idxRel);
720 		foreach(el, onconflict->arbiterElems)
721 		{
722 			InferenceElem *elem = (InferenceElem *) lfirst(el);
723 
724 			/*
725 			 * Ensure that collation/opclass aspects of inference expression
726 			 * element match.  Even though this loop is primarily concerned
727 			 * with matching expressions, it is a convenient point to check
728 			 * this for both expressions and ordinary (non-expression)
729 			 * attributes appearing as inference elements.
730 			 */
731 			if (!infer_collation_opclass_match(elem, idxRel, idxExprs))
732 				goto next;
733 
734 			/*
735 			 * Plain Vars don't factor into count of expression elements, and
736 			 * the question of whether or not they satisfy the index
737 			 * definition has already been considered (they must).
738 			 */
739 			if (IsA(elem->expr, Var))
740 				continue;
741 
742 			/*
743 			 * Might as well avoid redundant check in the rare cases where
744 			 * infer_collation_opclass_match() is required to do real work.
745 			 * Otherwise, check that element expression appears in cataloged
746 			 * index definition.
747 			 */
748 			if (elem->infercollid != InvalidOid ||
749 				elem->inferopclass != InvalidOid ||
750 				list_member(idxExprs, elem->expr))
751 				continue;
752 
753 			goto next;
754 		}
755 
756 		/*
757 		 * Now that all inference elements were matched, ensure that the
758 		 * expression elements from inference clause are not missing any
759 		 * cataloged expressions.  This does the right thing when unique
760 		 * indexes redundantly repeat the same attribute, or if attributes
761 		 * redundantly appear multiple times within an inference clause.
762 		 */
763 		if (list_difference(idxExprs, inferElems) != NIL)
764 			goto next;
765 
766 		/*
767 		 * If it's a partial index, its predicate must be implied by the ON
768 		 * CONFLICT's WHERE clause.
769 		 */
770 		predExprs = RelationGetIndexPredicate(idxRel);
771 
772 		if (!predicate_implied_by(predExprs, (List *) onconflict->arbiterWhere))
773 			goto next;
774 
775 		results = lappend_oid(results, idxForm->indexrelid);
776 next:
777 		index_close(idxRel, NoLock);
778 	}
779 
780 	list_free(indexList);
781 	heap_close(relation, NoLock);
782 
783 	if (results == NIL)
784 		ereport(ERROR,
785 				(errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
786 				 errmsg("there is no unique or exclusion constraint matching the ON CONFLICT specification")));
787 
788 	return results;
789 }
790 
791 /*
792  * infer_collation_opclass_match - ensure infer element opclass/collation match
793  *
794  * Given unique index inference element from inference specification, if
795  * collation was specified, or if opclass was specified, verify that there is
796  * at least one matching indexed attribute (occasionally, there may be more).
797  * Skip this in the common case where inference specification does not include
798  * collation or opclass (instead matching everything, regardless of cataloged
799  * collation/opclass of indexed attribute).
800  *
801  * At least historically, Postgres has not offered collations or opclasses
802  * with alternative-to-default notions of equality, so these additional
803  * criteria should only be required infrequently.
804  *
805  * Don't give up immediately when an inference element matches some attribute
806  * cataloged as indexed but not matching additional opclass/collation
807  * criteria.  This is done so that the implementation is as forgiving as
808  * possible of redundancy within cataloged index attributes (or, less
809  * usefully, within inference specification elements).  If collations actually
810  * differ between apparently redundantly indexed attributes (redundant within
811  * or across indexes), then there really is no redundancy as such.
812  *
813  * Note that if an inference element specifies an opclass and a collation at
814  * once, both must match in at least one particular attribute within index
815  * catalog definition in order for that inference element to be considered
816  * inferred/satisfied.
817  */
818 static bool
infer_collation_opclass_match(InferenceElem * elem,Relation idxRel,List * idxExprs)819 infer_collation_opclass_match(InferenceElem *elem, Relation idxRel,
820 							  List *idxExprs)
821 {
822 	AttrNumber	natt;
823 	Oid			inferopfamily = InvalidOid;		/* OID of opclass opfamily */
824 	Oid			inferopcinputtype = InvalidOid; /* OID of opclass input type */
825 	int			nplain = 0;		/* # plain attrs observed */
826 
827 	/*
828 	 * If inference specification element lacks collation/opclass, then no
829 	 * need to check for exact match.
830 	 */
831 	if (elem->infercollid == InvalidOid && elem->inferopclass == InvalidOid)
832 		return true;
833 
834 	/*
835 	 * Lookup opfamily and input type, for matching indexes
836 	 */
837 	if (elem->inferopclass)
838 	{
839 		inferopfamily = get_opclass_family(elem->inferopclass);
840 		inferopcinputtype = get_opclass_input_type(elem->inferopclass);
841 	}
842 
843 	for (natt = 1; natt <= idxRel->rd_att->natts; natt++)
844 	{
845 		Oid			opfamily = idxRel->rd_opfamily[natt - 1];
846 		Oid			opcinputtype = idxRel->rd_opcintype[natt - 1];
847 		Oid			collation = idxRel->rd_indcollation[natt - 1];
848 		int			attno = idxRel->rd_index->indkey.values[natt - 1];
849 
850 		if (attno != 0)
851 			nplain++;
852 
853 		if (elem->inferopclass != InvalidOid &&
854 			(inferopfamily != opfamily || inferopcinputtype != opcinputtype))
855 		{
856 			/* Attribute needed to match opclass, but didn't */
857 			continue;
858 		}
859 
860 		if (elem->infercollid != InvalidOid &&
861 			elem->infercollid != collation)
862 		{
863 			/* Attribute needed to match collation, but didn't */
864 			continue;
865 		}
866 
867 		/* If one matching index att found, good enough -- return true */
868 		if (IsA(elem->expr, Var))
869 		{
870 			if (((Var *) elem->expr)->varattno == attno)
871 				return true;
872 		}
873 		else if (attno == 0)
874 		{
875 			Node	   *nattExpr = list_nth(idxExprs, (natt - 1) - nplain);
876 
877 			/*
878 			 * Note that unlike routines like match_index_to_operand() we
879 			 * don't need to care about RelabelType.  Neither the index
880 			 * definition nor the inference clause should contain them.
881 			 */
882 			if (equal(elem->expr, nattExpr))
883 				return true;
884 		}
885 	}
886 
887 	return false;
888 }
889 
890 /*
891  * estimate_rel_size - estimate # pages and # tuples in a table or index
892  *
893  * We also estimate the fraction of the pages that are marked all-visible in
894  * the visibility map, for use in estimation of index-only scans.
895  *
896  * If attr_widths isn't NULL, it points to the zero-index entry of the
897  * relation's attr_widths[] cache; we fill this in if we have need to compute
898  * the attribute widths for estimation purposes.
899  */
900 void
estimate_rel_size(Relation rel,int32 * attr_widths,BlockNumber * pages,double * tuples,double * allvisfrac)901 estimate_rel_size(Relation rel, int32 *attr_widths,
902 				  BlockNumber *pages, double *tuples, double *allvisfrac)
903 {
904 	BlockNumber curpages;
905 	BlockNumber relpages;
906 	double		reltuples;
907 	BlockNumber relallvisible;
908 	double		density;
909 
910 	switch (rel->rd_rel->relkind)
911 	{
912 		case RELKIND_RELATION:
913 		case RELKIND_INDEX:
914 		case RELKIND_MATVIEW:
915 		case RELKIND_TOASTVALUE:
916 			/* it has storage, ok to call the smgr */
917 			curpages = RelationGetNumberOfBlocks(rel);
918 
919 			/*
920 			 * HACK: if the relation has never yet been vacuumed, use a
921 			 * minimum size estimate of 10 pages.  The idea here is to avoid
922 			 * assuming a newly-created table is really small, even if it
923 			 * currently is, because that may not be true once some data gets
924 			 * loaded into it.  Once a vacuum or analyze cycle has been done
925 			 * on it, it's more reasonable to believe the size is somewhat
926 			 * stable.
927 			 *
928 			 * (Note that this is only an issue if the plan gets cached and
929 			 * used again after the table has been filled.  What we're trying
930 			 * to avoid is using a nestloop-type plan on a table that has
931 			 * grown substantially since the plan was made.  Normally,
932 			 * autovacuum/autoanalyze will occur once enough inserts have
933 			 * happened and cause cached-plan invalidation; but that doesn't
934 			 * happen instantaneously, and it won't happen at all for cases
935 			 * such as temporary tables.)
936 			 *
937 			 * We approximate "never vacuumed" by "has relpages = 0", which
938 			 * means this will also fire on genuinely empty relations.  Not
939 			 * great, but fortunately that's a seldom-seen case in the real
940 			 * world, and it shouldn't degrade the quality of the plan too
941 			 * much anyway to err in this direction.
942 			 *
943 			 * There are two exceptions wherein we don't apply this heuristic.
944 			 * One is if the table has inheritance children.  Totally empty
945 			 * parent tables are quite common, so we should be willing to
946 			 * believe that they are empty.  Also, we don't apply the 10-page
947 			 * minimum to indexes.
948 			 */
949 			if (curpages < 10 &&
950 				rel->rd_rel->relpages == 0 &&
951 				!rel->rd_rel->relhassubclass &&
952 				rel->rd_rel->relkind != RELKIND_INDEX)
953 				curpages = 10;
954 
955 			/* report estimated # pages */
956 			*pages = curpages;
957 			/* quick exit if rel is clearly empty */
958 			if (curpages == 0)
959 			{
960 				*tuples = 0;
961 				*allvisfrac = 0;
962 				break;
963 			}
964 			/* coerce values in pg_class to more desirable types */
965 			relpages = (BlockNumber) rel->rd_rel->relpages;
966 			reltuples = (double) rel->rd_rel->reltuples;
967 			relallvisible = (BlockNumber) rel->rd_rel->relallvisible;
968 
969 			/*
970 			 * If it's an index, discount the metapage while estimating the
971 			 * number of tuples.  This is a kluge because it assumes more than
972 			 * it ought to about index structure.  Currently it's OK for
973 			 * btree, hash, and GIN indexes but suspect for GiST indexes.
974 			 */
975 			if (rel->rd_rel->relkind == RELKIND_INDEX &&
976 				relpages > 0)
977 			{
978 				curpages--;
979 				relpages--;
980 			}
981 
982 			/* estimate number of tuples from previous tuple density */
983 			if (relpages > 0)
984 				density = reltuples / (double) relpages;
985 			else
986 			{
987 				/*
988 				 * When we have no data because the relation was truncated,
989 				 * estimate tuple width from attribute datatypes.  We assume
990 				 * here that the pages are completely full, which is OK for
991 				 * tables (since they've presumably not been VACUUMed yet) but
992 				 * is probably an overestimate for indexes.  Fortunately
993 				 * get_relation_info() can clamp the overestimate to the
994 				 * parent table's size.
995 				 *
996 				 * Note: this code intentionally disregards alignment
997 				 * considerations, because (a) that would be gilding the lily
998 				 * considering how crude the estimate is, and (b) it creates
999 				 * platform dependencies in the default plans which are kind
1000 				 * of a headache for regression testing.
1001 				 */
1002 				int32		tuple_width;
1003 
1004 				tuple_width = get_rel_data_width(rel, attr_widths);
1005 				tuple_width += MAXALIGN(SizeofHeapTupleHeader);
1006 				tuple_width += sizeof(ItemIdData);
1007 				/* note: integer division is intentional here */
1008 				density = (BLCKSZ - SizeOfPageHeaderData) / tuple_width;
1009 			}
1010 			*tuples = rint(density * (double) curpages);
1011 
1012 			/*
1013 			 * We use relallvisible as-is, rather than scaling it up like we
1014 			 * do for the pages and tuples counts, on the theory that any
1015 			 * pages added since the last VACUUM are most likely not marked
1016 			 * all-visible.  But costsize.c wants it converted to a fraction.
1017 			 */
1018 			if (relallvisible == 0 || curpages <= 0)
1019 				*allvisfrac = 0;
1020 			else if ((double) relallvisible >= curpages)
1021 				*allvisfrac = 1;
1022 			else
1023 				*allvisfrac = (double) relallvisible / curpages;
1024 			break;
1025 		case RELKIND_SEQUENCE:
1026 			/* Sequences always have a known size */
1027 			*pages = 1;
1028 			*tuples = 1;
1029 			*allvisfrac = 0;
1030 			break;
1031 		case RELKIND_FOREIGN_TABLE:
1032 			/* Just use whatever's in pg_class */
1033 			*pages = rel->rd_rel->relpages;
1034 			*tuples = rel->rd_rel->reltuples;
1035 			*allvisfrac = 0;
1036 			break;
1037 		default:
1038 			/* else it has no disk storage; probably shouldn't get here? */
1039 			*pages = 0;
1040 			*tuples = 0;
1041 			*allvisfrac = 0;
1042 			break;
1043 	}
1044 }
1045 
1046 
1047 /*
1048  * get_rel_data_width
1049  *
1050  * Estimate the average width of (the data part of) the relation's tuples.
1051  *
1052  * If attr_widths isn't NULL, it points to the zero-index entry of the
1053  * relation's attr_widths[] cache; use and update that cache as appropriate.
1054  *
1055  * Currently we ignore dropped columns.  Ideally those should be included
1056  * in the result, but we haven't got any way to get info about them; and
1057  * since they might be mostly NULLs, treating them as zero-width is not
1058  * necessarily the wrong thing anyway.
1059  */
1060 static int32
get_rel_data_width(Relation rel,int32 * attr_widths)1061 get_rel_data_width(Relation rel, int32 *attr_widths)
1062 {
1063 	int32		tuple_width = 0;
1064 	int			i;
1065 
1066 	for (i = 1; i <= RelationGetNumberOfAttributes(rel); i++)
1067 	{
1068 		Form_pg_attribute att = rel->rd_att->attrs[i - 1];
1069 		int32		item_width;
1070 
1071 		if (att->attisdropped)
1072 			continue;
1073 
1074 		/* use previously cached data, if any */
1075 		if (attr_widths != NULL && attr_widths[i] > 0)
1076 		{
1077 			tuple_width += attr_widths[i];
1078 			continue;
1079 		}
1080 
1081 		/* This should match set_rel_width() in costsize.c */
1082 		item_width = get_attavgwidth(RelationGetRelid(rel), i);
1083 		if (item_width <= 0)
1084 		{
1085 			item_width = get_typavgwidth(att->atttypid, att->atttypmod);
1086 			Assert(item_width > 0);
1087 		}
1088 		if (attr_widths != NULL)
1089 			attr_widths[i] = item_width;
1090 		tuple_width += item_width;
1091 	}
1092 
1093 	return tuple_width;
1094 }
1095 
1096 /*
1097  * get_relation_data_width
1098  *
1099  * External API for get_rel_data_width: same behavior except we have to
1100  * open the relcache entry.
1101  */
1102 int32
get_relation_data_width(Oid relid,int32 * attr_widths)1103 get_relation_data_width(Oid relid, int32 *attr_widths)
1104 {
1105 	int32		result;
1106 	Relation	relation;
1107 
1108 	/* As above, assume relation is already locked */
1109 	relation = heap_open(relid, NoLock);
1110 
1111 	result = get_rel_data_width(relation, attr_widths);
1112 
1113 	heap_close(relation, NoLock);
1114 
1115 	return result;
1116 }
1117 
1118 
1119 /*
1120  * get_relation_constraints
1121  *
1122  * Retrieve the validated CHECK constraint expressions of the given relation.
1123  *
1124  * Returns a List (possibly empty) of constraint expressions.  Each one
1125  * has been canonicalized, and its Vars are changed to have the varno
1126  * indicated by rel->relid.  This allows the expressions to be easily
1127  * compared to expressions taken from WHERE.
1128  *
1129  * If include_notnull is true, "col IS NOT NULL" expressions are generated
1130  * and added to the result for each column that's marked attnotnull.
1131  *
1132  * Note: at present this is invoked at most once per relation per planner
1133  * run, and in many cases it won't be invoked at all, so there seems no
1134  * point in caching the data in RelOptInfo.
1135  */
1136 static List *
get_relation_constraints(PlannerInfo * root,Oid relationObjectId,RelOptInfo * rel,bool include_notnull)1137 get_relation_constraints(PlannerInfo *root,
1138 						 Oid relationObjectId, RelOptInfo *rel,
1139 						 bool include_notnull)
1140 {
1141 	List	   *result = NIL;
1142 	Index		varno = rel->relid;
1143 	Relation	relation;
1144 	TupleConstr *constr;
1145 
1146 	/*
1147 	 * We assume the relation has already been safely locked.
1148 	 */
1149 	relation = heap_open(relationObjectId, NoLock);
1150 
1151 	constr = relation->rd_att->constr;
1152 	if (constr != NULL)
1153 	{
1154 		int			num_check = constr->num_check;
1155 		int			i;
1156 
1157 		for (i = 0; i < num_check; i++)
1158 		{
1159 			Node	   *cexpr;
1160 
1161 			/*
1162 			 * If this constraint hasn't been fully validated yet, we must
1163 			 * ignore it here.
1164 			 */
1165 			if (!constr->check[i].ccvalid)
1166 				continue;
1167 
1168 			cexpr = stringToNode(constr->check[i].ccbin);
1169 
1170 			/*
1171 			 * Run each expression through const-simplification and
1172 			 * canonicalization.  This is not just an optimization, but is
1173 			 * necessary, because we will be comparing it to
1174 			 * similarly-processed qual clauses, and may fail to detect valid
1175 			 * matches without this.  This must match the processing done to
1176 			 * qual clauses in preprocess_expression()!  (We can skip the
1177 			 * stuff involving subqueries, however, since we don't allow any
1178 			 * in check constraints.)
1179 			 */
1180 			cexpr = eval_const_expressions(root, cexpr);
1181 
1182 			cexpr = (Node *) canonicalize_qual_ext((Expr *) cexpr, true);
1183 
1184 			/* Fix Vars to have the desired varno */
1185 			if (varno != 1)
1186 				ChangeVarNodes(cexpr, 1, varno, 0);
1187 
1188 			/*
1189 			 * Finally, convert to implicit-AND format (that is, a List) and
1190 			 * append the resulting item(s) to our output list.
1191 			 */
1192 			result = list_concat(result,
1193 								 make_ands_implicit((Expr *) cexpr));
1194 		}
1195 
1196 		/* Add NOT NULL constraints in expression form, if requested */
1197 		if (include_notnull && constr->has_not_null)
1198 		{
1199 			int			natts = relation->rd_att->natts;
1200 
1201 			for (i = 1; i <= natts; i++)
1202 			{
1203 				Form_pg_attribute att = relation->rd_att->attrs[i - 1];
1204 
1205 				if (att->attnotnull && !att->attisdropped)
1206 				{
1207 					NullTest   *ntest = makeNode(NullTest);
1208 
1209 					ntest->arg = (Expr *) makeVar(varno,
1210 												  i,
1211 												  att->atttypid,
1212 												  att->atttypmod,
1213 												  att->attcollation,
1214 												  0);
1215 					ntest->nulltesttype = IS_NOT_NULL;
1216 
1217 					/*
1218 					 * argisrow=false is correct even for a composite column,
1219 					 * because attnotnull does not represent a SQL-spec IS NOT
1220 					 * NULL test in such a case, just IS DISTINCT FROM NULL.
1221 					 */
1222 					ntest->argisrow = false;
1223 					ntest->location = -1;
1224 					result = lappend(result, ntest);
1225 				}
1226 			}
1227 		}
1228 	}
1229 
1230 	heap_close(relation, NoLock);
1231 
1232 	return result;
1233 }
1234 
1235 
1236 /*
1237  * relation_excluded_by_constraints
1238  *
1239  * Detect whether the relation need not be scanned because it has either
1240  * self-inconsistent restrictions, or restrictions inconsistent with the
1241  * relation's validated CHECK constraints.
1242  *
1243  * Note: this examines only rel->relid, rel->reloptkind, and
1244  * rel->baserestrictinfo; therefore it can be called before filling in
1245  * other fields of the RelOptInfo.
1246  */
1247 bool
relation_excluded_by_constraints(PlannerInfo * root,RelOptInfo * rel,RangeTblEntry * rte)1248 relation_excluded_by_constraints(PlannerInfo *root,
1249 								 RelOptInfo *rel, RangeTblEntry *rte)
1250 {
1251 	List	   *safe_restrictions;
1252 	List	   *constraint_pred;
1253 	List	   *safe_constraints;
1254 	ListCell   *lc;
1255 
1256 	/*
1257 	 * Regardless of the setting of constraint_exclusion, detect
1258 	 * constant-FALSE-or-NULL restriction clauses.  Because const-folding will
1259 	 * reduce "anything AND FALSE" to just "FALSE", any such case should
1260 	 * result in exactly one baserestrictinfo entry.  This doesn't fire very
1261 	 * often, but it seems cheap enough to be worth doing anyway.  (Without
1262 	 * this, we'd miss some optimizations that 9.5 and earlier found via much
1263 	 * more roundabout methods.)
1264 	 */
1265 	if (list_length(rel->baserestrictinfo) == 1)
1266 	{
1267 		RestrictInfo *rinfo = (RestrictInfo *) linitial(rel->baserestrictinfo);
1268 		Expr	   *clause = rinfo->clause;
1269 
1270 		if (clause && IsA(clause, Const) &&
1271 			(((Const *) clause)->constisnull ||
1272 			 !DatumGetBool(((Const *) clause)->constvalue)))
1273 			return true;
1274 	}
1275 
1276 	/* Skip further tests if constraint exclusion is disabled for the rel */
1277 	if (constraint_exclusion == CONSTRAINT_EXCLUSION_OFF ||
1278 		(constraint_exclusion == CONSTRAINT_EXCLUSION_PARTITION &&
1279 		 !(rel->reloptkind == RELOPT_OTHER_MEMBER_REL ||
1280 		   (root->hasInheritedTarget &&
1281 			rel->reloptkind == RELOPT_BASEREL &&
1282 			rel->relid == root->parse->resultRelation))))
1283 		return false;
1284 
1285 	/*
1286 	 * Check for self-contradictory restriction clauses.  We dare not make
1287 	 * deductions with non-immutable functions, but any immutable clauses that
1288 	 * are self-contradictory allow us to conclude the scan is unnecessary.
1289 	 *
1290 	 * Note: strip off RestrictInfo because predicate_refuted_by() isn't
1291 	 * expecting to see any in its predicate argument.
1292 	 */
1293 	safe_restrictions = NIL;
1294 	foreach(lc, rel->baserestrictinfo)
1295 	{
1296 		RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1297 
1298 		if (!contain_mutable_functions((Node *) rinfo->clause))
1299 			safe_restrictions = lappend(safe_restrictions, rinfo->clause);
1300 	}
1301 
1302 	if (predicate_refuted_by(safe_restrictions, safe_restrictions))
1303 		return true;
1304 
1305 	/* Only plain relations have constraints */
1306 	if (rte->rtekind != RTE_RELATION || rte->inh)
1307 		return false;
1308 
1309 	/*
1310 	 * OK to fetch the constraint expressions.  Include "col IS NOT NULL"
1311 	 * expressions for attnotnull columns, in case we can refute those.
1312 	 */
1313 	constraint_pred = get_relation_constraints(root, rte->relid, rel, true);
1314 
1315 	/*
1316 	 * We do not currently enforce that CHECK constraints contain only
1317 	 * immutable functions, so it's necessary to check here. We daren't draw
1318 	 * conclusions from plan-time evaluation of non-immutable functions. Since
1319 	 * they're ANDed, we can just ignore any mutable constraints in the list,
1320 	 * and reason about the rest.
1321 	 */
1322 	safe_constraints = NIL;
1323 	foreach(lc, constraint_pred)
1324 	{
1325 		Node	   *pred = (Node *) lfirst(lc);
1326 
1327 		if (!contain_mutable_functions(pred))
1328 			safe_constraints = lappend(safe_constraints, pred);
1329 	}
1330 
1331 	/*
1332 	 * The constraints are effectively ANDed together, so we can just try to
1333 	 * refute the entire collection at once.  This may allow us to make proofs
1334 	 * that would fail if we took them individually.
1335 	 *
1336 	 * Note: we use rel->baserestrictinfo, not safe_restrictions as might seem
1337 	 * an obvious optimization.  Some of the clauses might be OR clauses that
1338 	 * have volatile and nonvolatile subclauses, and it's OK to make
1339 	 * deductions with the nonvolatile parts.
1340 	 */
1341 	if (predicate_refuted_by(safe_constraints, rel->baserestrictinfo))
1342 		return true;
1343 
1344 	return false;
1345 }
1346 
1347 
1348 /*
1349  * build_physical_tlist
1350  *
1351  * Build a targetlist consisting of exactly the relation's user attributes,
1352  * in order.  The executor can special-case such tlists to avoid a projection
1353  * step at runtime, so we use such tlists preferentially for scan nodes.
1354  *
1355  * Exception: if there are any dropped columns, we punt and return NIL.
1356  * Ideally we would like to handle the dropped-column case too.  However this
1357  * creates problems for ExecTypeFromTL, which may be asked to build a tupdesc
1358  * for a tlist that includes vars of no-longer-existent types.  In theory we
1359  * could dig out the required info from the pg_attribute entries of the
1360  * relation, but that data is not readily available to ExecTypeFromTL.
1361  * For now, we don't apply the physical-tlist optimization when there are
1362  * dropped cols.
1363  *
1364  * We also support building a "physical" tlist for subqueries, functions,
1365  * values lists, and CTEs, since the same optimization can occur in
1366  * SubqueryScan, FunctionScan, ValuesScan, CteScan, and WorkTableScan nodes.
1367  */
1368 List *
build_physical_tlist(PlannerInfo * root,RelOptInfo * rel)1369 build_physical_tlist(PlannerInfo *root, RelOptInfo *rel)
1370 {
1371 	List	   *tlist = NIL;
1372 	Index		varno = rel->relid;
1373 	RangeTblEntry *rte = planner_rt_fetch(varno, root);
1374 	Relation	relation;
1375 	Query	   *subquery;
1376 	Var		   *var;
1377 	ListCell   *l;
1378 	int			attrno,
1379 				numattrs;
1380 	List	   *colvars;
1381 
1382 	switch (rte->rtekind)
1383 	{
1384 		case RTE_RELATION:
1385 			/* Assume we already have adequate lock */
1386 			relation = heap_open(rte->relid, NoLock);
1387 
1388 			numattrs = RelationGetNumberOfAttributes(relation);
1389 			for (attrno = 1; attrno <= numattrs; attrno++)
1390 			{
1391 				Form_pg_attribute att_tup = relation->rd_att->attrs[attrno - 1];
1392 
1393 				if (att_tup->attisdropped)
1394 				{
1395 					/* found a dropped col, so punt */
1396 					tlist = NIL;
1397 					break;
1398 				}
1399 
1400 				var = makeVar(varno,
1401 							  attrno,
1402 							  att_tup->atttypid,
1403 							  att_tup->atttypmod,
1404 							  att_tup->attcollation,
1405 							  0);
1406 
1407 				tlist = lappend(tlist,
1408 								makeTargetEntry((Expr *) var,
1409 												attrno,
1410 												NULL,
1411 												false));
1412 			}
1413 
1414 			heap_close(relation, NoLock);
1415 			break;
1416 
1417 		case RTE_SUBQUERY:
1418 			subquery = rte->subquery;
1419 			foreach(l, subquery->targetList)
1420 			{
1421 				TargetEntry *tle = (TargetEntry *) lfirst(l);
1422 
1423 				/*
1424 				 * A resjunk column of the subquery can be reflected as
1425 				 * resjunk in the physical tlist; we need not punt.
1426 				 */
1427 				var = makeVarFromTargetEntry(varno, tle);
1428 
1429 				tlist = lappend(tlist,
1430 								makeTargetEntry((Expr *) var,
1431 												tle->resno,
1432 												NULL,
1433 												tle->resjunk));
1434 			}
1435 			break;
1436 
1437 		case RTE_FUNCTION:
1438 		case RTE_VALUES:
1439 		case RTE_CTE:
1440 			/* Not all of these can have dropped cols, but share code anyway */
1441 			expandRTE(rte, varno, 0, -1, true /* include dropped */ ,
1442 					  NULL, &colvars);
1443 			foreach(l, colvars)
1444 			{
1445 				var = (Var *) lfirst(l);
1446 
1447 				/*
1448 				 * A non-Var in expandRTE's output means a dropped column;
1449 				 * must punt.
1450 				 */
1451 				if (!IsA(var, Var))
1452 				{
1453 					tlist = NIL;
1454 					break;
1455 				}
1456 
1457 				tlist = lappend(tlist,
1458 								makeTargetEntry((Expr *) var,
1459 												var->varattno,
1460 												NULL,
1461 												false));
1462 			}
1463 			break;
1464 
1465 		default:
1466 			/* caller error */
1467 			elog(ERROR, "unsupported RTE kind %d in build_physical_tlist",
1468 				 (int) rte->rtekind);
1469 			break;
1470 	}
1471 
1472 	return tlist;
1473 }
1474 
1475 /*
1476  * build_index_tlist
1477  *
1478  * Build a targetlist representing the columns of the specified index.
1479  * Each column is represented by a Var for the corresponding base-relation
1480  * column, or an expression in base-relation Vars, as appropriate.
1481  *
1482  * There are never any dropped columns in indexes, so unlike
1483  * build_physical_tlist, we need no failure case.
1484  */
1485 static List *
build_index_tlist(PlannerInfo * root,IndexOptInfo * index,Relation heapRelation)1486 build_index_tlist(PlannerInfo *root, IndexOptInfo *index,
1487 				  Relation heapRelation)
1488 {
1489 	List	   *tlist = NIL;
1490 	Index		varno = index->rel->relid;
1491 	ListCell   *indexpr_item;
1492 	int			i;
1493 
1494 	indexpr_item = list_head(index->indexprs);
1495 	for (i = 0; i < index->ncolumns; i++)
1496 	{
1497 		int			indexkey = index->indexkeys[i];
1498 		Expr	   *indexvar;
1499 
1500 		if (indexkey != 0)
1501 		{
1502 			/* simple column */
1503 			Form_pg_attribute att_tup;
1504 
1505 			if (indexkey < 0)
1506 				att_tup = SystemAttributeDefinition(indexkey,
1507 										   heapRelation->rd_rel->relhasoids);
1508 			else
1509 				att_tup = heapRelation->rd_att->attrs[indexkey - 1];
1510 
1511 			indexvar = (Expr *) makeVar(varno,
1512 										indexkey,
1513 										att_tup->atttypid,
1514 										att_tup->atttypmod,
1515 										att_tup->attcollation,
1516 										0);
1517 		}
1518 		else
1519 		{
1520 			/* expression column */
1521 			if (indexpr_item == NULL)
1522 				elog(ERROR, "wrong number of index expressions");
1523 			indexvar = (Expr *) lfirst(indexpr_item);
1524 			indexpr_item = lnext(indexpr_item);
1525 		}
1526 
1527 		tlist = lappend(tlist,
1528 						makeTargetEntry(indexvar,
1529 										i + 1,
1530 										NULL,
1531 										false));
1532 	}
1533 	if (indexpr_item != NULL)
1534 		elog(ERROR, "wrong number of index expressions");
1535 
1536 	return tlist;
1537 }
1538 
1539 /*
1540  * restriction_selectivity
1541  *
1542  * Returns the selectivity of a specified restriction operator clause.
1543  * This code executes registered procedures stored in the
1544  * operator relation, by calling the function manager.
1545  *
1546  * See clause_selectivity() for the meaning of the additional parameters.
1547  */
1548 Selectivity
restriction_selectivity(PlannerInfo * root,Oid operatorid,List * args,Oid inputcollid,int varRelid)1549 restriction_selectivity(PlannerInfo *root,
1550 						Oid operatorid,
1551 						List *args,
1552 						Oid inputcollid,
1553 						int varRelid)
1554 {
1555 	RegProcedure oprrest = get_oprrest(operatorid);
1556 	float8		result;
1557 
1558 	/*
1559 	 * if the oprrest procedure is missing for whatever reason, use a
1560 	 * selectivity of 0.5
1561 	 */
1562 	if (!oprrest)
1563 		return (Selectivity) 0.5;
1564 
1565 	result = DatumGetFloat8(OidFunctionCall4Coll(oprrest,
1566 												 inputcollid,
1567 												 PointerGetDatum(root),
1568 												 ObjectIdGetDatum(operatorid),
1569 												 PointerGetDatum(args),
1570 												 Int32GetDatum(varRelid)));
1571 
1572 	if (result < 0.0 || result > 1.0)
1573 		elog(ERROR, "invalid restriction selectivity: %f", result);
1574 
1575 	return (Selectivity) result;
1576 }
1577 
1578 /*
1579  * join_selectivity
1580  *
1581  * Returns the selectivity of a specified join operator clause.
1582  * This code executes registered procedures stored in the
1583  * operator relation, by calling the function manager.
1584  */
1585 Selectivity
join_selectivity(PlannerInfo * root,Oid operatorid,List * args,Oid inputcollid,JoinType jointype,SpecialJoinInfo * sjinfo)1586 join_selectivity(PlannerInfo *root,
1587 				 Oid operatorid,
1588 				 List *args,
1589 				 Oid inputcollid,
1590 				 JoinType jointype,
1591 				 SpecialJoinInfo *sjinfo)
1592 {
1593 	RegProcedure oprjoin = get_oprjoin(operatorid);
1594 	float8		result;
1595 
1596 	/*
1597 	 * if the oprjoin procedure is missing for whatever reason, use a
1598 	 * selectivity of 0.5
1599 	 */
1600 	if (!oprjoin)
1601 		return (Selectivity) 0.5;
1602 
1603 	result = DatumGetFloat8(OidFunctionCall5Coll(oprjoin,
1604 												 inputcollid,
1605 												 PointerGetDatum(root),
1606 												 ObjectIdGetDatum(operatorid),
1607 												 PointerGetDatum(args),
1608 												 Int16GetDatum(jointype),
1609 												 PointerGetDatum(sjinfo)));
1610 
1611 	if (result < 0.0 || result > 1.0)
1612 		elog(ERROR, "invalid join selectivity: %f", result);
1613 
1614 	return (Selectivity) result;
1615 }
1616 
1617 /*
1618  * has_unique_index
1619  *
1620  * Detect whether there is a unique index on the specified attribute
1621  * of the specified relation, thus allowing us to conclude that all
1622  * the (non-null) values of the attribute are distinct.
1623  *
1624  * This function does not check the index's indimmediate property, which
1625  * means that uniqueness may transiently fail to hold intra-transaction.
1626  * That's appropriate when we are making statistical estimates, but beware
1627  * of using this for any correctness proofs.
1628  */
1629 bool
has_unique_index(RelOptInfo * rel,AttrNumber attno)1630 has_unique_index(RelOptInfo *rel, AttrNumber attno)
1631 {
1632 	ListCell   *ilist;
1633 
1634 	foreach(ilist, rel->indexlist)
1635 	{
1636 		IndexOptInfo *index = (IndexOptInfo *) lfirst(ilist);
1637 
1638 		/*
1639 		 * Note: ignore partial indexes, since they don't allow us to conclude
1640 		 * that all attr values are distinct, *unless* they are marked predOK
1641 		 * which means we know the index's predicate is satisfied by the
1642 		 * query. We don't take any interest in expressional indexes either.
1643 		 * Also, a multicolumn unique index doesn't allow us to conclude that
1644 		 * just the specified attr is unique.
1645 		 */
1646 		if (index->unique &&
1647 			index->ncolumns == 1 &&
1648 			index->indexkeys[0] == attno &&
1649 			(index->indpred == NIL || index->predOK))
1650 			return true;
1651 	}
1652 	return false;
1653 }
1654 
1655 
1656 /*
1657  * has_row_triggers
1658  *
1659  * Detect whether the specified relation has any row-level triggers for event.
1660  */
1661 bool
has_row_triggers(PlannerInfo * root,Index rti,CmdType event)1662 has_row_triggers(PlannerInfo *root, Index rti, CmdType event)
1663 {
1664 	RangeTblEntry *rte = planner_rt_fetch(rti, root);
1665 	Relation	relation;
1666 	TriggerDesc *trigDesc;
1667 	bool		result = false;
1668 
1669 	/* Assume we already have adequate lock */
1670 	relation = heap_open(rte->relid, NoLock);
1671 
1672 	trigDesc = relation->trigdesc;
1673 	switch (event)
1674 	{
1675 		case CMD_INSERT:
1676 			if (trigDesc &&
1677 				(trigDesc->trig_insert_after_row ||
1678 				 trigDesc->trig_insert_before_row))
1679 				result = true;
1680 			break;
1681 		case CMD_UPDATE:
1682 			if (trigDesc &&
1683 				(trigDesc->trig_update_after_row ||
1684 				 trigDesc->trig_update_before_row))
1685 				result = true;
1686 			break;
1687 		case CMD_DELETE:
1688 			if (trigDesc &&
1689 				(trigDesc->trig_delete_after_row ||
1690 				 trigDesc->trig_delete_before_row))
1691 				result = true;
1692 			break;
1693 		default:
1694 			elog(ERROR, "unrecognized CmdType: %d", (int) event);
1695 			break;
1696 	}
1697 
1698 	heap_close(relation, NoLock);
1699 	return result;
1700 }
1701