PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
partbounds.h File Reference
#include "fmgr.h"
#include "parser/parse_node.h"
#include "partitioning/partdefs.h"
Include dependency graph for partbounds.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  PartitionBoundInfoData
 

Macros

#define partition_bound_accepts_nulls(bi)   ((bi)->null_index != -1)
 
#define partition_bound_has_default(bi)   ((bi)->default_index != -1)
 

Typedefs

typedef struct RelOptInfo RelOptInfo
 
typedef struct PartitionBoundInfoData PartitionBoundInfoData
 

Functions

int get_hash_partition_greatest_modulus (PartitionBoundInfo bound)
 
uint64 compute_partition_hash_value (int partnatts, FmgrInfo *partsupfunc, const Oid *partcollation, const Datum *values, const bool *isnull)
 
Listget_qual_from_partbound (Relation parent, PartitionBoundSpec *spec)
 
PartitionBoundInfo partition_bounds_create (PartitionBoundSpec **boundspecs, int nparts, PartitionKey key, int **mapping)
 
bool partition_bounds_equal (int partnatts, int16 *parttyplen, bool *parttypbyval, PartitionBoundInfo b1, PartitionBoundInfo b2)
 
PartitionBoundInfo partition_bounds_copy (PartitionBoundInfo src, PartitionKey key)
 
PartitionBoundInfo partition_bounds_merge (int partnatts, FmgrInfo *partsupfunc, Oid *partcollation, RelOptInfo *outer_rel, RelOptInfo *inner_rel, JoinType jointype, List **outer_parts, List **inner_parts)
 
bool partitions_are_ordered (PartitionBoundInfo boundinfo, Bitmapset *live_parts)
 
void check_new_partition_bound (char *relname, Relation parent, PartitionBoundSpec *spec, ParseState *pstate)
 
void check_default_partition_contents (Relation parent, Relation default_rel, PartitionBoundSpec *new_spec)
 
int32 partition_rbound_datum_cmp (FmgrInfo *partsupfunc, Oid *partcollation, const Datum *rb_datums, PartitionRangeDatumKind *rb_kind, const Datum *tuple_datums, int n_tuple_datums)
 
int partition_list_bsearch (FmgrInfo *partsupfunc, Oid *partcollation, PartitionBoundInfo boundinfo, Datum value, bool *is_equal)
 
int partition_range_datum_bsearch (FmgrInfo *partsupfunc, Oid *partcollation, PartitionBoundInfo boundinfo, int nvalues, const Datum *values, bool *is_equal)
 
int partition_hash_bsearch (PartitionBoundInfo boundinfo, int modulus, int remainder)
 

Macro Definition Documentation

◆ partition_bound_accepts_nulls

#define partition_bound_accepts_nulls (   bi)    ((bi)->null_index != -1)

Definition at line 98 of file partbounds.h.

◆ partition_bound_has_default

#define partition_bound_has_default (   bi)    ((bi)->default_index != -1)

Definition at line 99 of file partbounds.h.

Typedef Documentation

◆ PartitionBoundInfoData

◆ RelOptInfo

typedef struct RelOptInfo RelOptInfo

Definition at line 18 of file partbounds.h.

Function Documentation

◆ check_default_partition_contents()

void check_default_partition_contents ( Relation  parent,
Relation  default_rel,
PartitionBoundSpec new_spec 
)

Definition at line 3252 of file partbounds.c.

