1 /*-------------------------------------------------------------------------
2  *
3  * partbounds.c
4  *		Support routines for manipulating partition bounds
5  *
6  * Portions Copyright (c) 1996-2018, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  * IDENTIFICATION
10  *		  src/backend/partitioning/partbounds.c
11  *
12  *-------------------------------------------------------------------------
13 */
14 #include "postgres.h"
15 
16 #include "catalog/partition.h"
17 #include "catalog/pg_inherits.h"
18 #include "catalog/pg_type.h"
19 #include "commands/tablecmds.h"
20 #include "executor/executor.h"
21 #include "miscadmin.h"
22 #include "nodes/makefuncs.h"
23 #include "nodes/nodeFuncs.h"
24 #include "optimizer/clauses.h"
25 #include "parser/parse_coerce.h"
26 #include "partitioning/partprune.h"
27 #include "partitioning/partbounds.h"
28 #include "utils/builtins.h"
29 #include "utils/datum.h"
30 #include "utils/fmgroids.h"
31 #include "utils/hashutils.h"
32 #include "utils/lsyscache.h"
33 #include "utils/partcache.h"
34 #include "utils/rel.h"
35 #include "utils/snapmgr.h"
36 #include "utils/ruleutils.h"
37 #include "utils/syscache.h"
38 
39 static Expr *make_partition_op_expr(PartitionKey key, int keynum,
40 					   uint16 strategy, Expr *arg1, Expr *arg2);
41 static Oid get_partition_operator(PartitionKey key, int col,
42 					   StrategyNumber strategy, bool *need_relabel);
43 static List *get_qual_for_hash(Relation parent, PartitionBoundSpec *spec);
44 static List *get_qual_for_list(Relation parent, PartitionBoundSpec *spec);
45 static List *get_qual_for_range(Relation parent, PartitionBoundSpec *spec,
46 				   bool for_default);
47 static void get_range_key_properties(PartitionKey key, int keynum,
48 						 PartitionRangeDatum *ldatum,
49 						 PartitionRangeDatum *udatum,
50 						 ListCell **partexprs_item,
51 						 Expr **keyCol,
52 						 Const **lower_val, Const **upper_val);
53 static List *get_range_nulltest(PartitionKey key);
54 
55 /*
56  * get_qual_from_partbound
57  *		Given a parser node for partition bound, return the list of executable
58  *		expressions as partition constraint
59  */
60 List *
get_qual_from_partbound(Relation rel,Relation parent,PartitionBoundSpec * spec)61 get_qual_from_partbound(Relation rel, Relation parent,
62 						PartitionBoundSpec *spec)
63 {
64 	PartitionKey key = RelationGetPartitionKey(parent);
65 	List	   *my_qual = NIL;
66 
67 	Assert(key != NULL);
68 
69 	switch (key->strategy)
70 	{
71 		case PARTITION_STRATEGY_HASH:
72 			Assert(spec->strategy == PARTITION_STRATEGY_HASH);
73 			my_qual = get_qual_for_hash(parent, spec);
74 			break;
75 
76 		case PARTITION_STRATEGY_LIST:
77 			Assert(spec->strategy == PARTITION_STRATEGY_LIST);
78 			my_qual = get_qual_for_list(parent, spec);
79 			break;
80 
81 		case PARTITION_STRATEGY_RANGE:
82 			Assert(spec->strategy == PARTITION_STRATEGY_RANGE);
83 			my_qual = get_qual_for_range(parent, spec, false);
84 			break;
85 
86 		default:
87 			elog(ERROR, "unexpected partition strategy: %d",
88 				 (int) key->strategy);
89 	}
90 
91 	return my_qual;
92 }
93 
94 /*
95  * Are two partition bound collections logically equal?
96  *
97  * Used in the keep logic of relcache.c (ie, in RelationClearRelation()).
98  * This is also useful when b1 and b2 are bound collections of two separate
99  * relations, respectively, because PartitionBoundInfo is a canonical
100  * representation of partition bounds.
101  */
102 bool
partition_bounds_equal(int partnatts,int16 * parttyplen,bool * parttypbyval,PartitionBoundInfo b1,PartitionBoundInfo b2)103 partition_bounds_equal(int partnatts, int16 *parttyplen, bool *parttypbyval,
104 					   PartitionBoundInfo b1, PartitionBoundInfo b2)
105 {
106 	int			i;
107 
108 	if (b1->strategy != b2->strategy)
109 		return false;
110 
111 	if (b1->ndatums != b2->ndatums)
112 		return false;
113 
114 	if (b1->nindexes != b2->nindexes)
115 		return false;
116 
117 	if (b1->null_index != b2->null_index)
118 		return false;
119 
120 	if (b1->default_index != b2->default_index)
121 		return false;
122 
123 	/* For all partition strategies, the indexes[] arrays have to match */
124 	for (i = 0; i < b1->nindexes; i++)
125 	{
126 		if (b1->indexes[i] != b2->indexes[i])
127 			return false;
128 	}
129 
130 	/* Finally, compare the datums[] arrays */
131 	if (b1->strategy == PARTITION_STRATEGY_HASH)
132 	{
133 		/*
134 		 * We arrange the partitions in the ascending order of their moduli
135 		 * and remainders.  Also every modulus is factor of next larger
136 		 * modulus.  Therefore we can safely store index of a given partition
137 		 * in indexes array at remainder of that partition.  Also entries at
138 		 * (remainder + N * modulus) positions in indexes array are all same
139 		 * for (modulus, remainder) specification for any partition.  Thus the
140 		 * datums arrays from the given bounds are the same, if and only if
141 		 * their indexes arrays are the same.  So, it suffices to compare the
142 		 * indexes arrays.
143 		 *
144 		 * Nonetheless make sure that the bounds are indeed the same when the
145 		 * indexes match.  Hash partition bound stores modulus and remainder
146 		 * at b1->datums[i][0] and b1->datums[i][1] position respectively.
147 		 */
148 #ifdef USE_ASSERT_CHECKING
149 		for (i = 0; i < b1->ndatums; i++)
150 			Assert((b1->datums[i][0] == b2->datums[i][0] &&
151 					b1->datums[i][1] == b2->datums[i][1]));
152 #endif
153 	}
154 	else
155 	{
156 		for (i = 0; i < b1->ndatums; i++)
157 		{
158 			int			j;
159 
160 			for (j = 0; j < partnatts; j++)
161 			{
162 				/* For range partitions, the bounds might not be finite. */
163 				if (b1->kind != NULL)
164 				{
165 					/* The different kinds of bound all differ from each other */
166 					if (b1->kind[i][j] != b2->kind[i][j])
167 						return false;
168 
169 					/*
170 					 * Non-finite bounds are equal without further
171 					 * examination.
172 					 */
173 					if (b1->kind[i][j] != PARTITION_RANGE_DATUM_VALUE)
174 						continue;
175 				}
176 
177 				/*
178 				 * Compare the actual values. Note that it would be both
179 				 * incorrect and unsafe to invoke the comparison operator
180 				 * derived from the partitioning specification here.  It would
181 				 * be incorrect because we want the relcache entry to be
182 				 * updated for ANY change to the partition bounds, not just
183 				 * those that the partitioning operator thinks are
184 				 * significant.  It would be unsafe because we might reach
185 				 * this code in the context of an aborted transaction, and an
186 				 * arbitrary partitioning operator might not be safe in that
187 				 * context.  datumIsEqual() should be simple enough to be
188 				 * safe.
189 				 */
190 				if (!datumIsEqual(b1->datums[i][j], b2->datums[i][j],
191 								  parttypbyval[j], parttyplen[j]))
192 					return false;
193 			}
194 		}
195 	}
196 	return true;
197 }
198 
199 /*
200  * Return a copy of given PartitionBoundInfo structure. The data types of bounds
201  * are described by given partition key specification.
202  */
203 PartitionBoundInfo
partition_bounds_copy(PartitionBoundInfo src,PartitionKey key)204 partition_bounds_copy(PartitionBoundInfo src,
205 					  PartitionKey key)
206 {
207 	PartitionBoundInfo dest;
208 	int			i;
209 	int			ndatums;
210 	int			nindexes;
211 	int			partnatts;
212 
213 	dest = (PartitionBoundInfo) palloc(sizeof(PartitionBoundInfoData));
214 
215 	dest->strategy = src->strategy;
216 	ndatums = dest->ndatums = src->ndatums;
217 	nindexes = dest->nindexes = src->nindexes;
218 	partnatts = key->partnatts;
219 
220 	/* List partitioned tables have only a single partition key. */
221 	Assert(key->strategy != PARTITION_STRATEGY_LIST || partnatts == 1);
222 
223 	dest->datums = (Datum **) palloc(sizeof(Datum *) * ndatums);
224 
225 	if (src->kind != NULL)
226 	{
227 		dest->kind = (PartitionRangeDatumKind **) palloc(ndatums *
228 														 sizeof(PartitionRangeDatumKind *));
229 		for (i = 0; i < ndatums; i++)
230 		{
231 			dest->kind[i] = (PartitionRangeDatumKind *) palloc(partnatts *
232 															   sizeof(PartitionRangeDatumKind));
233 
234 			memcpy(dest->kind[i], src->kind[i],
235 				   sizeof(PartitionRangeDatumKind) * key->partnatts);
236 		}
237 	}
238 	else
239 		dest->kind = NULL;
240 
241 	for (i = 0; i < ndatums; i++)
242 	{
243 		int			j;
244 
245 		/*
246 		 * For a corresponding hash partition, datums array will have two
247 		 * elements - modulus and remainder.
248 		 */
249 		bool		hash_part = (key->strategy == PARTITION_STRATEGY_HASH);
250 		int			natts = hash_part ? 2 : partnatts;
251 
252 		dest->datums[i] = (Datum *) palloc(sizeof(Datum) * natts);
253 
254 		for (j = 0; j < natts; j++)
255 		{
256 			bool		byval;
257 			int			typlen;
258 
259 			if (hash_part)
260 			{
261 				typlen = sizeof(int32); /* Always int4 */
262 				byval = true;	/* int4 is pass-by-value */
263 			}
264 			else
265 			{
266 				byval = key->parttypbyval[j];
267 				typlen = key->parttyplen[j];
268 			}
269 
270 			if (dest->kind == NULL ||
271 				dest->kind[i][j] == PARTITION_RANGE_DATUM_VALUE)
272 				dest->datums[i][j] = datumCopy(src->datums[i][j],
273 											   byval, typlen);
274 		}
275 	}
276 
277 	dest->indexes = (int *) palloc(sizeof(int) * nindexes);
278 	memcpy(dest->indexes, src->indexes, sizeof(int) * nindexes);
279 
280 	dest->null_index = src->null_index;
281 	dest->default_index = src->default_index;
282 
283 	return dest;
284 }
285 
286 /*
287  * check_new_partition_bound
288  *
289  * Checks if the new partition's bound overlaps any of the existing partitions
290  * of parent.  Also performs additional checks as necessary per strategy.
291  */
292 void
check_new_partition_bound(char * relname,Relation parent,PartitionBoundSpec * spec)293 check_new_partition_bound(char *relname, Relation parent,
294 						  PartitionBoundSpec *spec)
295 {
296 	PartitionKey key = RelationGetPartitionKey(parent);
297 	PartitionDesc partdesc = RelationGetPartitionDesc(parent);
298 	PartitionBoundInfo boundinfo = partdesc->boundinfo;
299 	ParseState *pstate = make_parsestate(NULL);
300 	int			with = -1;
301 	bool		overlap = false;
302 
303 	if (spec->is_default)
304 	{
305 		/*
306 		 * The default partition bound never conflicts with any other
307 		 * partition's; if that's what we're attaching, the only possible
308 		 * problem is that one already exists, so check for that and we're
309 		 * done.
310 		 */
311 		if (boundinfo == NULL || !partition_bound_has_default(boundinfo))
312 			return;
313 
314 		/* Default partition already exists, error out. */
315 		ereport(ERROR,
316 				(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
317 				 errmsg("partition \"%s\" conflicts with existing default partition \"%s\"",
318 						relname, get_rel_name(partdesc->oids[boundinfo->default_index])),
319 				 parser_errposition(pstate, spec->location)));
320 	}
321 
322 	switch (key->strategy)
323 	{
324 		case PARTITION_STRATEGY_HASH:
325 			{
326 				Assert(spec->strategy == PARTITION_STRATEGY_HASH);
327 				Assert(spec->remainder >= 0 && spec->remainder < spec->modulus);
328 
329 				if (partdesc->nparts > 0)
330 				{
331 					Datum	  **datums = boundinfo->datums;
332 					int			ndatums = boundinfo->ndatums;
333 					int			greatest_modulus;
334 					int			remainder;
335 					int			offset;
336 					bool		valid_modulus = true;
337 					int			prev_modulus,	/* Previous largest modulus */
338 								next_modulus;	/* Next largest modulus */
339 
340 					/*
341 					 * Check rule that every modulus must be a factor of the
342 					 * next larger modulus.  For example, if you have a bunch
343 					 * of partitions that all have modulus 5, you can add a
344 					 * new partition with modulus 10 or a new partition with
345 					 * modulus 15, but you cannot add both a partition with
346 					 * modulus 10 and a partition with modulus 15, because 10
347 					 * is not a factor of 15.
348 					 *
349 					 * Get the greatest (modulus, remainder) pair contained in
350 					 * boundinfo->datums that is less than or equal to the
351 					 * (spec->modulus, spec->remainder) pair.
352 					 */
353 					offset = partition_hash_bsearch(boundinfo,
354 													spec->modulus,
355 													spec->remainder);
356 					if (offset < 0)
357 					{
358 						next_modulus = DatumGetInt32(datums[0][0]);
359 						valid_modulus = (next_modulus % spec->modulus) == 0;
360 					}
361 					else
362 					{
363 						prev_modulus = DatumGetInt32(datums[offset][0]);
364 						valid_modulus = (spec->modulus % prev_modulus) == 0;
365 
366 						if (valid_modulus && (offset + 1) < ndatums)
367 						{
368 							next_modulus = DatumGetInt32(datums[offset + 1][0]);
369 							valid_modulus = (next_modulus % spec->modulus) == 0;
370 						}
371 					}
372 
373 					if (!valid_modulus)
374 						ereport(ERROR,
375 								(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
376 								 errmsg("every hash partition modulus must be a factor of the next larger modulus")));
377 
378 					greatest_modulus = boundinfo->nindexes;
379 					remainder = spec->remainder;
380 
381 					/*
382 					 * Normally, the lowest remainder that could conflict with
383 					 * the new partition is equal to the remainder specified
384 					 * for the new partition, but when the new partition has a
385 					 * modulus higher than any used so far, we need to adjust.
386 					 */
387 					if (remainder >= greatest_modulus)
388 						remainder = remainder % greatest_modulus;
389 
390 					/* Check every potentially-conflicting remainder. */
391 					do
392 					{
393 						if (boundinfo->indexes[remainder] != -1)
394 						{
395 							overlap = true;
396 							with = boundinfo->indexes[remainder];
397 							break;
398 						}
399 						remainder += spec->modulus;
400 					} while (remainder < greatest_modulus);
401 				}
402 
403 				break;
404 			}
405 
406 		case PARTITION_STRATEGY_LIST:
407 			{
408 				Assert(spec->strategy == PARTITION_STRATEGY_LIST);
409 
410 				if (partdesc->nparts > 0)
411 				{
412 					ListCell   *cell;
413 
414 					Assert(boundinfo &&
415 						   boundinfo->strategy == PARTITION_STRATEGY_LIST &&
416 						   (boundinfo->ndatums > 0 ||
417 							partition_bound_accepts_nulls(boundinfo) ||
418 							partition_bound_has_default(boundinfo)));
419 
420 					foreach(cell, spec->listdatums)
421 					{
422 						Const	   *val = castNode(Const, lfirst(cell));
423 
424 						if (!val->constisnull)
425 						{
426 							int			offset;
427 							bool		equal;
428 
429 							offset = partition_list_bsearch(&key->partsupfunc[0],
430 															key->partcollation,
431 															boundinfo,
432 															val->constvalue,
433 															&equal);
434 							if (offset >= 0 && equal)
435 							{
436 								overlap = true;
437 								with = boundinfo->indexes[offset];
438 								break;
439 							}
440 						}
441 						else if (partition_bound_accepts_nulls(boundinfo))
442 						{
443 							overlap = true;
444 							with = boundinfo->null_index;
445 							break;
446 						}
447 					}
448 				}
449 
450 				break;
451 			}
452 
453 		case PARTITION_STRATEGY_RANGE:
454 			{
455 				PartitionRangeBound *lower,
456 						   *upper;
457 
458 				Assert(spec->strategy == PARTITION_STRATEGY_RANGE);
459 				lower = make_one_partition_rbound(key, -1, spec->lowerdatums, true);
460 				upper = make_one_partition_rbound(key, -1, spec->upperdatums, false);
461 
462 				/*
463 				 * First check if the resulting range would be empty with
464 				 * specified lower and upper bounds
465 				 */
466 				if (partition_rbound_cmp(key->partnatts, key->partsupfunc,
467 										 key->partcollation, lower->datums,
468 										 lower->kind, true, upper) >= 0)
469 				{
470 					ereport(ERROR,
471 							(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
472 							 errmsg("empty range bound specified for partition \"%s\"",
473 									relname),
474 							 errdetail("Specified lower bound %s is greater than or equal to upper bound %s.",
475 									   get_range_partbound_string(spec->lowerdatums),
476 									   get_range_partbound_string(spec->upperdatums)),
477 							 parser_errposition(pstate, spec->location)));
478 				}
479 
480 				if (partdesc->nparts > 0)
481 				{
482 					int			offset;
483 					bool		equal;
484 
485 					Assert(boundinfo &&
486 						   boundinfo->strategy == PARTITION_STRATEGY_RANGE &&
487 						   (boundinfo->ndatums > 0 ||
488 							partition_bound_has_default(boundinfo)));
489 
490 					/*
491 					 * Test whether the new lower bound (which is treated
492 					 * inclusively as part of the new partition) lies inside
493 					 * an existing partition, or in a gap.
494 					 *
495 					 * If it's inside an existing partition, the bound at
496 					 * offset + 1 will be the upper bound of that partition,
497 					 * and its index will be >= 0.
498 					 *
499 					 * If it's in a gap, the bound at offset + 1 will be the
500 					 * lower bound of the next partition, and its index will
501 					 * be -1. This is also true if there is no next partition,
502 					 * since the index array is initialised with an extra -1
503 					 * at the end.
504 					 */
505 					offset = partition_range_bsearch(key->partnatts,
506 													 key->partsupfunc,
507 													 key->partcollation,
508 													 boundinfo, lower,
509 													 &equal);
510 
511 					if (boundinfo->indexes[offset + 1] < 0)
512 					{
513 						/*
514 						 * Check that the new partition will fit in the gap.
515 						 * For it to fit, the new upper bound must be less
516 						 * than or equal to the lower bound of the next
517 						 * partition, if there is one.
518 						 */
519 						if (offset + 1 < boundinfo->ndatums)
520 						{
521 							int32		cmpval;
522 							Datum	   *datums;
523 							PartitionRangeDatumKind *kind;
524 							bool		is_lower;
525 
526 							datums = boundinfo->datums[offset + 1];
527 							kind = boundinfo->kind[offset + 1];
528 							is_lower = (boundinfo->indexes[offset + 1] == -1);
529 
530 							cmpval = partition_rbound_cmp(key->partnatts,
531 														  key->partsupfunc,
532 														  key->partcollation,
533 														  datums, kind,
534 														  is_lower, upper);
535 							if (cmpval < 0)
536 							{
537 								/*
538 								 * The new partition overlaps with the
539 								 * existing partition between offset + 1 and
540 								 * offset + 2.
541 								 */
542 								overlap = true;
543 								with = boundinfo->indexes[offset + 2];
544 							}
545 						}
546 					}
547 					else
548 					{
549 						/*
550 						 * The new partition overlaps with the existing
551 						 * partition between offset and offset + 1.
552 						 */
553 						overlap = true;
554 						with = boundinfo->indexes[offset + 1];
555 					}
556 				}
557 
558 				break;
559 			}
560 
561 		default:
562 			elog(ERROR, "unexpected partition strategy: %d",
563 				 (int) key->strategy);
564 	}
565 
566 	if (overlap)
567 	{
568 		Assert(with >= 0);
569 		ereport(ERROR,
570 				(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
571 				 errmsg("partition \"%s\" would overlap partition \"%s\"",
572 						relname, get_rel_name(partdesc->oids[with])),
573 				 parser_errposition(pstate, spec->location)));
574 	}
575 }
576 
577 /*
578  * check_default_partition_contents
579  *
580  * This function checks if there exists a row in the default partition that
581  * would properly belong to the new partition being added.  If it finds one,
582  * it throws an error.
583  */
584 void
check_default_partition_contents(Relation parent,Relation default_rel,PartitionBoundSpec * new_spec)585 check_default_partition_contents(Relation parent, Relation default_rel,
586 								 PartitionBoundSpec *new_spec)
587 {
588 	List	   *new_part_constraints;
589 	List	   *def_part_constraints;
590 	List	   *all_parts;
591 	ListCell   *lc;
592 
593 	new_part_constraints = (new_spec->strategy == PARTITION_STRATEGY_LIST)
594 		? get_qual_for_list(parent, new_spec)
595 		: get_qual_for_range(parent, new_spec, false);
596 	def_part_constraints =
597 		get_proposed_default_constraint(new_part_constraints);
598 	/*
599 	 * Map the Vars in the constraint expression from parent's attnos to
600 	 * default_rel's.
601 	 */
602 	def_part_constraints =
603 			map_partition_varattnos(def_part_constraints, 1, default_rel,
604 									parent, NULL);
605 
606 	/*
607 	 * If the existing constraints on the default partition imply that it will
608 	 * not contain any row that would belong to the new partition, we can
609 	 * avoid scanning the default partition.
610 	 */
611 	if (PartConstraintImpliedByRelConstraint(default_rel, def_part_constraints))
612 	{
613 		ereport(INFO,
614 				(errmsg("updated partition constraint for default partition \"%s\" is implied by existing constraints",
615 						RelationGetRelationName(default_rel))));
616 		return;
617 	}
618 
619 	/*
620 	 * Scan the default partition and its subpartitions, and check for rows
621 	 * that do not satisfy the revised partition constraints.
622 	 */
623 	if (default_rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
624 		all_parts = find_all_inheritors(RelationGetRelid(default_rel),
625 										AccessExclusiveLock, NULL);
626 	else
627 		all_parts = list_make1_oid(RelationGetRelid(default_rel));
628 
629 	foreach(lc, all_parts)
630 	{
631 		Oid			part_relid = lfirst_oid(lc);
632 		Relation	part_rel;
633 		Expr	   *partition_constraint;
634 		EState	   *estate;
635 		HeapTuple	tuple;
636 		ExprState  *partqualstate = NULL;
637 		Snapshot	snapshot;
638 		TupleDesc	tupdesc;
639 		ExprContext *econtext;
640 		HeapScanDesc scan;
641 		MemoryContext oldCxt;
642 		TupleTableSlot *tupslot;
643 
644 		/* Lock already taken above. */
645 		if (part_relid != RelationGetRelid(default_rel))
646 		{
647 			part_rel = heap_open(part_relid, NoLock);
648 
649 			/*
650 			 * Map the Vars in the constraint expression from default_rel's
651 			 * the sub-partition's.
652 			 */
653 			partition_constraint = make_ands_explicit(def_part_constraints);
654 			partition_constraint = (Expr *)
655 				map_partition_varattnos((List *) partition_constraint, 1,
656 										part_rel, default_rel, NULL);
657 
658 			/*
659 			 * If the partition constraints on default partition child imply
660 			 * that it will not contain any row that would belong to the new
661 			 * partition, we can avoid scanning the child table.
662 			 */
663 			if (PartConstraintImpliedByRelConstraint(part_rel,
664 													 def_part_constraints))
665 			{
666 				ereport(INFO,
667 						(errmsg("updated partition constraint for default partition \"%s\" is implied by existing constraints",
668 								RelationGetRelationName(part_rel))));
669 
670 				heap_close(part_rel, NoLock);
671 				continue;
672 			}
673 		}
674 		else
675 		{
676 			part_rel = default_rel;
677 			partition_constraint = make_ands_explicit(def_part_constraints);
678 		}
679 
680 		/*
681 		 * Only RELKIND_RELATION relations (i.e. leaf partitions) need to be
682 		 * scanned.
683 		 */
684 		if (part_rel->rd_rel->relkind != RELKIND_RELATION)
685 		{
686 			if (part_rel->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
687 				ereport(WARNING,
688 						(errcode(ERRCODE_CHECK_VIOLATION),
689 						 errmsg("skipped scanning foreign table \"%s\" which is a partition of default partition \"%s\"",
690 								RelationGetRelationName(part_rel),
691 								RelationGetRelationName(default_rel))));
692 
693 			if (RelationGetRelid(default_rel) != RelationGetRelid(part_rel))
694 				heap_close(part_rel, NoLock);
695 
696 			continue;
697 		}
698 
699 		tupdesc = CreateTupleDescCopy(RelationGetDescr(part_rel));
700 		estate = CreateExecutorState();
701 
702 		/* Build expression execution states for partition check quals */
703 		partqualstate = ExecPrepareExpr(partition_constraint, estate);
704 
705 		econtext = GetPerTupleExprContext(estate);
706 		snapshot = RegisterSnapshot(GetLatestSnapshot());
707 		scan = heap_beginscan(part_rel, snapshot, 0, NULL);
708 		tupslot = MakeSingleTupleTableSlot(tupdesc);
709 
710 		/*
711 		 * Switch to per-tuple memory context and reset it for each tuple
712 		 * produced, so we don't leak memory.
713 		 */
714 		oldCxt = MemoryContextSwitchTo(GetPerTupleMemoryContext(estate));
715 
716 		while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
717 		{
718 			ExecStoreTuple(tuple, tupslot, InvalidBuffer, false);
719 			econtext->ecxt_scantuple = tupslot;
720 
721 			if (!ExecCheck(partqualstate, econtext))
722 				ereport(ERROR,
723 						(errcode(ERRCODE_CHECK_VIOLATION),
724 						 errmsg("updated partition constraint for default partition \"%s\" would be violated by some row",
725 								RelationGetRelationName(default_rel))));
726 
727 			ResetExprContext(econtext);
728 			CHECK_FOR_INTERRUPTS();
729 		}
730 
731 		MemoryContextSwitchTo(oldCxt);
732 		heap_endscan(scan);
733 		UnregisterSnapshot(snapshot);
734 		ExecDropSingleTupleTableSlot(tupslot);
735 		FreeExecutorState(estate);
736 
737 		if (RelationGetRelid(default_rel) != RelationGetRelid(part_rel))
738 			heap_close(part_rel, NoLock);	/* keep the lock until commit */
739 	}
740 }
741 
742 /*
743  * get_hash_partition_greatest_modulus
744  *
745  * Returns the greatest modulus of the hash partition bound.
746  * This is no longer used in the core code, but we keep it around
747  * in case external modules are using it.
748  */
749 int
get_hash_partition_greatest_modulus(PartitionBoundInfo bound)750 get_hash_partition_greatest_modulus(PartitionBoundInfo bound)
751 {
752 	Assert(bound && bound->strategy == PARTITION_STRATEGY_HASH);
753 	return bound->nindexes;
754 }
755 
756 /*
757  * make_one_partition_rbound
758  *
759  * Return a PartitionRangeBound given a list of PartitionRangeDatum elements
760  * and a flag telling whether the bound is lower or not.  Made into a function
761  * because there are multiple sites that want to use this facility.
762  */
763 PartitionRangeBound *
make_one_partition_rbound(PartitionKey key,int index,List * datums,bool lower)764 make_one_partition_rbound(PartitionKey key, int index, List *datums, bool lower)
765 {
766 	PartitionRangeBound *bound;
767 	ListCell   *lc;
768 	int			i;
769 
770 	Assert(datums != NIL);
771 
772 	bound = (PartitionRangeBound *) palloc0(sizeof(PartitionRangeBound));
773 	bound->index = index;
774 	bound->datums = (Datum *) palloc0(key->partnatts * sizeof(Datum));
775 	bound->kind = (PartitionRangeDatumKind *) palloc0(key->partnatts *
776 													  sizeof(PartitionRangeDatumKind));
777 	bound->lower = lower;
778 
779 	i = 0;
780 	foreach(lc, datums)
781 	{
782 		PartitionRangeDatum *datum = castNode(PartitionRangeDatum, lfirst(lc));
783 
784 		/* What's contained in this range datum? */
785 		bound->kind[i] = datum->kind;
786 
787 		if (datum->kind == PARTITION_RANGE_DATUM_VALUE)
788 		{
789 			Const	   *val = castNode(Const, datum->value);
790 
791 			if (val->constisnull)
792 				elog(ERROR, "invalid range bound datum");
793 			bound->datums[i] = val->constvalue;
794 		}
795 
796 		i++;
797 	}
798 
799 	return bound;
800 }
801 
802 /*
803  * partition_rbound_cmp
804  *
805  * Return for two range bounds whether the 1st one (specified in datums1,
806  * kind1, and lower1) is <, =, or > the bound specified in *b2.
807  *
808  * partnatts, partsupfunc and partcollation give the number of attributes in the
809  * bounds to be compared, comparison function to be used and the collations of
810  * attributes, respectively.
811  *
812  * Note that if the values of the two range bounds compare equal, then we take
813  * into account whether they are upper or lower bounds, and an upper bound is
814  * considered to be smaller than a lower bound. This is important to the way
815  * that RelationBuildPartitionDesc() builds the PartitionBoundInfoData
816  * structure, which only stores the upper bound of a common boundary between
817  * two contiguous partitions.
818  */
819 int32
partition_rbound_cmp(int partnatts,FmgrInfo * partsupfunc,Oid * partcollation,Datum * datums1,PartitionRangeDatumKind * kind1,bool lower1,PartitionRangeBound * b2)820 partition_rbound_cmp(int partnatts, FmgrInfo *partsupfunc,
821 					 Oid *partcollation,
822 					 Datum *datums1, PartitionRangeDatumKind *kind1,
823 					 bool lower1, PartitionRangeBound *b2)
824 {
825 	int32		cmpval = 0;		/* placate compiler */
826 	int			i;
827 	Datum	   *datums2 = b2->datums;
828 	PartitionRangeDatumKind *kind2 = b2->kind;
829 	bool		lower2 = b2->lower;
830 
831 	for (i = 0; i < partnatts; i++)
832 	{
833 		/*
834 		 * First, handle cases where the column is unbounded, which should not
835 		 * invoke the comparison procedure, and should not consider any later
836 		 * columns. Note that the PartitionRangeDatumKind enum elements
837 		 * compare the same way as the values they represent.
838 		 */
839 		if (kind1[i] < kind2[i])
840 			return -1;
841 		else if (kind1[i] > kind2[i])
842 			return 1;
843 		else if (kind1[i] != PARTITION_RANGE_DATUM_VALUE)
844 
845 			/*
846 			 * The column bounds are both MINVALUE or both MAXVALUE. No later
847 			 * columns should be considered, but we still need to compare
848 			 * whether they are upper or lower bounds.
849 			 */
850 			break;
851 
852 		cmpval = DatumGetInt32(FunctionCall2Coll(&partsupfunc[i],
853 												 partcollation[i],
854 												 datums1[i],
855 												 datums2[i]));
856 		if (cmpval != 0)
857 			break;
858 	}
859 
860 	/*
861 	 * If the comparison is anything other than equal, we're done. If they
862 	 * compare equal though, we still have to consider whether the boundaries
863 	 * are inclusive or exclusive.  Exclusive one is considered smaller of the
864 	 * two.
865 	 */
866 	if (cmpval == 0 && lower1 != lower2)
867 		cmpval = lower1 ? 1 : -1;
868 
869 	return cmpval;
870 }
871 
872 /*
873  * partition_rbound_datum_cmp
874  *
875  * Return whether range bound (specified in rb_datums, rb_kind, and rb_lower)
876  * is <, =, or > partition key of tuple (tuple_datums)
877  *
878  * n_tuple_datums, partsupfunc and partcollation give number of attributes in
879  * the bounds to be compared, comparison function to be used and the collations
880  * of attributes resp.
881  *
882  */
883 int32
partition_rbound_datum_cmp(FmgrInfo * partsupfunc,Oid * partcollation,Datum * rb_datums,PartitionRangeDatumKind * rb_kind,Datum * tuple_datums,int n_tuple_datums)884 partition_rbound_datum_cmp(FmgrInfo *partsupfunc, Oid *partcollation,
885 						   Datum *rb_datums, PartitionRangeDatumKind *rb_kind,
886 						   Datum *tuple_datums, int n_tuple_datums)
887 {
888 	int			i;
889 	int32		cmpval = -1;
890 
891 	for (i = 0; i < n_tuple_datums; i++)
892 	{
893 		if (rb_kind[i] == PARTITION_RANGE_DATUM_MINVALUE)
894 			return -1;
895 		else if (rb_kind[i] == PARTITION_RANGE_DATUM_MAXVALUE)
896 			return 1;
897 
898 		cmpval = DatumGetInt32(FunctionCall2Coll(&partsupfunc[i],
899 												 partcollation[i],
900 												 rb_datums[i],
901 												 tuple_datums[i]));
902 		if (cmpval != 0)
903 			break;
904 	}
905 
906 	return cmpval;
907 }
908 
909 /*
910  * partition_hbound_cmp
911  *
912  * Compares modulus first, then remainder if modulus is equal.
913  */
914 int32
partition_hbound_cmp(int modulus1,int remainder1,int modulus2,int remainder2)915 partition_hbound_cmp(int modulus1, int remainder1, int modulus2, int remainder2)
916 {
917 	if (modulus1 < modulus2)
918 		return -1;
919 	if (modulus1 > modulus2)
920 		return 1;
921 	if (modulus1 == modulus2 && remainder1 != remainder2)
922 		return (remainder1 > remainder2) ? 1 : -1;
923 	return 0;
924 }
925 
926 /*
927  * partition_list_bsearch
928  *		Returns the index of the greatest bound datum that is less than equal
929  * 		to the given value or -1 if all of the bound datums are greater
930  *
931  * *is_equal is set to true if the bound datum at the returned index is equal
932  * to the input value.
933  */
934 int
partition_list_bsearch(FmgrInfo * partsupfunc,Oid * partcollation,PartitionBoundInfo boundinfo,Datum value,bool * is_equal)935 partition_list_bsearch(FmgrInfo *partsupfunc, Oid *partcollation,
936 					   PartitionBoundInfo boundinfo,
937 					   Datum value, bool *is_equal)
938 {
939 	int			lo,
940 				hi,
941 				mid;
942 
943 	lo = -1;
944 	hi = boundinfo->ndatums - 1;
945 	while (lo < hi)
946 	{
947 		int32		cmpval;
948 
949 		mid = (lo + hi + 1) / 2;
950 		cmpval = DatumGetInt32(FunctionCall2Coll(&partsupfunc[0],
951 												 partcollation[0],
952 												 boundinfo->datums[mid][0],
953 												 value));
954 		if (cmpval <= 0)
955 		{
956 			lo = mid;
957 			*is_equal = (cmpval == 0);
958 			if (*is_equal)
959 				break;
960 		}
961 		else
962 			hi = mid - 1;
963 	}
964 
965 	return lo;
966 }
967 
968 /*
969  * partition_range_bsearch
970  *		Returns the index of the greatest range bound that is less than or
971  *		equal to the given range bound or -1 if all of the range bounds are
972  *		greater
973  *
974  * *is_equal is set to true if the range bound at the returned index is equal
975  * to the input range bound
976  */
977 int
partition_range_bsearch(int partnatts,FmgrInfo * partsupfunc,Oid * partcollation,PartitionBoundInfo boundinfo,PartitionRangeBound * probe,bool * is_equal)978 partition_range_bsearch(int partnatts, FmgrInfo *partsupfunc,
979 						Oid *partcollation,
980 						PartitionBoundInfo boundinfo,
981 						PartitionRangeBound *probe, bool *is_equal)
982 {
983 	int			lo,
984 				hi,
985 				mid;
986 
987 	lo = -1;
988 	hi = boundinfo->ndatums - 1;
989 	while (lo < hi)
990 	{
991 		int32		cmpval;
992 
993 		mid = (lo + hi + 1) / 2;
994 		cmpval = partition_rbound_cmp(partnatts, partsupfunc,
995 									  partcollation,
996 									  boundinfo->datums[mid],
997 									  boundinfo->kind[mid],
998 									  (boundinfo->indexes[mid] == -1),
999 									  probe);
1000 		if (cmpval <= 0)
1001 		{
1002 			lo = mid;
1003 			*is_equal = (cmpval == 0);
1004 
1005 			if (*is_equal)
1006 				break;
1007 		}
1008 		else
1009 			hi = mid - 1;
1010 	}
1011 
1012 	return lo;
1013 }
1014 
1015 /*
1016  * partition_range_bsearch
1017  *		Returns the index of the greatest range bound that is less than or
1018  *		equal to the given tuple or -1 if all of the range bounds are greater
1019  *
1020  * *is_equal is set to true if the range bound at the returned index is equal
1021  * to the input tuple.
1022  */
1023 int
partition_range_datum_bsearch(FmgrInfo * partsupfunc,Oid * partcollation,PartitionBoundInfo boundinfo,int nvalues,Datum * values,bool * is_equal)1024 partition_range_datum_bsearch(FmgrInfo *partsupfunc, Oid *partcollation,
1025 							  PartitionBoundInfo boundinfo,
1026 							  int nvalues, Datum *values, bool *is_equal)
1027 {
1028 	int			lo,
1029 				hi,
1030 				mid;
1031 
1032 	lo = -1;
1033 	hi = boundinfo->ndatums - 1;
1034 	while (lo < hi)
1035 	{
1036 		int32		cmpval;
1037 
1038 		mid = (lo + hi + 1) / 2;
1039 		cmpval = partition_rbound_datum_cmp(partsupfunc,
1040 											partcollation,
1041 											boundinfo->datums[mid],
1042 											boundinfo->kind[mid],
1043 											values,
1044 											nvalues);
1045 		if (cmpval <= 0)
1046 		{
1047 			lo = mid;
1048 			*is_equal = (cmpval == 0);
1049 
1050 			if (*is_equal)
1051 				break;
1052 		}
1053 		else
1054 			hi = mid - 1;
1055 	}
1056 
1057 	return lo;
1058 }
1059 
1060 /*
1061  * partition_hash_bsearch
1062  *		Returns the index of the greatest (modulus, remainder) pair that is
1063  *		less than or equal to the given (modulus, remainder) pair or -1 if
1064  *		all of them are greater
1065  */
1066 int
partition_hash_bsearch(PartitionBoundInfo boundinfo,int modulus,int remainder)1067 partition_hash_bsearch(PartitionBoundInfo boundinfo,
1068 					   int modulus, int remainder)
1069 {
1070 	int			lo,
1071 				hi,
1072 				mid;
1073 
1074 	lo = -1;
1075 	hi = boundinfo->ndatums - 1;
1076 	while (lo < hi)
1077 	{
1078 		int32		cmpval,
1079 					bound_modulus,
1080 					bound_remainder;
1081 
1082 		mid = (lo + hi + 1) / 2;
1083 		bound_modulus = DatumGetInt32(boundinfo->datums[mid][0]);
1084 		bound_remainder = DatumGetInt32(boundinfo->datums[mid][1]);
1085 		cmpval = partition_hbound_cmp(bound_modulus, bound_remainder,
1086 									  modulus, remainder);
1087 		if (cmpval <= 0)
1088 		{
1089 			lo = mid;
1090 
1091 			if (cmpval == 0)
1092 				break;
1093 		}
1094 		else
1095 			hi = mid - 1;
1096 	}
1097 
1098 	return lo;
1099 }
1100 
1101 /*
1102  * get_partition_operator
1103  *
1104  * Return oid of the operator of the given strategy for the given partition
1105  * key column.  It is assumed that the partitioning key is of the same type as
1106  * the chosen partitioning opclass, or at least binary-compatible.  In the
1107  * latter case, *need_relabel is set to true if the opclass is not of a
1108  * polymorphic type (indicating a RelabelType node needed on top), otherwise
1109  * false.
1110  */
1111 static Oid
get_partition_operator(PartitionKey key,int col,StrategyNumber strategy,bool * need_relabel)1112 get_partition_operator(PartitionKey key, int col, StrategyNumber strategy,
1113 					   bool *need_relabel)
1114 {
1115 	Oid			operoid;
1116 
1117 	/*
1118 	 * Get the operator in the partitioning opfamily using the opclass'
1119 	 * declared input type as both left- and righttype.
1120 	 */
1121 	operoid = get_opfamily_member(key->partopfamily[col],
1122 								  key->partopcintype[col],
1123 								  key->partopcintype[col],
1124 								  strategy);
1125 	if (!OidIsValid(operoid))
1126 		elog(ERROR, "missing operator %d(%u,%u) in partition opfamily %u",
1127 			 strategy, key->partopcintype[col], key->partopcintype[col],
1128 			 key->partopfamily[col]);
1129 
1130 	/*
1131 	 * If the partition key column is not of the same type as the operator
1132 	 * class and not polymorphic, tell caller to wrap the non-Const expression
1133 	 * in a RelabelType.  This matches what parse_coerce.c does.
1134 	 */
1135 	*need_relabel = (key->parttypid[col] != key->partopcintype[col] &&
1136 					 key->partopcintype[col] != RECORDOID &&
1137 					 !IsPolymorphicType(key->partopcintype[col]));
1138 
1139 	return operoid;
1140 }
1141 
1142 /*
1143  * make_partition_op_expr
1144  *		Returns an Expr for the given partition key column with arg1 and
1145  *		arg2 as its leftop and rightop, respectively
1146  */
1147 static Expr *
make_partition_op_expr(PartitionKey key,int keynum,uint16 strategy,Expr * arg1,Expr * arg2)1148 make_partition_op_expr(PartitionKey key, int keynum,
1149 					   uint16 strategy, Expr *arg1, Expr *arg2)
1150 {
1151 	Oid			operoid;
1152 	bool		need_relabel = false;
1153 	Expr	   *result = NULL;
1154 
1155 	/* Get the correct btree operator for this partitioning column */
1156 	operoid = get_partition_operator(key, keynum, strategy, &need_relabel);
1157 
1158 	/*
1159 	 * Chosen operator may be such that the non-Const operand needs to be
1160 	 * coerced, so apply the same; see the comment in
1161 	 * get_partition_operator().
1162 	 */
1163 	if (!IsA(arg1, Const) &&
1164 		(need_relabel ||
1165 		 key->partcollation[keynum] != key->parttypcoll[keynum]))
1166 		arg1 = (Expr *) makeRelabelType(arg1,
1167 										key->partopcintype[keynum],
1168 										-1,
1169 										key->partcollation[keynum],
1170 										COERCE_EXPLICIT_CAST);
1171 
1172 	/* Generate the actual expression */
1173 	switch (key->strategy)
1174 	{
1175 		case PARTITION_STRATEGY_LIST:
1176 			{
1177 				List	   *elems = (List *) arg2;
1178 				int			nelems = list_length(elems);
1179 
1180 				Assert(nelems >= 1);
1181 				Assert(keynum == 0);
1182 
1183 				if (nelems > 1 &&
1184 					!type_is_array(key->parttypid[keynum]))
1185 				{
1186 					ArrayExpr  *arrexpr;
1187 					ScalarArrayOpExpr *saopexpr;
1188 
1189 					/* Construct an ArrayExpr for the right-hand inputs */
1190 					arrexpr = makeNode(ArrayExpr);
1191 					arrexpr->array_typeid =
1192 						get_array_type(key->parttypid[keynum]);
1193 					arrexpr->array_collid = key->parttypcoll[keynum];
1194 					arrexpr->element_typeid = key->parttypid[keynum];
1195 					arrexpr->elements = elems;
1196 					arrexpr->multidims = false;
1197 					arrexpr->location = -1;
1198 
1199 					/* Build leftop = ANY (rightop) */
1200 					saopexpr = makeNode(ScalarArrayOpExpr);
1201 					saopexpr->opno = operoid;
1202 					saopexpr->opfuncid = get_opcode(operoid);
1203 					saopexpr->useOr = true;
1204 					saopexpr->inputcollid = key->partcollation[keynum];
1205 					saopexpr->args = list_make2(arg1, arrexpr);
1206 					saopexpr->location = -1;
1207 
1208 					result = (Expr *) saopexpr;
1209 				}
1210 				else
1211 				{
1212 					List	   *elemops = NIL;
1213 					ListCell   *lc;
1214 
1215 					foreach(lc, elems)
1216 					{
1217 						Expr	   *elem = lfirst(lc),
1218 								   *elemop;
1219 
1220 						elemop = make_opclause(operoid,
1221 											   BOOLOID,
1222 											   false,
1223 											   arg1, elem,
1224 											   InvalidOid,
1225 											   key->partcollation[keynum]);
1226 						elemops = lappend(elemops, elemop);
1227 					}
1228 
1229 					result = nelems > 1 ? makeBoolExpr(OR_EXPR, elemops, -1) : linitial(elemops);
1230 				}
1231 				break;
1232 			}
1233 
1234 		case PARTITION_STRATEGY_RANGE:
1235 			result = make_opclause(operoid,
1236 								   BOOLOID,
1237 								   false,
1238 								   arg1, arg2,
1239 								   InvalidOid,
1240 								   key->partcollation[keynum]);
1241 			break;
1242 
1243 		default:
1244 			elog(ERROR, "invalid partitioning strategy");
1245 			break;
1246 	}
1247 
1248 	return result;
1249 }
1250 
1251 /*
1252  * get_qual_for_hash
1253  *
1254  * Returns a CHECK constraint expression to use as a hash partition's
1255  * constraint, given the parent relation and partition bound structure.
1256  *
1257  * The partition constraint for a hash partition is always a call to the
1258  * built-in function satisfies_hash_partition().
1259  */
1260 static List *
get_qual_for_hash(Relation parent,PartitionBoundSpec * spec)1261 get_qual_for_hash(Relation parent, PartitionBoundSpec *spec)
1262 {
1263 	PartitionKey key = RelationGetPartitionKey(parent);
1264 	FuncExpr   *fexpr;
1265 	Node	   *relidConst;
1266 	Node	   *modulusConst;
1267 	Node	   *remainderConst;
1268 	List	   *args;
1269 	ListCell   *partexprs_item;
1270 	int			i;
1271 
1272 	/* Fixed arguments. */
1273 	relidConst = (Node *) makeConst(OIDOID,
1274 									-1,
1275 									InvalidOid,
1276 									sizeof(Oid),
1277 									ObjectIdGetDatum(RelationGetRelid(parent)),
1278 									false,
1279 									true);
1280 
1281 	modulusConst = (Node *) makeConst(INT4OID,
1282 									  -1,
1283 									  InvalidOid,
1284 									  sizeof(int32),
1285 									  Int32GetDatum(spec->modulus),
1286 									  false,
1287 									  true);
1288 
1289 	remainderConst = (Node *) makeConst(INT4OID,
1290 										-1,
1291 										InvalidOid,
1292 										sizeof(int32),
1293 										Int32GetDatum(spec->remainder),
1294 										false,
1295 										true);
1296 
1297 	args = list_make3(relidConst, modulusConst, remainderConst);
1298 	partexprs_item = list_head(key->partexprs);
1299 
1300 	/* Add an argument for each key column. */
1301 	for (i = 0; i < key->partnatts; i++)
1302 	{
1303 		Node	   *keyCol;
1304 
1305 		/* Left operand */
1306 		if (key->partattrs[i] != 0)
1307 		{
1308 			keyCol = (Node *) makeVar(1,
1309 									  key->partattrs[i],
1310 									  key->parttypid[i],
1311 									  key->parttypmod[i],
1312 									  key->parttypcoll[i],
1313 									  0);
1314 		}
1315 		else
1316 		{
1317 			keyCol = (Node *) copyObject(lfirst(partexprs_item));
1318 			partexprs_item = lnext(partexprs_item);
1319 		}
1320 
1321 		args = lappend(args, keyCol);
1322 	}
1323 
1324 	fexpr = makeFuncExpr(F_SATISFIES_HASH_PARTITION,
1325 						 BOOLOID,
1326 						 args,
1327 						 InvalidOid,
1328 						 InvalidOid,
1329 						 COERCE_EXPLICIT_CALL);
1330 
1331 	return list_make1(fexpr);
1332 }
1333 
1334 /*
1335  * get_qual_for_list
1336  *
1337  * Returns an implicit-AND list of expressions to use as a list partition's
1338  * constraint, given the parent relation and partition bound structure.
1339  *
1340  * The function returns NIL for a default partition when it's the only
1341  * partition since in that case there is no constraint.
1342  */
1343 static List *
get_qual_for_list(Relation parent,PartitionBoundSpec * spec)1344 get_qual_for_list(Relation parent, PartitionBoundSpec *spec)
1345 {
1346 	PartitionKey key = RelationGetPartitionKey(parent);
1347 	List	   *result;
1348 	Expr	   *keyCol;
1349 	Expr	   *opexpr;
1350 	NullTest   *nulltest;
1351 	ListCell   *cell;
1352 	List	   *elems = NIL;
1353 	bool		list_has_null = false;
1354 
1355 	/*
1356 	 * Only single-column list partitioning is supported, so we are worried
1357 	 * only about the partition key with index 0.
1358 	 */
1359 	Assert(key->partnatts == 1);
1360 
1361 	/* Construct Var or expression representing the partition column */
1362 	if (key->partattrs[0] != 0)
1363 		keyCol = (Expr *) makeVar(1,
1364 								  key->partattrs[0],
1365 								  key->parttypid[0],
1366 								  key->parttypmod[0],
1367 								  key->parttypcoll[0],
1368 								  0);
1369 	else
1370 		keyCol = (Expr *) copyObject(linitial(key->partexprs));
1371 
1372 	/*
1373 	 * For default list partition, collect datums for all the partitions. The
1374 	 * default partition constraint should check that the partition key is
1375 	 * equal to none of those.
1376 	 */
1377 	if (spec->is_default)
1378 	{
1379 		int			i;
1380 		int			ndatums = 0;
1381 		PartitionDesc pdesc = RelationGetPartitionDesc(parent);
1382 		PartitionBoundInfo boundinfo = pdesc->boundinfo;
1383 
1384 		if (boundinfo)
1385 		{
1386 			ndatums = boundinfo->ndatums;
1387 
1388 			if (partition_bound_accepts_nulls(boundinfo))
1389 				list_has_null = true;
1390 		}
1391 
1392 		/*
1393 		 * If default is the only partition, there need not be any partition
1394 		 * constraint on it.
1395 		 */
1396 		if (ndatums == 0 && !list_has_null)
1397 			return NIL;
1398 
1399 		for (i = 0; i < ndatums; i++)
1400 		{
1401 			Const	   *val;
1402 
1403 			/*
1404 			 * Construct Const from known-not-null datum.  We must be careful
1405 			 * to copy the value, because our result has to be able to outlive
1406 			 * the relcache entry we're copying from.
1407 			 */
1408 			val = makeConst(key->parttypid[0],
1409 							key->parttypmod[0],
1410 							key->parttypcoll[0],
1411 							key->parttyplen[0],
1412 							datumCopy(*boundinfo->datums[i],
1413 									  key->parttypbyval[0],
1414 									  key->parttyplen[0]),
1415 							false,	/* isnull */
1416 							key->parttypbyval[0]);
1417 
1418 			elems = lappend(elems, val);
1419 		}
1420 	}
1421 	else
1422 	{
1423 		/*
1424 		 * Create list of Consts for the allowed values, excluding any nulls.
1425 		 */
1426 		foreach(cell, spec->listdatums)
1427 		{
1428 			Const	   *val = castNode(Const, lfirst(cell));
1429 
1430 			if (val->constisnull)
1431 				list_has_null = true;
1432 			else
1433 				elems = lappend(elems, copyObject(val));
1434 		}
1435 	}
1436 
1437 	if (elems)
1438 	{
1439 		/*
1440 		 * Generate the operator expression from the non-null partition
1441 		 * values.
1442 		 */
1443 		opexpr = make_partition_op_expr(key, 0, BTEqualStrategyNumber,
1444 										keyCol, (Expr *) elems);
1445 	}
1446 	else
1447 	{
1448 		/*
1449 		 * If there are no partition values, we don't need an operator
1450 		 * expression.
1451 		 */
1452 		opexpr = NULL;
1453 	}
1454 
1455 	if (!list_has_null)
1456 	{
1457 		/*
1458 		 * Gin up a "col IS NOT NULL" test that will be AND'd with the main
1459 		 * expression.  This might seem redundant, but the partition routing
1460 		 * machinery needs it.
1461 		 */
1462 		nulltest = makeNode(NullTest);
1463 		nulltest->arg = keyCol;
1464 		nulltest->nulltesttype = IS_NOT_NULL;
1465 		nulltest->argisrow = false;
1466 		nulltest->location = -1;
1467 
1468 		result = opexpr ? list_make2(nulltest, opexpr) : list_make1(nulltest);
1469 	}
1470 	else
1471 	{
1472 		/*
1473 		 * Gin up a "col IS NULL" test that will be OR'd with the main
1474 		 * expression.
1475 		 */
1476 		nulltest = makeNode(NullTest);
1477 		nulltest->arg = keyCol;
1478 		nulltest->nulltesttype = IS_NULL;
1479 		nulltest->argisrow = false;
1480 		nulltest->location = -1;
1481 
1482 		if (opexpr)
1483 		{
1484 			Expr	   *or;
1485 
1486 			or = makeBoolExpr(OR_EXPR, list_make2(nulltest, opexpr), -1);
1487 			result = list_make1(or);
1488 		}
1489 		else
1490 			result = list_make1(nulltest);
1491 	}
1492 
1493 	/*
1494 	 * Note that, in general, applying NOT to a constraint expression doesn't
1495 	 * necessarily invert the set of rows it accepts, because NOT (NULL) is
1496 	 * NULL.  However, the partition constraints we construct here never
1497 	 * evaluate to NULL, so applying NOT works as intended.
1498 	 */
1499 	if (spec->is_default)
1500 	{
1501 		result = list_make1(make_ands_explicit(result));
1502 		result = list_make1(makeBoolExpr(NOT_EXPR, result, -1));
1503 	}
1504 
1505 	return result;
1506 }
1507 
1508 /*
1509  * get_qual_for_range
1510  *
1511  * Returns an implicit-AND list of expressions to use as a range partition's
1512  * constraint, given the parent relation and partition bound structure.
1513  *
1514  * For a multi-column range partition key, say (a, b, c), with (al, bl, cl)
1515  * as the lower bound tuple and (au, bu, cu) as the upper bound tuple, we
1516  * generate an expression tree of the following form:
1517  *
1518  *	(a IS NOT NULL) and (b IS NOT NULL) and (c IS NOT NULL)
1519  *		AND
1520  *	(a > al OR (a = al AND b > bl) OR (a = al AND b = bl AND c >= cl))
1521  *		AND
1522  *	(a < au OR (a = au AND b < bu) OR (a = au AND b = bu AND c < cu))
1523  *
1524  * It is often the case that a prefix of lower and upper bound tuples contains
1525  * the same values, for example, (al = au), in which case, we will emit an
1526  * expression tree of the following form:
1527  *
1528  *	(a IS NOT NULL) and (b IS NOT NULL) and (c IS NOT NULL)
1529  *		AND
1530  *	(a = al)
1531  *		AND
1532  *	(b > bl OR (b = bl AND c >= cl))
1533  *		AND
1534  *	(b < bu OR (b = bu AND c < cu))
1535  *
1536  * If a bound datum is either MINVALUE or MAXVALUE, these expressions are
1537  * simplified using the fact that any value is greater than MINVALUE and less
1538  * than MAXVALUE. So, for example, if cu = MAXVALUE, c < cu is automatically
1539  * true, and we need not emit any expression for it, and the last line becomes
1540  *
1541  *	(b < bu) OR (b = bu), which is simplified to (b <= bu)
1542  *
1543  * In most common cases with only one partition column, say a, the following
1544  * expression tree will be generated: a IS NOT NULL AND a >= al AND a < au
1545  *
1546  * For default partition, it returns the negation of the constraints of all
1547  * the other partitions.
1548  *
1549  * External callers should pass for_default as false; we set it to true only
1550  * when recursing.
1551  */
1552 static List *
get_qual_for_range(Relation parent,PartitionBoundSpec * spec,bool for_default)1553 get_qual_for_range(Relation parent, PartitionBoundSpec *spec,
1554 				   bool for_default)
1555 {
1556 	List	   *result = NIL;
1557 	ListCell   *cell1,
1558 			   *cell2,
1559 			   *partexprs_item,
1560 			   *partexprs_item_saved;
1561 	int			i,
1562 				j;
1563 	PartitionRangeDatum *ldatum,
1564 			   *udatum;
1565 	PartitionKey key = RelationGetPartitionKey(parent);
1566 	Expr	   *keyCol;
1567 	Const	   *lower_val,
1568 			   *upper_val;
1569 	List	   *lower_or_arms,
1570 			   *upper_or_arms;
1571 	int			num_or_arms,
1572 				current_or_arm;
1573 	ListCell   *lower_or_start_datum,
1574 			   *upper_or_start_datum;
1575 	bool		need_next_lower_arm,
1576 				need_next_upper_arm;
1577 
1578 	if (spec->is_default)
1579 	{
1580 		List	   *or_expr_args = NIL;
1581 		PartitionDesc pdesc = RelationGetPartitionDesc(parent);
1582 		Oid		   *inhoids = pdesc->oids;
1583 		int			nparts = pdesc->nparts,
1584 					i;
1585 
1586 		for (i = 0; i < nparts; i++)
1587 		{
1588 			Oid			inhrelid = inhoids[i];
1589 			HeapTuple	tuple;
1590 			Datum		datum;
1591 			bool		isnull;
1592 			PartitionBoundSpec *bspec;
1593 
1594 			tuple = SearchSysCache1(RELOID, inhrelid);
1595 			if (!HeapTupleIsValid(tuple))
1596 				elog(ERROR, "cache lookup failed for relation %u", inhrelid);
1597 
1598 			datum = SysCacheGetAttr(RELOID, tuple,
1599 									Anum_pg_class_relpartbound,
1600 									&isnull);
1601 			if (isnull)
1602 				elog(ERROR, "null relpartbound for relation %u", inhrelid);
1603 
1604 			bspec = (PartitionBoundSpec *)
1605 				stringToNode(TextDatumGetCString(datum));
1606 			if (!IsA(bspec, PartitionBoundSpec))
1607 				elog(ERROR, "expected PartitionBoundSpec");
1608 
1609 			if (!bspec->is_default)
1610 			{
1611 				List	   *part_qual;
1612 
1613 				part_qual = get_qual_for_range(parent, bspec, true);
1614 
1615 				/*
1616 				 * AND the constraints of the partition and add to
1617 				 * or_expr_args
1618 				 */
1619 				or_expr_args = lappend(or_expr_args, list_length(part_qual) > 1
1620 									   ? makeBoolExpr(AND_EXPR, part_qual, -1)
1621 									   : linitial(part_qual));
1622 			}
1623 			ReleaseSysCache(tuple);
1624 		}
1625 
1626 		if (or_expr_args != NIL)
1627 		{
1628 			Expr	   *other_parts_constr;
1629 
1630 			/*
1631 			 * Combine the constraints obtained for non-default partitions
1632 			 * using OR.  As requested, each of the OR's args doesn't include
1633 			 * the NOT NULL test for partition keys (which is to avoid its
1634 			 * useless repetition).  Add the same now.
1635 			 */
1636 			other_parts_constr =
1637 				makeBoolExpr(AND_EXPR,
1638 							 lappend(get_range_nulltest(key),
1639 									 list_length(or_expr_args) > 1
1640 									 ? makeBoolExpr(OR_EXPR, or_expr_args,
1641 													-1)
1642 									 : linitial(or_expr_args)),
1643 							 -1);
1644 
1645 			/*
1646 			 * Finally, the default partition contains everything *NOT*
1647 			 * contained in the non-default partitions.
1648 			 */
1649 			result = list_make1(makeBoolExpr(NOT_EXPR,
1650 											 list_make1(other_parts_constr), -1));
1651 		}
1652 
1653 		return result;
1654 	}
1655 
1656 	lower_or_start_datum = list_head(spec->lowerdatums);
1657 	upper_or_start_datum = list_head(spec->upperdatums);
1658 	num_or_arms = key->partnatts;
1659 
1660 	/*
1661 	 * If it is the recursive call for default, we skip the get_range_nulltest
1662 	 * to avoid accumulating the NullTest on the same keys for each partition.
1663 	 */
1664 	if (!for_default)
1665 		result = get_range_nulltest(key);
1666 
1667 	/*
1668 	 * Iterate over the key columns and check if the corresponding lower and
1669 	 * upper datums are equal using the btree equality operator for the
1670 	 * column's type.  If equal, we emit single keyCol = common_value
1671 	 * expression.  Starting from the first column for which the corresponding
1672 	 * lower and upper bound datums are not equal, we generate OR expressions
1673 	 * as shown in the function's header comment.
1674 	 */
1675 	i = 0;
1676 	partexprs_item = list_head(key->partexprs);
1677 	partexprs_item_saved = partexprs_item;	/* placate compiler */
1678 	forboth(cell1, spec->lowerdatums, cell2, spec->upperdatums)
1679 	{
1680 		EState	   *estate;
1681 		MemoryContext oldcxt;
1682 		Expr	   *test_expr;
1683 		ExprState  *test_exprstate;
1684 		Datum		test_result;
1685 		bool		isNull;
1686 
1687 		ldatum = castNode(PartitionRangeDatum, lfirst(cell1));
1688 		udatum = castNode(PartitionRangeDatum, lfirst(cell2));
1689 
1690 		/*
1691 		 * Since get_range_key_properties() modifies partexprs_item, and we
1692 		 * might need to start over from the previous expression in the later
1693 		 * part of this function, save away the current value.
1694 		 */
1695 		partexprs_item_saved = partexprs_item;
1696 
1697 		get_range_key_properties(key, i, ldatum, udatum,
1698 								 &partexprs_item,
1699 								 &keyCol,
1700 								 &lower_val, &upper_val);
1701 
1702 		/*
1703 		 * If either value is NULL, the corresponding partition bound is
1704 		 * either MINVALUE or MAXVALUE, and we treat them as unequal, because
1705 		 * even if they're the same, there is no common value to equate the
1706 		 * key column with.
1707 		 */
1708 		if (!lower_val || !upper_val)
1709 			break;
1710 
1711 		/* Create the test expression */
1712 		estate = CreateExecutorState();
1713 		oldcxt = MemoryContextSwitchTo(estate->es_query_cxt);
1714 		test_expr = make_partition_op_expr(key, i, BTEqualStrategyNumber,
1715 										   (Expr *) lower_val,
1716 										   (Expr *) upper_val);
1717 		fix_opfuncids((Node *) test_expr);
1718 		test_exprstate = ExecInitExpr(test_expr, NULL);
1719 		test_result = ExecEvalExprSwitchContext(test_exprstate,
1720 												GetPerTupleExprContext(estate),
1721 												&isNull);
1722 		MemoryContextSwitchTo(oldcxt);
1723 		FreeExecutorState(estate);
1724 
1725 		/* If not equal, go generate the OR expressions */
1726 		if (!DatumGetBool(test_result))
1727 			break;
1728 
1729 		/*
1730 		 * The bounds for the last key column can't be equal, because such a
1731 		 * range partition would never be allowed to be defined (it would have
1732 		 * an empty range otherwise).
1733 		 */
1734 		if (i == key->partnatts - 1)
1735 			elog(ERROR, "invalid range bound specification");
1736 
1737 		/* Equal, so generate keyCol = lower_val expression */
1738 		result = lappend(result,
1739 						 make_partition_op_expr(key, i, BTEqualStrategyNumber,
1740 												keyCol, (Expr *) lower_val));
1741 
1742 		i++;
1743 	}
1744 
1745 	/* First pair of lower_val and upper_val that are not equal. */
1746 	lower_or_start_datum = cell1;
1747 	upper_or_start_datum = cell2;
1748 
1749 	/* OR will have as many arms as there are key columns left. */
1750 	num_or_arms = key->partnatts - i;
1751 	current_or_arm = 0;
1752 	lower_or_arms = upper_or_arms = NIL;
1753 	need_next_lower_arm = need_next_upper_arm = true;
1754 	while (current_or_arm < num_or_arms)
1755 	{
1756 		List	   *lower_or_arm_args = NIL,
1757 				   *upper_or_arm_args = NIL;
1758 
1759 		/* Restart scan of columns from the i'th one */
1760 		j = i;
1761 		partexprs_item = partexprs_item_saved;
1762 
1763 		for_both_cell(cell1, lower_or_start_datum, cell2, upper_or_start_datum)
1764 		{
1765 			PartitionRangeDatum *ldatum_next = NULL,
1766 					   *udatum_next = NULL;
1767 
1768 			ldatum = castNode(PartitionRangeDatum, lfirst(cell1));
1769 			if (lnext(cell1))
1770 				ldatum_next = castNode(PartitionRangeDatum,
1771 									   lfirst(lnext(cell1)));
1772 			udatum = castNode(PartitionRangeDatum, lfirst(cell2));
1773 			if (lnext(cell2))
1774 				udatum_next = castNode(PartitionRangeDatum,
1775 									   lfirst(lnext(cell2)));
1776 			get_range_key_properties(key, j, ldatum, udatum,
1777 									 &partexprs_item,
1778 									 &keyCol,
1779 									 &lower_val, &upper_val);
1780 
1781 			if (need_next_lower_arm && lower_val)
1782 			{
1783 				uint16		strategy;
1784 
1785 				/*
1786 				 * For the non-last columns of this arm, use the EQ operator.
1787 				 * For the last column of this arm, use GT, unless this is the
1788 				 * last column of the whole bound check, or the next bound
1789 				 * datum is MINVALUE, in which case use GE.
1790 				 */
1791 				if (j - i < current_or_arm)
1792 					strategy = BTEqualStrategyNumber;
1793 				else if (j == key->partnatts - 1 ||
1794 						 (ldatum_next &&
1795 						  ldatum_next->kind == PARTITION_RANGE_DATUM_MINVALUE))
1796 					strategy = BTGreaterEqualStrategyNumber;
1797 				else
1798 					strategy = BTGreaterStrategyNumber;
1799 
1800 				lower_or_arm_args = lappend(lower_or_arm_args,
1801 											make_partition_op_expr(key, j,
1802 																   strategy,
1803 																   keyCol,
1804 																   (Expr *) lower_val));
1805 			}
1806 
1807 			if (need_next_upper_arm && upper_val)
1808 			{
1809 				uint16		strategy;
1810 
1811 				/*
1812 				 * For the non-last columns of this arm, use the EQ operator.
1813 				 * For the last column of this arm, use LT, unless the next
1814 				 * bound datum is MAXVALUE, in which case use LE.
1815 				 */
1816 				if (j - i < current_or_arm)
1817 					strategy = BTEqualStrategyNumber;
1818 				else if (udatum_next &&
1819 						 udatum_next->kind == PARTITION_RANGE_DATUM_MAXVALUE)
1820 					strategy = BTLessEqualStrategyNumber;
1821 				else
1822 					strategy = BTLessStrategyNumber;
1823 
1824 				upper_or_arm_args = lappend(upper_or_arm_args,
1825 											make_partition_op_expr(key, j,
1826 																   strategy,
1827 																   keyCol,
1828 																   (Expr *) upper_val));
1829 			}
1830 
1831 			/*
1832 			 * Did we generate enough of OR's arguments?  First arm considers
1833 			 * the first of the remaining columns, second arm considers first
1834 			 * two of the remaining columns, and so on.
1835 			 */
1836 			++j;
1837 			if (j - i > current_or_arm)
1838 			{
1839 				/*
1840 				 * We must not emit any more arms if the new column that will
1841 				 * be considered is unbounded, or this one was.
1842 				 */
1843 				if (!lower_val || !ldatum_next ||
1844 					ldatum_next->kind != PARTITION_RANGE_DATUM_VALUE)
1845 					need_next_lower_arm = false;
1846 				if (!upper_val || !udatum_next ||
1847 					udatum_next->kind != PARTITION_RANGE_DATUM_VALUE)
1848 					need_next_upper_arm = false;
1849 				break;
1850 			}
1851 		}
1852 
1853 		if (lower_or_arm_args != NIL)
1854 			lower_or_arms = lappend(lower_or_arms,
1855 									list_length(lower_or_arm_args) > 1
1856 									? makeBoolExpr(AND_EXPR, lower_or_arm_args, -1)
1857 									: linitial(lower_or_arm_args));
1858 
1859 		if (upper_or_arm_args != NIL)
1860 			upper_or_arms = lappend(upper_or_arms,
1861 									list_length(upper_or_arm_args) > 1
1862 									? makeBoolExpr(AND_EXPR, upper_or_arm_args, -1)
1863 									: linitial(upper_or_arm_args));
1864 
1865 		/* If no work to do in the next iteration, break away. */
1866 		if (!need_next_lower_arm && !need_next_upper_arm)
1867 			break;
1868 
1869 		++current_or_arm;
1870 	}
1871 
1872 	/*
1873 	 * Generate the OR expressions for each of lower and upper bounds (if
1874 	 * required), and append to the list of implicitly ANDed list of
1875 	 * expressions.
1876 	 */
1877 	if (lower_or_arms != NIL)
1878 		result = lappend(result,
1879 						 list_length(lower_or_arms) > 1
1880 						 ? makeBoolExpr(OR_EXPR, lower_or_arms, -1)
1881 						 : linitial(lower_or_arms));
1882 	if (upper_or_arms != NIL)
1883 		result = lappend(result,
1884 						 list_length(upper_or_arms) > 1
1885 						 ? makeBoolExpr(OR_EXPR, upper_or_arms, -1)
1886 						 : linitial(upper_or_arms));
1887 
1888 	/*
1889 	 * As noted above, for non-default, we return list with constant TRUE. If
1890 	 * the result is NIL during the recursive call for default, it implies
1891 	 * this is the only other partition which can hold every value of the key
1892 	 * except NULL. Hence we return the NullTest result skipped earlier.
1893 	 */
1894 	if (result == NIL)
1895 		result = for_default
1896 			? get_range_nulltest(key)
1897 			: list_make1(makeBoolConst(true, false));
1898 
1899 	return result;
1900 }
1901 
1902 /*
1903  * get_range_key_properties
1904  *		Returns range partition key information for a given column
1905  *
1906  * This is a subroutine for get_qual_for_range, and its API is pretty
1907  * specialized to that caller.
1908  *
1909  * Constructs an Expr for the key column (returned in *keyCol) and Consts
1910  * for the lower and upper range limits (returned in *lower_val and
1911  * *upper_val).  For MINVALUE/MAXVALUE limits, NULL is returned instead of
1912  * a Const.  All of these structures are freshly palloc'd.
1913  *
1914  * *partexprs_item points to the cell containing the next expression in
1915  * the key->partexprs list, or NULL.  It may be advanced upon return.
1916  */
1917 static void
get_range_key_properties(PartitionKey key,int keynum,PartitionRangeDatum * ldatum,PartitionRangeDatum * udatum,ListCell ** partexprs_item,Expr ** keyCol,Const ** lower_val,Const ** upper_val)1918 get_range_key_properties(PartitionKey key, int keynum,
1919 						 PartitionRangeDatum *ldatum,
1920 						 PartitionRangeDatum *udatum,
1921 						 ListCell **partexprs_item,
1922 						 Expr **keyCol,
1923 						 Const **lower_val, Const **upper_val)
1924 {
1925 	/* Get partition key expression for this column */
1926 	if (key->partattrs[keynum] != 0)
1927 	{
1928 		*keyCol = (Expr *) makeVar(1,
1929 								   key->partattrs[keynum],
1930 								   key->parttypid[keynum],
1931 								   key->parttypmod[keynum],
1932 								   key->parttypcoll[keynum],
1933 								   0);
1934 	}
1935 	else
1936 	{
1937 		if (*partexprs_item == NULL)
1938 			elog(ERROR, "wrong number of partition key expressions");
1939 		*keyCol = copyObject(lfirst(*partexprs_item));
1940 		*partexprs_item = lnext(*partexprs_item);
1941 	}
1942 
1943 	/* Get appropriate Const nodes for the bounds */
1944 	if (ldatum->kind == PARTITION_RANGE_DATUM_VALUE)
1945 		*lower_val = castNode(Const, copyObject(ldatum->value));
1946 	else
1947 		*lower_val = NULL;
1948 
1949 	if (udatum->kind == PARTITION_RANGE_DATUM_VALUE)
1950 		*upper_val = castNode(Const, copyObject(udatum->value));
1951 	else
1952 		*upper_val = NULL;
1953 }
1954 
1955 /*
1956  * get_range_nulltest
1957  *
1958  * A non-default range partition table does not currently allow partition
1959  * keys to be null, so emit an IS NOT NULL expression for each key column.
1960  */
1961 static List *
get_range_nulltest(PartitionKey key)1962 get_range_nulltest(PartitionKey key)
1963 {
1964 	List	   *result = NIL;
1965 	NullTest   *nulltest;
1966 	ListCell   *partexprs_item;
1967 	int			i;
1968 
1969 	partexprs_item = list_head(key->partexprs);
1970 	for (i = 0; i < key->partnatts; i++)
1971 	{
1972 		Expr	   *keyCol;
1973 
1974 		if (key->partattrs[i] != 0)
1975 		{
1976 			keyCol = (Expr *) makeVar(1,
1977 									  key->partattrs[i],
1978 									  key->parttypid[i],
1979 									  key->parttypmod[i],
1980 									  key->parttypcoll[i],
1981 									  0);
1982 		}
1983 		else
1984 		{
1985 			if (partexprs_item == NULL)
1986 				elog(ERROR, "wrong number of partition key expressions");
1987 			keyCol = copyObject(lfirst(partexprs_item));
1988 			partexprs_item = lnext(partexprs_item);
1989 		}
1990 
1991 		nulltest = makeNode(NullTest);
1992 		nulltest->arg = keyCol;
1993 		nulltest->nulltesttype = IS_NOT_NULL;
1994 		nulltest->argisrow = false;
1995 		nulltest->location = -1;
1996 		result = lappend(result, nulltest);
1997 	}
1998 
1999 	return result;
2000 }
2001 
2002 /*
2003  * compute_partition_hash_value
2004  *
2005  * Compute the hash value for given partition key values.
2006  */
2007 uint64
compute_partition_hash_value(int partnatts,FmgrInfo * partsupfunc,Datum * values,bool * isnull)2008 compute_partition_hash_value(int partnatts, FmgrInfo *partsupfunc,
2009 							 Datum *values, bool *isnull)
2010 {
2011 	int			i;
2012 	uint64		rowHash = 0;
2013 	Datum		seed = UInt64GetDatum(HASH_PARTITION_SEED);
2014 
2015 	for (i = 0; i < partnatts; i++)
2016 	{
2017 		/* Nulls are just ignored */
2018 		if (!isnull[i])
2019 		{
2020 			Datum		hash;
2021 
2022 			Assert(OidIsValid(partsupfunc[i].fn_oid));
2023 
2024 			/*
2025 			 * Compute hash for each datum value by calling respective
2026 			 * datatype-specific hash functions of each partition key
2027 			 * attribute.
2028 			 */
2029 			hash = FunctionCall2(&partsupfunc[i], values[i], seed);
2030 
2031 			/* Form a single 64-bit hash value */
2032 			rowHash = hash_combine64(rowHash, DatumGetUInt64(hash));
2033 		}
2034 	}
2035 
2036 	return rowHash;
2037 }
2038 
2039 /*
2040  * satisfies_hash_partition
2041  *
2042  * This is an SQL-callable function for use in hash partition constraints.
2043  * The first three arguments are the parent table OID, modulus, and remainder.
2044  * The remaining arguments are the value of the partitioning columns (or
2045  * expressions); these are hashed and the results are combined into a single
2046  * hash value by calling hash_combine64.
2047  *
2048  * Returns true if remainder produced when this computed single hash value is
2049  * divided by the given modulus is equal to given remainder, otherwise false.
2050  * NB: it's important that this never return null, as the constraint machinery
2051  * would consider that to be a "pass".
2052  *
2053  * See get_qual_for_hash() for usage.
2054  */
2055 Datum
satisfies_hash_partition(PG_FUNCTION_ARGS)2056 satisfies_hash_partition(PG_FUNCTION_ARGS)
2057 {
2058 	typedef struct ColumnsHashData
2059 	{
2060 		Oid			relid;
2061 		int			nkeys;
2062 		Oid			variadic_type;
2063 		int16		variadic_typlen;
2064 		bool		variadic_typbyval;
2065 		char		variadic_typalign;
2066 		FmgrInfo	partsupfunc[PARTITION_MAX_KEYS];
2067 	} ColumnsHashData;
2068 	Oid			parentId;
2069 	int			modulus;
2070 	int			remainder;
2071 	Datum		seed = UInt64GetDatum(HASH_PARTITION_SEED);
2072 	ColumnsHashData *my_extra;
2073 	uint64		rowHash = 0;
2074 
2075 	/* Return false if the parent OID, modulus, or remainder is NULL. */
2076 	if (PG_ARGISNULL(0) || PG_ARGISNULL(1) || PG_ARGISNULL(2))
2077 		PG_RETURN_BOOL(false);
2078 	parentId = PG_GETARG_OID(0);
2079 	modulus = PG_GETARG_INT32(1);
2080 	remainder = PG_GETARG_INT32(2);
2081 
2082 	/* Sanity check modulus and remainder. */
2083 	if (modulus <= 0)
2084 		ereport(ERROR,
2085 				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2086 				 errmsg("modulus for hash partition must be an integer value greater than zero")));
2087 	if (remainder < 0)
2088 		ereport(ERROR,
2089 				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2090 				 errmsg("remainder for hash partition must be an integer value greater than or equal to zero")));
2091 	if (remainder >= modulus)
2092 		ereport(ERROR,
2093 				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2094 				 errmsg("remainder for hash partition must be less than modulus")));
2095 
2096 	/*
2097 	 * Cache hash function information.
2098 	 */
2099 	my_extra = (ColumnsHashData *) fcinfo->flinfo->fn_extra;
2100 	if (my_extra == NULL || my_extra->relid != parentId)
2101 	{
2102 		Relation	parent;
2103 		PartitionKey key;
2104 		int			j;
2105 
2106 		/* Open parent relation and fetch partition key info */
2107 		parent = relation_open(parentId, AccessShareLock);
2108 		key = RelationGetPartitionKey(parent);
2109 
2110 		/* Reject parent table that is not hash-partitioned. */
2111 		if (key == NULL || key->strategy != PARTITION_STRATEGY_HASH)
2112 			ereport(ERROR,
2113 					(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2114 					 errmsg("\"%s\" is not a hash partitioned table",
2115 							get_rel_name(parentId))));
2116 
2117 		if (!get_fn_expr_variadic(fcinfo->flinfo))
2118 		{
2119 			int			nargs = PG_NARGS() - 3;
2120 
2121 			/* complain if wrong number of column values */
2122 			if (key->partnatts != nargs)
2123 				ereport(ERROR,
2124 						(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2125 						 errmsg("number of partitioning columns (%d) does not match number of partition keys provided (%d)",
2126 								key->partnatts, nargs)));
2127 
2128 			/* allocate space for our cache */
2129 			fcinfo->flinfo->fn_extra =
2130 				MemoryContextAllocZero(fcinfo->flinfo->fn_mcxt,
2131 									   offsetof(ColumnsHashData, partsupfunc) +
2132 									   sizeof(FmgrInfo) * nargs);
2133 			my_extra = (ColumnsHashData *) fcinfo->flinfo->fn_extra;
2134 			my_extra->relid = parentId;
2135 			my_extra->nkeys = key->partnatts;
2136 
2137 			/* check argument types and save fmgr_infos */
2138 			for (j = 0; j < key->partnatts; ++j)
2139 			{
2140 				Oid			argtype = get_fn_expr_argtype(fcinfo->flinfo, j + 3);
2141 
2142 				if (argtype != key->parttypid[j] && !IsBinaryCoercible(argtype, key->parttypid[j]))
2143 					ereport(ERROR,
2144 							(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2145 							 errmsg("column %d of the partition key has type \"%s\", but supplied value is of type \"%s\"",
2146 									j + 1, format_type_be(key->parttypid[j]), format_type_be(argtype))));
2147 
2148 				fmgr_info_copy(&my_extra->partsupfunc[j],
2149 							   &key->partsupfunc[j],
2150 							   fcinfo->flinfo->fn_mcxt);
2151 			}
2152 
2153 		}
2154 		else
2155 		{
2156 			ArrayType  *variadic_array = PG_GETARG_ARRAYTYPE_P(3);
2157 
2158 			/* allocate space for our cache -- just one FmgrInfo in this case */
2159 			fcinfo->flinfo->fn_extra =
2160 				MemoryContextAllocZero(fcinfo->flinfo->fn_mcxt,
2161 									   offsetof(ColumnsHashData, partsupfunc) +
2162 									   sizeof(FmgrInfo));
2163 			my_extra = (ColumnsHashData *) fcinfo->flinfo->fn_extra;
2164 			my_extra->relid = parentId;
2165 			my_extra->nkeys = key->partnatts;
2166 			my_extra->variadic_type = ARR_ELEMTYPE(variadic_array);
2167 			get_typlenbyvalalign(my_extra->variadic_type,
2168 								 &my_extra->variadic_typlen,
2169 								 &my_extra->variadic_typbyval,
2170 								 &my_extra->variadic_typalign);
2171 
2172 			/* check argument types */
2173 			for (j = 0; j < key->partnatts; ++j)
2174 				if (key->parttypid[j] != my_extra->variadic_type)
2175 					ereport(ERROR,
2176 							(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2177 							 errmsg("column %d of the partition key has type \"%s\", but supplied value is of type \"%s\"",
2178 									j + 1,
2179 									format_type_be(key->parttypid[j]),
2180 									format_type_be(my_extra->variadic_type))));
2181 
2182 			fmgr_info_copy(&my_extra->partsupfunc[0],
2183 						   &key->partsupfunc[0],
2184 						   fcinfo->flinfo->fn_mcxt);
2185 		}
2186 
2187 		/* Hold lock until commit */
2188 		relation_close(parent, NoLock);
2189 	}
2190 
2191 	if (!OidIsValid(my_extra->variadic_type))
2192 	{
2193 		int			nkeys = my_extra->nkeys;
2194 		int			i;
2195 
2196 		/*
2197 		 * For a non-variadic call, neither the number of arguments nor their
2198 		 * types can change across calls, so avoid the expense of rechecking
2199 		 * here.
2200 		 */
2201 
2202 		for (i = 0; i < nkeys; i++)
2203 		{
2204 			Datum		hash;
2205 
2206 			/* keys start from fourth argument of function. */
2207 			int			argno = i + 3;
2208 
2209 			if (PG_ARGISNULL(argno))
2210 				continue;
2211 
2212 			Assert(OidIsValid(my_extra->partsupfunc[i].fn_oid));
2213 
2214 			hash = FunctionCall2(&my_extra->partsupfunc[i],
2215 								 PG_GETARG_DATUM(argno),
2216 								 seed);
2217 
2218 			/* Form a single 64-bit hash value */
2219 			rowHash = hash_combine64(rowHash, DatumGetUInt64(hash));
2220 		}
2221 	}
2222 	else
2223 	{
2224 		ArrayType  *variadic_array = PG_GETARG_ARRAYTYPE_P(3);
2225 		int			i;
2226 		int			nelems;
2227 		Datum	   *datum;
2228 		bool	   *isnull;
2229 
2230 		deconstruct_array(variadic_array,
2231 						  my_extra->variadic_type,
2232 						  my_extra->variadic_typlen,
2233 						  my_extra->variadic_typbyval,
2234 						  my_extra->variadic_typalign,
2235 						  &datum, &isnull, &nelems);
2236 
2237 		/* complain if wrong number of column values */
2238 		if (nelems != my_extra->nkeys)
2239 			ereport(ERROR,
2240 					(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2241 					 errmsg("number of partitioning columns (%d) does not match number of partition keys provided (%d)",
2242 							my_extra->nkeys, nelems)));
2243 
2244 		for (i = 0; i < nelems; i++)
2245 		{
2246 			Datum		hash;
2247 
2248 			if (isnull[i])
2249 				continue;
2250 
2251 			Assert(OidIsValid(my_extra->partsupfunc[0].fn_oid));
2252 
2253 			hash = FunctionCall2(&my_extra->partsupfunc[0],
2254 								 datum[i],
2255 								 seed);
2256 
2257 			/* Form a single 64-bit hash value */
2258 			rowHash = hash_combine64(rowHash, DatumGetUInt64(hash));
2259 		}
2260 	}
2261 
2262 	PG_RETURN_BOOL(rowHash % modulus == remainder);
2263 }
2264