PostgreSQL Source Code  git master
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 PartitionBoundInfoData PartitionBoundInfoData
 

Functions

int get_hash_partition_greatest_modulus (PartitionBoundInfo bound)
 
uint64 compute_partition_hash_value (int partnatts, FmgrInfo *partsupfunc, Oid *partcollation, Datum *values, 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, struct RelOptInfo *outer_rel, struct 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, Datum *rb_datums, PartitionRangeDatumKind *rb_kind, 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, 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

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  {
3281  ereport(DEBUG1,
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  {
3332  ereport(DEBUG1,
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)
3353  ereport(WARNING,
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:1156
int errcode(int sqlerrcode)
Definition: elog.c:858
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define WARNING
Definition: elog.h:36
#define DEBUG1
Definition: elog.h:30
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
ExprState * ExecPrepareExpr(Expr *node, EState *estate)
Definition: execExpr.c:747
bool ExecCheck(ExprState *state, ExprContext *econtext)
Definition: execExpr.c:854
void ExecDropSingleTupleTableSlot(TupleTableSlot *slot)
Definition: execTuples.c:1254
EState * CreateExecutorState(void)
Definition: execUtils.c:93
void FreeExecutorState(EState *estate)
Definition: execUtils.c:193
#define GetPerTupleExprContext(estate)
Definition: executor.h:538
#define ResetExprContext(econtext)
Definition: executor.h:532
#define GetPerTupleMemoryContext(estate)
Definition: executor.h:543
#define NoLock
Definition: lockdefs.h:34
#define AccessExclusiveLock
Definition: lockdefs.h:43
Expr * make_ands_explicit(List *andclauses)
Definition: makefuncs.c:710
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:121
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:138
@ PARTITION_STRATEGY_LIST
Definition: parsenodes.h:866
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:221
List * get_proposed_default_constraint(List *new_part_constraints)
Definition: partition.c:369
List * find_all_inheritors(Oid parentrelId, LOCKMODE lockmode, List **numparents)
Definition: pg_inherits.c:256
#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:31
#define RelationGetRelid(relation)
Definition: rel.h:501
#define RelationGetRelationName(relation)
Definition: rel.h:535
int errtable(Relation rel)
Definition: relcache.c:5876
@ ForwardScanDirection
Definition: sdir.h:28
Snapshot GetLatestSnapshot(void)
Definition: snapmgr.c:326
void UnregisterSnapshot(Snapshot snapshot)
Definition: snapmgr.c:871
Snapshot RegisterSnapshot(Snapshot snapshot)
Definition: snapmgr.c:829
List * es_tupleTable
Definition: execnodes.h:658
TupleTableSlot * ecxt_scantuple
Definition: execnodes.h:247
Definition: pg_list.h:54
Form_pg_class rd_rel
Definition: rel.h:110
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:91
static TableScanDesc table_beginscan(Relation rel, Snapshot snapshot, int nkeys, struct ScanKeyData *key)
Definition: tableam.h:885
static void table_endscan(TableScanDesc scan)
Definition: tableam.h:993
static bool table_scan_getnextslot(TableScanDesc sscan, ScanDirection direction, TupleTableSlot *slot)
Definition: tableam.h:1034
bool PartConstraintImpliedByRelConstraint(Relation scanrel, List *partConstraint)
Definition: tablecmds.c:17412

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. */
3123  PartitionRangeDatum *datum = list_nth(spec->lowerdatums,
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:1202
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:225
long val
Definition: informix.c:664
Assert(fmt[strlen(fmt) - 1] !='\n')
char * get_rel_name(Oid relid)
Definition: lsyscache.c:1910
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:111
@ PARTITION_STRATEGY_HASH
Definition: parsenodes.h:868
@ PARTITION_STRATEGY_RANGE
Definition: parsenodes.h:867
PartitionRangeDatumKind
Definition: parsenodes.h:917
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:54
PartitionDesc RelationGetPartitionDesc(Relation rel, bool omit_detached)
Definition: partdesc.c:72
NameData relname
Definition: pg_class.h:38
#define lfirst_node(type, lc)
Definition: pg_list.h:176
#define linitial(l)
Definition: pg_list.h:178
static void * list_nth(const List *list, int n)
Definition: pg_list.h:299
uintptr_t Datum
Definition: postgres.h:64
static int32 DatumGetInt32(Datum X)
Definition: postgres.h:202
char * get_range_partbound_string(List *bound_datums)
Definition: ruleutils.c:12174
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, 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,
Oid partcollation,
Datum values,
bool isnull 
)

Definition at line 4728 of file partbounds.c.

4730 {
4731  int i;
4732  uint64 rowHash = 0;
4734 
4735  for (i = 0; i < partnatts; i++)
4736  {
4737  /* Nulls are just ignored */
4738  if (!isnull[i])
4739  {
4740  Datum hash;
4741 
4742  Assert(OidIsValid(partsupfunc[i].fn_oid));
4743 
4744  /*
4745  * Compute hash for each datum value by calling respective
4746  * datatype-specific hash functions of each partition key
4747  * attribute.
4748  */
4749  hash = FunctionCall2Coll(&partsupfunc[i], partcollation[i],
4750  values[i], seed);
4751 
4752  /* Form a single 64-bit hash value */
4753  rowHash = hash_combine64(rowHash, DatumGetUInt64(hash));
4754  }
4755  }
4756 
4757  return rowHash;
4758 }
static Datum values[MAXATTR]
Definition: bootstrap.c:156
#define OidIsValid(objectId)
Definition: c.h:759
Datum FunctionCall2Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1136
static uint64 hash_combine64(uint64 a, uint64 b)
Definition: hashfn.h:80
int i
Definition: isn.c:73
#define HASH_PARTITION_SEED
Definition: partition.h:20
static uint64 DatumGetUInt64(Datum X)
Definition: postgres.h:419
static Datum UInt64GetDatum(uint64 X)
Definition: postgres.h:436
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 250 of file partbounds.c.

251 {
253  List *my_qual = NIL;
254 
255  Assert(key != NULL);
256 
257  switch (key->strategy)
258  {
261  my_qual = get_qual_for_hash(parent, spec);
262  break;
263 
266  my_qual = get_qual_for_list(parent, spec);
267  break;
268 
271  my_qual = get_qual_for_range(parent, spec, false);
272  break;
273  }
274 
275  return my_qual;
276 }
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 1003 of file partbounds.c.

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

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

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 897 of file partbounds.c.

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

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 @143 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,
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, Datum *rb_datums, PartitionRangeDatumKind *rb_kind, 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,
Datum rb_datums,
PartitionRangeDatumKind rb_kind,
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:920
@ PARTITION_RANGE_DATUM_MINVALUE
Definition: parsenodes.h:918

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:428
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:495

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().