3254{
3255 List *new_part_constraints;
3256 List *def_part_constraints;
3257 List *all_parts;
3258 ListCell *lc;
3259
3260 new_part_constraints = (new_spec->strategy == PARTITION_STRATEGY_LIST)
3261 ? get_qual_for_list(parent, new_spec)
3262 : get_qual_for_range(parent, new_spec, false);
3263 def_part_constraints =
3264 get_proposed_default_constraint(new_part_constraints);
3265
3266 /*
3267 * Map the Vars in the constraint expression from parent's attnos to
3268 * default_rel's.
3269 */
3270 def_part_constraints =
3271 map_partition_varattnos(def_part_constraints, 1, default_rel,
3272 parent);
3273
3274 /*
3275 * If the existing constraints on the default partition imply that it will
3276 * not contain any row that would belong to the new partition, we can
3277 * avoid scanning the default partition.
3278 */
3279 if (PartConstraintImpliedByRelConstraint(default_rel, def_part_constraints))
3280 {
3282 (errmsg_internal("updated partition constraint for default partition \"%s\" is implied by existing constraints",
3283 RelationGetRelationName(default_rel))));
3284 return;
3285 }
3286
3287 /*
3288 * Scan the default partition and its subpartitions, and check for rows
3289 * that do not satisfy the revised partition constraints.
3290 */
3291 if (default_rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
3292 all_parts = find_all_inheritors(RelationGetRelid(default_rel),
3293 AccessExclusiveLock, NULL);
3294 else
3295 all_parts = list_make1_oid(RelationGetRelid(default_rel));
3296
3297 foreach(lc, all_parts)
3298 {
3299 Oid part_relid = lfirst_oid(lc);
3300 Relation part_rel;
3301 Expr *partition_constraint;
3302 EState *estate;
3303 ExprState *partqualstate = NULL;
3304 Snapshot snapshot;
3305 ExprContext *econtext;
3306 TableScanDesc scan;
3307 MemoryContext oldCxt;
3308 TupleTableSlot *tupslot;
3309
3310 /* Lock already taken above. */
3311 if (part_relid != RelationGetRelid(default_rel))
3312 {
3313 part_rel = table_open(part_relid, NoLock);
3314
3315 /*
3316 * Map the Vars in the constraint expression from default_rel's
3317 * the sub-partition's.
3318 */
3319 partition_constraint = make_ands_explicit(def_part_constraints);
3320 partition_constraint = (Expr *)
3321 map_partition_varattnos((List *) partition_constraint, 1,
3322 part_rel, default_rel);
3323
3324 /*
3325 * If the partition constraints on default partition child imply
3326 * that it will not contain any row that would belong to the new
3327 * partition, we can avoid scanning the child table.
3328 */
3330 def_part_constraints))
3331 {
3333 (errmsg_internal("updated partition constraint for default partition \"%s\" is implied by existing constraints",
3334 RelationGetRelationName(part_rel))));
3335
3336 table_close(part_rel, NoLock);
3337 continue;
3338 }
3339 }
3340 else
3341 {
3342 part_rel = default_rel;
3343 partition_constraint = make_ands_explicit(def_part_constraints);
3344 }
3345
3346 /*
3347 * Only RELKIND_RELATION relations (i.e. leaf partitions) need to be
3348 * scanned.
3349 */
3350 if (part_rel->rd_rel->relkind != RELKIND_RELATION)
3351 {
3352 if (part_rel->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
3354 (errcode(ERRCODE_CHECK_VIOLATION),
3355 errmsg("skipped scanning foreign table \"%s\" which is a partition of default partition \"%s\"",
3356 RelationGetRelationName(part_rel),
3357 RelationGetRelationName(default_rel))));
3358
3359 if (RelationGetRelid(default_rel) != RelationGetRelid(part_rel))
3360 table_close(part_rel, NoLock);
3361
3362 continue;
3363 }
3364
3365 estate = CreateExecutorState();
3366
3367 /* Build expression execution states for partition check quals */
3368 partqualstate = ExecPrepareExpr(partition_constraint, estate);
3369
3370 econtext = GetPerTupleExprContext(estate);
3371 snapshot = RegisterSnapshot(GetLatestSnapshot());
3372 tupslot = table_slot_create(part_rel, &estate->es_tupleTable);
3373 scan = table_beginscan(part_rel, snapshot, 0, NULL);
3374
3375 /*
3376 * Switch to per-tuple memory context and reset it for each tuple
3377 * produced, so we don't leak memory.
3378 */
3380
3381 while (table_scan_getnextslot(scan, ForwardScanDirection, tupslot))
3382 {
3383 econtext->ecxt_scantuple = tupslot;
3384
3385 if (!ExecCheck(partqualstate, econtext))
3386 ereport(ERROR,
3387 (errcode(ERRCODE_CHECK_VIOLATION),
3388 errmsg("updated partition constraint for default partition \"%s\" would be violated by some row",
3389 RelationGetRelationName(default_rel)),
3390 errtable(default_rel)));
3391
3392 ResetExprContext(econtext);
3394 }
3395
3396 MemoryContextSwitchTo(oldCxt);
3397 table_endscan(scan);
3398 UnregisterSnapshot(snapshot);
3400 FreeExecutorState(estate);
3401
3402 if (RelationGetRelid(default_rel) != RelationGetRelid(part_rel))
3403 table_close(part_rel, NoLock); /* keep the lock until commit */
3404 }
3405}
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1170
int errcode(int sqlerrcode)
Definition: elog.c:863
int errmsg(const char *fmt,...)
Definition: elog.c:1080
#define WARNING
Definition: elog.h:36
#define DEBUG1
Definition: elog.h:30
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:150
ExprState * ExecPrepareExpr(Expr *node, EState *estate)
Definition: execExpr.c:765
bool ExecCheck(ExprState *state, ExprContext *econtext)
Definition: execExpr.c:872
void ExecDropSingleTupleTableSlot(TupleTableSlot *slot)
Definition: execTuples.c:1443
void FreeExecutorState(EState *estate)
Definition: execUtils.c:192
EState * CreateExecutorState(void)
Definition: execUtils.c:88
#define GetPerTupleExprContext(estate)
Definition: executor.h:656
#define ResetExprContext(econtext)
Definition: executor.h:650
#define GetPerTupleMemoryContext(estate)
Definition: executor.h:661
#define NoLock
Definition: lockdefs.h:34
#define AccessExclusiveLock
Definition: lockdefs.h:43
Expr * make_ands_explicit(List *andclauses)
Definition: makefuncs.c:799
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:123
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:124
@ PARTITION_STRATEGY_LIST
Definition: parsenodes.h:900
static List * get_qual_for_list(Relation parent, PartitionBoundSpec *spec)
Definition: partbounds.c:4067
static List * get_qual_for_range(Relation parent, PartitionBoundSpec *spec, bool for_default)
Definition: partbounds.c:4276
List * map_partition_varattnos(List *expr, int fromrel_varno, Relation to_rel, Relation from_rel)
Definition: partition.c:222
List * get_proposed_default_constraint(List *new_part_constraints)
Definition: partition.c:370
List * find_all_inheritors(Oid parentrelId, LOCKMODE lockmode, List **numparents)
Definition: pg_inherits.c:255
#define list_make1_oid(x1)
Definition: pg_list.h:242
#define lfirst_oid(lc)
Definition: pg_list.h:174
unsigned int Oid
Definition: postgres_ext.h:32
#define RelationGetRelid(relation)
Definition: rel.h:515
#define RelationGetRelationName(relation)
Definition: rel.h:549
int errtable(Relation rel)
Definition: relcache.c:6049
@ ForwardScanDirection
Definition: sdir.h:28
Snapshot GetLatestSnapshot(void)
Definition: snapmgr.c:353
void UnregisterSnapshot(Snapshot snapshot)
Definition: snapmgr.c:864
Snapshot RegisterSnapshot(Snapshot snapshot)
Definition: snapmgr.c:822
List * es_tupleTable
Definition: execnodes.h:712
TupleTableSlot * ecxt_scantuple
Definition: execnodes.h:273
Definition: pg_list.h:54
Form_pg_class rd_rel
Definition: rel.h:111
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:40
TupleTableSlot * table_slot_create(Relation relation, List **reglist)
Definition: tableam.c:92
static void table_endscan(TableScanDesc scan)
Definition: tableam.h:985
static bool table_scan_getnextslot(TableScanDesc sscan, ScanDirection direction, TupleTableSlot *slot)
Definition: tableam.h:1020
static TableScanDesc table_beginscan(Relation rel, Snapshot snapshot, int nkeys, ScanKeyData *key)
Definition: tableam.h:876
bool PartConstraintImpliedByRelConstraint(Relation scanrel, List *partConstraint)
Definition: tablecmds.c:20049

References AccessExclusiveLock, CHECK_FOR_INTERRUPTS, CreateExecutorState(), DEBUG1, ExprContext::ecxt_scantuple, ereport, errcode(), errmsg(), errmsg_internal(), ERROR, errtable(), EState::es_tupleTable, ExecCheck(), ExecDropSingleTupleTableSlot(), ExecPrepareExpr(), find_all_inheritors(), ForwardScanDirection, FreeExecutorState(), get_proposed_default_constraint(), get_qual_for_list(), get_qual_for_range(), GetLatestSnapshot(), GetPerTupleExprContext, GetPerTupleMemoryContext, lfirst_oid, list_make1_oid, make_ands_explicit(), map_partition_varattnos(), MemoryContextSwitchTo(), NoLock, PartConstraintImpliedByRelConstraint(), PARTITION_STRATEGY_LIST, RelationData::rd_rel, RegisterSnapshot(), RelationGetRelationName, RelationGetRelid, ResetExprContext, PartitionBoundSpec::strategy, table_beginscan(), table_close(), table_endscan(), table_open(), table_scan_getnextslot(), table_slot_create(), UnregisterSnapshot(), and WARNING.

Referenced by DefineRelation().

◆ check_new_partition_bound()

void check_new_partition_bound ( char *  relname,
Relation  parent,
PartitionBoundSpec spec,
ParseState pstate 
)

Definition at line 2897 of file partbounds.c.

