PostgreSQL Source Code  git master
partbounds.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * partbounds.h
4  *
5  * Copyright (c) 2007-2021, PostgreSQL Global Development Group
6  *
7  * src/include/partitioning/partbounds.h
8  *
9  *-------------------------------------------------------------------------
10  */
11 #ifndef PARTBOUNDS_H
12 #define PARTBOUNDS_H
13 
14 #include "fmgr.h"
15 #include "parser/parse_node.h"
16 #include "partitioning/partdefs.h"
17 
18 struct RelOptInfo; /* avoid including pathnodes.h here */
19 
20 
21 /*
22  * PartitionBoundInfoData encapsulates a set of partition bounds. It is
23  * usually associated with partitioned tables as part of its partition
24  * descriptor, but may also be used to represent a virtual partitioned
25  * table such as a partitioned joinrel within the planner.
26  *
27  * A list partition datum that is known to be NULL is never put into the
28  * datums array. Instead, it is tracked using the null_index field.
29  *
30  * In the case of range partitioning, ndatums will typically be far less than
31  * 2 * nparts, because a partition's upper bound and the next partition's lower
32  * bound are the same in most common cases, and we only store one of them (the
33  * upper bound). In case of hash partitioning, ndatums will be the same as the
34  * number of partitions.
35  *
36  * For range and list partitioned tables, datums is an array of datum-tuples
37  * with key->partnatts datums each. For hash partitioned tables, it is an array
38  * of datum-tuples with 2 datums, modulus and remainder, corresponding to a
39  * given partition.
40  *
41  * The datums in datums array are arranged in increasing order as defined by
42  * functions qsort_partition_rbound_cmp(), qsort_partition_list_value_cmp() and
43  * qsort_partition_hbound_cmp() for range, list and hash partitioned tables
44  * respectively. For range and list partitions this simply means that the
45  * datums in the datums array are arranged in increasing order as defined by
46  * the partition key's operator classes and collations.
47  *
48  * In the case of list partitioning, the indexes array stores one entry for
49  * each datum-array entry, which is the index of the partition that accepts
50  * rows matching that datum. So nindexes == ndatums.
51  *
52  * In the case of range partitioning, the indexes array stores one entry per
53  * distinct range datum, which is the index of the partition for which that
54  * datum is an upper bound (or -1 for a "gap" that has no partition). It is
55  * convenient to have an extra -1 entry representing values above the last
56  * range datum, so nindexes == ndatums + 1.
57  *
58  * In the case of hash partitioning, the number of entries in the indexes
59  * array is the same as the greatest modulus amongst all partitions (which
60  * is a multiple of all partition moduli), so nindexes == greatest modulus.
61  * The indexes array is indexed according to the hash key's remainder modulo
62  * the greatest modulus, and it contains either the partition index accepting
63  * that remainder, or -1 if there is no partition for that remainder.
64  *
65  * For LIST partitioned tables, we track the partition indexes of partitions
66  * which are possibly "interleaved" partitions. A partition is considered
67  * interleaved if it allows multiple values and there exists at least one
68  * other partition which could contain a value that lies between those values.
69  * For example, if a partition exists FOR VALUES IN(3,5) and another partition
70  * exists FOR VALUES IN (4), then the IN(3,5) partition is an interleaved
71  * partition. The same is possible with DEFAULT partitions since they can
72  * contain any value that does not belong in another partition. This field
73  * only serves as proof that a particular partition is not interleaved, not
74  * proof that it is interleaved. When we're uncertain, we marked the
75  * partition as interleaved.
76  */
77 typedef struct PartitionBoundInfoData
78 {
79  char strategy; /* hash, list or range? */
80  int ndatums; /* Length of the datums[] array */
82  PartitionRangeDatumKind **kind; /* The kind of each range bound datum;
83  * NULL for hash and list partitioned
84  * tables */
85  Bitmapset *interleaved_parts; /* Partition indexes of partitions which
86  * may be interleaved. See above. This is
87  * only set for LIST partitioned tables */
88  int nindexes; /* Length of the indexes[] array */
89  int *indexes; /* Partition indexes */
90  int null_index; /* Index of the null-accepting partition; -1
91  * if there isn't one */
92  int default_index; /* Index of the default partition; -1 if there
93  * isn't one */
95 
96 #define partition_bound_accepts_nulls(bi) ((bi)->null_index != -1)
97 #define partition_bound_has_default(bi) ((bi)->default_index != -1)
98 
100 extern uint64 compute_partition_hash_value(int partnatts, FmgrInfo *partsupfunc,
101  Oid *partcollation,
102  Datum *values, bool *isnull);
103 extern List *get_qual_from_partbound(Relation parent,
104  PartitionBoundSpec *spec);
106  int nparts, PartitionKey key, int **mapping);
107 extern bool partition_bounds_equal(int partnatts, int16 *parttyplen,
108  bool *parttypbyval, PartitionBoundInfo b1,
109  PartitionBoundInfo b2);
111  PartitionKey key);
112 extern PartitionBoundInfo partition_bounds_merge(int partnatts,
113  FmgrInfo *partsupfunc,
114  Oid *partcollation,
115  struct RelOptInfo *outer_rel,
116  struct RelOptInfo *inner_rel,
117  JoinType jointype,
118  List **outer_parts,
119  List **inner_parts);
120 extern bool partitions_are_ordered(PartitionBoundInfo boundinfo,
121  Bitmapset *live_parts);
122 extern void check_new_partition_bound(char *relname, Relation parent,
123  PartitionBoundSpec *spec,
124  ParseState *pstate);
125 extern void check_default_partition_contents(Relation parent,
126  Relation defaultRel,
127  PartitionBoundSpec *new_spec);
128 
129 extern int32 partition_rbound_datum_cmp(FmgrInfo *partsupfunc,
130  Oid *partcollation,
131  Datum *rb_datums, PartitionRangeDatumKind *rb_kind,
132  Datum *tuple_datums, int n_tuple_datums);
133 extern int partition_list_bsearch(FmgrInfo *partsupfunc,
134  Oid *partcollation,
135  PartitionBoundInfo boundinfo,
136  Datum value, bool *is_equal);
137 extern int partition_range_datum_bsearch(FmgrInfo *partsupfunc,
138  Oid *partcollation,
139  PartitionBoundInfo boundinfo,
140  int nvalues, Datum *values, bool *is_equal);
141 extern int partition_hash_bsearch(PartitionBoundInfo boundinfo,
142  int modulus, int remainder);
143 
144 #endif /* PARTBOUNDS_H */
signed short int16
Definition: c.h:428
Definition: fmgr.h:56
PartitionRangeDatumKind ** kind
Definition: partbounds.h:82
int partition_list_bsearch(FmgrInfo *partsupfunc, Oid *partcollation, PartitionBoundInfo boundinfo, Datum value, bool *is_equal)
Definition: partbounds.c:3620
PartitionRangeDatumKind
Definition: parsenodes.h:862
Bitmapset * interleaved_parts
Definition: partbounds.h:85
PartitionBoundInfo partition_bounds_create(PartitionBoundSpec **boundspecs, int nparts, PartitionKey key, int **mapping)
Definition: partbounds.c:303
NameData relname
Definition: pg_class.h:38
unsigned int Oid
Definition: postgres_ext.h:31
int partition_hash_bsearch(PartitionBoundInfo boundinfo, int modulus, int remainder)
Definition: partbounds.c:3751
signed int int32
Definition: c.h:429
JoinType
Definition: nodes.h:704
void check_default_partition_contents(Relation parent, Relation defaultRel, PartitionBoundSpec *new_spec)
Definition: partbounds.c:3264
bool partition_bounds_equal(int partnatts, int16 *parttyplen, bool *parttypbyval, PartitionBoundInfo b1, PartitionBoundInfo b2)
Definition: partbounds.c:904
struct PartitionBoundInfoData PartitionBoundInfoData
bool partitions_are_ordered(PartitionBoundInfo boundinfo, Bitmapset *live_parts)
Definition: partbounds.c:2860
uintptr_t Datum
Definition: postgres.h:411
static struct @143 value
List * get_qual_from_partbound(Relation parent, PartitionBoundSpec *spec)
Definition: partbounds.c:249
int32 partition_rbound_datum_cmp(FmgrInfo *partsupfunc, Oid *partcollation, Datum *rb_datums, PartitionRangeDatumKind *rb_kind, Datum *tuple_datums, int n_tuple_datums)
Definition: partbounds.c:3569
int get_hash_partition_greatest_modulus(PartitionBoundInfo b)
Definition: partbounds.c:3427
void check_new_partition_bound(char *relname, Relation parent, PartitionBoundSpec *spec, ParseState *pstate)
Definition: partbounds.c:2905
static Datum values[MAXATTR]
Definition: bootstrap.c:156
PartitionBoundInfo partition_bounds_copy(PartitionBoundInfo src, PartitionKey key)
Definition: partbounds.c:1010
uint64 compute_partition_hash_value(int partnatts, FmgrInfo *partsupfunc, Oid *partcollation, Datum *values, bool *isnull)
Definition: partbounds.c:4740
Definition: pg_list.h:50
int partition_range_datum_bsearch(FmgrInfo *partsupfunc, Oid *partcollation, PartitionBoundInfo boundinfo, int nvalues, Datum *values, bool *is_equal)
Definition: partbounds.c:3708
PartitionBoundInfo partition_bounds_merge(int partnatts, FmgrInfo *partsupfunc, Oid *partcollation, struct RelOptInfo *outer_rel, struct RelOptInfo *inner_rel, JoinType jointype, List **outer_parts, List **inner_parts)
Definition: partbounds.c:1126