2899{
2901 PartitionDesc partdesc = RelationGetPartitionDesc(parent, false);
2902 PartitionBoundInfo boundinfo = partdesc->boundinfo;
2903 int with = -1;
2904 bool overlap = false;
2905 int overlap_location = -1;
2906
2907 if (spec->is_default)
2908 {
2909 /*
2910 * The default partition bound never conflicts with any other
2911 * partition's; if that's what we're attaching, the only possible
2912 * problem is that one already exists, so check for that and we're
2913 * done.
2914 */
2915 if (boundinfo == NULL || !partition_bound_has_default(boundinfo))
2916 return;
2917
2918 /* Default partition already exists, error out. */
2919 ereport(ERROR,
2920 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
2921 errmsg("partition \"%s\" conflicts with existing default partition \"%s\"",
2922 relname, get_rel_name(partdesc->oids[boundinfo->default_index])),
2923 parser_errposition(pstate, spec->location)));
2924 }
2925
2926 switch (key->strategy)
2927 {
2929 {
2931 Assert(spec->remainder >= 0 && spec->remainder < spec->modulus);
2932
2933 if (partdesc->nparts > 0)
2934 {
2935 int greatest_modulus;
2936 int remainder;
2937 int offset;
2938
2939 /*
2940 * Check rule that every modulus must be a factor of the
2941 * next larger modulus. (For example, if you have a bunch
2942 * of partitions that all have modulus 5, you can add a
2943 * new partition with modulus 10 or a new partition with
2944 * modulus 15, but you cannot add both a partition with
2945 * modulus 10 and a partition with modulus 15, because 10
2946 * is not a factor of 15.) We need only check the next
2947 * smaller and next larger existing moduli, relying on
2948 * previous enforcement of this rule to be sure that the
2949 * rest are in line.
2950 */
2951
2952 /*
2953 * Get the greatest (modulus, remainder) pair contained in
2954 * boundinfo->datums that is less than or equal to the
2955 * (spec->modulus, spec->remainder) pair.
2956 */
2957 offset = partition_hash_bsearch(boundinfo,
2958 spec->modulus,
2959 spec->remainder);
2960 if (offset < 0)
2961 {
2962 int next_modulus;
2963
2964 /*
2965 * All existing moduli are greater or equal, so the
2966 * new one must be a factor of the smallest one, which
2967 * is first in the boundinfo.
2968 */
2969 next_modulus = DatumGetInt32(boundinfo->datums[0][0]);
2970 if (next_modulus % spec->modulus != 0)
2971 ereport(ERROR,
2972 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
2973 errmsg("every hash partition modulus must be a factor of the next larger modulus"),
2974 errdetail("The new modulus %d is not a factor of %d, the modulus of existing partition \"%s\".",
2975 spec->modulus, next_modulus,
2976 get_rel_name(partdesc->oids[0]))));
2977 }
2978 else
2979 {
2980 int prev_modulus;
2981
2982 /*
2983 * We found the largest (modulus, remainder) pair less
2984 * than or equal to the new one. That modulus must be
2985 * a divisor of, or equal to, the new modulus.
2986 */
2987 prev_modulus = DatumGetInt32(boundinfo->datums[offset][0]);
2988
2989 if (spec->modulus % prev_modulus != 0)
2990 ereport(ERROR,
2991 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
2992 errmsg("every hash partition modulus must be a factor of the next larger modulus"),
2993 errdetail("The new modulus %d is not divisible by %d, the modulus of existing partition \"%s\".",
2994 spec->modulus,
2995 prev_modulus,
2996 get_rel_name(partdesc->oids[offset]))));
2997
2998 if (offset + 1 < boundinfo->ndatums)
2999 {
3000 int next_modulus;
3001
3002 /*
3003 * Look at the next higher (modulus, remainder)
3004 * pair. That could have the same modulus and a
3005 * larger remainder than the new pair, in which
3006 * case we're good. If it has a larger modulus,
3007 * the new modulus must divide that one.
3008 */
3009 next_modulus = DatumGetInt32(boundinfo->datums[offset + 1][0]);
3010
3011 if (next_modulus % spec->modulus != 0)
3012 ereport(ERROR,
3013 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
3014 errmsg("every hash partition modulus must be a factor of the next larger modulus"),
3015 errdetail("The new modulus %d is not a factor of %d, the modulus of existing partition \"%s\".",
3016 spec->modulus, next_modulus,
3017 get_rel_name(partdesc->oids[offset + 1]))));
3018 }
3019 }
3020
3021 greatest_modulus = boundinfo->nindexes;
3022 remainder = spec->remainder;
3023
3024 /*
3025 * Normally, the lowest remainder that could conflict with
3026 * the new partition is equal to the remainder specified
3027 * for the new partition, but when the new partition has a
3028 * modulus higher than any used so far, we need to adjust.
3029 */
3030 if (remainder >= greatest_modulus)
3031 remainder = remainder % greatest_modulus;
3032
3033 /* Check every potentially-conflicting remainder. */
3034 do
3035 {
3036 if (boundinfo->indexes[remainder] != -1)
3037 {
3038 overlap = true;
3039 overlap_location = spec->location;
3040 with = boundinfo->indexes[remainder];
3041 break;
3042 }
3043 remainder += spec->modulus;
3044 } while (remainder < greatest_modulus);
3045 }
3046
3047 break;
3048 }
3049
3051 {
3053
3054 if (partdesc->nparts > 0)
3055 {
3056 ListCell *cell;
3057
3058 Assert(boundinfo &&
3059 boundinfo->strategy == PARTITION_STRATEGY_LIST &&
3060 (boundinfo->ndatums > 0 ||
3061 partition_bound_accepts_nulls(boundinfo) ||
3062 partition_bound_has_default(boundinfo)));
3063
3064 foreach(cell, spec->listdatums)
3065 {
3066 Const *val = lfirst_node(Const, cell);
3067
3068 overlap_location = val->location;
3069 if (!val->constisnull)
3070 {
3071 int offset;
3072 bool equal;
3073
3074 offset = partition_list_bsearch(&key->partsupfunc[0],
3075 key->partcollation,
3076 boundinfo,
3077 val->constvalue,
3078 &equal);
3079 if (offset >= 0 && equal)
3080 {
3081 overlap = true;
3082 with = boundinfo->indexes[offset];
3083 break;
3084 }
3085 }
3086 else if (partition_bound_accepts_nulls(boundinfo))
3087 {
3088 overlap = true;
3089 with = boundinfo->null_index;
3090 break;
3091 }
3092 }
3093 }
3094
3095 break;
3096 }
3097
3099 {
3101 *upper;
3102 int cmpval;
3103
3105 lower = make_one_partition_rbound(key, -1, spec->lowerdatums, true);
3106 upper = make_one_partition_rbound(key, -1, spec->upperdatums, false);
3107
3108 /*
3109 * First check if the resulting range would be empty with
3110 * specified lower and upper bounds. partition_rbound_cmp
3111 * cannot return zero here, since the lower-bound flags are
3112 * different.
3113 */
3114 cmpval = partition_rbound_cmp(key->partnatts,
3115 key->partsupfunc,
3116 key->partcollation,
3117 lower->datums, lower->kind,
3118 true, upper);
3119 Assert(cmpval != 0);
3120 if (cmpval > 0)
3121 {
3122 /* Point to problematic key in the lower datums list. */
3124 cmpval - 1);
3125
3126 ereport(ERROR,
3127 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
3128 errmsg("empty range bound specified for partition \"%s\"",
3129 relname),
3130 errdetail("Specified lower bound %s is greater than or equal to upper bound %s.",
3133 parser_errposition(pstate, datum->location)));
3134 }
3135
3136 if (partdesc->nparts > 0)
3137 {
3138 int offset;
3139
3140 Assert(boundinfo &&
3141 boundinfo->strategy == PARTITION_STRATEGY_RANGE &&
3142 (boundinfo->ndatums > 0 ||
3143 partition_bound_has_default(boundinfo)));
3144
3145 /*
3146 * Test whether the new lower bound (which is treated
3147 * inclusively as part of the new partition) lies inside
3148 * an existing partition, or in a gap.
3149 *
3150 * If it's inside an existing partition, the bound at
3151 * offset + 1 will be the upper bound of that partition,
3152 * and its index will be >= 0.
3153 *
3154 * If it's in a gap, the bound at offset + 1 will be the
3155 * lower bound of the next partition, and its index will
3156 * be -1. This is also true if there is no next partition,
3157 * since the index array is initialised with an extra -1
3158 * at the end.
3159 */
3160 offset = partition_range_bsearch(key->partnatts,
3161 key->partsupfunc,
3162 key->partcollation,
3163 boundinfo, lower,
3164 &cmpval);
3165
3166 if (boundinfo->indexes[offset + 1] < 0)
3167 {
3168 /*
3169 * Check that the new partition will fit in the gap.
3170 * For it to fit, the new upper bound must be less
3171 * than or equal to the lower bound of the next
3172 * partition, if there is one.
3173 */
3174 if (offset + 1 < boundinfo->ndatums)
3175 {
3176 Datum *datums;
3178 bool is_lower;
3179
3180 datums = boundinfo->datums[offset + 1];
3181 kind = boundinfo->kind[offset + 1];
3182 is_lower = (boundinfo->indexes[offset + 1] == -1);
3183
3184 cmpval = partition_rbound_cmp(key->partnatts,
3185 key->partsupfunc,
3186 key->partcollation,
3187 datums, kind,
3188 is_lower, upper);
3189 if (cmpval < 0)
3190 {
3191 /*
3192 * Point to problematic key in the upper
3193 * datums list.
3194 */
3195 PartitionRangeDatum *datum =
3196 list_nth(spec->upperdatums, abs(cmpval) - 1);
3197
3198 /*
3199 * The new partition overlaps with the
3200 * existing partition between offset + 1 and
3201 * offset + 2.
3202 */
3203 overlap = true;
3204 overlap_location = datum->location;
3205 with = boundinfo->indexes[offset + 2];
3206 }
3207 }
3208 }
3209 else
3210 {
3211 /*
3212 * The new partition overlaps with the existing
3213 * partition between offset and offset + 1.
3214 */
3215 PartitionRangeDatum *datum;
3216
3217 /*
3218 * Point to problematic key in the lower datums list;
3219 * if we have equality, point to the first one.
3220 */
3221 datum = cmpval == 0 ? linitial(spec->lowerdatums) :
3222 list_nth(spec->lowerdatums, abs(cmpval) - 1);
3223 overlap = true;
3224 overlap_location = datum->location;
3225 with = boundinfo->indexes[offset + 1];
3226 }
3227 }
3228
3229 break;
3230 }
3231 }
3232
3233 if (overlap)
3234 {
3235 Assert(with >= 0);
3236 ereport(ERROR,
3237 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
3238 errmsg("partition \"%s\" would overlap partition \"%s\"",
3239 relname, get_rel_name(partdesc->oids[with])),
3240 parser_errposition(pstate, overlap_location)));
3241 }
3242}
int errdetail(const char *fmt,...)
Definition: elog.c:1216
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:223
Assert(PointerIsAligned(start, uint64))
size_t remainder
long val
Definition: informix.c:689
char * get_rel_name(Oid relid)
Definition: lsyscache.c:2095
Datum lower(PG_FUNCTION_ARGS)
Definition: oracle_compat.c:49
Datum upper(PG_FUNCTION_ARGS)
Definition: oracle_compat.c:80
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:106
@ PARTITION_STRATEGY_HASH
Definition: parsenodes.h:902
@ PARTITION_STRATEGY_RANGE
Definition: parsenodes.h:901
PartitionRangeDatumKind
Definition: parsenodes.h:951
static int partition_range_bsearch(int partnatts, FmgrInfo *partsupfunc, Oid *partcollation, PartitionBoundInfo boundinfo, PartitionRangeBound *probe, int32 *cmpval)
Definition: partbounds.c:3654
static int32 partition_rbound_cmp(int partnatts, FmgrInfo *partsupfunc, Oid *partcollation, Datum *datums1, PartitionRangeDatumKind *kind1, bool lower1, PartitionRangeBound *b2)
Definition: partbounds.c:3489
static PartitionRangeBound * make_one_partition_rbound(PartitionKey key, int index, List *datums, bool lower)
Definition: partbounds.c:3429
int partition_hash_bsearch(PartitionBoundInfo boundinfo, int modulus, int remainder)
Definition: partbounds.c:3739
int partition_list_bsearch(FmgrInfo *partsupfunc, Oid *partcollation, PartitionBoundInfo boundinfo, Datum value, bool *is_equal)
Definition: partbounds.c:3608
#define partition_bound_has_default(bi)
Definition: partbounds.h:99
#define partition_bound_accepts_nulls(bi)
Definition: partbounds.h:98
PartitionKey RelationGetPartitionKey(Relation rel)
Definition: partcache.c:51
PartitionDesc RelationGetPartitionDesc(Relation rel, bool omit_detached)
Definition: partdesc.c:71
NameData relname
Definition: pg_class.h:38
#define lfirst_node(type, lc)
Definition: pg_list.h:176
static void * list_nth(const List *list, int n)
Definition: pg_list.h:299
#define linitial(l)
Definition: pg_list.h:178
uint64_t Datum
Definition: postgres.h:70
static int32 DatumGetInt32(Datum X)
Definition: postgres.h:212
char * get_range_partbound_string(List *bound_datums)
Definition: ruleutils.c:13711
PartitionRangeDatumKind ** kind
Definition: partbounds.h:84
PartitionStrategy strategy
Definition: partbounds.h:81
PartitionBoundInfo boundinfo
Definition: partdesc.h:38

References Assert(), PartitionDescData::boundinfo, DatumGetInt32(), PartitionBoundInfoData::datums, PartitionBoundInfoData::default_index, equal(), ereport, errcode(), errdetail(), errmsg(), ERROR, get_range_partbound_string(), get_rel_name(), PartitionBoundInfoData::indexes, PartitionBoundSpec::is_default, sort-test::key, PartitionBoundInfoData::kind, lfirst_node, linitial, list_nth(), PartitionBoundSpec::listdatums, PartitionBoundSpec::location, PartitionRangeDatum::location, lower(), PartitionBoundSpec::lowerdatums, make_one_partition_rbound(), PartitionBoundSpec::modulus, PartitionBoundInfoData::ndatums, PartitionBoundInfoData::nindexes, PartitionDescData::nparts, PartitionBoundInfoData::null_index, PartitionDescData::oids, parser_errposition(), partition_bound_accepts_nulls, partition_bound_has_default, partition_hash_bsearch(), partition_list_bsearch(), partition_range_bsearch(), partition_rbound_cmp(), PARTITION_STRATEGY_HASH, PARTITION_STRATEGY_LIST, PARTITION_STRATEGY_RANGE, RelationGetPartitionDesc(), RelationGetPartitionKey(), relname, remainder, PartitionBoundSpec::remainder, PartitionBoundSpec::strategy, PartitionBoundInfoData::strategy, upper(), PartitionBoundSpec::upperdatums, and val.

Referenced by ATExecAttachPartition(), and DefineRelation().

◆ compute_partition_hash_value()

uint64 compute_partition_hash_value ( int  partnatts,
FmgrInfo partsupfunc,
const Oid partcollation,
const Datum values,
const bool *  isnull 
)

Definition at line 4723 of file partbounds.c.

4725{
4726 int i;
4727 uint64 rowHash = 0;
4729
4730 for (i = 0; i < partnatts; i++)
4731 {
4732 /* Nulls are just ignored */
4733 if (!isnull[i])
4734 {
4735 Datum hash;
4736
4737 Assert(OidIsValid(partsupfunc[i].fn_oid));
4738
4739 /*
4740 * Compute hash for each datum value by calling respective
4741 * datatype-specific hash functions of each partition key
4742 * attribute.
4743 */
4744 hash = FunctionCall2Coll(&partsupfunc[i], partcollation[i],
4745 values[i], seed);
4746
4747 /* Form a single 64-bit hash value */
4748 rowHash = hash_combine64(rowHash, DatumGetUInt64(hash));
4749 }
4750 }
4751
4752 return rowHash;
4753}
static Datum values[MAXATTR]
Definition: bootstrap.c:153
uint64_t uint64
Definition: c.h:543
#define OidIsValid(objectId)
Definition: c.h:778
Datum FunctionCall2Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1150
static uint64 hash_combine64(uint64 a, uint64 b)
Definition: hashfn.h:80
int i
Definition: isn.c:77
#define HASH_PARTITION_SEED
Definition: partition.h:20
static uint64 DatumGetUInt64(Datum X)
Definition: postgres.h:413
static Datum UInt64GetDatum(uint64 X)
Definition: postgres.h:423
static unsigned hash(unsigned *uv, int n)
Definition: rege_dfa.c:715

References Assert(), DatumGetUInt64(), FunctionCall2Coll(), hash(), hash_combine64(), HASH_PARTITION_SEED, i, OidIsValid, UInt64GetDatum(), and values.

Referenced by get_matching_hash_bounds(), and get_partition_for_tuple().

◆ get_hash_partition_greatest_modulus()

int get_hash_partition_greatest_modulus ( PartitionBoundInfo  bound)

Definition at line 3415 of file partbounds.c.

3416{
3417 Assert(bound && bound->strategy == PARTITION_STRATEGY_HASH);
3418 return bound->nindexes;
3419}

References Assert(), PartitionBoundInfoData::nindexes, PARTITION_STRATEGY_HASH, and PartitionBoundInfoData::strategy.

◆ get_qual_from_partbound()

List * get_qual_from_partbound ( Relation  parent,
PartitionBoundSpec spec 
)

Definition at line 249 of file partbounds.c.

250{
252 List *my_qual = NIL;
253
254 Assert(key != NULL);
255
256 switch (key->strategy)
257 {
260 my_qual = get_qual_for_hash(parent, spec);
261 break;
262
265 my_qual = get_qual_for_list(parent, spec);
266 break;
267
270 my_qual = get_qual_for_range(parent, spec, false);
271 break;
272 }
273
274 return my_qual;
275}
static List * get_qual_for_hash(Relation parent, PartitionBoundSpec *spec)
Definition: partbounds.c:3984
#define NIL
Definition: pg_list.h:68

References Assert(), get_qual_for_hash(), get_qual_for_list(), get_qual_for_range(), sort-test::key, NIL, PARTITION_STRATEGY_HASH, PARTITION_STRATEGY_LIST, PARTITION_STRATEGY_RANGE, RelationGetPartitionKey(), and PartitionBoundSpec::strategy.

Referenced by ATExecAttachPartition(), and generate_partition_qual().

◆ partition_bounds_copy()

PartitionBoundInfo partition_bounds_copy ( PartitionBoundInfo  src,
PartitionKey  key 
)

Definition at line 1002 of file partbounds.c.

1004{
1006 int i;
1007 int ndatums;
1008 int nindexes;
1009 int partnatts;
1010
1012
1013 dest->strategy = src->strategy;
1014 ndatums = dest->ndatums = src->ndatums;
1015 nindexes = dest->nindexes = src->nindexes;
1016 partnatts = key->partnatts;
1017
1018 /* List partitioned tables have only a single partition key. */
1019 Assert(key->strategy != PARTITION_STRATEGY_LIST || partnatts == 1);
1020
1021 dest->datums = (Datum **) palloc(sizeof(Datum *) * ndatums);
1022
1023 if (src->kind != NULL && ndatums > 0)
1024 {
1025 PartitionRangeDatumKind *boundKinds;
1026
1027 /* only RANGE partition should have a non-NULL kind */
1028 Assert(key->strategy == PARTITION_STRATEGY_RANGE);
1029
1030 dest->kind = (PartitionRangeDatumKind **) palloc(ndatums *
1031 sizeof(PartitionRangeDatumKind *));
1032
1033 /*
1034 * In the loop below, to save from allocating a series of small arrays
1035 * for storing the PartitionRangeDatumKind, we allocate a single chunk
1036 * here and use a smaller portion of it for each datum.
1037 */
1038 boundKinds = (PartitionRangeDatumKind *) palloc(ndatums * partnatts *
1039 sizeof(PartitionRangeDatumKind));
1040
1041 for (i = 0; i < ndatums; i++)
1042 {
1043 dest->kind[i] = &boundKinds[i * partnatts];
1044 memcpy(dest->kind[i], src->kind[i],
1045 sizeof(PartitionRangeDatumKind) * partnatts);
1046 }
1047 }
1048 else
1049 dest->kind = NULL;
1050
1051 /* copy interleaved partitions for LIST partitioned tables */
1052 dest->interleaved_parts = bms_copy(src->interleaved_parts);
1053
1054 /*
1055 * For hash partitioning, datums array will have two elements - modulus
1056 * and remainder.
1057 */
1058 if (ndatums > 0)
1059 {
1060 bool hash_part = (key->strategy == PARTITION_STRATEGY_HASH);
1061 int natts = hash_part ? 2 : partnatts;
1062 Datum *boundDatums = palloc(ndatums * natts * sizeof(Datum));
1063
1064 for (i = 0; i < ndatums; i++)
1065 {
1066 int j;
1067
1068 dest->datums[i] = &boundDatums[i * natts];
1069
1070 for (j = 0; j < natts; j++)
1071 {
1072 if (dest->kind == NULL ||
1074 {
1075 bool byval;
1076 int typlen;
1077
1078 if (hash_part)
1079 {
1080 typlen = sizeof(int32); /* Always int4 */
1081 byval = true; /* int4 is pass-by-value */
1082 }
1083 else
1084 {
1085 byval = key->parttypbyval[j];
1086 typlen = key->parttyplen[j];
1087 }
1088 dest->datums[i][j] = datumCopy(src->datums[i][j],
1089 byval, typlen);
1090 }
1091 }
1092 }
1093 }
1094
1095 dest->indexes = (int *) palloc(sizeof(int) * nindexes);
1096 memcpy(dest->indexes, src->indexes, sizeof(int) * nindexes);
1097
1098 dest->null_index = src->null_index;
1099 dest->default_index = src->default_index;
1100
1101 return dest;
1102}
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:122
int32_t int32
Definition: c.h:538
Datum datumCopy(Datum value, bool typByVal, int typLen)
Definition: datum.c:132
int j
Definition: isn.c:78
void * palloc(Size size)
Definition: mcxt.c:1365
@ PARTITION_RANGE_DATUM_VALUE
Definition: parsenodes.h:953
struct PartitionBoundInfoData * PartitionBoundInfo
Definition: partdefs.h:16
Bitmapset * interleaved_parts
Definition: partbounds.h:87

References Assert(), bms_copy(), datumCopy(), PartitionBoundInfoData::datums, PartitionBoundInfoData::default_index, generate_unaccent_rules::dest, i, PartitionBoundInfoData::indexes, PartitionBoundInfoData::interleaved_parts, j, sort-test::key, PartitionBoundInfoData::kind, PartitionBoundInfoData::ndatums, PartitionBoundInfoData::nindexes, PartitionBoundInfoData::null_index, palloc(), PARTITION_RANGE_DATUM_VALUE, PARTITION_STRATEGY_HASH, PARTITION_STRATEGY_LIST, PARTITION_STRATEGY_RANGE, and PartitionBoundInfoData::strategy.

Referenced by RelationBuildPartitionDesc().

◆ partition_bounds_create()

PartitionBoundInfo partition_bounds_create ( PartitionBoundSpec **  boundspecs,
int  nparts,
PartitionKey  key,
int **  mapping 
)

Definition at line 299 of file partbounds.c.

301{
302 int i;
303
304 Assert(nparts > 0);
305
306 /*
307 * For each partitioning method, we first convert the partition bounds
308 * from their parser node representation to the internal representation,
309 * along with any additional preprocessing (such as de-duplicating range
310 * bounds). Resulting bound datums are then added to the 'datums' array
311 * in PartitionBoundInfo. For each datum added, an integer indicating the
312 * canonical partition index is added to the 'indexes' array.
313 *
314 * For each bound, we remember its partition's position (0-based) in the
315 * original list to later map it to the canonical index.
316 */
317
318 /*
319 * Initialize mapping array with invalid values, this is filled within
320 * each sub-routine below depending on the bound type.
321 */
322 *mapping = (int *) palloc(sizeof(int) * nparts);
323 for (i = 0; i < nparts; i++)
324 (*mapping)[i] = -1;
325
326 switch (key->strategy)
327 {
329 return create_hash_bounds(boundspecs, nparts, key, mapping);
330
332 return create_list_bounds(boundspecs, nparts, key, mapping);
333
335 return create_range_bounds(boundspecs, nparts, key, mapping);
336 }
337
338 Assert(false);
339 return NULL; /* keep compiler quiet */
340}
static PartitionBoundInfo create_list_bounds(PartitionBoundSpec **boundspecs, int nparts, PartitionKey key, int **mapping)
Definition: partbounds.c:462
static PartitionBoundInfo create_hash_bounds(PartitionBoundSpec **boundspecs, int nparts, PartitionKey key, int **mapping)
Definition: partbounds.c:347
static PartitionBoundInfo create_range_bounds(PartitionBoundSpec **boundspecs, int nparts, PartitionKey key, int **mapping)
Definition: partbounds.c:677

References Assert(), create_hash_bounds(), create_list_bounds(), create_range_bounds(), i, sort-test::key, palloc(), PARTITION_STRATEGY_HASH, PARTITION_STRATEGY_LIST, and PARTITION_STRATEGY_RANGE.

Referenced by RelationBuildPartitionDesc().

◆ partition_bounds_equal()

bool partition_bounds_equal ( int  partnatts,
int16 parttyplen,
bool *  parttypbyval,
PartitionBoundInfo  b1,
PartitionBoundInfo  b2 
)

Definition at line 896 of file partbounds.c.

898{
899 int i;
900
901 if (b1->strategy != b2->strategy)
902 return false;
903
904 if (b1->ndatums != b2->ndatums)
905 return false;
906
907 if (b1->nindexes != b2->nindexes)
908 return false;
909
910 if (b1->null_index != b2->null_index)
911 return false;
912
913 if (b1->default_index != b2->default_index)
914 return false;
915
916 /* For all partition strategies, the indexes[] arrays have to match */
917 for (i = 0; i < b1->nindexes; i++)
918 {
919 if (b1->indexes[i] != b2->indexes[i])
920 return false;
921 }
922
923 /* Finally, compare the datums[] arrays */
925 {
926 /*
927 * We arrange the partitions in the ascending order of their moduli
928 * and remainders. Also every modulus is factor of next larger
929 * modulus. Therefore we can safely store index of a given partition
930 * in indexes array at remainder of that partition. Also entries at
931 * (remainder + N * modulus) positions in indexes array are all same
932 * for (modulus, remainder) specification for any partition. Thus the
933 * datums arrays from the given bounds are the same, if and only if
934 * their indexes arrays are the same. So, it suffices to compare the
935 * indexes arrays.
936 *
937 * Nonetheless make sure that the bounds are indeed the same when the
938 * indexes match. Hash partition bound stores modulus and remainder
939 * at b1->datums[i][0] and b1->datums[i][1] position respectively.
940 */
941#ifdef USE_ASSERT_CHECKING
942 for (i = 0; i < b1->ndatums; i++)
943 Assert((b1->datums[i][0] == b2->datums[i][0] &&
944 b1->datums[i][1] == b2->datums[i][1]));
945#endif
946 }
947 else
948 {
949 for (i = 0; i < b1->ndatums; i++)
950 {
951 int j;
952
953 for (j = 0; j < partnatts; j++)
954 {
955 /* For range partitions, the bounds might not be finite. */
956 if (b1->kind != NULL)
957 {
958 /* The different kinds of bound all differ from each other */
959 if (b1->kind[i][j] != b2->kind[i][j])
960 return false;
961
962 /*
963 * Non-finite bounds are equal without further
964 * examination.
965 */
966 if (b1->kind[i][j] != PARTITION_RANGE_DATUM_VALUE)
967 continue;
968 }
969
970 /*
971 * Compare the actual values. Note that it would be both
972 * incorrect and unsafe to invoke the comparison operator
973 * derived from the partitioning specification here. It would
974 * be incorrect because we want the relcache entry to be
975 * updated for ANY change to the partition bounds, not just
976 * those that the partitioning operator thinks are
977 * significant. It would be unsafe because we might reach
978 * this code in the context of an aborted transaction, and an
979 * arbitrary partitioning operator might not be safe in that
980 * context. datumIsEqual() should be simple enough to be
981 * safe.
982 */
983 if (!datumIsEqual(b1->datums[i][j], b2->datums[i][j],
984 parttypbyval[j], parttyplen[j]))
985 return false;
986 }
987 }
988 }
989 return true;
990}
bool datumIsEqual(Datum value1, Datum value2, bool typByVal, int typLen)
Definition: datum.c:223

References Assert(), datumIsEqual(), PartitionBoundInfoData::datums, PartitionBoundInfoData::default_index, i, PartitionBoundInfoData::indexes, j, PartitionBoundInfoData::kind, PartitionBoundInfoData::ndatums, PartitionBoundInfoData::nindexes, PartitionBoundInfoData::null_index, PARTITION_RANGE_DATUM_VALUE, PARTITION_STRATEGY_HASH, and PartitionBoundInfoData::strategy.

Referenced by compute_partition_bounds().

◆ partition_bounds_merge()

PartitionBoundInfo partition_bounds_merge ( int  partnatts,
FmgrInfo partsupfunc,
Oid partcollation,
RelOptInfo outer_rel,
RelOptInfo inner_rel,
JoinType  jointype,
List **  outer_parts,
List **  inner_parts 
)

Definition at line 1119 of file partbounds.c.

1124{
1125 /*
1126 * Currently, this function is called only from try_partitionwise_join(),
1127 * so the join type should be INNER, LEFT, FULL, SEMI, or ANTI.
1128 */
1129 Assert(jointype == JOIN_INNER || jointype == JOIN_LEFT ||
1130 jointype == JOIN_FULL || jointype == JOIN_SEMI ||
1131 jointype == JOIN_ANTI);
1132
1133 /* The partitioning strategies should be the same. */
1134 Assert(outer_rel->boundinfo->strategy == inner_rel->boundinfo->strategy);
1135
1136 *outer_parts = *inner_parts = NIL;
1137 switch (outer_rel->boundinfo->strategy)
1138 {
1140
1141 /*
1142 * For hash partitioned tables, we currently support partitioned
1143 * join only when they have exactly the same partition bounds.
1144 *
1145 * XXX: it might be possible to relax the restriction to support
1146 * cases where hash partitioned tables have missing partitions
1147 * and/or different moduli, but it's not clear if it would be
1148 * useful to support the former case since it's unusual to have
1149 * missing partitions. On the other hand, it would be useful to
1150 * support the latter case, but in that case, there is a high
1151 * probability that a partition on one side will match multiple
1152 * partitions on the other side, which is the scenario the current
1153 * implementation of partitioned join can't handle.
1154 */
1155 return NULL;
1156
1158 return merge_list_bounds(partsupfunc,
1159 partcollation,
1160 outer_rel,
1161 inner_rel,
1162 jointype,
1163 outer_parts,
1164 inner_parts);
1165
1167 return merge_range_bounds(partnatts,
1168 partsupfunc,
1169 partcollation,
1170 outer_rel,
1171 inner_rel,
1172 jointype,
1173 outer_parts,
1174 inner_parts);
1175 }
1176
1177 return NULL;
1178}
@ JOIN_SEMI
Definition: nodes.h:317
@ JOIN_FULL
Definition: nodes.h:305
@ JOIN_INNER
Definition: nodes.h:303
@ JOIN_LEFT
Definition: nodes.h:304
@ JOIN_ANTI
Definition: nodes.h:318
static PartitionBoundInfo merge_range_bounds(int partnatts, FmgrInfo *partsupfuncs, Oid *partcollations, RelOptInfo *outer_rel, RelOptInfo *inner_rel, JoinType jointype, List **outer_parts, List **inner_parts)
Definition: partbounds.c:1507
static PartitionBoundInfo merge_list_bounds(FmgrInfo *partsupfunc, Oid *partcollation, RelOptInfo *outer_rel, RelOptInfo *inner_rel, JoinType jointype, List **outer_parts, List **inner_parts)
Definition: partbounds.c:1199

References Assert(), JOIN_ANTI, JOIN_FULL, JOIN_INNER, JOIN_LEFT, JOIN_SEMI, merge_list_bounds(), merge_range_bounds(), NIL, PARTITION_STRATEGY_HASH, PARTITION_STRATEGY_LIST, and PARTITION_STRATEGY_RANGE.

Referenced by compute_partition_bounds().

◆ partition_hash_bsearch()

int partition_hash_bsearch ( PartitionBoundInfo  boundinfo,
int  modulus,
int  remainder 
)

Definition at line 3739 of file partbounds.c.

3741{
3742 int lo,
3743 hi,
3744 mid;
3745
3746 lo = -1;
3747 hi = boundinfo->ndatums - 1;
3748 while (lo < hi)
3749 {
3750 int32 cmpval,
3751 bound_modulus,
3752 bound_remainder;
3753
3754 mid = (lo + hi + 1) / 2;
3755 bound_modulus = DatumGetInt32(boundinfo->datums[mid][0]);
3756 bound_remainder = DatumGetInt32(boundinfo->datums[mid][1]);
3757 cmpval = partition_hbound_cmp(bound_modulus, bound_remainder,
3758 modulus, remainder);
3759 if (cmpval <= 0)
3760 {
3761 lo = mid;
3762
3763 if (cmpval == 0)
3764 break;
3765 }
3766 else
3767 hi = mid - 1;
3768 }
3769
3770 return lo;
3771}
static int32 partition_hbound_cmp(int modulus1, int remainder1, int modulus2, int remainder2)
Definition: partbounds.c:3588

References DatumGetInt32(), PartitionBoundInfoData::datums, PartitionBoundInfoData::ndatums, partition_hbound_cmp(), and remainder.

Referenced by check_new_partition_bound().

◆ partition_list_bsearch()

int partition_list_bsearch ( FmgrInfo partsupfunc,
Oid partcollation,
PartitionBoundInfo  boundinfo,
Datum  value,
bool *  is_equal 
)

Definition at line 3608 of file partbounds.c.

3611{
3612 int lo,
3613 hi,
3614 mid;
3615
3616 lo = -1;
3617 hi = boundinfo->ndatums - 1;
3618 while (lo < hi)
3619 {
3620 int32 cmpval;
3621
3622 mid = (lo + hi + 1) / 2;
3623 cmpval = DatumGetInt32(FunctionCall2Coll(&partsupfunc[0],
3624 partcollation[0],
3625 boundinfo->datums[mid][0],
3626 value));
3627 if (cmpval <= 0)
3628 {
3629 lo = mid;
3630 *is_equal = (cmpval == 0);
3631 if (*is_equal)
3632 break;
3633 }
3634 else
3635 hi = mid - 1;
3636 }
3637
3638 return lo;
3639}
static struct @171 value

References DatumGetInt32(), PartitionBoundInfoData::datums, FunctionCall2Coll(), PartitionBoundInfoData::ndatums, and value.

Referenced by check_new_partition_bound(), get_matching_list_bounds(), and get_partition_for_tuple().

◆ partition_range_datum_bsearch()

int partition_range_datum_bsearch ( FmgrInfo partsupfunc,
Oid partcollation,
PartitionBoundInfo  boundinfo,
int  nvalues,
const Datum values,
bool *  is_equal 
)

Definition at line 3696 of file partbounds.c.

3699{
3700 int lo,
3701 hi,
3702 mid;
3703
3704 lo = -1;
3705 hi = boundinfo->ndatums - 1;
3706 while (lo < hi)
3707 {
3708 int32 cmpval;
3709
3710 mid = (lo + hi + 1) / 2;
3711 cmpval = partition_rbound_datum_cmp(partsupfunc,
3712 partcollation,
3713 boundinfo->datums[mid],
3714 boundinfo->kind[mid],
3715 values,
3716 nvalues);
3717 if (cmpval <= 0)
3718 {
3719 lo = mid;
3720 *is_equal = (cmpval == 0);
3721
3722 if (*is_equal)
3723 break;
3724 }
3725 else
3726 hi = mid - 1;
3727 }
3728
3729 return lo;
3730}
int32 partition_rbound_datum_cmp(FmgrInfo *partsupfunc, Oid *partcollation, const Datum *rb_datums, PartitionRangeDatumKind *rb_kind, const Datum *tuple_datums, int n_tuple_datums)
Definition: partbounds.c:3557

References PartitionBoundInfoData::datums, PartitionBoundInfoData::kind, PartitionBoundInfoData::ndatums, partition_rbound_datum_cmp(), and values.

Referenced by get_matching_range_bounds(), and get_partition_for_tuple().

◆ partition_rbound_datum_cmp()

int32 partition_rbound_datum_cmp ( FmgrInfo partsupfunc,
Oid partcollation,
const Datum rb_datums,
PartitionRangeDatumKind rb_kind,
const Datum tuple_datums,
int  n_tuple_datums 
)

Definition at line 3557 of file partbounds.c.

3560{
3561 int i;
3562 int32 cmpval = -1;
3563
3564 for (i = 0; i < n_tuple_datums; i++)
3565 {
3566 if (rb_kind[i] == PARTITION_RANGE_DATUM_MINVALUE)
3567 return -1;
3568 else if (rb_kind[i] == PARTITION_RANGE_DATUM_MAXVALUE)
3569 return 1;
3570
3571 cmpval = DatumGetInt32(FunctionCall2Coll(&partsupfunc[i],
3572 partcollation[i],
3573 rb_datums[i],
3574 tuple_datums[i]));
3575 if (cmpval != 0)
3576 break;
3577 }
3578
3579 return cmpval;
3580}
@ PARTITION_RANGE_DATUM_MAXVALUE
Definition: parsenodes.h:954
@ PARTITION_RANGE_DATUM_MINVALUE
Definition: parsenodes.h:952

References DatumGetInt32(), FunctionCall2Coll(), i, PARTITION_RANGE_DATUM_MAXVALUE, and PARTITION_RANGE_DATUM_MINVALUE.

Referenced by get_matching_range_bounds(), get_partition_for_tuple(), and partition_range_datum_bsearch().

◆ partitions_are_ordered()

bool partitions_are_ordered ( PartitionBoundInfo  boundinfo,
Bitmapset live_parts 
)

Definition at line 2853 of file partbounds.c.

2854{
2855 Assert(boundinfo != NULL);
2856
2857 switch (boundinfo->strategy)
2858 {
2860
2861 /*
2862 * RANGE-type partitioning guarantees that the partitions can be
2863 * scanned in the order that they're defined in the PartitionDesc
2864 * to provide sequential, non-overlapping ranges of tuples.
2865 * However, if a DEFAULT partition exists and it's contained
2866 * within live_parts, then the partitions are not ordered.
2867 */
2868 if (!partition_bound_has_default(boundinfo) ||
2869 !bms_is_member(boundinfo->default_index, live_parts))
2870 return true;
2871 break;
2872
2874
2875 /*
2876 * LIST partitioned are ordered providing none of live_parts
2877 * overlap with the partitioned table's interleaved partitions.
2878 */
2879 if (!bms_overlap(live_parts, boundinfo->interleaved_parts))
2880 return true;
2881
2882 break;
2884 break;
2885 }
2886
2887 return false;
2888}
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:510
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:582

References Assert(), bms_is_member(), bms_overlap(), PartitionBoundInfoData::default_index, PartitionBoundInfoData::interleaved_parts, partition_bound_has_default, PARTITION_STRATEGY_HASH, PARTITION_STRATEGY_LIST, PARTITION_STRATEGY_RANGE, and PartitionBoundInfoData::strategy.

Referenced by build_partition_pathkeys(), and generate_orderedappend_paths().