PostgreSQL Source Code  git master
partbounds.c File Reference
#include "postgres.h"
#include "access/relation.h"
#include "access/table.h"
#include "access/tableam.h"
#include "catalog/partition.h"
#include "catalog/pg_inherits.h"
#include "catalog/pg_type.h"
#include "commands/tablecmds.h"
#include "common/hashfn.h"
#include "executor/executor.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "nodes/pathnodes.h"
#include "parser/parse_coerce.h"
#include "partitioning/partbounds.h"
#include "partitioning/partdesc.h"
#include "partitioning/partprune.h"
#include "utils/builtins.h"
#include "utils/datum.h"
#include "utils/fmgroids.h"
#include "utils/lsyscache.h"
#include "utils/partcache.h"
#include "utils/ruleutils.h"
#include "utils/snapmgr.h"
#include "utils/syscache.h"
Include dependency graph for partbounds.c:

Go to the source code of this file.

Data Structures

struct  PartitionHashBound
 
struct  PartitionListValue
 
struct  PartitionRangeBound
 
struct  PartitionMap
 

Macros

#define compare_range_bounds(partnatts, partsupfunc, partcollations, bound1, bound2)
 

Typedefs

typedef struct PartitionHashBound PartitionHashBound
 
typedef struct PartitionListValue PartitionListValue
 
typedef struct PartitionRangeBound PartitionRangeBound
 
typedef struct PartitionMap PartitionMap
 

Functions

static int32 qsort_partition_hbound_cmp (const void *a, const void *b)
 
static int32 qsort_partition_list_value_cmp (const void *a, const void *b, void *arg)
 
static int32 qsort_partition_rbound_cmp (const void *a, const void *b, void *arg)
 
static PartitionBoundInfo create_hash_bounds (PartitionBoundSpec **boundspecs, int nparts, PartitionKey key, int **mapping)
 
static PartitionBoundInfo create_list_bounds (PartitionBoundSpec **boundspecs, int nparts, PartitionKey key, int **mapping)
 
static PartitionBoundInfo create_range_bounds (PartitionBoundSpec **boundspecs, int nparts, PartitionKey key, int **mapping)
 
static PartitionBoundInfo merge_list_bounds (FmgrInfo *partsupfunc, Oid *collations, RelOptInfo *outer_rel, RelOptInfo *inner_rel, JoinType jointype, List **outer_parts, List **inner_parts)
 
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)
 
static void init_partition_map (RelOptInfo *rel, PartitionMap *map)
 
static void free_partition_map (PartitionMap *map)
 
static bool is_dummy_partition (RelOptInfo *rel, int part_index)
 
static int merge_matching_partitions (PartitionMap *outer_map, PartitionMap *inner_map, int outer_part, int inner_part, int *next_index)
 
static int process_outer_partition (PartitionMap *outer_map, PartitionMap *inner_map, bool outer_has_default, bool inner_has_default, int outer_index, int inner_default, JoinType jointype, int *next_index, int *default_index)
 
static int process_inner_partition (PartitionMap *outer_map, PartitionMap *inner_map, bool outer_has_default, bool inner_has_default, int inner_index, int outer_default, JoinType jointype, int *next_index, int *default_index)
 
static void merge_null_partitions (PartitionMap *outer_map, PartitionMap *inner_map, bool outer_has_null, bool inner_has_null, int outer_null, int inner_null, JoinType jointype, int *next_index, int *null_index)
 
static void merge_default_partitions (PartitionMap *outer_map, PartitionMap *inner_map, bool outer_has_default, bool inner_has_default, int outer_default, int inner_default, JoinType jointype, int *next_index, int *default_index)
 
static int merge_partition_with_dummy (PartitionMap *map, int index, int *next_index)
 
static void fix_merged_indexes (PartitionMap *outer_map, PartitionMap *inner_map, int nmerged, List *merged_indexes)
 
static void generate_matching_part_pairs (RelOptInfo *outer_rel, RelOptInfo *inner_rel, PartitionMap *outer_map, PartitionMap *inner_map, int nmerged, List **outer_parts, List **inner_parts)
 
static PartitionBoundInfo build_merged_partition_bounds (char strategy, List *merged_datums, List *merged_kinds, List *merged_indexes, int null_index, int default_index)
 
static int get_range_partition (RelOptInfo *rel, PartitionBoundInfo bi, int *lb_pos, PartitionRangeBound *lb, PartitionRangeBound *ub)
 
static int get_range_partition_internal (PartitionBoundInfo bi, int *lb_pos, PartitionRangeBound *lb, PartitionRangeBound *ub)
 
static bool compare_range_partitions (int partnatts, FmgrInfo *partsupfuncs, Oid *partcollations, PartitionRangeBound *outer_lb, PartitionRangeBound *outer_ub, PartitionRangeBound *inner_lb, PartitionRangeBound *inner_ub, int *lb_cmpval, int *ub_cmpval)
 
static void get_merged_range_bounds (int partnatts, FmgrInfo *partsupfuncs, Oid *partcollations, JoinType jointype, PartitionRangeBound *outer_lb, PartitionRangeBound *outer_ub, PartitionRangeBound *inner_lb, PartitionRangeBound *inner_ub, int lb_cmpval, int ub_cmpval, PartitionRangeBound *merged_lb, PartitionRangeBound *merged_ub)
 
static void add_merged_range_bounds (int partnatts, FmgrInfo *partsupfuncs, Oid *partcollations, PartitionRangeBound *merged_lb, PartitionRangeBound *merged_ub, int merged_index, List **merged_datums, List **merged_kinds, List **merged_indexes)
 
static PartitionRangeBoundmake_one_partition_rbound (PartitionKey key, int index, List *datums, bool lower)
 
static int32 partition_hbound_cmp (int modulus1, int remainder1, int modulus2, int remainder2)
 
static int32 partition_rbound_cmp (int partnatts, FmgrInfo *partsupfunc, Oid *partcollation, Datum *datums1, PartitionRangeDatumKind *kind1, bool lower1, PartitionRangeBound *b2)
 
static int partition_range_bsearch (int partnatts, FmgrInfo *partsupfunc, Oid *partcollation, PartitionBoundInfo boundinfo, PartitionRangeBound *probe, int32 *cmpval)
 
static Exprmake_partition_op_expr (PartitionKey key, int keynum, uint16 strategy, Expr *arg1, Expr *arg2)
 
static Oid get_partition_operator (PartitionKey key, int col, StrategyNumber strategy, bool *need_relabel)
 
static Listget_qual_for_hash (Relation parent, PartitionBoundSpec *spec)
 
static Listget_qual_for_list (Relation parent, PartitionBoundSpec *spec)
 
static Listget_qual_for_range (Relation parent, PartitionBoundSpec *spec, bool for_default)
 
static void get_range_key_properties (PartitionKey key, int keynum, PartitionRangeDatum *ldatum, PartitionRangeDatum *udatum, ListCell **partexprs_item, Expr **keyCol, Const **lower_val, Const **upper_val)
 
static Listget_range_nulltest (PartitionKey key)
 
Listget_qual_from_partbound (Relation parent, PartitionBoundSpec *spec)
 
PartitionBoundInfo partition_bounds_create (PartitionBoundSpec **boundspecs, int nparts, PartitionKey key, int **mapping)
 
static int get_non_null_list_datum_count (PartitionBoundSpec **boundspecs, int nparts)
 
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)
 
int get_hash_partition_greatest_modulus (PartitionBoundInfo bound)
 
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)
 
uint64 compute_partition_hash_value (int partnatts, FmgrInfo *partsupfunc, Oid *partcollation, Datum *values, bool *isnull)
 
Datum satisfies_hash_partition (PG_FUNCTION_ARGS)
 

Macro Definition Documentation

◆ compare_range_bounds

#define compare_range_bounds (   partnatts,
  partsupfunc,
  partcollations,
  bound1,
  bound2 
)
Value:
(partition_rbound_cmp(partnatts, partsupfunc, partcollations, \
(bound1)->datums, (bound1)->kind, (bound1)->lower, \
bound2))
Datum lower(PG_FUNCTION_ARGS)
Definition: oracle_compat.c:46
static int32 partition_rbound_cmp(int partnatts, FmgrInfo *partsupfunc, Oid *partcollation, Datum *datums1, PartitionRangeDatumKind *kind1, bool lower1, PartitionRangeBound *b2)
Definition: partbounds.c:3501

Definition at line 88 of file partbounds.c.

Referenced by compare_range_partitions(), get_merged_range_bounds(), merge_range_bounds(), and qsort_partition_rbound_cmp().

Typedef Documentation

◆ PartitionHashBound

◆ PartitionListValue

◆ PartitionMap

typedef struct PartitionMap PartitionMap

◆ PartitionRangeBound

Function Documentation

◆ add_merged_range_bounds()

static void add_merged_range_bounds ( int  partnatts,
FmgrInfo partsupfuncs,
Oid partcollations,
PartitionRangeBound merged_lb,
PartitionRangeBound merged_ub,
int  merged_index,
List **  merged_datums,
List **  merged_kinds,
List **  merged_indexes 
)
static

Definition at line 2783 of file partbounds.c.

References Assert, PartitionRangeBound::datums, PartitionRangeBound::index, PartitionRangeBound::kind, lappend(), lappend_int(), llast, llast_int, PartitionRangeBound::lower, and partition_rbound_cmp().

Referenced by merge_range_bounds().

2791 {
2792  int cmpval;
2793 
2794  if (!*merged_datums)
2795  {
2796  /* First merged partition */
2797  Assert(!*merged_kinds);
2798  Assert(!*merged_indexes);
2799  cmpval = 1;
2800  }
2801  else
2802  {
2803  PartitionRangeBound prev_ub;
2804 
2805  Assert(*merged_datums);
2806  Assert(*merged_kinds);
2807  Assert(*merged_indexes);
2808 
2809  /* Get the last upper bound. */
2810  prev_ub.index = llast_int(*merged_indexes);
2811  prev_ub.datums = (Datum *) llast(*merged_datums);
2812  prev_ub.kind = (PartitionRangeDatumKind *) llast(*merged_kinds);
2813  prev_ub.lower = false;
2814 
2815  /*
2816  * We pass lower1 = false to partition_rbound_cmp() to prevent it from
2817  * considering the last upper bound to be smaller than the lower bound
2818  * of the merged partition when the values of the two range bounds
2819  * compare equal.
2820  */
2821  cmpval = partition_rbound_cmp(partnatts, partsupfuncs, partcollations,
2822  merged_lb->datums, merged_lb->kind,
2823  false, &prev_ub);
2824  Assert(cmpval >= 0);
2825  }
2826 
2827  /*
2828  * If the lower bound is higher than the last upper bound, add the lower
2829  * bound with the index as -1 indicating that that is a lower bound; else,
2830  * the last upper bound will be reused as the lower bound of the merged
2831  * partition, so skip this.
2832  */
2833  if (cmpval > 0)
2834  {
2835  *merged_datums = lappend(*merged_datums, merged_lb->datums);
2836  *merged_kinds = lappend(*merged_kinds, merged_lb->kind);
2837  *merged_indexes = lappend_int(*merged_indexes, -1);
2838  }
2839 
2840  /* Add the upper bound and index of the merged partition. */
2841  *merged_datums = lappend(*merged_datums, merged_ub->datums);
2842  *merged_kinds = lappend(*merged_kinds, merged_ub->kind);
2843  *merged_indexes = lappend_int(*merged_indexes, merged_index);
2844 }
PartitionRangeDatumKind * kind
Definition: partbounds.c:68
PartitionRangeDatumKind
Definition: parsenodes.h:850
#define llast(l)
Definition: pg_list.h:194
List * lappend_int(List *list, int datum)
Definition: list.c:354
List * lappend(List *list, void *datum)
Definition: list.c:336
uintptr_t Datum
Definition: postgres.h:411
static int32 partition_rbound_cmp(int partnatts, FmgrInfo *partsupfunc, Oid *partcollation, Datum *datums1, PartitionRangeDatumKind *kind1, bool lower1, PartitionRangeBound *b2)
Definition: partbounds.c:3501
#define Assert(condition)
Definition: c.h:804
#define llast_int(l)
Definition: pg_list.h:195

◆ build_merged_partition_bounds()

static PartitionBoundInfo build_merged_partition_bounds ( char  strategy,
List merged_datums,
List merged_kinds,
List merged_indexes,
int  null_index,
int  default_index 
)
static

Definition at line 2529 of file partbounds.c.

References Assert, PartitionBoundInfoData::datums, PartitionBoundInfoData::default_index, PartitionBoundInfoData::indexes, PartitionBoundInfoData::kind, lappend_int(), lfirst, lfirst_int, list_length(), PartitionBoundInfoData::ndatums, NIL, PartitionBoundInfoData::nindexes, PartitionBoundInfoData::null_index, palloc(), PARTITION_STRATEGY_LIST, PARTITION_STRATEGY_RANGE, and PartitionBoundInfoData::strategy.

Referenced by merge_list_bounds(), and merge_range_bounds().

2532 {
2533  PartitionBoundInfo merged_bounds;
2534  int ndatums = list_length(merged_datums);
2535  int pos;
2536  ListCell *lc;
2537 
2538  merged_bounds = (PartitionBoundInfo) palloc(sizeof(PartitionBoundInfoData));
2539  merged_bounds->strategy = strategy;
2540  merged_bounds->ndatums = ndatums;
2541 
2542  merged_bounds->datums = (Datum **) palloc(sizeof(Datum *) * ndatums);
2543  pos = 0;
2544  foreach(lc, merged_datums)
2545  merged_bounds->datums[pos++] = (Datum *) lfirst(lc);
2546 
2547  if (strategy == PARTITION_STRATEGY_RANGE)
2548  {
2549  Assert(list_length(merged_kinds) == ndatums);
2550  merged_bounds->kind = (PartitionRangeDatumKind **)
2551  palloc(sizeof(PartitionRangeDatumKind *) * ndatums);
2552  pos = 0;
2553  foreach(lc, merged_kinds)
2554  merged_bounds->kind[pos++] = (PartitionRangeDatumKind *) lfirst(lc);
2555 
2556  /* There are ndatums+1 indexes in the case of range partitioning. */
2557  merged_indexes = lappend_int(merged_indexes, -1);
2558  ndatums++;
2559  }
2560  else
2561  {
2562  Assert(strategy == PARTITION_STRATEGY_LIST);
2563  Assert(merged_kinds == NIL);
2564  merged_bounds->kind = NULL;
2565  }
2566 
2567  Assert(list_length(merged_indexes) == ndatums);
2568  merged_bounds->nindexes = ndatums;
2569  merged_bounds->indexes = (int *) palloc(sizeof(int) * ndatums);
2570  pos = 0;
2571  foreach(lc, merged_indexes)
2572  merged_bounds->indexes[pos++] = lfirst_int(lc);
2573 
2574  merged_bounds->null_index = null_index;
2575  merged_bounds->default_index = default_index;
2576 
2577  return merged_bounds;
2578 }
#define NIL
Definition: pg_list.h:65
PartitionRangeDatumKind ** kind
Definition: partbounds.h:82
PartitionRangeDatumKind
Definition: parsenodes.h:850
#define lfirst_int(lc)
Definition: pg_list.h:170
List * lappend_int(List *list, int datum)
Definition: list.c:354
uintptr_t Datum
Definition: postgres.h:411
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
static int list_length(const List *l)
Definition: pg_list.h:149
#define PARTITION_STRATEGY_LIST
Definition: parsenodes.h:815
#define PARTITION_STRATEGY_RANGE
Definition: parsenodes.h:816
void * palloc(Size size)
Definition: mcxt.c:1062
struct PartitionBoundInfoData * PartitionBoundInfo
Definition: partdefs.h:16

◆ check_default_partition_contents()

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

Definition at line 3264 of file partbounds.c.

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

3266 {
3267  List *new_part_constraints;
3268  List *def_part_constraints;
3269  List *all_parts;
3270  ListCell *lc;
3271 
3272  new_part_constraints = (new_spec->strategy == PARTITION_STRATEGY_LIST)
3273  ? get_qual_for_list(parent, new_spec)
3274  : get_qual_for_range(parent, new_spec, false);
3275  def_part_constraints =
3276  get_proposed_default_constraint(new_part_constraints);
3277 
3278  /*
3279  * Map the Vars in the constraint expression from parent's attnos to
3280  * default_rel's.
3281  */
3282  def_part_constraints =
3283  map_partition_varattnos(def_part_constraints, 1, default_rel,
3284  parent);
3285 
3286  /*
3287  * If the existing constraints on the default partition imply that it will
3288  * not contain any row that would belong to the new partition, we can
3289  * avoid scanning the default partition.
3290  */
3291  if (PartConstraintImpliedByRelConstraint(default_rel, def_part_constraints))
3292  {
3293  ereport(DEBUG1,
3294  (errmsg_internal("updated partition constraint for default partition \"%s\" is implied by existing constraints",
3295  RelationGetRelationName(default_rel))));
3296  return;
3297  }
3298 
3299  /*
3300  * Scan the default partition and its subpartitions, and check for rows
3301  * that do not satisfy the revised partition constraints.
3302  */
3303  if (default_rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
3304  all_parts = find_all_inheritors(RelationGetRelid(default_rel),
3305  AccessExclusiveLock, NULL);
3306  else
3307  all_parts = list_make1_oid(RelationGetRelid(default_rel));
3308 
3309  foreach(lc, all_parts)
3310  {
3311  Oid part_relid = lfirst_oid(lc);
3312  Relation part_rel;
3313  Expr *partition_constraint;
3314  EState *estate;
3315  ExprState *partqualstate = NULL;
3316  Snapshot snapshot;
3317  ExprContext *econtext;
3318  TableScanDesc scan;
3319  MemoryContext oldCxt;
3320  TupleTableSlot *tupslot;
3321 
3322  /* Lock already taken above. */
3323  if (part_relid != RelationGetRelid(default_rel))
3324  {
3325  part_rel = table_open(part_relid, NoLock);
3326 
3327  /*
3328  * Map the Vars in the constraint expression from default_rel's
3329  * the sub-partition's.
3330  */
3331  partition_constraint = make_ands_explicit(def_part_constraints);
3332  partition_constraint = (Expr *)
3333  map_partition_varattnos((List *) partition_constraint, 1,
3334  part_rel, default_rel);
3335 
3336  /*
3337  * If the partition constraints on default partition child imply
3338  * that it will not contain any row that would belong to the new
3339  * partition, we can avoid scanning the child table.
3340  */
3342  def_part_constraints))
3343  {
3344  ereport(DEBUG1,
3345  (errmsg_internal("updated partition constraint for default partition \"%s\" is implied by existing constraints",
3346  RelationGetRelationName(part_rel))));
3347 
3348  table_close(part_rel, NoLock);
3349  continue;
3350  }
3351  }
3352  else
3353  {
3354  part_rel = default_rel;
3355  partition_constraint = make_ands_explicit(def_part_constraints);
3356  }
3357 
3358  /*
3359  * Only RELKIND_RELATION relations (i.e. leaf partitions) need to be
3360  * scanned.
3361  */
3362  if (part_rel->rd_rel->relkind != RELKIND_RELATION)
3363  {
3364  if (part_rel->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
3365  ereport(WARNING,
3366  (errcode(ERRCODE_CHECK_VIOLATION),
3367  errmsg("skipped scanning foreign table \"%s\" which is a partition of default partition \"%s\"",
3368  RelationGetRelationName(part_rel),
3369  RelationGetRelationName(default_rel))));
3370 
3371  if (RelationGetRelid(default_rel) != RelationGetRelid(part_rel))
3372  table_close(part_rel, NoLock);
3373 
3374  continue;
3375  }
3376 
3377  estate = CreateExecutorState();
3378 
3379  /* Build expression execution states for partition check quals */
3380  partqualstate = ExecPrepareExpr(partition_constraint, estate);
3381 
3382  econtext = GetPerTupleExprContext(estate);
3383  snapshot = RegisterSnapshot(GetLatestSnapshot());
3384  tupslot = table_slot_create(part_rel, &estate->es_tupleTable);
3385  scan = table_beginscan(part_rel, snapshot, 0, NULL);
3386 
3387  /*
3388  * Switch to per-tuple memory context and reset it for each tuple
3389  * produced, so we don't leak memory.
3390  */
3392 
3393  while (table_scan_getnextslot(scan, ForwardScanDirection, tupslot))
3394  {
3395  econtext->ecxt_scantuple = tupslot;
3396 
3397  if (!ExecCheck(partqualstate, econtext))
3398  ereport(ERROR,
3399  (errcode(ERRCODE_CHECK_VIOLATION),
3400  errmsg("updated partition constraint for default partition \"%s\" would be violated by some row",
3401  RelationGetRelationName(default_rel)),
3402  errtable(default_rel)));
3403 
3404  ResetExprContext(econtext);
3406  }
3407 
3408  MemoryContextSwitchTo(oldCxt);
3409  table_endscan(scan);
3410  UnregisterSnapshot(snapshot);
3412  FreeExecutorState(estate);
3413 
3414  if (RelationGetRelid(default_rel) != RelationGetRelid(part_rel))
3415  table_close(part_rel, NoLock); /* keep the lock until commit */
3416  }
3417 }
TupleTableSlot * table_slot_create(Relation relation, List **reglist)
Definition: tableam.c:91
#define DEBUG1
Definition: elog.h:25
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
Snapshot RegisterSnapshot(Snapshot snapshot)
Definition: snapmgr.c:810
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:698
static bool table_scan_getnextslot(TableScanDesc sscan, ScanDirection direction, TupleTableSlot *slot)
Definition: tableam.h:1032
Form_pg_class rd_rel
Definition: rel.h:109
unsigned int Oid
Definition: postgres_ext.h:31
ExprState * ExecPrepareExpr(Expr *node, EState *estate)
Definition: execExpr.c:746
void FreeExecutorState(EState *estate)
Definition: execUtils.c:186
#define GetPerTupleExprContext(estate)
Definition: executor.h:533
#define ERROR
Definition: elog.h:46
static TableScanDesc table_beginscan(Relation rel, Snapshot snapshot, int nkeys, struct ScanKeyData *key)
Definition: tableam.h:883
#define NoLock
Definition: lockdefs.h:34
List * get_proposed_default_constraint(List *new_part_constraints)
Definition: partition.c:368
void ExecDropSingleTupleTableSlot(TupleTableSlot *slot)
Definition: execTuples.c:1254
#define RelationGetRelationName(relation)
Definition: rel.h:511
EState * CreateExecutorState(void)
Definition: execUtils.c:90
void UnregisterSnapshot(Snapshot snapshot)
Definition: snapmgr.c:852
#define WARNING
Definition: elog.h:40
List * es_tupleTable
Definition: execnodes.h:602
Expr * make_ands_explicit(List *andclauses)
Definition: makefuncs.c:708
#define list_make1_oid(x1)
Definition: pg_list.h:236
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg_internal(const char *fmt,...)
Definition: elog.c:996
static List * get_qual_for_range(Relation parent, PartitionBoundSpec *spec, bool for_default)
Definition: partbounds.c:4288
TupleTableSlot * ecxt_scantuple
Definition: execnodes.h:226
#define PARTITION_STRATEGY_LIST
Definition: parsenodes.h:815
Snapshot GetLatestSnapshot(void)
Definition: snapmgr.c:325
#define GetPerTupleMemoryContext(estate)
Definition: executor.h:538
static void table_endscan(TableScanDesc scan)
Definition: tableam.h:991
#define AccessExclusiveLock
Definition: lockdefs.h:45
List * find_all_inheritors(Oid parentrelId, LOCKMODE lockmode, List **numparents)
Definition: pg_inherits.c:256
int errmsg(const char *fmt,...)
Definition: elog.c:909
bool ExecCheck(ExprState *state, ExprContext *econtext)
Definition: execExpr.c:853
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:120
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
Definition: pg_list.h:50
int errtable(Relation rel)
Definition: relcache.c:5636
List * map_partition_varattnos(List *expr, int fromrel_varno, Relation to_rel, Relation from_rel)
Definition: partition.c:221
#define RelationGetRelid(relation)
Definition: rel.h:477
static List * get_qual_for_list(Relation parent, PartitionBoundSpec *spec)
Definition: partbounds.c:4079
#define ResetExprContext(econtext)
Definition: executor.h:527
#define lfirst_oid(lc)
Definition: pg_list.h:171
bool PartConstraintImpliedByRelConstraint(Relation scanrel, List *partConstraint)
Definition: tablecmds.c:17017

◆ check_new_partition_bound()

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

Definition at line 2905 of file partbounds.c.

References Abs, Assert, PartitionDescData::boundinfo, Const::constisnull, Const::constvalue, DatumGetInt32, PartitionRangeBound::datums, PartitionBoundInfoData::datums, PartitionBoundInfoData::default_index, elog, equal(), ereport, errcode(), errdetail(), errmsg(), ERROR, get_range_partbound_string(), get_rel_name(), PartitionBoundInfoData::indexes, PartitionBoundSpec::is_default, sort-test::key, PartitionRangeBound::kind, PartitionBoundInfoData::kind, lfirst_node, linitial, list_nth(), PartitionBoundSpec::listdatums, Const::location, 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(), PartitionKeyData::partcollation, 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, PartitionKeyData::partnatts, PartitionKeyData::partsupfunc, RelationGetPartitionDesc(), RelationGetPartitionKey(), PartitionHashBound::remainder, PartitionBoundSpec::remainder, PartitionKeyData::strategy, PartitionBoundInfoData::strategy, PartitionBoundSpec::strategy, upper(), PartitionBoundSpec::upperdatums, and val.

Referenced by ATExecAttachPartition(), and DefineRelation().

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

◆ compare_range_partitions()

static bool compare_range_partitions ( int  partnatts,
FmgrInfo partsupfuncs,
Oid partcollations,
PartitionRangeBound outer_lb,
PartitionRangeBound outer_ub,
PartitionRangeBound inner_lb,
PartitionRangeBound inner_ub,
int *  lb_cmpval,
int *  ub_cmpval 
)
static

Definition at line 2670 of file partbounds.c.

References compare_range_bounds.

Referenced by merge_range_bounds().

2677 {
2678  /*
2679  * Check if the outer partition's upper bound is lower than the inner
2680  * partition's lower bound; if so the partitions aren't overlapping.
2681  */
2682  if (compare_range_bounds(partnatts, partsupfuncs, partcollations,
2683  outer_ub, inner_lb) < 0)
2684  {
2685  *lb_cmpval = -1;
2686  *ub_cmpval = -1;
2687  return false;
2688  }
2689 
2690  /*
2691  * Check if the outer partition's lower bound is higher than the inner
2692  * partition's upper bound; if so the partitions aren't overlapping.
2693  */
2694  if (compare_range_bounds(partnatts, partsupfuncs, partcollations,
2695  outer_lb, inner_ub) > 0)
2696  {
2697  *lb_cmpval = 1;
2698  *ub_cmpval = 1;
2699  return false;
2700  }
2701 
2702  /* All other cases indicate overlapping partitions. */
2703  *lb_cmpval = compare_range_bounds(partnatts, partsupfuncs, partcollations,
2704  outer_lb, inner_lb);
2705  *ub_cmpval = compare_range_bounds(partnatts, partsupfuncs, partcollations,
2706  outer_ub, inner_ub);
2707  return true;
2708 }
#define compare_range_bounds(partnatts, partsupfunc, partcollations, bound1, bound2)
Definition: partbounds.c:88

◆ compute_partition_hash_value()

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

Definition at line 4740 of file partbounds.c.

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

Referenced by get_matching_hash_bounds(), and get_partition_for_tuple().

4742 {
4743  int i;
4744  uint64 rowHash = 0;
4746 
4747  for (i = 0; i < partnatts; i++)
4748  {
4749  /* Nulls are just ignored */
4750  if (!isnull[i])
4751  {
4752  Datum hash;
4753 
4754  Assert(OidIsValid(partsupfunc[i].fn_oid));
4755 
4756  /*
4757  * Compute hash for each datum value by calling respective
4758  * datatype-specific hash functions of each partition key
4759  * attribute.
4760  */
4761  hash = FunctionCall2Coll(&partsupfunc[i], partcollation[i],
4762  values[i], seed);
4763 
4764  /* Form a single 64-bit hash value */
4765  rowHash = hash_combine64(rowHash, DatumGetUInt64(hash));
4766  }
4767  }
4768 
4769  return rowHash;
4770 }
#define UInt64GetDatum(X)
Definition: postgres.h:692
Datum FunctionCall2Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1148
static uint64 hash_combine64(uint64 a, uint64 b)
Definition: hashfn.h:80
#define OidIsValid(objectId)
Definition: c.h:710
uintptr_t Datum
Definition: postgres.h:411
#define DatumGetUInt64(X)
Definition: postgres.h:678
#define Assert(condition)
Definition: c.h:804
#define HASH_PARTITION_SEED
Definition: partition.h:20
static Datum values[MAXATTR]
Definition: bootstrap.c:166
int i
static unsigned hash(unsigned *uv, int n)
Definition: rege_dfa.c:719

◆ create_hash_bounds()

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

Definition at line 356 of file partbounds.c.

References Assert, PartitionBoundInfoData::datums, PartitionBoundInfoData::default_index, elog, ERROR, i, PartitionHashBound::index, PartitionBoundInfoData::indexes, Int32GetDatum, PartitionBoundInfoData::interleaved_parts, PartitionBoundInfoData::kind, PartitionHashBound::modulus, PartitionBoundSpec::modulus, PartitionBoundInfoData::ndatums, PartitionBoundInfoData::nindexes, PartitionBoundInfoData::null_index, palloc(), palloc0(), PARTITION_STRATEGY_HASH, pfree(), qsort, qsort_partition_hbound_cmp(), PartitionHashBound::remainder, PartitionBoundSpec::remainder, PartitionKeyData::strategy, PartitionBoundInfoData::strategy, and PartitionBoundSpec::strategy.

Referenced by partition_bounds_create().

358 {
359  PartitionBoundInfo boundinfo;
360  PartitionHashBound *hbounds;
361  int i;
362  int greatest_modulus;
363  Datum *boundDatums;
364 
365  boundinfo = (PartitionBoundInfoData *)
367  boundinfo->strategy = key->strategy;
368  /* No special hash partitions. */
369  boundinfo->null_index = -1;
370  boundinfo->default_index = -1;
371 
372  hbounds = (PartitionHashBound *)
373  palloc(nparts * sizeof(PartitionHashBound));
374 
375  /* Convert from node to the internal representation */
376  for (i = 0; i < nparts; i++)
377  {
378  PartitionBoundSpec *spec = boundspecs[i];
379 
380  if (spec->strategy != PARTITION_STRATEGY_HASH)
381  elog(ERROR, "invalid strategy in partition bound spec");
382 
383  hbounds[i].modulus = spec->modulus;
384  hbounds[i].remainder = spec->remainder;
385  hbounds[i].index = i;
386  }
387 
388  /* Sort all the bounds in ascending order */
389  qsort(hbounds, nparts, sizeof(PartitionHashBound),
391 
392  /* After sorting, moduli are now stored in ascending order. */
393  greatest_modulus = hbounds[nparts - 1].modulus;
394 
395  boundinfo->ndatums = nparts;
396  boundinfo->datums = (Datum **) palloc0(nparts * sizeof(Datum *));
397  boundinfo->kind = NULL;
398  boundinfo->interleaved_parts = NULL;
399  boundinfo->nindexes = greatest_modulus;
400  boundinfo->indexes = (int *) palloc(greatest_modulus * sizeof(int));
401  for (i = 0; i < greatest_modulus; i++)
402  boundinfo->indexes[i] = -1;
403 
404  /*
405  * In the loop below, to save from allocating a series of small datum
406  * arrays, here we just allocate a single array and below we'll just
407  * assign a portion of this array per partition.
408  */
409  boundDatums = (Datum *) palloc(nparts * 2 * sizeof(Datum));
410 
411  /*
412  * For hash partitioning, there are as many datums (modulus and remainder
413  * pairs) as there are partitions. Indexes are simply values ranging from
414  * 0 to (nparts - 1).
415  */
416  for (i = 0; i < nparts; i++)
417  {
418  int modulus = hbounds[i].modulus;
419  int remainder = hbounds[i].remainder;
420 
421  boundinfo->datums[i] = &boundDatums[i * 2];
422  boundinfo->datums[i][0] = Int32GetDatum(modulus);
423  boundinfo->datums[i][1] = Int32GetDatum(remainder);
424 
425  while (remainder < greatest_modulus)
426  {
427  /* overlap? */
428  Assert(boundinfo->indexes[remainder] == -1);
429  boundinfo->indexes[remainder] = i;
430  remainder += modulus;
431  }
432 
433  (*mapping)[hbounds[i].index] = i;
434  }
435  pfree(hbounds);
436 
437  return boundinfo;
438 }
PartitionRangeDatumKind ** kind
Definition: partbounds.h:82
static int32 qsort_partition_hbound_cmp(const void *a, const void *b)
Definition: partbounds.c:3791
Bitmapset * interleaved_parts
Definition: partbounds.h:85
void pfree(void *pointer)
Definition: mcxt.c:1169
#define ERROR
Definition: elog.h:46
void * palloc0(Size size)
Definition: mcxt.c:1093
uintptr_t Datum
Definition: postgres.h:411
#define PARTITION_STRATEGY_HASH
Definition: parsenodes.h:814
#define Assert(condition)
Definition: c.h:804
#define Int32GetDatum(X)
Definition: postgres.h:523
void * palloc(Size size)
Definition: mcxt.c:1062
#define elog(elevel,...)
Definition: elog.h:232
int i
#define qsort(a, b, c, d)
Definition: port.h:504

◆ create_list_bounds()

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

Definition at line 471 of file partbounds.c.

References Assert, bms_add_member(), Const::constisnull, Const::constvalue, datumCopy(), PartitionBoundInfoData::datums, PartitionBoundInfoData::default_index, elog, ERROR, get_non_null_list_datum_count(), i, PartitionHashBound::index, PartitionListValue::index, PartitionBoundInfoData::indexes, PartitionBoundInfoData::interleaved_parts, PartitionBoundSpec::is_default, PartitionBoundInfoData::kind, lfirst_node, PartitionBoundSpec::listdatums, PartitionBoundInfoData::ndatums, PartitionBoundInfoData::nindexes, PartitionBoundInfoData::null_index, palloc(), palloc0(), partition_bound_accepts_nulls, partition_bound_has_default, PARTITION_STRATEGY_LIST, pfree(), qsort_arg(), qsort_partition_list_value_cmp(), PartitionKeyData::strategy, PartitionBoundInfoData::strategy, PartitionBoundSpec::strategy, val, PartitionListValue::value, and value.

Referenced by partition_bounds_create().

473 {
474  PartitionBoundInfo boundinfo;
475  PartitionListValue *all_values;
476  int i;
477  int j;
478  int ndatums;
479  int next_index = 0;
480  int default_index = -1;
481  int null_index = -1;
482  Datum *boundDatums;
483 
484  boundinfo = (PartitionBoundInfoData *)
486  boundinfo->strategy = key->strategy;
487  /* Will be set correctly below. */
488  boundinfo->null_index = -1;
489  boundinfo->default_index = -1;
490 
491  ndatums = get_non_null_list_datum_count(boundspecs, nparts);
492  all_values = (PartitionListValue *)
493  palloc(ndatums * sizeof(PartitionListValue));
494 
495  /* Create a unified list of non-null values across all partitions. */
496  for (j = 0, i = 0; i < nparts; i++)
497  {
498  PartitionBoundSpec *spec = boundspecs[i];
499  ListCell *c;
500 
501  if (spec->strategy != PARTITION_STRATEGY_LIST)
502  elog(ERROR, "invalid strategy in partition bound spec");
503 
504  /*
505  * Note the index of the partition bound spec for the default
506  * partition. There's no datum to add to the list on non-null datums
507  * for this partition.
508  */
509  if (spec->is_default)
510  {
511  default_index = i;
512  continue;
513  }
514 
515  foreach(c, spec->listdatums)
516  {
517  Const *val = lfirst_node(Const, c);
518 
519  if (!val->constisnull)
520  {
521  all_values[j].index = i;
522  all_values[j].value = val->constvalue;
523  j++;
524  }
525  else
526  {
527  /*
528  * Never put a null into the values array; save the index of
529  * the partition that stores nulls, instead.
530  */
531  if (null_index != -1)
532  elog(ERROR, "found null more than once");
533  null_index = i;
534  }
535  }
536  }
537 
538  /* ensure we found a Datum for every slot in the all_values array */
539  Assert(j == ndatums);
540 
541  qsort_arg(all_values, ndatums, sizeof(PartitionListValue),
542  qsort_partition_list_value_cmp, (void *) key);
543 
544  boundinfo->ndatums = ndatums;
545  boundinfo->datums = (Datum **) palloc0(ndatums * sizeof(Datum *));
546  boundinfo->kind = NULL;
547  boundinfo->interleaved_parts = NULL;
548  boundinfo->nindexes = ndatums;
549  boundinfo->indexes = (int *) palloc(ndatums * sizeof(int));
550 
551  /*
552  * In the loop below, to save from allocating a series of small datum
553  * arrays, here we just allocate a single array and below we'll just
554  * assign a portion of this array per datum.
555  */
556  boundDatums = (Datum *) palloc(ndatums * sizeof(Datum));
557 
558  /*
559  * Copy values. Canonical indexes are values ranging from 0 to (nparts -
560  * 1) assigned to each partition such that all datums of a given partition
561  * receive the same value. The value for a given partition is the index of
562  * that partition's smallest datum in the all_values[] array.
563  */
564  for (i = 0; i < ndatums; i++)
565  {
566  int orig_index = all_values[i].index;
567 
568  boundinfo->datums[i] = &boundDatums[i];
569  boundinfo->datums[i][0] = datumCopy(all_values[i].value,
570  key->parttypbyval[0],
571  key->parttyplen[0]);
572 
573  /* If the old index has no mapping, assign one */
574  if ((*mapping)[orig_index] == -1)
575  (*mapping)[orig_index] = next_index++;
576 
577  boundinfo->indexes[i] = (*mapping)[orig_index];
578  }
579 
580  pfree(all_values);
581 
582  /*
583  * Set the canonical value for null_index, if any.
584  *
585  * It is possible that the null-accepting partition has not been assigned
586  * an index yet, which could happen if such partition accepts only null
587  * and hence not handled in the above loop which only looked at non-null
588  * values.
589  */
590  if (null_index != -1)
591  {
592  Assert(null_index >= 0);
593  if ((*mapping)[null_index] == -1)
594  (*mapping)[null_index] = next_index++;
595  boundinfo->null_index = (*mapping)[null_index];
596  }
597 
598  /* Set the canonical value for default_index, if any. */
599  if (default_index != -1)
600  {
601  /*
602  * The default partition accepts any value not specified in the lists
603  * of other partitions, hence it should not get mapped index while
604  * assigning those for non-null datums.
605  */
606  Assert(default_index >= 0);
607  Assert((*mapping)[default_index] == -1);
608  (*mapping)[default_index] = next_index++;
609  boundinfo->default_index = (*mapping)[default_index];
610  }
611 
612  /*
613  * Calculate interleaved partitions. Here we look for partitions which
614  * might be interleaved with other partitions and set a bit in
615  * interleaved_parts for any partitions which may be interleaved with
616  * another partition.
617  */
618 
619  /*
620  * There must be multiple partitions to have any interleaved partitions,
621  * otherwise there's nothing to interleave with.
622  */
623  if (nparts > 1)
624  {
625  /*
626  * Short-circuit check to see if only 1 Datum is allowed per
627  * partition. When this is true there's no need to do the more
628  * expensive checks to look for interleaved values.
629  */
630  if (boundinfo->ndatums +
631  partition_bound_accepts_nulls(boundinfo) +
632  partition_bound_has_default(boundinfo) != nparts)
633  {
634  int last_index = -1;
635 
636  /*
637  * Since the indexes array is sorted in Datum order, if any
638  * partitions are interleaved then it will show up by the
639  * partition indexes not being in ascending order. Here we check
640  * for that and record all partitions that are out of order.
641  */
642  for (i = 0; i < boundinfo->nindexes; i++)
643  {
644  int index = boundinfo->indexes[i];
645 
646  if (index < last_index)
647  boundinfo->interleaved_parts = bms_add_member(boundinfo->interleaved_parts,
648  index);
649 
650  /*
651  * Mark the NULL partition as interleaved if we find that it
652  * allows some other non-NULL Datum.
653  */
654  if (partition_bound_accepts_nulls(boundinfo) &&
655  index == boundinfo->null_index)
656  boundinfo->interleaved_parts = bms_add_member(boundinfo->interleaved_parts,
657  boundinfo->null_index);
658 
659  last_index = index;
660  }
661  }
662 
663  /*
664  * The DEFAULT partition is the "catch-all" partition that can contain
665  * anything that does not belong to any other partition. If there are
666  * any other partitions then the DEFAULT partition must be marked as
667  * interleaved.
668  */
669  if (partition_bound_has_default(boundinfo))
670  boundinfo->interleaved_parts = bms_add_member(boundinfo->interleaved_parts,
671  boundinfo->default_index);
672  }
673 
674 
675  /* All partitions must now have been assigned canonical indexes. */
676  Assert(next_index == nparts);
677  return boundinfo;
678 }
Datum constvalue
Definition: primnodes.h:219
PartitionRangeDatumKind ** kind
Definition: partbounds.h:82
Bitmapset * interleaved_parts
Definition: partbounds.h:85
Definition: type.h:89
void pfree(void *pointer)
Definition: mcxt.c:1169
#define ERROR
Definition: elog.h:46
static int get_non_null_list_datum_count(PartitionBoundSpec **boundspecs, int nparts)
Definition: partbounds.c:445
#define lfirst_node(type, lc)
Definition: pg_list.h:172
char * c
#define partition_bound_has_default(bi)
Definition: partbounds.h:97
Datum datumCopy(Datum value, bool typByVal, int typLen)
Definition: datum.c:131
void qsort_arg(void *base, size_t nel, size_t elsize, qsort_arg_comparator cmp, void *arg)
void * palloc0(Size size)
Definition: mcxt.c:1093
uintptr_t Datum
Definition: postgres.h:411
#define partition_bound_accepts_nulls(bi)
Definition: partbounds.h:96
static struct @143 value
bool * parttypbyval
Definition: partcache.h:44
#define Assert(condition)
Definition: c.h:804
int16 * parttyplen
Definition: partcache.h:43
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736
#define PARTITION_STRATEGY_LIST
Definition: parsenodes.h:815
void * palloc(Size size)
Definition: mcxt.c:1062
#define elog(elevel,...)
Definition: elog.h:232
int i
static int32 qsort_partition_list_value_cmp(const void *a, const void *b, void *arg)
Definition: partbounds.c:3806
long val
Definition: informix.c:664
bool constisnull
Definition: primnodes.h:220

◆ create_range_bounds()

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

Definition at line 685 of file partbounds.c.

References Assert, cur, datumCopy(), DatumGetInt32, PartitionRangeBound::datums, PartitionBoundInfoData::datums, PartitionBoundInfoData::default_index, elog, ERROR, FunctionCall2Coll(), i, PartitionRangeBound::index, PartitionBoundInfoData::indexes, PartitionBoundInfoData::interleaved_parts, PartitionBoundSpec::is_default, PartitionRangeBound::kind, PartitionBoundInfoData::kind, lower(), PartitionBoundSpec::lowerdatums, make_one_partition_rbound(), PartitionBoundInfoData::ndatums, PartitionBoundInfoData::nindexes, PartitionBoundInfoData::null_index, palloc(), palloc0(), PARTITION_RANGE_DATUM_VALUE, PARTITION_STRATEGY_RANGE, pfree(), qsort_arg(), qsort_partition_rbound_cmp(), PartitionKeyData::strategy, PartitionBoundInfoData::strategy, PartitionBoundSpec::strategy, upper(), and PartitionBoundSpec::upperdatums.

Referenced by partition_bounds_create().

687 {
688  PartitionBoundInfo boundinfo;
689  PartitionRangeBound **rbounds = NULL;
690  PartitionRangeBound **all_bounds,
691  *prev;
692  int i,
693  k,
694  partnatts;
695  int ndatums = 0;
696  int default_index = -1;
697  int next_index = 0;
698  Datum *boundDatums;
699  PartitionRangeDatumKind *boundKinds;
700 
701  boundinfo = (PartitionBoundInfoData *)
703  boundinfo->strategy = key->strategy;
704  /* There is no special null-accepting range partition. */
705  boundinfo->null_index = -1;
706  /* Will be set correctly below. */
707  boundinfo->default_index = -1;
708 
709  all_bounds = (PartitionRangeBound **)
710  palloc0(2 * nparts * sizeof(PartitionRangeBound *));
711 
712  /* Create a unified list of range bounds across all the partitions. */
713  ndatums = 0;
714  for (i = 0; i < nparts; i++)
715  {
716  PartitionBoundSpec *spec = boundspecs[i];
718  *upper;
719 
720  if (spec->strategy != PARTITION_STRATEGY_RANGE)
721  elog(ERROR, "invalid strategy in partition bound spec");
722 
723  /*
724  * Note the index of the partition bound spec for the default
725  * partition. There's no datum to add to the all_bounds array for
726  * this partition.
727  */
728  if (spec->is_default)
729  {
730  default_index = i;
731  continue;
732  }
733 
734  lower = make_one_partition_rbound(key, i, spec->lowerdatums, true);
735  upper = make_one_partition_rbound(key, i, spec->upperdatums, false);
736  all_bounds[ndatums++] = lower;
737  all_bounds[ndatums++] = upper;
738  }
739 
740  Assert(ndatums == nparts * 2 ||
741  (default_index != -1 && ndatums == (nparts - 1) * 2));
742 
743  /* Sort all the bounds in ascending order */
744  qsort_arg(all_bounds, ndatums,
745  sizeof(PartitionRangeBound *),
747  (void *) key);
748 
749  /* Save distinct bounds from all_bounds into rbounds. */
750  rbounds = (PartitionRangeBound **)
751  palloc(ndatums * sizeof(PartitionRangeBound *));
752  k = 0;
753  prev = NULL;
754  for (i = 0; i < ndatums; i++)
755  {
756  PartitionRangeBound *cur = all_bounds[i];
757  bool is_distinct = false;
758  int j;
759 
760  /* Is the current bound distinct from the previous one? */
761  for (j = 0; j < key->partnatts; j++)
762  {
763  Datum cmpval;
764 
765  if (prev == NULL || cur->kind[j] != prev->kind[j])
766  {
767  is_distinct = true;
768  break;
769  }
770 
771  /*
772  * If the bounds are both MINVALUE or MAXVALUE, stop now and treat
773  * them as equal, since any values after this point must be
774  * ignored.
775  */
776  if (cur->kind[j] != PARTITION_RANGE_DATUM_VALUE)
777  break;
778 
779  cmpval = FunctionCall2Coll(&key->partsupfunc[j],
780  key->partcollation[j],
781  cur->datums[j],
782  prev->datums[j]);
783  if (DatumGetInt32(cmpval) != 0)
784  {
785  is_distinct = true;
786  break;
787  }
788  }
789 
790  /*
791  * Only if the bound is distinct save it into a temporary array, i.e,
792  * rbounds which is later copied into boundinfo datums array.
793  */
794  if (is_distinct)
795  rbounds[k++] = all_bounds[i];
796 
797  prev = cur;
798  }
799 
800  pfree(all_bounds);
801 
802  /* Update ndatums to hold the count of distinct datums. */
803  ndatums = k;
804 
805  /*
806  * Add datums to boundinfo. Canonical indexes are values ranging from 0
807  * to nparts - 1, assigned in that order to each partition's upper bound.
808  * For 'datums' elements that are lower bounds, there is -1 in the
809  * 'indexes' array to signify that no partition exists for the values less
810  * than such a bound and greater than or equal to the previous upper
811  * bound.
812  */
813  boundinfo->ndatums = ndatums;
814  boundinfo->datums = (Datum **) palloc0(ndatums * sizeof(Datum *));
815  boundinfo->kind = (PartitionRangeDatumKind **)
816  palloc(ndatums *
817  sizeof(PartitionRangeDatumKind *));
818  boundinfo->interleaved_parts = NULL;
819 
820  /*
821  * For range partitioning, an additional value of -1 is stored as the last
822  * element of the indexes[] array.
823  */
824  boundinfo->nindexes = ndatums + 1;
825  boundinfo->indexes = (int *) palloc((ndatums + 1) * sizeof(int));
826 
827  /*
828  * In the loop below, to save from allocating a series of small arrays,
829  * here we just allocate a single array for Datums and another for
830  * PartitionRangeDatumKinds, below we'll just assign a portion of these
831  * arrays in each loop.
832  */
833  partnatts = key->partnatts;
834  boundDatums = (Datum *) palloc(ndatums * partnatts * sizeof(Datum));
835  boundKinds = (PartitionRangeDatumKind *) palloc(ndatums * partnatts *
836  sizeof(PartitionRangeDatumKind));
837 
838  for (i = 0; i < ndatums; i++)
839  {
840  int j;
841 
842  boundinfo->datums[i] = &boundDatums[i * partnatts];
843  boundinfo->kind[i] = &boundKinds[i * partnatts];
844  for (j = 0; j < partnatts; j++)
845  {
846  if (rbounds[i]->kind[j] == PARTITION_RANGE_DATUM_VALUE)
847  boundinfo->datums[i][j] =
848  datumCopy(rbounds[i]->datums[j],
849  key->parttypbyval[j],
850  key->parttyplen[j]);
851  boundinfo->kind[i][j] = rbounds[i]->kind[j];
852  }
853 
854  /*
855  * There is no mapping for invalid indexes.
856  *
857  * Any lower bounds in the rbounds array have invalid indexes
858  * assigned, because the values between the previous bound (if there
859  * is one) and this (lower) bound are not part of the range of any
860  * existing partition.
861  */
862  if (rbounds[i]->lower)
863  boundinfo->indexes[i] = -1;
864  else
865  {
866  int orig_index = rbounds[i]->index;
867 
868  /* If the old index has no mapping, assign one */
869  if ((*mapping)[orig_index] == -1)
870  (*mapping)[orig_index] = next_index++;
871 
872  boundinfo->indexes[i] = (*mapping)[orig_index];
873  }
874  }
875 
876  pfree(rbounds);
877 
878  /* Set the canonical value for default_index, if any. */
879  if (default_index != -1)
880  {
881  Assert(default_index >= 0 && (*mapping)[default_index] == -1);
882  (*mapping)[default_index] = next_index++;
883  boundinfo->default_index = (*mapping)[default_index];
884  }
885 
886  /* The extra -1 element. */
887  Assert(i == ndatums);
888  boundinfo->indexes[i] = -1;
889 
890  /* All partitions must now have been assigned canonical indexes. */
891  Assert(next_index == nparts);
892  return boundinfo;
893 }
PartitionRangeDatumKind ** kind
Definition: partbounds.h:82
PartitionRangeDatumKind * kind
Definition: partbounds.c:68
#define DatumGetInt32(X)
Definition: postgres.h:516
Datum lower(PG_FUNCTION_ARGS)
Definition: oracle_compat.c:46
FmgrInfo * partsupfunc
Definition: partcache.h:35
PartitionRangeDatumKind
Definition: parsenodes.h:850
Bitmapset * interleaved_parts
Definition: partbounds.h:85
struct cursor * cur
Definition: ecpg.c:28
static int32 qsort_partition_rbound_cmp(const void *a, const void *b, void *arg)
Definition: partbounds.c:3823
Datum upper(PG_FUNCTION_ARGS)
Definition: oracle_compat.c:77
Datum FunctionCall2Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1148
static PartitionRangeBound * make_one_partition_rbound(PartitionKey key, int index, List *datums, bool lower)
Definition: partbounds.c:3441
void pfree(void *pointer)
Definition: mcxt.c:1169
#define ERROR
Definition: elog.h:46
Datum datumCopy(Datum value, bool typByVal, int typLen)
Definition: datum.c:131
void qsort_arg(void *base, size_t nel, size_t elsize, qsort_arg_comparator cmp, void *arg)
Oid * partcollation
Definition: partcache.h:38
void * palloc0(Size size)
Definition: mcxt.c:1093
uintptr_t Datum
Definition: postgres.h:411
bool * parttypbyval
Definition: partcache.h:44
#define Assert(condition)
Definition: c.h:804
int16 * parttyplen
Definition: partcache.h:43
#define PARTITION_STRATEGY_RANGE
Definition: parsenodes.h:816
void * palloc(Size size)
Definition: mcxt.c:1062
#define elog(elevel,...)
Definition: elog.h:232
int i

◆ fix_merged_indexes()

static void fix_merged_indexes ( PartitionMap outer_map,
PartitionMap inner_map,
int  nmerged,
List merged_indexes 
)
static

Definition at line 2396 of file partbounds.c.

References Assert, PartitionMap::did_remapping, i, lfirst_int, PartitionMap::merged_indexes, PartitionMap::nparts, PartitionMap::old_indexes, palloc(), and pfree().

Referenced by merge_list_bounds().

2398 {
2399  int *new_indexes;
2400  int merged_index;
2401  int i;
2402  ListCell *lc;
2403 
2404  Assert(nmerged > 0);
2405 
2406  new_indexes = (int *) palloc(sizeof(int) * nmerged);
2407  for (i = 0; i < nmerged; i++)
2408  new_indexes[i] = -1;
2409 
2410  /* Build the mapping of old merged indexes to new merged indexes. */
2411  if (outer_map->did_remapping)
2412  {
2413  for (i = 0; i < outer_map->nparts; i++)
2414  {
2415  merged_index = outer_map->old_indexes[i];
2416  if (merged_index >= 0)
2417  new_indexes[merged_index] = outer_map->merged_indexes[i];
2418  }
2419  }
2420  if (inner_map->did_remapping)
2421  {
2422  for (i = 0; i < inner_map->nparts; i++)
2423  {
2424  merged_index = inner_map->old_indexes[i];
2425  if (merged_index >= 0)
2426  new_indexes[merged_index] = inner_map->merged_indexes[i];
2427  }
2428  }
2429 
2430  /* Fix the merged_indexes list using the mapping. */
2431  foreach(lc, merged_indexes)
2432  {
2433  merged_index = lfirst_int(lc);
2434  Assert(merged_index >= 0);
2435  if (new_indexes[merged_index] >= 0)
2436  lfirst_int(lc) = new_indexes[merged_index];
2437  }
2438 
2439  pfree(new_indexes);
2440 }
bool did_remapping
Definition: partbounds.c:82
void pfree(void *pointer)
Definition: mcxt.c:1169
#define lfirst_int(lc)
Definition: pg_list.h:170
#define Assert(condition)
Definition: c.h:804
void * palloc(Size size)
Definition: mcxt.c:1062
int * old_indexes
Definition: partbounds.c:83
int i
int * merged_indexes
Definition: partbounds.c:79

◆ free_partition_map()

static void free_partition_map ( PartitionMap map)
static

Definition at line 1843 of file partbounds.c.

References PartitionMap::merged, PartitionMap::merged_indexes, PartitionMap::old_indexes, and pfree().

Referenced by merge_list_bounds(), and merge_range_bounds().

1844 {
1845  pfree(map->merged_indexes);
1846  pfree(map->merged);
1847  pfree(map->old_indexes);
1848 }
void pfree(void *pointer)
Definition: mcxt.c:1169
bool * merged
Definition: partbounds.c:80
int * old_indexes
Definition: partbounds.c:83
int * merged_indexes
Definition: partbounds.c:79

◆ generate_matching_part_pairs()

static void generate_matching_part_pairs ( RelOptInfo outer_rel,
RelOptInfo inner_rel,
PartitionMap outer_map,
PartitionMap inner_map,
int  nmerged,
List **  outer_parts,
List **  inner_parts 
)
static

Definition at line 2450 of file partbounds.c.

References Assert, i, lappend(), Max, PartitionMap::merged_indexes, NIL, PartitionMap::nparts, RelOptInfo::nparts, palloc(), RelOptInfo::part_rels, and pfree().

Referenced by merge_list_bounds(), and merge_range_bounds().

2454 {
2455  int outer_nparts = outer_map->nparts;
2456  int inner_nparts = inner_map->nparts;
2457  int *outer_indexes;
2458  int *inner_indexes;
2459  int max_nparts;
2460  int i;
2461 
2462  Assert(nmerged > 0);
2463  Assert(*outer_parts == NIL);
2464  Assert(*inner_parts == NIL);
2465 
2466  outer_indexes = (int *) palloc(sizeof(int) * nmerged);
2467  inner_indexes = (int *) palloc(sizeof(int) * nmerged);
2468  for (i = 0; i < nmerged; i++)
2469  outer_indexes[i] = inner_indexes[i] = -1;
2470 
2471  /* Set pairs of matching partitions. */
2472  Assert(outer_nparts == outer_rel->nparts);
2473  Assert(inner_nparts == inner_rel->nparts);
2474  max_nparts = Max(outer_nparts, inner_nparts);
2475  for (i = 0; i < max_nparts; i++)
2476  {
2477  if (i < outer_nparts)
2478  {
2479  int merged_index = outer_map->merged_indexes[i];
2480 
2481  if (merged_index >= 0)
2482  {
2483  Assert(merged_index < nmerged);
2484  outer_indexes[merged_index] = i;
2485  }
2486  }
2487  if (i < inner_nparts)
2488  {
2489  int merged_index = inner_map->merged_indexes[i];
2490 
2491  if (merged_index >= 0)
2492  {
2493  Assert(merged_index < nmerged);
2494  inner_indexes[merged_index] = i;
2495  }
2496  }
2497  }
2498 
2499  /* Build the list pairs. */
2500  for (i = 0; i < nmerged; i++)
2501  {
2502  int outer_index = outer_indexes[i];
2503  int inner_index = inner_indexes[i];
2504 
2505  /*
2506  * If both partitions are dummy, it means the merged partition that
2507  * had been assigned to the outer/inner partition was removed when
2508  * re-merging the outer/inner partition in
2509  * merge_matching_partitions(); ignore the merged partition.
2510  */
2511  if (outer_index == -1 && inner_index == -1)
2512  continue;
2513 
2514  *outer_parts = lappend(*outer_parts, outer_index >= 0 ?
2515  outer_rel->part_rels[outer_index] : NULL);
2516  *inner_parts = lappend(*inner_parts, inner_index >= 0 ?
2517  inner_rel->part_rels[inner_index] : NULL);
2518  }
2519 
2520  pfree(outer_indexes);
2521  pfree(inner_indexes);
2522 }
#define NIL
Definition: pg_list.h:65
void pfree(void *pointer)
Definition: mcxt.c:1169
int nparts
Definition: pathnodes.h:756
List * lappend(List *list, void *datum)
Definition: list.c:336
#define Max(x, y)
Definition: c.h:980
#define Assert(condition)
Definition: c.h:804
struct RelOptInfo ** part_rels
Definition: pathnodes.h:763
void * palloc(Size size)
Definition: mcxt.c:1062
int i
int * merged_indexes
Definition: partbounds.c:79

◆ get_hash_partition_greatest_modulus()

int get_hash_partition_greatest_modulus ( PartitionBoundInfo  bound)

Definition at line 3427 of file partbounds.c.

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

3428 {
3429  Assert(bound && bound->strategy == PARTITION_STRATEGY_HASH);
3430  return bound->nindexes;
3431 }
#define PARTITION_STRATEGY_HASH
Definition: parsenodes.h:814
#define Assert(condition)
Definition: c.h:804

◆ get_merged_range_bounds()

static void get_merged_range_bounds ( int  partnatts,
FmgrInfo partsupfuncs,
Oid partcollations,
JoinType  jointype,
PartitionRangeBound outer_lb,
PartitionRangeBound outer_ub,
PartitionRangeBound inner_lb,
PartitionRangeBound inner_ub,
int  lb_cmpval,
int  ub_cmpval,
PartitionRangeBound merged_lb,
PartitionRangeBound merged_ub 
)
static

Definition at line 2719 of file partbounds.c.

References Assert, compare_range_bounds, elog, ERROR, JOIN_ANTI, JOIN_FULL, JOIN_INNER, JOIN_LEFT, and JOIN_SEMI.

Referenced by merge_range_bounds().

2728 {
2729  Assert(compare_range_bounds(partnatts, partsupfuncs, partcollations,
2730  outer_lb, inner_lb) == lb_cmpval);
2731  Assert(compare_range_bounds(partnatts, partsupfuncs, partcollations,
2732  outer_ub, inner_ub) == ub_cmpval);
2733 
2734  switch (jointype)
2735  {
2736  case JOIN_INNER:
2737  case JOIN_SEMI:
2738 
2739  /*
2740  * An INNER/SEMI join will have the rows that fit both sides, so
2741  * the lower bound of the merged partition will be the higher of
2742  * the two lower bounds, and the upper bound of the merged
2743  * partition will be the lower of the two upper bounds.
2744  */
2745  *merged_lb = (lb_cmpval > 0) ? *outer_lb : *inner_lb;
2746  *merged_ub = (ub_cmpval < 0) ? *outer_ub : *inner_ub;
2747  break;
2748 
2749  case JOIN_LEFT:
2750  case JOIN_ANTI:
2751 
2752  /*
2753  * A LEFT/ANTI join will have all the rows from the outer side, so
2754  * the bounds of the merged partition will be the same as the
2755  * outer bounds.
2756  */
2757  *merged_lb = *outer_lb;
2758  *merged_ub = *outer_ub;
2759  break;
2760 
2761  case JOIN_FULL:
2762 
2763  /*
2764  * A FULL join will have all the rows from both sides, so the
2765  * lower bound of the merged partition will be the lower of the
2766  * two lower bounds, and the upper bound of the merged partition
2767  * will be the higher of the two upper bounds.
2768  */
2769  *merged_lb = (lb_cmpval < 0) ? *outer_lb : *inner_lb;
2770  *merged_ub = (ub_cmpval > 0) ? *outer_ub : *inner_ub;
2771  break;
2772 
2773  default:
2774  elog(ERROR, "unrecognized join type: %d", (int) jointype);
2775  }
2776 }
#define ERROR
Definition: elog.h:46
#define Assert(condition)
Definition: c.h:804
#define elog(elevel,...)
Definition: elog.h:232
#define compare_range_bounds(partnatts, partsupfunc, partcollations, bound1, bound2)
Definition: partbounds.c:88

◆ get_non_null_list_datum_count()

static int get_non_null_list_datum_count ( PartitionBoundSpec **  boundspecs,
int  nparts 
)
static

Definition at line 445 of file partbounds.c.

References Const::constisnull, i, lfirst_node, and val.

Referenced by create_list_bounds().

446 {
447  int i;
448  int count = 0;
449 
450  for (i = 0; i < nparts; i++)
451  {
452  ListCell *lc;
453 
454  foreach(lc, boundspecs[i]->listdatums)
455  {
456  Const *val = lfirst_node(Const, lc);
457 
458  if (!val->constisnull)
459  count++;
460  }
461  }
462 
463  return count;
464 }
#define lfirst_node(type, lc)
Definition: pg_list.h:172
int i
long val
Definition: informix.c:664
bool constisnull
Definition: primnodes.h:220

◆ get_partition_operator()

static Oid get_partition_operator ( PartitionKey  key,
int  col,
StrategyNumber  strategy,
bool need_relabel 
)
static

Definition at line 3845 of file partbounds.c.

References elog, ERROR, get_opfamily_member(), OidIsValid, PartitionKeyData::partopcintype, PartitionKeyData::partopfamily, and PartitionKeyData::parttypid.

Referenced by make_partition_op_expr().

3847 {
3848  Oid operoid;
3849 
3850  /*
3851  * Get the operator in the partitioning opfamily using the opclass'
3852  * declared input type as both left- and righttype.
3853  */
3854  operoid = get_opfamily_member(key->partopfamily[col],
3855  key->partopcintype[col],
3856  key->partopcintype[col],
3857  strategy);
3858  if (!OidIsValid(operoid))
3859  elog(ERROR, "missing operator %d(%u,%u) in partition opfamily %u",
3860  strategy, key->partopcintype[col], key->partopcintype[col],
3861  key->partopfamily[col]);
3862 
3863  /*
3864  * If the partition key column is not of the same type as the operator
3865  * class and not polymorphic, tell caller to wrap the non-Const expression
3866  * in a RelabelType. This matches what parse_coerce.c does.
3867  */
3868  *need_relabel = (key->parttypid[col] != key->partopcintype[col] &&
3869  key->partopcintype[col] != RECORDOID &&
3870  !IsPolymorphicType(key->partopcintype[col]));
3871 
3872  return operoid;
3873 }
Oid * partopfamily
Definition: partcache.h:33
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:710
#define ERROR
Definition: elog.h:46
Oid get_opfamily_member(Oid opfamily, Oid lefttype, Oid righttype, int16 strategy)
Definition: lsyscache.c:164
Oid * partopcintype
Definition: partcache.h:34
#define elog(elevel,...)
Definition: elog.h:232

◆ get_qual_for_hash()

static List * get_qual_for_hash ( Relation  parent,
PartitionBoundSpec spec 
)
static

Definition at line 3996 of file partbounds.c.

References generate_unaccent_rules::args, COERCE_EXPLICIT_CALL, copyObject, i, Int32GetDatum, InvalidOid, sort-test::key, lappend(), lfirst, list_head(), list_make1, list_make3, lnext(), makeConst(), makeFuncExpr(), makeVar(), PartitionBoundSpec::modulus, ObjectIdGetDatum, PartitionKeyData::partattrs, PartitionKeyData::partexprs, PartitionKeyData::partnatts, PartitionKeyData::parttypcoll, PartitionKeyData::parttypid, PartitionKeyData::parttypmod, RelationGetPartitionKey(), RelationGetRelid, and PartitionBoundSpec::remainder.

Referenced by get_qual_from_partbound().

3997 {
3999  FuncExpr *fexpr;
4000  Node *relidConst;
4001  Node *modulusConst;
4002  Node *remainderConst;
4003  List *args;
4004  ListCell *partexprs_item;
4005  int i;
4006 
4007  /* Fixed arguments. */
4008  relidConst = (Node *) makeConst(OIDOID,
4009  -1,
4010  InvalidOid,
4011  sizeof(Oid),
4013  false,
4014  true);
4015 
4016  modulusConst = (Node *) makeConst(INT4OID,
4017  -1,
4018  InvalidOid,
4019  sizeof(int32),
4020  Int32GetDatum(spec->modulus),
4021  false,
4022  true);
4023 
4024  remainderConst = (Node *) makeConst(INT4OID,
4025  -1,
4026  InvalidOid,
4027  sizeof(int32),
4028  Int32GetDatum(spec->remainder),
4029  false,
4030  true);
4031 
4032  args = list_make3(relidConst, modulusConst, remainderConst);
4033  partexprs_item = list_head(key->partexprs);
4034 
4035  /* Add an argument for each key column. */
4036  for (i = 0; i < key->partnatts; i++)
4037  {
4038  Node *keyCol;
4039 
4040  /* Left operand */
4041  if (key->partattrs[i] != 0)
4042  {
4043  keyCol = (Node *) makeVar(1,
4044  key->partattrs[i],
4045  key->parttypid[i],
4046  key->parttypmod[i],
4047  key->parttypcoll[i],
4048  0);
4049  }
4050  else
4051  {
4052  keyCol = (Node *) copyObject(lfirst(partexprs_item));
4053  partexprs_item = lnext(key->partexprs, partexprs_item);
4054  }
4055 
4056  args = lappend(args, keyCol);
4057  }
4058 
4059  fexpr = makeFuncExpr(F_SATISFIES_HASH_PARTITION,
4060  BOOLOID,
4061  args,
4062  InvalidOid,
4063  InvalidOid,
4065 
4066  return list_make1(fexpr);
4067 }
#define list_make3(x1, x2, x3)
Definition: pg_list.h:210
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:322
Definition: nodes.h:539
List * partexprs
Definition: partcache.h:30
PartitionKey RelationGetPartitionKey(Relation rel)
Definition: partcache.c:54
unsigned int Oid
Definition: postgres_ext.h:31
Const * makeConst(Oid consttype, int32 consttypmod, Oid constcollid, int constlen, Datum constvalue, bool constisnull, bool constbyval)
Definition: makefuncs.c:299
signed int int32
Definition: c.h:429
#define list_make1(x1)
Definition: pg_list.h:206
Oid * parttypcoll
Definition: partcache.h:46
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
static ListCell * list_head(const List *l)
Definition: pg_list.h:125
Var * makeVar(Index varno, AttrNumber varattno, Oid vartype, int32 vartypmod, Oid varcollid, Index varlevelsup)
Definition: makefuncs.c:66
List * lappend(List *list, void *datum)
Definition: list.c:336
AttrNumber * partattrs
Definition: partcache.h:28
int32 * parttypmod
Definition: partcache.h:42
#define InvalidOid
Definition: postgres_ext.h:36
#define lfirst(lc)
Definition: pg_list.h:169
#define Int32GetDatum(X)
Definition: postgres.h:523
int i
#define copyObject(obj)
Definition: nodes.h:655
Definition: pg_list.h:50
#define RelationGetRelid(relation)
Definition: rel.h:477
FuncExpr * makeFuncExpr(Oid funcid, Oid rettype, List *args, Oid funccollid, Oid inputcollid, CoercionForm fformat)
Definition: makefuncs.c:519

◆ get_qual_for_list()

static List * get_qual_for_list ( Relation  parent,
PartitionBoundSpec spec 
)
static

Definition at line 4079 of file partbounds.c.

References NullTest::arg, NullTest::argisrow, Assert, PartitionDescData::boundinfo, BTEqualStrategyNumber, Const::constisnull, copyObject, datumCopy(), PartitionBoundInfoData::datums, i, PartitionBoundSpec::is_default, IS_NOT_NULL, IS_NULL, sort-test::key, lappend(), lfirst_node, linitial, list_make1, list_make2, PartitionBoundSpec::listdatums, NullTest::location, make_ands_explicit(), make_partition_op_expr(), makeBoolExpr(), makeConst(), makeNode, makeVar(), PartitionBoundInfoData::ndatums, NIL, NOT_EXPR, NullTest::nulltesttype, OR_EXPR, PartitionKeyData::partattrs, PartitionKeyData::partexprs, partition_bound_accepts_nulls, PartitionKeyData::partnatts, PartitionKeyData::parttypbyval, PartitionKeyData::parttypcoll, PartitionKeyData::parttypid, PartitionKeyData::parttyplen, PartitionKeyData::parttypmod, RelationGetPartitionDesc(), RelationGetPartitionKey(), and val.

Referenced by check_default_partition_contents(), and get_qual_from_partbound().

4080 {
4082  List *result;
4083  Expr *keyCol;
4084  Expr *opexpr;
4085  NullTest *nulltest;
4086  ListCell *cell;
4087  List *elems = NIL;
4088  bool list_has_null = false;
4089 
4090  /*
4091  * Only single-column list partitioning is supported, so we are worried
4092  * only about the partition key with index 0.
4093  */
4094  Assert(key->partnatts == 1);
4095 
4096  /* Construct Var or expression representing the partition column */
4097  if (key->partattrs[0] != 0)
4098  keyCol = (Expr *) makeVar(1,
4099  key->partattrs[0],
4100  key->parttypid[0],
4101  key->parttypmod[0],
4102  key->parttypcoll[0],
4103  0);
4104  else
4105  keyCol = (Expr *) copyObject(linitial(key->partexprs));
4106 
4107  /*
4108  * For default list partition, collect datums for all the partitions. The
4109  * default partition constraint should check that the partition key is
4110  * equal to none of those.
4111  */
4112  if (spec->is_default)
4113  {
4114  int i;
4115  int ndatums = 0;
4116  PartitionDesc pdesc = RelationGetPartitionDesc(parent, false);
4117  PartitionBoundInfo boundinfo = pdesc->boundinfo;
4118 
4119  if (boundinfo)
4120  {
4121  ndatums = boundinfo->ndatums;
4122 
4123  if (partition_bound_accepts_nulls(boundinfo))
4124  list_has_null = true;
4125  }
4126 
4127  /*
4128  * If default is the only partition, there need not be any partition
4129  * constraint on it.
4130  */
4131  if (ndatums == 0 && !list_has_null)
4132  return NIL;
4133 
4134  for (i = 0; i < ndatums; i++)
4135  {
4136  Const *val;
4137 
4138  /*
4139  * Construct Const from known-not-null datum. We must be careful
4140  * to copy the value, because our result has to be able to outlive
4141  * the relcache entry we're copying from.
4142  */
4143  val = makeConst(key->parttypid[0],
4144  key->parttypmod[0],
4145  key->parttypcoll[0],
4146  key->parttyplen[0],
4147  datumCopy(*boundinfo->datums[i],
4148  key->parttypbyval[0],
4149  key->parttyplen[0]),
4150  false, /* isnull */
4151  key->parttypbyval[0]);
4152 
4153  elems = lappend(elems, val);
4154  }
4155  }
4156  else
4157  {
4158  /*
4159  * Create list of Consts for the allowed values, excluding any nulls.
4160  */
4161  foreach(cell, spec->listdatums)
4162  {
4163  Const *val = lfirst_node(Const, cell);
4164 
4165  if (val->constisnull)
4166  list_has_null = true;
4167  else
4168  elems = lappend(elems, copyObject(val));
4169  }
4170  }
4171 
4172  if (elems)
4173  {
4174  /*
4175  * Generate the operator expression from the non-null partition
4176  * values.
4177  */
4179  keyCol, (Expr *) elems);
4180  }
4181  else
4182  {
4183  /*
4184  * If there are no partition values, we don't need an operator
4185  * expression.
4186  */
4187  opexpr = NULL;
4188  }
4189 
4190  if (!list_has_null)
4191  {
4192  /*
4193  * Gin up a "col IS NOT NULL" test that will be ANDed with the main
4194  * expression. This might seem redundant, but the partition routing
4195  * machinery needs it.
4196  */
4197  nulltest = makeNode(NullTest);
4198  nulltest->arg = keyCol;
4199  nulltest->nulltesttype = IS_NOT_NULL;
4200  nulltest->argisrow = false;
4201  nulltest->location = -1;
4202 
4203  result = opexpr ? list_make2(nulltest, opexpr) : list_make1(nulltest);
4204  }
4205  else
4206  {
4207  /*
4208  * Gin up a "col IS NULL" test that will be OR'd with the main
4209  * expression.
4210  */
4211  nulltest = makeNode(NullTest);
4212  nulltest->arg = keyCol;
4213  nulltest->nulltesttype = IS_NULL;
4214  nulltest->argisrow = false;
4215  nulltest->location = -1;
4216 
4217  if (opexpr)
4218  {
4219  Expr *or;
4220 
4221  or = makeBoolExpr(OR_EXPR, list_make2(nulltest, opexpr), -1);
4222  result = list_make1(or);
4223  }
4224  else
4225  result = list_make1(nulltest);
4226  }
4227 
4228  /*
4229  * Note that, in general, applying NOT to a constraint expression doesn't
4230  * necessarily invert the set of rows it accepts, because NOT (NULL) is
4231  * NULL. However, the partition constraints we construct here never
4232  * evaluate to NULL, so applying NOT works as intended.
4233  */
4234  if (spec->is_default)
4235  {
4236  result = list_make1(make_ands_explicit(result));
4237  result = list_make1(makeBoolExpr(NOT_EXPR, result, -1));
4238  }
4239 
4240  return result;
4241 }
#define list_make2(x1, x2)
Definition: pg_list.h:208
#define NIL
Definition: pg_list.h:65
List * partexprs
Definition: partcache.h:30
PartitionKey RelationGetPartitionKey(Relation rel)
Definition: partcache.c:54
Const * makeConst(Oid consttype, int32 consttypmod, Oid constcollid, int constlen, Datum constvalue, bool constisnull, bool constbyval)
Definition: makefuncs.c:299
Expr * makeBoolExpr(BoolExprType boolop, List *args, int location)
Definition: makefuncs.c:369
PartitionBoundInfo boundinfo
Definition: partdesc.h:38
#define list_make1(x1)
Definition: pg_list.h:206
static Expr * make_partition_op_expr(PartitionKey key, int keynum, uint16 strategy, Expr *arg1, Expr *arg2)
Definition: partbounds.c:3881
Oid * parttypcoll
Definition: partcache.h:46
#define linitial(l)
Definition: pg_list.h:174
Expr * arg
Definition: primnodes.h:1265
#define lfirst_node(type, lc)
Definition: pg_list.h:172
PartitionDesc RelationGetPartitionDesc(Relation rel, bool omit_detached)
Definition: partdesc.c:72
Var * makeVar(Index varno, AttrNumber varattno, Oid vartype, int32 vartypmod, Oid varcollid, Index varlevelsup)
Definition: makefuncs.c:66
Datum datumCopy(Datum value, bool typByVal, int typLen)
Definition: datum.c:131
List * lappend(List *list, void *datum)
Definition: list.c:336
AttrNumber * partattrs
Definition: partcache.h:28
Expr * make_ands_explicit(List *andclauses)
Definition: makefuncs.c:708
NullTestType nulltesttype
Definition: primnodes.h:1266
#define partition_bound_accepts_nulls(bi)
Definition: partbounds.h:96
int32 * parttypmod
Definition: partcache.h:42
#define makeNode(_type_)
Definition: nodes.h:587
bool * parttypbyval
Definition: partcache.h:44
#define Assert(condition)
Definition: c.h:804
int16 * parttyplen
Definition: partcache.h:43
int location
Definition: primnodes.h:1268
int i
bool argisrow
Definition: primnodes.h:1267
#define copyObject(obj)
Definition: nodes.h:655
Definition: pg_list.h:50
long val
Definition: informix.c:664
bool constisnull
Definition: primnodes.h:220
#define BTEqualStrategyNumber
Definition: stratnum.h:31

◆ get_qual_for_range()

static List * get_qual_for_range ( Relation  parent,
PartitionBoundSpec spec,
bool  for_default 
)
static

Definition at line 4288 of file partbounds.c.

References AND_EXPR, BTEqualStrategyNumber, BTGreaterEqualStrategyNumber, BTGreaterStrategyNumber, BTLessEqualStrategyNumber, BTLessStrategyNumber, castNode, CreateExecutorState(), DatumGetBool, elog, ERROR, EState::es_query_cxt, ExecEvalExprSwitchContext(), ExecInitExpr(), fix_opfuncids(), for_both_cell, forboth, FreeExecutorState(), get_range_key_properties(), get_range_nulltest(), GetPerTupleExprContext, HeapTupleIsValid, i, PartitionBoundSpec::is_default, IsA, sort-test::key, PartitionRangeDatum::kind, lappend(), lfirst, lfirst_node, linitial, list_head(), list_length(), list_make1, lnext(), PartitionBoundSpec::lowerdatums, make_partition_op_expr(), makeBoolConst(), makeBoolExpr(), MemoryContextSwitchTo(), NIL, NOT_EXPR, PartitionDescData::nparts, PartitionDescData::oids, OR_EXPR, PartitionKeyData::partexprs, PARTITION_RANGE_DATUM_MAXVALUE, PARTITION_RANGE_DATUM_MINVALUE, PARTITION_RANGE_DATUM_VALUE, PartitionKeyData::partnatts, RelationGetPartitionDesc(), RelationGetPartitionKey(), ReleaseSysCache(), RELOID, SearchSysCache1(), stringToNode(), SysCacheGetAttr(), TextDatumGetCString, and PartitionBoundSpec::upperdatums.

Referenced by check_default_partition_contents(), and get_qual_from_partbound().

4290 {
4291  List *result = NIL;
4292  ListCell *cell1,
4293  *cell2,
4294  *partexprs_item,
4295  *partexprs_item_saved;
4296  int i,
4297  j;
4298  PartitionRangeDatum *ldatum,
4299  *udatum;
4301  Expr *keyCol;
4302  Const *lower_val,
4303  *upper_val;
4304  List *lower_or_arms,
4305  *upper_or_arms;
4306  int num_or_arms,
4307  current_or_arm;
4308  ListCell *lower_or_start_datum,
4309  *upper_or_start_datum;
4310  bool need_next_lower_arm,
4311  need_next_upper_arm;
4312 
4313  if (spec->is_default)
4314  {
4315  List *or_expr_args = NIL;
4316  PartitionDesc pdesc = RelationGetPartitionDesc(parent, false);
4317  Oid *inhoids = pdesc->oids;
4318  int nparts = pdesc->nparts,
4319  i;
4320 
4321  for (i = 0; i < nparts; i++)
4322  {
4323  Oid inhrelid = inhoids[i];
4324  HeapTuple tuple;
4325  Datum datum;
4326  bool isnull;
4327  PartitionBoundSpec *bspec;
4328 
4329  tuple = SearchSysCache1(RELOID, inhrelid);
4330  if (!HeapTupleIsValid(tuple))
4331  elog(ERROR, "cache lookup failed for relation %u", inhrelid);
4332 
4333  datum = SysCacheGetAttr(RELOID, tuple,
4334  Anum_pg_class_relpartbound,
4335  &isnull);
4336  if (isnull)
4337  elog(ERROR, "null relpartbound for relation %u", inhrelid);
4338 
4339  bspec = (PartitionBoundSpec *)
4341  if (!IsA(bspec, PartitionBoundSpec))
4342  elog(ERROR, "expected PartitionBoundSpec");
4343 
4344  if (!bspec->is_default)
4345  {
4346  List *part_qual;
4347 
4348  part_qual = get_qual_for_range(parent, bspec, true);
4349 
4350  /*
4351  * AND the constraints of the partition and add to
4352  * or_expr_args
4353  */
4354  or_expr_args = lappend(or_expr_args, list_length(part_qual) > 1
4355  ? makeBoolExpr(AND_EXPR, part_qual, -1)
4356  : linitial(part_qual));
4357  }
4358  ReleaseSysCache(tuple);
4359  }
4360 
4361  if (or_expr_args != NIL)
4362  {
4363  Expr *other_parts_constr;
4364 
4365  /*
4366  * Combine the constraints obtained for non-default partitions
4367  * using OR. As requested, each of the OR's args doesn't include
4368  * the NOT NULL test for partition keys (which is to avoid its
4369  * useless repetition). Add the same now.
4370  */
4371  other_parts_constr =
4374  list_length(or_expr_args) > 1
4375  ? makeBoolExpr(OR_EXPR, or_expr_args,
4376  -1)
4377  : linitial(or_expr_args)),
4378  -1);
4379 
4380  /*
4381  * Finally, the default partition contains everything *NOT*
4382  * contained in the non-default partitions.
4383  */
4384  result = list_make1(makeBoolExpr(NOT_EXPR,
4385  list_make1(other_parts_constr), -1));
4386  }
4387 
4388  return result;
4389  }
4390 
4391  /*
4392  * If it is the recursive call for default, we skip the get_range_nulltest
4393  * to avoid accumulating the NullTest on the same keys for each partition.
4394  */
4395  if (!for_default)
4396  result = get_range_nulltest(key);
4397 
4398  /*
4399  * Iterate over the key columns and check if the corresponding lower and
4400  * upper datums are equal using the btree equality operator for the
4401  * column's type. If equal, we emit single keyCol = common_value
4402  * expression. Starting from the first column for which the corresponding
4403  * lower and upper bound datums are not equal, we generate OR expressions
4404  * as shown in the function's header comment.
4405  */
4406  i = 0;
4407  partexprs_item = list_head(key->partexprs);
4408  partexprs_item_saved = partexprs_item; /* placate compiler */
4409  forboth(cell1, spec->lowerdatums, cell2, spec->upperdatums)
4410  {
4411  EState *estate;
4412  MemoryContext oldcxt;
4413  Expr *test_expr;
4414  ExprState *test_exprstate;
4415  Datum test_result;
4416  bool isNull;
4417 
4418  ldatum = lfirst_node(PartitionRangeDatum, cell1);
4419  udatum = lfirst_node(PartitionRangeDatum, cell2);
4420 
4421  /*
4422  * Since get_range_key_properties() modifies partexprs_item, and we
4423  * might need to start over from the previous expression in the later
4424  * part of this function, save away the current value.
4425  */
4426  partexprs_item_saved = partexprs_item;
4427 
4428  get_range_key_properties(key, i, ldatum, udatum,
4429  &partexprs_item,
4430  &keyCol,
4431  &lower_val, &upper_val);
4432 
4433  /*
4434  * If either value is NULL, the corresponding partition bound is
4435  * either MINVALUE or MAXVALUE, and we treat them as unequal, because
4436  * even if they're the same, there is no common value to equate the
4437  * key column with.
4438  */
4439  if (!lower_val || !upper_val)
4440  break;
4441 
4442  /* Create the test expression */
4443  estate = CreateExecutorState();
4444  oldcxt = MemoryContextSwitchTo(estate->es_query_cxt);
4445  test_expr = make_partition_op_expr(key, i, BTEqualStrategyNumber,
4446  (Expr *) lower_val,
4447  (Expr *) upper_val);
4448  fix_opfuncids((Node *) test_expr);
4449  test_exprstate = ExecInitExpr(test_expr, NULL);
4450  test_result = ExecEvalExprSwitchContext(test_exprstate,
4451  GetPerTupleExprContext(estate),
4452  &isNull);
4453  MemoryContextSwitchTo(oldcxt);
4454  FreeExecutorState(estate);
4455 
4456  /* If not equal, go generate the OR expressions */
4457  if (!DatumGetBool(test_result))
4458  break;
4459 
4460  /*
4461  * The bounds for the last key column can't be equal, because such a
4462  * range partition would never be allowed to be defined (it would have
4463  * an empty range otherwise).
4464  */
4465  if (i == key->partnatts - 1)
4466  elog(ERROR, "invalid range bound specification");
4467 
4468  /* Equal, so generate keyCol = lower_val expression */
4469  result = lappend(result,
4471  keyCol, (Expr *) lower_val));
4472 
4473  i++;
4474  }
4475 
4476  /* First pair of lower_val and upper_val that are not equal. */
4477  lower_or_start_datum = cell1;
4478  upper_or_start_datum = cell2;
4479 
4480  /* OR will have as many arms as there are key columns left. */
4481  num_or_arms = key->partnatts - i;
4482  current_or_arm = 0;
4483  lower_or_arms = upper_or_arms = NIL;
4484  need_next_lower_arm = need_next_upper_arm = true;
4485  while (current_or_arm < num_or_arms)
4486  {
4487  List *lower_or_arm_args = NIL,
4488  *upper_or_arm_args = NIL;
4489 
4490  /* Restart scan of columns from the i'th one */
4491  j = i;
4492  partexprs_item = partexprs_item_saved;
4493 
4494  for_both_cell(cell1, spec->lowerdatums, lower_or_start_datum,
4495  cell2, spec->upperdatums, upper_or_start_datum)
4496  {
4497  PartitionRangeDatum *ldatum_next = NULL,
4498  *udatum_next = NULL;
4499 
4500  ldatum = lfirst_node(PartitionRangeDatum, cell1);
4501  if (lnext(spec->lowerdatums, cell1))
4502  ldatum_next = castNode(PartitionRangeDatum,
4503  lfirst(lnext(spec->lowerdatums, cell1)));
4504  udatum = lfirst_node(PartitionRangeDatum, cell2);
4505  if (lnext(spec->upperdatums, cell2))
4506  udatum_next = castNode(PartitionRangeDatum,
4507  lfirst(lnext(spec->upperdatums, cell2)));
4508  get_range_key_properties(key, j, ldatum, udatum,
4509  &partexprs_item,
4510  &keyCol,
4511  &lower_val, &upper_val);
4512 
4513  if (need_next_lower_arm && lower_val)
4514  {
4515  uint16 strategy;
4516 
4517  /*
4518  * For the non-last columns of this arm, use the EQ operator.
4519  * For the last column of this arm, use GT, unless this is the
4520  * last column of the whole bound check, or the next bound
4521  * datum is MINVALUE, in which case use GE.
4522  */
4523  if (j - i < current_or_arm)
4524  strategy = BTEqualStrategyNumber;
4525  else if (j == key->partnatts - 1 ||
4526  (ldatum_next &&
4527  ldatum_next->kind == PARTITION_RANGE_DATUM_MINVALUE))
4528  strategy = BTGreaterEqualStrategyNumber;
4529  else
4530  strategy = BTGreaterStrategyNumber;
4531 
4532  lower_or_arm_args = lappend(lower_or_arm_args,
4533  make_partition_op_expr(key, j,
4534  strategy,
4535  keyCol,
4536  (Expr *) lower_val));
4537  }
4538 
4539  if (need_next_upper_arm && upper_val)
4540  {
4541  uint16 strategy;
4542 
4543  /*
4544  * For the non-last columns of this arm, use the EQ operator.
4545  * For the last column of this arm, use LT, unless the next
4546  * bound datum is MAXVALUE, in which case use LE.
4547  */
4548  if (j - i < current_or_arm)
4549  strategy = BTEqualStrategyNumber;
4550  else if (udatum_next &&
4551  udatum_next->kind == PARTITION_RANGE_DATUM_MAXVALUE)
4552  strategy = BTLessEqualStrategyNumber;
4553  else
4554  strategy = BTLessStrategyNumber;
4555 
4556  upper_or_arm_args = lappend(upper_or_arm_args,
4557  make_partition_op_expr(key, j,
4558  strategy,
4559  keyCol,
4560  (Expr *) upper_val));
4561  }
4562 
4563  /*
4564  * Did we generate enough of OR's arguments? First arm considers
4565  * the first of the remaining columns, second arm considers first
4566  * two of the remaining columns, and so on.
4567  */
4568  ++j;
4569  if (j - i > current_or_arm)
4570  {
4571  /*
4572  * We must not emit any more arms if the new column that will
4573  * be considered is unbounded, or this one was.
4574  */
4575  if (!lower_val || !ldatum_next ||
4576  ldatum_next->kind != PARTITION_RANGE_DATUM_VALUE)
4577  need_next_lower_arm = false;
4578  if (!upper_val || !udatum_next ||
4579  udatum_next->kind != PARTITION_RANGE_DATUM_VALUE)
4580  need_next_upper_arm = false;
4581  break;
4582  }
4583  }
4584 
4585  if (lower_or_arm_args != NIL)
4586  lower_or_arms = lappend(lower_or_arms,
4587  list_length(lower_or_arm_args) > 1
4588  ? makeBoolExpr(AND_EXPR, lower_or_arm_args, -1)
4589  : linitial(lower_or_arm_args));
4590 
4591  if (upper_or_arm_args != NIL)
4592  upper_or_arms = lappend(upper_or_arms,
4593  list_length(upper_or_arm_args) > 1
4594  ? makeBoolExpr(AND_EXPR, upper_or_arm_args, -1)
4595  : linitial(upper_or_arm_args));
4596 
4597  /* If no work to do in the next iteration, break away. */
4598  if (!need_next_lower_arm && !need_next_upper_arm)
4599  break;
4600 
4601  ++current_or_arm;
4602  }
4603 
4604  /*
4605  * Generate the OR expressions for each of lower and upper bounds (if
4606  * required), and append to the list of implicitly ANDed list of
4607  * expressions.
4608  */
4609  if (lower_or_arms != NIL)
4610  result = lappend(result,
4611  list_length(lower_or_arms) > 1
4612  ? makeBoolExpr(OR_EXPR, lower_or_arms, -1)
4613  : linitial(lower_or_arms));
4614  if (upper_or_arms != NIL)
4615  result = lappend(result,
4616  list_length(upper_or_arms) > 1
4617  ? makeBoolExpr(OR_EXPR, upper_or_arms, -1)
4618  : linitial(upper_or_arms));
4619 
4620  /*
4621  * As noted above, for non-default, we return list with constant TRUE. If
4622  * the result is NIL during the recursive call for default, it implies
4623  * this is the only other partition which can hold every value of the key
4624  * except NULL. Hence we return the NullTest result skipped earlier.
4625  */
4626  if (result == NIL)
4627  result = for_default
4628  ? get_range_nulltest(key)
4630 
4631  return result;
4632 }
#define NIL
Definition: pg_list.h:65
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
static Datum ExecEvalExprSwitchContext(ExprState *state, ExprContext *econtext, bool *isNull)
Definition: executor.h:331
#define BTGreaterStrategyNumber
Definition: stratnum.h:33
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:446
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:322
#define castNode(_type_, nodeptr)
Definition: nodes.h:608
void fix_opfuncids(Node *node)
Definition: nodeFuncs.c:1652
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
PartitionRangeDatumKind kind
Definition: parsenodes.h:861
Definition: nodes.h:539
static List * get_range_nulltest(PartitionKey key)
Definition: partbounds.c:4694
void * stringToNode(const char *str)
Definition: read.c:89
#define false
Definition: c.h:399
List * partexprs
Definition: partcache.h:30
PartitionKey RelationGetPartitionKey(Relation rel)
Definition: partcache.c:54
unsigned int Oid
Definition: postgres_ext.h:31
Expr * makeBoolExpr(BoolExprType boolop, List *args, int location)
Definition: makefuncs.c:369
#define BTLessEqualStrategyNumber
Definition: stratnum.h:30
#define list_make1(x1)
Definition: pg_list.h:206
void FreeExecutorState(EState *estate)
Definition: execUtils.c:186
static Expr * make_partition_op_expr(PartitionKey key, int keynum, uint16 strategy, Expr *arg1, Expr *arg2)
Definition: partbounds.c:3881
static void get_range_key_properties(PartitionKey key, int keynum, PartitionRangeDatum *ldatum, PartitionRangeDatum *udatum, ListCell **partexprs_item, Expr **keyCol, Const **lower_val, Const **upper_val)
Definition: partbounds.c:4650
#define GetPerTupleExprContext(estate)
Definition: executor.h:533
#define true
Definition: c.h:395
unsigned short uint16
Definition: c.h:440
MemoryContext es_query_cxt
Definition: execnodes.h:600
#define linitial(l)
Definition: pg_list.h:174
#define ERROR
Definition: elog.h:46
Node * makeBoolConst(bool value, bool isnull)
Definition: makefuncs.c:357
#define lfirst_node(type, lc)
Definition: pg_list.h:172
PartitionDesc RelationGetPartitionDesc(Relation rel, bool omit_detached)
Definition: partdesc.c:72
#define DatumGetBool(X)
Definition: postgres.h:437
static ListCell * list_head(const List *l)
Definition: pg_list.h:125
EState * CreateExecutorState(void)
Definition: execUtils.c:90
List * lappend(List *list, void *datum)
Definition: list.c:336
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
#define TextDatumGetCString(d)
Definition: builtins.h:83
uintptr_t Datum
Definition: postgres.h:411
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
#define for_both_cell(cell1, list1, initcell1, cell2, list2, initcell2)
Definition: pg_list.h:468
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1388
static List * get_qual_for_range(Relation parent, PartitionBoundSpec *spec, bool for_default)
Definition: partbounds.c:4288
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define lfirst(lc)
Definition: pg_list.h:169
static int list_length(const List *l)
Definition: pg_list.h:149
#define elog(elevel,...)
Definition: elog.h:232
int i
ExprState * ExecInitExpr(Expr *node, PlanState *parent)
Definition: execExpr.c:123
#define BTLessStrategyNumber
Definition: stratnum.h:29
Definition: pg_list.h:50
#define BTEqualStrategyNumber
Definition: stratnum.h:31
#define BTGreaterEqualStrategyNumber
Definition: stratnum.h:32

◆ get_qual_from_partbound()

List* get_qual_from_partbound ( Relation  parent,
PartitionBoundSpec spec 
)

Definition at line 249 of file partbounds.c.

References Assert, elog, ERROR, 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(), PartitionKeyData::strategy, and PartitionBoundSpec::strategy.

Referenced by ATExecAttachPartition(), and generate_partition_qual().

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  default:
274  elog(ERROR, "unexpected partition strategy: %d",
275  (int) key->strategy);
276  }
277 
278  return my_qual;
279 }
#define NIL
Definition: pg_list.h:65
static List * get_qual_for_hash(Relation parent, PartitionBoundSpec *spec)
Definition: partbounds.c:3996
PartitionKey RelationGetPartitionKey(Relation rel)
Definition: partcache.c:54
#define ERROR
Definition: elog.h:46
#define PARTITION_STRATEGY_HASH
Definition: parsenodes.h:814
static List * get_qual_for_range(Relation parent, PartitionBoundSpec *spec, bool for_default)
Definition: partbounds.c:4288
#define Assert(condition)
Definition: c.h:804
#define PARTITION_STRATEGY_LIST
Definition: parsenodes.h:815
#define PARTITION_STRATEGY_RANGE
Definition: parsenodes.h:816
#define elog(elevel,...)
Definition: elog.h:232
Definition: pg_list.h:50
static List * get_qual_for_list(Relation parent, PartitionBoundSpec *spec)
Definition: partbounds.c:4079

◆ get_range_key_properties()

static void get_range_key_properties ( PartitionKey  key,
int  keynum,
PartitionRangeDatum ldatum,
PartitionRangeDatum udatum,
ListCell **  partexprs_item,
Expr **  keyCol,
Const **  lower_val,
Const **  upper_val 
)
static

Definition at line 4650 of file partbounds.c.

References castNode, copyObject, elog, ERROR, PartitionRangeDatum::kind, lfirst, lnext(), makeVar(), PartitionKeyData::partattrs, PartitionKeyData::partexprs, PARTITION_RANGE_DATUM_VALUE, PartitionKeyData::parttypcoll, PartitionKeyData::parttypid, PartitionKeyData::parttypmod, and PartitionRangeDatum::value.

Referenced by get_qual_for_range().

4656 {
4657  /* Get partition key expression for this column */
4658  if (key->partattrs[keynum] != 0)
4659  {
4660  *keyCol = (Expr *) makeVar(1,
4661  key->partattrs[keynum],
4662  key->parttypid[keynum],
4663  key->parttypmod[keynum],
4664  key->parttypcoll[keynum],
4665  0);
4666  }
4667  else
4668  {
4669  if (*partexprs_item == NULL)
4670  elog(ERROR, "wrong number of partition key expressions");
4671  *keyCol = copyObject(lfirst(*partexprs_item));
4672  *partexprs_item = lnext(key->partexprs, *partexprs_item);
4673  }
4674 
4675  /* Get appropriate Const nodes for the bounds */
4676  if (ldatum->kind == PARTITION_RANGE_DATUM_VALUE)
4677  *lower_val = castNode(Const, copyObject(ldatum->value));
4678  else
4679  *lower_val = NULL;
4680 
4681  if (udatum->kind == PARTITION_RANGE_DATUM_VALUE)
4682  *upper_val = castNode(Const, copyObject(udatum->value));
4683  else
4684  *upper_val = NULL;
4685 }
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:322
#define castNode(_type_, nodeptr)
Definition: nodes.h:608
PartitionRangeDatumKind kind
Definition: parsenodes.h:861
List * partexprs
Definition: partcache.h:30
Oid * parttypcoll
Definition: partcache.h:46
#define ERROR
Definition: elog.h:46
Var * makeVar(Index varno, AttrNumber varattno, Oid vartype, int32 vartypmod, Oid varcollid, Index varlevelsup)
Definition: makefuncs.c:66
AttrNumber * partattrs
Definition: partcache.h:28
int32 * parttypmod
Definition: partcache.h:42
#define lfirst(lc)
Definition: pg_list.h:169
#define elog(elevel,...)
Definition: elog.h:232
#define copyObject(obj)
Definition: nodes.h:655

◆ get_range_nulltest()

static List * get_range_nulltest ( PartitionKey  key)
static

Definition at line 4694 of file partbounds.c.

References NullTest::arg, NullTest::argisrow, copyObject, elog, ERROR, i, IS_NOT_NULL, lappend(), lfirst, list_head(), lnext(), NullTest::location, makeNode, makeVar(), NIL, NullTest::nulltesttype, PartitionKeyData::partattrs, PartitionKeyData::partexprs, PartitionKeyData::partnatts, PartitionKeyData::parttypcoll, PartitionKeyData::parttypid, and PartitionKeyData::parttypmod.

Referenced by get_qual_for_range().

4695 {
4696  List *result = NIL;
4697  NullTest *nulltest;
4698  ListCell *partexprs_item;
4699  int i;
4700 
4701  partexprs_item = list_head(key->partexprs);
4702  for (i = 0; i < key->partnatts; i++)
4703  {
4704  Expr *keyCol;
4705 
4706  if (key->partattrs[i] != 0)
4707  {
4708  keyCol = (Expr *) makeVar(1,
4709  key->partattrs[i],
4710  key->parttypid[i],
4711  key->parttypmod[i],
4712  key->parttypcoll[i],
4713  0);
4714  }
4715  else
4716  {
4717  if (partexprs_item == NULL)
4718  elog(ERROR, "wrong number of partition key expressions");
4719  keyCol = copyObject(lfirst(partexprs_item));
4720  partexprs_item = lnext(key->partexprs, partexprs_item);
4721  }
4722 
4723  nulltest = makeNode(NullTest);
4724  nulltest->arg = keyCol;
4725  nulltest->nulltesttype = IS_NOT_NULL;
4726  nulltest->argisrow = false;
4727  nulltest->location = -1;
4728  result = lappend(result, nulltest);
4729  }
4730 
4731  return result;
4732 }
#define NIL
Definition: pg_list.h:65
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:322
List * partexprs
Definition: partcache.h:30
Oid * parttypcoll
Definition: partcache.h:46
#define ERROR
Definition: elog.h:46
Expr * arg
Definition: primnodes.h:1265
static ListCell * list_head(const List *l)
Definition: pg_list.h:125
Var * makeVar(Index varno, AttrNumber varattno, Oid vartype, int32 vartypmod, Oid varcollid, Index varlevelsup)
Definition: makefuncs.c:66
List * lappend(List *list, void *datum)
Definition: list.c:336
AttrNumber * partattrs
Definition: partcache.h:28
NullTestType nulltesttype
Definition: primnodes.h:1266
int32 * parttypmod
Definition: partcache.h:42
#define makeNode(_type_)
Definition: nodes.h:587
#define lfirst(lc)
Definition: pg_list.h:169
int location
Definition: primnodes.h:1268
#define elog(elevel,...)
Definition: elog.h:232
int i
bool argisrow
Definition: primnodes.h:1267
#define copyObject(obj)
Definition: nodes.h:655
Definition: pg_list.h:50

◆ get_range_partition()

static int get_range_partition ( RelOptInfo rel,
PartitionBoundInfo  bi,
int *  lb_pos,
PartitionRangeBound lb,
PartitionRangeBound ub 
)
static

Definition at line 2589 of file partbounds.c.

References Assert, get_range_partition_internal(), is_dummy_partition(), PARTITION_STRATEGY_RANGE, and PartitionBoundInfoData::strategy.

Referenced by merge_range_bounds().

2594 {
2595  int part_index;
2596 
2598 
2599  do
2600  {
2601  part_index = get_range_partition_internal(bi, lb_pos, lb, ub);
2602  if (part_index == -1)
2603  return -1;
2604  } while (is_dummy_partition(rel, part_index));
2605 
2606  return part_index;
2607 }
static int get_range_partition_internal(PartitionBoundInfo bi, int *lb_pos, PartitionRangeBound *lb, PartitionRangeBound *ub)
Definition: partbounds.c:2610
static bool is_dummy_partition(RelOptInfo *rel, int part_index)
Definition: partbounds.c:1854
#define Assert(condition)
Definition: c.h:804
#define PARTITION_STRATEGY_RANGE
Definition: parsenodes.h:816

◆ get_range_partition_internal()

static int get_range_partition_internal ( PartitionBoundInfo  bi,
int *  lb_pos,
PartitionRangeBound lb,
PartitionRangeBound ub 
)
static

Definition at line 2610 of file partbounds.c.

References Assert, PartitionRangeBound::datums, PartitionBoundInfoData::datums, PartitionRangeBound::index, PartitionBoundInfoData::indexes, PartitionRangeBound::kind, PartitionBoundInfoData::kind, PartitionRangeBound::lower, and PartitionBoundInfoData::ndatums.

Referenced by get_range_partition().

2614 {
2615  /* Return the index as -1 if we've exhausted all lower bounds. */
2616  if (*lb_pos >= bi->ndatums)
2617  return -1;
2618 
2619  /* A lower bound should have at least one more bound after it. */
2620  Assert(*lb_pos + 1 < bi->ndatums);
2621 
2622  /* Set the lower bound. */
2623  lb->index = bi->indexes[*lb_pos];
2624  lb->datums = bi->datums[*lb_pos];
2625  lb->kind = bi->kind[*lb_pos];
2626  lb->lower = true;
2627  /* Set the upper bound. */
2628  ub->index = bi->indexes[*lb_pos + 1];
2629  ub->datums = bi->datums[*lb_pos + 1];
2630  ub->kind = bi->kind[*lb_pos + 1];
2631  ub->lower = false;
2632 
2633  /* The index assigned to an upper bound should be valid. */
2634  Assert(ub->index >= 0);
2635 
2636  /*
2637  * Advance the position to the next lower bound. If there are no bounds
2638  * left beyond the upper bound, we have reached the last lower bound.
2639  */
2640  if (*lb_pos + 2 >= bi->ndatums)
2641  *lb_pos = bi->ndatums;
2642  else
2643  {
2644  /*
2645  * If the index assigned to the bound next to the upper bound isn't
2646  * valid, that is the next lower bound; else, the upper bound is also
2647  * the lower bound of the next range partition.
2648  */
2649  if (bi->indexes[*lb_pos + 2] < 0)
2650  *lb_pos = *lb_pos + 2;
2651  else
2652  *lb_pos = *lb_pos + 1;
2653  }
2654 
2655  return ub->index;
2656 }
PartitionRangeDatumKind ** kind
Definition: partbounds.h:82
PartitionRangeDatumKind * kind
Definition: partbounds.c:68
#define Assert(condition)
Definition: c.h:804

◆ init_partition_map()

static void init_partition_map ( RelOptInfo rel,
PartitionMap map 
)
static

Definition at line 1822 of file partbounds.c.

References PartitionMap::did_remapping, i, PartitionMap::merged, PartitionMap::merged_indexes, PartitionMap::nparts, RelOptInfo::nparts, PartitionMap::old_indexes, and palloc().

Referenced by merge_list_bounds(), and merge_range_bounds().

1823 {
1824  int nparts = rel->nparts;
1825  int i;
1826 
1827  map->nparts = nparts;
1828  map->merged_indexes = (int *) palloc(sizeof(int) * nparts);
1829  map->merged = (bool *) palloc(sizeof(bool) * nparts);
1830  map->did_remapping = false;
1831  map->old_indexes = (int *) palloc(sizeof(int) * nparts);
1832  for (i = 0; i < nparts; i++)
1833  {
1834  map->merged_indexes[i] = map->old_indexes[i] = -1;
1835  map->merged[i] = false;
1836  }
1837 }
bool did_remapping
Definition: partbounds.c:82
int nparts
Definition: pathnodes.h:756
bool * merged
Definition: partbounds.c:80
void * palloc(Size size)
Definition: mcxt.c:1062
int * old_indexes
Definition: partbounds.c:83
int i
int * merged_indexes
Definition: partbounds.c:79

◆ is_dummy_partition()

static bool is_dummy_partition ( RelOptInfo rel,
int  part_index 
)
static

Definition at line 1854 of file partbounds.c.

References Assert, IS_DUMMY_REL, and RelOptInfo::part_rels.

Referenced by get_range_partition(), merge_list_bounds(), and merge_range_bounds().

1855 {
1856  RelOptInfo *part_rel;
1857 
1858  Assert(part_index >= 0);
1859  part_rel = rel->part_rels[part_index];
1860  if (part_rel == NULL || IS_DUMMY_REL(part_rel))
1861  return true;
1862  return false;
1863 }
#define IS_DUMMY_REL(r)
Definition: pathnodes.h:1461
#define Assert(condition)
Definition: c.h:804
struct RelOptInfo ** part_rels
Definition: pathnodes.h:763

◆ make_one_partition_rbound()

static PartitionRangeBound * make_one_partition_rbound ( PartitionKey  key,
int  index,
List datums,
bool  lower 
)
static

Definition at line 3441 of file partbounds.c.

References Assert, castNode, Const::constisnull, Const::constvalue, PartitionRangeBound::datums, elog, ERROR, i, PartitionHashBound::index, PartitionRangeBound::index, PartitionRangeBound::kind, PartitionRangeDatum::kind, lfirst_node, lower(), PartitionRangeBound::lower, NIL, palloc0(), PARTITION_RANGE_DATUM_VALUE, PartitionKeyData::partnatts, val, and PartitionRangeDatum::value.

Referenced by check_new_partition_bound(), and create_range_bounds().

3442 {
3443  PartitionRangeBound *bound;
3444  ListCell *lc;
3445  int i;
3446 
3447  Assert(datums != NIL);
3448 
3449  bound = (PartitionRangeBound *) palloc0(sizeof(PartitionRangeBound));
3450  bound->index = index;
3451  bound->datums = (Datum *) palloc0(key->partnatts * sizeof(Datum));
3452  bound->kind = (PartitionRangeDatumKind *) palloc0(key->partnatts *
3453  sizeof(PartitionRangeDatumKind));
3454  bound->lower = lower;
3455 
3456  i = 0;
3457  foreach(lc, datums)
3458  {
3460 
3461  /* What's contained in this range datum? */
3462  bound->kind[i] = datum->kind;
3463 
3464  if (datum->kind == PARTITION_RANGE_DATUM_VALUE)
3465  {
3466  Const *val = castNode(Const, datum->value);
3467 
3468  if (val->constisnull)
3469  elog(ERROR, "invalid range bound datum");
3470  bound->datums[i] = val->constvalue;
3471  }
3472 
3473  i++;
3474  }
3475 
3476  return bound;
3477 }
Datum constvalue
Definition: primnodes.h:219
#define NIL
Definition: pg_list.h:65
PartitionRangeDatumKind * kind
Definition: partbounds.c:68
Datum lower(PG_FUNCTION_ARGS)
Definition: oracle_compat.c:46
#define castNode(_type_, nodeptr)
Definition: nodes.h:608
PartitionRangeDatumKind
Definition: parsenodes.h:850
PartitionRangeDatumKind kind
Definition: parsenodes.h:861
Definition: type.h:89
#define ERROR
Definition: elog.h:46
#define lfirst_node(type, lc)
Definition: pg_list.h:172
void * palloc0(Size size)
Definition: mcxt.c:1093
uintptr_t Datum
Definition: postgres.h:411
#define Assert(condition)
Definition: c.h:804
#define elog(elevel,...)
Definition: elog.h:232
int i
long val
Definition: informix.c:664
bool constisnull
Definition: primnodes.h:220

◆ make_partition_op_expr()

static Expr * make_partition_op_expr ( PartitionKey  key,
int  keynum,
uint16  strategy,
Expr arg1,
Expr arg2 
)
static

Definition at line 3881 of file partbounds.c.

References ScalarArrayOpExpr::args, ArrayExpr::array_collid, ArrayExpr::array_typeid, Assert, COERCE_EXPLICIT_CAST, ArrayExpr::element_typeid, ArrayExpr::elements, elog, ERROR, get_array_type(), get_opcode(), get_partition_operator(), ScalarArrayOpExpr::hashfuncid, ScalarArrayOpExpr::inputcollid, InvalidOid, IsA, lappend(), lfirst, linitial, list_length(), list_make2, ScalarArrayOpExpr::location, ArrayExpr::location, make_opclause(), makeBoolExpr(), makeNode, makeRelabelType(), ArrayExpr::multidims, ScalarArrayOpExpr::negfuncid, NIL, ScalarArrayOpExpr::opfuncid, ScalarArrayOpExpr::opno, OR_EXPR, PartitionKeyData::partcollation, PARTITION_STRATEGY_LIST, PARTITION_STRATEGY_RANGE, PartitionKeyData::partopcintype, PartitionKeyData::parttypcoll, PartitionKeyData::parttypid, PartitionKeyData::strategy, type_is_array, and ScalarArrayOpExpr::useOr.

Referenced by get_qual_for_list(), and get_qual_for_range().

3883 {
3884  Oid operoid;
3885  bool need_relabel = false;
3886  Expr *result = NULL;
3887 
3888  /* Get the correct btree operator for this partitioning column */
3889  operoid = get_partition_operator(key, keynum, strategy, &need_relabel);
3890 
3891  /*
3892  * Chosen operator may be such that the non-Const operand needs to be
3893  * coerced, so apply the same; see the comment in
3894  * get_partition_operator().
3895  */
3896  if (!IsA(arg1, Const) &&
3897  (need_relabel ||
3898  key->partcollation[keynum] != key->parttypcoll[keynum]))
3899  arg1 = (Expr *) makeRelabelType(arg1,
3900  key->partopcintype[keynum],
3901  -1,
3902  key->partcollation[keynum],
3904 
3905  /* Generate the actual expression */
3906  switch (key->strategy)
3907  {
3909  {
3910  List *elems = (List *) arg2;
3911  int nelems = list_length(elems);
3912 
3913  Assert(nelems >= 1);
3914  Assert(keynum == 0);
3915 
3916  if (nelems > 1 &&
3917  !type_is_array(key->parttypid[keynum]))
3918  {
3919  ArrayExpr *arrexpr;
3920  ScalarArrayOpExpr *saopexpr;
3921 
3922  /* Construct an ArrayExpr for the right-hand inputs */
3923  arrexpr = makeNode(ArrayExpr);
3924  arrexpr->array_typeid =
3925  get_array_type(key->parttypid[keynum]);
3926  arrexpr->array_collid = key->parttypcoll[keynum];
3927  arrexpr->element_typeid = key->parttypid[keynum];
3928  arrexpr->elements = elems;
3929  arrexpr->multidims = false;
3930  arrexpr->location = -1;
3931 
3932  /* Build leftop = ANY (rightop) */
3933  saopexpr = makeNode(ScalarArrayOpExpr);
3934  saopexpr->opno = operoid;
3935  saopexpr->opfuncid = get_opcode(operoid);
3936  saopexpr->hashfuncid = InvalidOid;
3937  saopexpr->negfuncid = InvalidOid;
3938  saopexpr->useOr = true;
3939  saopexpr->inputcollid = key->partcollation[keynum];
3940  saopexpr->args = list_make2(arg1, arrexpr);
3941  saopexpr->location = -1;
3942 
3943  result = (Expr *) saopexpr;
3944  }
3945  else
3946  {
3947  List *elemops = NIL;
3948  ListCell *lc;
3949 
3950  foreach(lc, elems)
3951  {
3952  Expr *elem = lfirst(lc),
3953  *elemop;
3954 
3955  elemop = make_opclause(operoid,
3956  BOOLOID,
3957  false,
3958  arg1, elem,
3959  InvalidOid,
3960  key->partcollation[keynum]);
3961  elemops = lappend(elemops, elemop);
3962  }
3963 
3964  result = nelems > 1 ? makeBoolExpr(OR_EXPR, elemops, -1) : linitial(elemops);
3965  }
3966  break;
3967  }
3968 
3970  result = make_opclause(operoid,
3971  BOOLOID,
3972  false,
3973  arg1, arg2,
3974  InvalidOid,
3975  key->partcollation[keynum]);
3976  break;
3977 
3978  default:
3979  elog(ERROR, "invalid partitioning strategy");
3980  break;
3981  }
3982 
3983  return result;
3984 }
#define list_make2(x1, x2)
Definition: pg_list.h:208
bool multidims
Definition: primnodes.h:1038
#define NIL
Definition: pg_list.h:65
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
Oid get_array_type(Oid typid)
Definition: lsyscache.c:2734
Oid array_typeid
Definition: primnodes.h:1034
unsigned int Oid
Definition: postgres_ext.h:31
Expr * make_opclause(Oid opno, Oid opresulttype, bool opretset, Expr *leftop, Expr *rightop, Oid opcollid, Oid inputcollid)
Definition: makefuncs.c:610
Expr * makeBoolExpr(BoolExprType boolop, List *args, int location)
Definition: makefuncs.c:369
Oid * parttypcoll
Definition: partcache.h:46
#define linitial(l)
Definition: pg_list.h:174
#define ERROR
Definition: elog.h:46
RelabelType * makeRelabelType(Expr *arg, Oid rtype, int32 rtypmod, Oid rcollid, CoercionForm rformat)
Definition: makefuncs.c:402
List * elements
Definition: primnodes.h:1037
List * lappend(List *list, void *datum)
Definition: list.c:336
Oid * partcollation
Definition: partcache.h:38
int location
Definition: primnodes.h:1039
static Oid get_partition_operator(PartitionKey key, int col, StrategyNumber strategy, bool *need_relabel)
Definition: partbounds.c:3845
#define InvalidOid
Definition: postgres_ext.h:36
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1256
#define makeNode(_type_)
Definition: nodes.h:587
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
Oid array_collid
Definition: primnodes.h:1035
static int list_length(const List *l)
Definition: pg_list.h:149
#define type_is_array(typid)
Definition: lsyscache.h:202
#define PARTITION_STRATEGY_LIST
Definition: parsenodes.h:815
Oid element_typeid
Definition: primnodes.h:1036
#define PARTITION_STRATEGY_RANGE
Definition: parsenodes.h:816
Oid * partopcintype
Definition: partcache.h:34
#define elog(elevel,...)
Definition: elog.h:232
Definition: pg_list.h:50

◆ merge_default_partitions()

static void merge_default_partitions ( PartitionMap outer_map,
PartitionMap inner_map,
bool  outer_has_default,
bool  inner_has_default,
int  outer_default,
int  inner_default,
JoinType  jointype,
int *  next_index,
int *  default_index 
)
static

Definition at line 2268 of file partbounds.c.

References Assert, IS_OUTER_JOIN, JOIN_FULL, JOIN_RIGHT, merge_matching_partitions(), merge_partition_with_dummy(), and PartitionMap::merged_indexes.

Referenced by merge_list_bounds(), and merge_range_bounds().

2277 {
2278  int outer_merged_index = -1;
2279  int inner_merged_index = -1;
2280 
2281  Assert(outer_has_default || inner_has_default);
2282 
2283  /* Get the merged partition indexes for the default partitions. */
2284  if (outer_has_default)
2285  {
2286  Assert(outer_default >= 0 && outer_default < outer_map->nparts);
2287  outer_merged_index = outer_map->merged_indexes[outer_default];
2288  }
2289  if (inner_has_default)
2290  {
2291  Assert(inner_default >= 0 && inner_default < inner_map->nparts);
2292  inner_merged_index = inner_map->merged_indexes[inner_default];
2293  }
2294 
2295  if (outer_has_default && !inner_has_default)
2296  {
2297  /*
2298  * If this is an outer join, the default partition on the outer side
2299  * has to be scanned all the way anyway; if we have not yet assigned a
2300  * partition, merge the default partition with a dummy partition on
2301  * the other side. The merged partition will act as the default
2302  * partition of the join relation (see comments in
2303  * process_inner_partition()).
2304  */
2305  if (IS_OUTER_JOIN(jointype))
2306  {
2307  Assert(jointype != JOIN_RIGHT);
2308  if (outer_merged_index == -1)
2309  {
2310  Assert(*default_index == -1);
2311  *default_index = merge_partition_with_dummy(outer_map,
2312  outer_default,
2313  next_index);
2314  }
2315  else
2316  Assert(*default_index == outer_merged_index);
2317  }
2318  else
2319  Assert(*default_index == -1);
2320  }
2321  else if (!outer_has_default && inner_has_default)
2322  {
2323  /*
2324  * If this is a FULL join, the default partition on the inner side has
2325  * to be scanned all the way anyway; if we have not yet assigned a
2326  * partition, merge the default partition with a dummy partition on
2327  * the other side. The merged partition will act as the default
2328  * partition of the join relation (see comments in
2329  * process_outer_partition()).
2330  */
2331  if (jointype == JOIN_FULL)
2332  {
2333  if (inner_merged_index == -1)
2334  {
2335  Assert(*default_index == -1);
2336  *default_index = merge_partition_with_dummy(inner_map,
2337  inner_default,
2338  next_index);
2339  }
2340  else
2341  Assert(*default_index == inner_merged_index);
2342  }
2343  else
2344  Assert(*default_index == -1);
2345  }
2346  else
2347  {
2348  Assert(outer_has_default && inner_has_default);
2349 
2350  /*
2351  * The default partitions have to be joined with each other, so merge
2352  * them. Note that each of the default partitions isn't merged yet
2353  * (see, process_outer_partition()/process_innerer_partition()), so
2354  * they should be merged successfully. The merged partition will act
2355  * as the default partition of the join relation.
2356  */
2357  Assert(outer_merged_index == -1);
2358  Assert(inner_merged_index == -1);
2359  Assert(*default_index == -1);
2360  *default_index = merge_matching_partitions(outer_map,
2361  inner_map,
2362  outer_default,
2363  inner_default,
2364  next_index);
2365  Assert(*default_index >= 0);
2366  }
2367 }
static int merge_matching_partitions(PartitionMap *outer_map, PartitionMap *inner_map, int outer_part, int inner_part, int *next_index)
Definition: partbounds.c:1873
#define IS_OUTER_JOIN(jointype)
Definition: nodes.h:755
static int merge_partition_with_dummy(PartitionMap *map, int index, int *next_index)
Definition: partbounds.c:2378
#define Assert(condition)
Definition: c.h:804
int * merged_indexes
Definition: partbounds.c:79

◆ merge_list_bounds()

static PartitionBoundInfo merge_list_bounds ( FmgrInfo partsupfunc,
Oid collations,
RelOptInfo outer_rel,
RelOptInfo inner_rel,
JoinType  jointype,
List **  outer_parts,
List **  inner_parts 
)
static

Definition at line 1209 of file partbounds.c.

References Assert, RelOptInfo::boundinfo, build_merged_partition_bounds(), cleanup(), DatumGetInt32, PartitionBoundInfoData::datums, PartitionBoundInfoData::default_index, PartitionMap::did_remapping, fix_merged_indexes(), free_partition_map(), FunctionCall2Coll(), generate_matching_part_pairs(), PartitionBoundInfoData::indexes, init_partition_map(), is_dummy_partition(), IS_OUTER_JOIN, JOIN_FULL, PartitionBoundInfoData::kind, lappend(), lappend_int(), list_free(), list_length(), merge_default_partitions(), merge_matching_partitions(), merge_null_partitions(), PartitionBoundInfoData::ndatums, NIL, PartitionBoundInfoData::null_index, partition_bound_accepts_nulls, partition_bound_has_default, PARTITION_STRATEGY_LIST, process_inner_partition(), process_outer_partition(), and PartitionBoundInfoData::strategy.

Referenced by partition_bounds_merge().

1213 {
1214  PartitionBoundInfo merged_bounds = NULL;
1215  PartitionBoundInfo outer_bi = outer_rel->boundinfo;
1216  PartitionBoundInfo inner_bi = inner_rel->boundinfo;
1217  bool outer_has_default = partition_bound_has_default(outer_bi);
1218  bool inner_has_default = partition_bound_has_default(inner_bi);
1219  int outer_default = outer_bi->default_index;
1220  int inner_default = inner_bi->default_index;
1221  bool outer_has_null = partition_bound_accepts_nulls(outer_bi);
1222  bool inner_has_null = partition_bound_accepts_nulls(inner_bi);
1223  PartitionMap outer_map;
1224  PartitionMap inner_map;
1225  int outer_pos;
1226  int inner_pos;
1227  int next_index = 0;
1228  int null_index = -1;
1229  int default_index = -1;
1230  List *merged_datums = NIL;
1231  List *merged_indexes = NIL;
1232 
1233  Assert(*outer_parts == NIL);
1234  Assert(*inner_parts == NIL);
1235  Assert(outer_bi->strategy == inner_bi->strategy &&
1236  outer_bi->strategy == PARTITION_STRATEGY_LIST);
1237  /* List partitioning doesn't require kinds. */
1238  Assert(!outer_bi->kind && !inner_bi->kind);
1239 
1240  init_partition_map(outer_rel, &outer_map);
1241  init_partition_map(inner_rel, &inner_map);
1242 
1243  /*
1244  * If the default partitions (if any) have been proven empty, deem them
1245  * non-existent.
1246  */
1247  if (outer_has_default && is_dummy_partition(outer_rel, outer_default))
1248  outer_has_default = false;
1249  if (inner_has_default && is_dummy_partition(inner_rel, inner_default))
1250  inner_has_default = false;
1251 
1252  /*
1253  * Merge partitions from both sides. In each iteration we compare a pair
1254  * of list values, one from each side, and decide whether the
1255  * corresponding partitions match or not. If the two values match
1256  * exactly, move to the next pair of list values, otherwise move to the
1257  * next list value on the side with a smaller list value.
1258  */
1259  outer_pos = inner_pos = 0;
1260  while (outer_pos < outer_bi->ndatums || inner_pos < inner_bi->ndatums)
1261  {
1262  int outer_index = -1;
1263  int inner_index = -1;
1264  Datum *outer_datums;
1265  Datum *inner_datums;
1266  int cmpval;
1267  Datum *merged_datum = NULL;
1268  int merged_index = -1;
1269 
1270  if (outer_pos < outer_bi->ndatums)
1271  {
1272  /*
1273  * If the partition on the outer side has been proven empty,
1274  * ignore it and move to the next datum on the outer side.
1275  */
1276  outer_index = outer_bi->indexes[outer_pos];
1277  if (is_dummy_partition(outer_rel, outer_index))
1278  {
1279  outer_pos++;
1280  continue;
1281  }
1282  }
1283  if (inner_pos < inner_bi->ndatums)
1284  {
1285  /*
1286  * If the partition on the inner side has been proven empty,
1287  * ignore it and move to the next datum on the inner side.
1288  */
1289  inner_index = inner_bi->indexes[inner_pos];
1290  if (is_dummy_partition(inner_rel, inner_index))
1291  {
1292  inner_pos++;
1293  continue;
1294  }
1295  }
1296 
1297  /* Get the list values. */
1298  outer_datums = outer_pos < outer_bi->ndatums ?
1299  outer_bi->datums[outer_pos] : NULL;
1300  inner_datums = inner_pos < inner_bi->ndatums ?
1301  inner_bi->datums[inner_pos] : NULL;
1302 
1303  /*
1304  * We run this loop till both sides finish. This allows us to avoid
1305  * duplicating code to handle the remaining values on the side which
1306  * finishes later. For that we set the comparison parameter cmpval in
1307  * such a way that it appears as if the side which finishes earlier
1308  * has an extra value higher than any other value on the unfinished
1309  * side. That way we advance the values on the unfinished side till
1310  * all of its values are exhausted.
1311  */
1312  if (outer_pos >= outer_bi->ndatums)
1313  cmpval = 1;
1314  else if (inner_pos >= inner_bi->ndatums)
1315  cmpval = -1;
1316  else
1317  {
1318  Assert(outer_datums != NULL && inner_datums != NULL);
1319  cmpval = DatumGetInt32(FunctionCall2Coll(&partsupfunc[0],
1320  partcollation[0],
1321  outer_datums[0],
1322  inner_datums[0]));
1323  }
1324 
1325  if (cmpval == 0)
1326  {
1327  /* Two list values match exactly. */
1328  Assert(outer_pos < outer_bi->ndatums);
1329  Assert(inner_pos < inner_bi->ndatums);
1330  Assert(outer_index >= 0);
1331  Assert(inner_index >= 0);
1332 
1333  /*
1334  * Try merging both partitions. If successful, add the list value
1335  * and index of the merged partition below.
1336  */
1337  merged_index = merge_matching_partitions(&outer_map, &inner_map,
1338  outer_index, inner_index,
1339  &next_index);
1340  if (merged_index == -1)
1341  goto cleanup;
1342 
1343  merged_datum = outer_datums;
1344 
1345  /* Move to the next pair of list values. */
1346  outer_pos++;
1347  inner_pos++;
1348  }
1349  else if (cmpval < 0)
1350  {
1351  /* A list value missing from the inner side. */
1352  Assert(outer_pos < outer_bi->ndatums);
1353 
1354  /*
1355  * If the inner side has the default partition, or this is an
1356  * outer join, try to assign a merged partition to the outer
1357  * partition (see process_outer_partition()). Otherwise, the
1358  * outer partition will not contribute to the result.
1359  */
1360  if (inner_has_default || IS_OUTER_JOIN(jointype))
1361  {
1362  /* Get the outer partition. */
1363  outer_index = outer_bi->indexes[outer_pos];
1364  Assert(outer_index >= 0);
1365  merged_index = process_outer_partition(&outer_map,
1366  &inner_map,
1367  outer_has_default,
1368  inner_has_default,
1369  outer_index,
1370  inner_default,
1371  jointype,
1372  &next_index,
1373  &default_index);
1374  if (merged_index == -1)
1375  goto cleanup;
1376  merged_datum = outer_datums;
1377  }
1378 
1379  /* Move to the next list value on the outer side. */
1380  outer_pos++;
1381  }
1382  else
1383  {
1384  /* A list value missing from the outer side. */
1385  Assert(cmpval > 0);
1386  Assert(inner_pos < inner_bi->ndatums);
1387 
1388  /*
1389  * If the outer side has the default partition, or this is a FULL
1390  * join, try to assign a merged partition to the inner partition
1391  * (see process_inner_partition()). Otherwise, the inner
1392  * partition will not contribute to the result.
1393  */
1394  if (outer_has_default || jointype == JOIN_FULL)
1395  {
1396  /* Get the inner partition. */
1397  inner_index = inner_bi->indexes[inner_pos];
1398  Assert(inner_index >= 0);
1399  merged_index = process_inner_partition(&outer_map,
1400  &inner_map,
1401  outer_has_default,
1402  inner_has_default,
1403  inner_index,
1404  outer_default,
1405  jointype,
1406  &next_index,
1407  &default_index);
1408  if (merged_index == -1)
1409  goto cleanup;
1410  merged_datum = inner_datums;
1411  }
1412 
1413  /* Move to the next list value on the inner side. */
1414  inner_pos++;
1415  }
1416 
1417  /*
1418  * If we assigned a merged partition, add the list value and index of
1419  * the merged partition if appropriate.
1420  */
1421  if (merged_index >= 0 && merged_index != default_index)
1422  {
1423  merged_datums = lappend(merged_datums, merged_datum);
1424  merged_indexes = lappend_int(merged_indexes, merged_index);
1425  }
1426  }
1427 
1428  /*
1429  * If the NULL partitions (if any) have been proven empty, deem them
1430  * non-existent.
1431  */
1432  if (outer_has_null &&
1433  is_dummy_partition(outer_rel, outer_bi->null_index))
1434  outer_has_null = false;
1435  if (inner_has_null &&
1436  is_dummy_partition(inner_rel, inner_bi->null_index))
1437  inner_has_null = false;
1438 
1439  /* Merge the NULL partitions if any. */
1440  if (outer_has_null || inner_has_null)
1441  merge_null_partitions(&outer_map, &inner_map,
1442  outer_has_null, inner_has_null,
1443  outer_bi->null_index, inner_bi->null_index,
1444  jointype, &next_index, &null_index);
1445  else
1446  Assert(null_index == -1);
1447 
1448  /* Merge the default partitions if any. */
1449  if (outer_has_default || inner_has_default)
1450  merge_default_partitions(&outer_map, &inner_map,
1451  outer_has_default, inner_has_default,
1452  outer_default, inner_default,
1453  jointype, &next_index, &default_index);
1454  else
1455  Assert(default_index == -1);
1456 
1457  /* If we have merged partitions, create the partition bounds. */
1458  if (next_index > 0)
1459  {
1460  /* Fix the merged_indexes list if necessary. */
1461  if (outer_map.did_remapping || inner_map.did_remapping)
1462  {
1463  Assert(jointype == JOIN_FULL);
1464  fix_merged_indexes(&outer_map, &inner_map,
1465  next_index, merged_indexes);
1466  }
1467 
1468  /* Use maps to match partitions from inputs. */
1469  generate_matching_part_pairs(outer_rel, inner_rel,
1470  &outer_map, &inner_map,
1471  next_index,
1472  outer_parts, inner_parts);
1473  Assert(*outer_parts != NIL);
1474  Assert(*inner_parts != NIL);
1475  Assert(list_length(*outer_parts) == list_length(*inner_parts));
1476  Assert(list_length(*outer_parts) <= next_index);
1477 
1478  /* Make a PartitionBoundInfo struct to return. */
1479  merged_bounds = build_merged_partition_bounds(outer_bi->strategy,
1480  merged_datums,
1481  NIL,
1482  merged_indexes,
1483  null_index,
1484  default_index);
1485  Assert(merged_bounds);
1486  }
1487 
1488 cleanup:
1489  /* Free local memory before returning. */
1490  list_free(merged_datums);
1491  list_free(merged_indexes);
1492  free_partition_map(&outer_map);
1493  free_partition_map(&inner_map);
1494 
1495  return merged_bounds;
1496 }
bool did_remapping
Definition: partbounds.c:82
static void generate_matching_part_pairs(RelOptInfo *outer_rel, RelOptInfo *inner_rel, PartitionMap *outer_map, PartitionMap *inner_map, int nmerged, List **outer_parts, List **inner_parts)
Definition: partbounds.c:2450
#define NIL
Definition: pg_list.h:65
PartitionRangeDatumKind ** kind
Definition: partbounds.h:82
static PartitionBoundInfo build_merged_partition_bounds(char strategy, List *merged_datums, List *merged_kinds, List *merged_indexes, int null_index, int default_index)
Definition: partbounds.c:2529
#define DatumGetInt32(X)
Definition: postgres.h:516
static int merge_matching_partitions(PartitionMap *outer_map, PartitionMap *inner_map, int outer_part, int inner_part, int *next_index)
Definition: partbounds.c:1873
static int process_inner_partition(PartitionMap *outer_map, PartitionMap *inner_map, bool outer_has_default, bool inner_has_default, int inner_index, int outer_default, JoinType jointype, int *next_index, int *default_index)
Definition: partbounds.c:2073
#define IS_OUTER_JOIN(jointype)
Definition: nodes.h:755
Datum FunctionCall2Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1148
static void free_partition_map(PartitionMap *map)
Definition: partbounds.c:1843
static void fix_merged_indexes(PartitionMap *outer_map, PartitionMap *inner_map, int nmerged, List *merged_indexes)
Definition: partbounds.c:2396
static bool is_dummy_partition(RelOptInfo *rel, int part_index)
Definition: partbounds.c:1854
#define partition_bound_has_default(bi)
Definition: partbounds.h:97
List * lappend_int(List *list, int datum)
Definition: list.c:354
List * lappend(List *list, void *datum)
Definition: list.c:336
uintptr_t Datum
Definition: postgres.h:411
static void cleanup(void)
Definition: bootstrap.c:867
struct PartitionBoundInfoData * boundinfo
Definition: pathnodes.h:759
#define partition_bound_accepts_nulls(bi)
Definition: partbounds.h:96
static void init_partition_map(RelOptInfo *rel, PartitionMap *map)
Definition: partbounds.c:1822
#define Assert(condition)
Definition: c.h:804
static int list_length(const List *l)
Definition: pg_list.h:149
static void merge_default_partitions(PartitionMap *outer_map, PartitionMap *inner_map, bool outer_has_default, bool inner_has_default, int outer_default, int inner_default, JoinType jointype, int *next_index, int *default_index)
Definition: partbounds.c:2268
#define PARTITION_STRATEGY_LIST
Definition: parsenodes.h:815
static int process_outer_partition(PartitionMap *outer_map, PartitionMap *inner_map, bool outer_has_default, bool inner_has_default, int outer_index, int inner_default, JoinType jointype, int *next_index, int *default_index)
Definition: partbounds.c:1991
void list_free(List *list)
Definition: list.c:1391
Definition: pg_list.h:50
static void merge_null_partitions(PartitionMap *outer_map, PartitionMap *inner_map, bool outer_has_null, bool inner_has_null, int outer_null, int inner_null, JoinType jointype, int *next_index, int *null_index)
Definition: partbounds.c:2158

◆ merge_matching_partitions()

static int merge_matching_partitions ( PartitionMap outer_map,
PartitionMap inner_map,
int  outer_part,
int  inner_part,
int *  next_index 
)
static

Definition at line 1873 of file partbounds.c.

References Assert, PartitionMap::did_remapping, PartitionMap::merged, PartitionMap::merged_indexes, and PartitionMap::old_indexes.

Referenced by merge_default_partitions(), merge_list_bounds(), merge_null_partitions(), merge_range_bounds(), process_inner_partition(), and process_outer_partition().

1875 {
1876  int outer_merged_index;
1877  int inner_merged_index;
1878  bool outer_merged;
1879  bool inner_merged;
1880 
1881  Assert(outer_index >= 0 && outer_index < outer_map->nparts);
1882  outer_merged_index = outer_map->merged_indexes[outer_index];
1883  outer_merged = outer_map->merged[outer_index];
1884  Assert(inner_index >= 0 && inner_index < inner_map->nparts);
1885  inner_merged_index = inner_map->merged_indexes[inner_index];
1886  inner_merged = inner_map->merged[inner_index];
1887 
1888  /*
1889  * Handle cases where we have already assigned a merged partition to each
1890  * of the given partitions.
1891  */
1892  if (outer_merged_index >= 0 && inner_merged_index >= 0)
1893  {
1894  /*
1895  * If the merged partitions are the same, no need to do anything;
1896  * return the index of the merged partitions. Otherwise, if each of
1897  * the given partitions has been merged with a dummy partition on the
1898  * other side, re-map them to either of the two merged partitions.
1899  * Otherwise, they can't be merged, so return -1.
1900  */
1901  if (outer_merged_index == inner_merged_index)
1902  {
1903  Assert(outer_merged);
1904  Assert(inner_merged);
1905  return outer_merged_index;
1906  }
1907  if (!outer_merged && !inner_merged)
1908  {
1909  /*
1910  * This can only happen for a list-partitioning case. We re-map
1911  * them to the merged partition with the smaller of the two merged
1912  * indexes to preserve the property that the canonical order of
1913  * list partitions is determined by the indexes assigned to the
1914  * smallest list value of each partition.
1915  */
1916  if (outer_merged_index < inner_merged_index)
1917  {
1918  outer_map->merged[outer_index] = true;
1919  inner_map->merged_indexes[inner_index] = outer_merged_index;
1920  inner_map->merged[inner_index] = true;
1921  inner_map->did_remapping = true;
1922  inner_map->old_indexes[inner_index] = inner_merged_index;
1923  return outer_merged_index;
1924  }
1925  else
1926  {
1927  inner_map->merged[inner_index] = true;
1928  outer_map->merged_indexes[outer_index] = inner_merged_index;
1929  outer_map->merged[outer_index] = true;
1930  outer_map->did_remapping = true;
1931  outer_map->old_indexes[outer_index] = outer_merged_index;
1932  return inner_merged_index;
1933  }
1934  }
1935  return -1;
1936  }
1937 
1938  /* At least one of the given partitions should not have yet been merged. */
1939  Assert(outer_merged_index == -1 || inner_merged_index == -1);
1940 
1941  /*
1942  * If neither of them has been merged, merge them. Otherwise, if one has
1943  * been merged with a dummy partition on the other side (and the other
1944  * hasn't yet been merged with anything), re-merge them. Otherwise, they
1945  * can't be merged, so return -1.
1946  */
1947  if (outer_merged_index == -1 && inner_merged_index == -1)
1948  {
1949  int merged_index = *next_index;
1950 
1951  Assert(!outer_merged);
1952  Assert(!inner_merged);
1953  outer_map->merged_indexes[outer_index] = merged_index;
1954  outer_map->merged[outer_index] = true;
1955  inner_map->merged_indexes[inner_index] = merged_index;
1956  inner_map->merged[inner_index] = true;
1957  *next_index = *next_index + 1;
1958  return merged_index;
1959  }
1960  if (outer_merged_index >= 0 && !outer_map->merged[outer_index])
1961  {
1962  Assert(inner_merged_index == -1);
1963  Assert(!inner_merged);
1964  inner_map->merged_indexes[inner_index] = outer_merged_index;
1965  inner_map->merged[inner_index] = true;
1966  outer_map->merged[outer_index] = true;
1967  return outer_merged_index;
1968  }
1969  if (inner_merged_index >= 0 && !inner_map->merged[inner_index])
1970  {
1971  Assert(outer_merged_index == -1);
1972  Assert(!outer_merged);
1973  outer_map->merged_indexes[outer_index] = inner_merged_index;
1974  outer_map->merged[outer_index] = true;
1975  inner_map->merged[inner_index] = true;
1976  return inner_merged_index;
1977  }
1978  return -1;
1979 }
bool did_remapping
Definition: partbounds.c:82
bool * merged
Definition: partbounds.c:80
#define Assert(condition)
Definition: c.h:804
int * old_indexes
Definition: partbounds.c:83
int * merged_indexes
Definition: partbounds.c:79

◆ merge_null_partitions()

static void merge_null_partitions ( PartitionMap outer_map,
PartitionMap inner_map,
bool  outer_has_null,
bool  inner_has_null,
int  outer_null,
int  inner_null,
JoinType  jointype,
int *  next_index,
int *  null_index 
)
static

Definition at line 2158 of file partbounds.c.

References Assert, IS_OUTER_JOIN, JOIN_FULL, JOIN_RIGHT, merge_matching_partitions(), merge_partition_with_dummy(), and PartitionMap::merged_indexes.

Referenced by merge_list_bounds().

2167 {
2168  bool consider_outer_null = false;
2169  bool consider_inner_null = false;
2170 
2171  Assert(outer_has_null || inner_has_null);
2172  Assert(*null_index == -1);
2173 
2174  /*
2175  * Check whether the NULL partitions have already been merged and if so,
2176  * set the consider_outer_null/consider_inner_null flags.
2177  */
2178  if (outer_has_null)
2179  {
2180  Assert(outer_null >= 0 && outer_null < outer_map->nparts);
2181  if (outer_map->merged_indexes[outer_null] == -1)
2182  consider_outer_null = true;
2183  }
2184  if (inner_has_null)
2185  {
2186  Assert(inner_null >= 0 && inner_null < inner_map->nparts);
2187  if (inner_map->merged_indexes[inner_null] == -1)
2188  consider_inner_null = true;
2189  }
2190 
2191  /* If both flags are set false, we don't need to do anything. */
2192  if (!consider_outer_null && !consider_inner_null)
2193  return;
2194 
2195  if (consider_outer_null && !consider_inner_null)
2196  {
2197  Assert(outer_has_null);
2198 
2199  /*
2200  * If this is an outer join, the NULL partition on the outer side has
2201  * to be scanned all the way anyway; merge the NULL partition with a
2202  * dummy partition on the other side. In that case
2203  * consider_outer_null means that the NULL partition only contains
2204  * NULL values as the key values, so the merged partition will do so;
2205  * treat it as the NULL partition of the join relation.
2206  */
2207  if (IS_OUTER_JOIN(jointype))
2208  {
2209  Assert(jointype != JOIN_RIGHT);
2210  *null_index = merge_partition_with_dummy(outer_map, outer_null,
2211  next_index);
2212  }
2213  }
2214  else if (!consider_outer_null && consider_inner_null)
2215  {
2216  Assert(inner_has_null);
2217 
2218  /*
2219  * If this is a FULL join, the NULL partition on the inner side has to
2220  * be scanned all the way anyway; merge the NULL partition with a
2221  * dummy partition on the other side. In that case
2222  * consider_inner_null means that the NULL partition only contains
2223  * NULL values as the key values, so the merged partition will do so;
2224  * treat it as the NULL partition of the join relation.
2225  */
2226  if (jointype == JOIN_FULL)
2227  *null_index = merge_partition_with_dummy(inner_map, inner_null,
2228  next_index);
2229  }
2230  else
2231  {
2232  Assert(consider_outer_null && consider_inner_null);
2233  Assert(outer_has_null);
2234  Assert(inner_has_null);
2235 
2236  /*
2237  * If this is an outer join, the NULL partition on the outer side (and
2238  * that on the inner side if this is a FULL join) have to be scanned
2239  * all the way anyway, so merge them. Note that each of the NULL
2240  * partitions isn't merged yet, so they should be merged successfully.
2241  * Like the above, each of the NULL partitions only contains NULL
2242  * values as the key values, so the merged partition will do so; treat
2243  * it as the NULL partition of the join relation.
2244  *
2245  * Note: if this an INNER/SEMI join, the join clause will never be
2246  * satisfied by two NULL values (see comments above), so both the NULL
2247  * partitions can be eliminated.
2248  */
2249  if (IS_OUTER_JOIN(jointype))
2250  {
2251  Assert(jointype != JOIN_RIGHT);
2252  *null_index = merge_matching_partitions(outer_map, inner_map,
2253  outer_null, inner_null,
2254  next_index);
2255  Assert(*null_index >= 0);
2256  }
2257  }
2258 }
static int merge_matching_partitions(PartitionMap *outer_map, PartitionMap *inner_map, int outer_part, int inner_part, int *next_index)
Definition: partbounds.c:1873
#define IS_OUTER_JOIN(jointype)
Definition: nodes.h:755
static int merge_partition_with_dummy(PartitionMap *map, int index, int *next_index)
Definition: partbounds.c:2378
#define Assert(condition)
Definition: c.h:804
int * merged_indexes
Definition: partbounds.c:79

◆ merge_partition_with_dummy()

static int merge_partition_with_dummy ( PartitionMap map,
int  index,
int *  next_index 
)
static

Definition at line 2378 of file partbounds.c.

References Assert, PartitionHashBound::index, PartitionMap::merged, and PartitionMap::merged_indexes.

Referenced by merge_default_partitions(), merge_null_partitions(), process_inner_partition(), and process_outer_partition().

2379 {
2380  int merged_index = *next_index;
2381 
2382  Assert(index >= 0 && index < map->nparts);
2383  Assert(map->merged_indexes[index] == -1);
2384  Assert(!map->merged[index]);
2385  map->merged_indexes[index] = merged_index;
2386  /* Leave the merged flag alone! */
2387  *next_index = *next_index + 1;
2388  return merged_index;
2389 }
Definition: type.h:89
bool * merged
Definition: partbounds.c:80
#define Assert(condition)
Definition: c.h:804
int * merged_indexes
Definition: partbounds.c:79

◆ merge_range_bounds()

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 
)
static

Definition at line 1517 of file partbounds.c.

References add_merged_range_bounds(), Assert, RelOptInfo::boundinfo, build_merged_partition_bounds(), cleanup(), compare_range_bounds, compare_range_partitions(), PartitionBoundInfoData::default_index, PartitionMap::did_remapping, free_partition_map(), generate_matching_part_pairs(), get_merged_range_bounds(), get_range_partition(), init_partition_map(), is_dummy_partition(), IS_OUTER_JOIN, JOIN_FULL, list_free(), list_length(), merge_default_partitions(), merge_matching_partitions(), PartitionMap::merged, PartitionMap::merged_indexes, NIL, partition_bound_has_default, PARTITION_STRATEGY_RANGE, process_inner_partition(), process_outer_partition(), and PartitionBoundInfoData::strategy.

Referenced by partition_bounds_merge().

1522 {
1523  PartitionBoundInfo merged_bounds = NULL;
1524  PartitionBoundInfo outer_bi = outer_rel->boundinfo;
1525  PartitionBoundInfo inner_bi = inner_rel->boundinfo;
1526  bool outer_has_default = partition_bound_has_default(outer_bi);
1527  bool inner_has_default = partition_bound_has_default(inner_bi);
1528  int outer_default = outer_bi->default_index;
1529  int inner_default = inner_bi->default_index;
1530  PartitionMap outer_map;
1531  PartitionMap inner_map;
1532  int outer_index;
1533  int inner_index;
1534  int outer_lb_pos;
1535  int inner_lb_pos;
1536  PartitionRangeBound outer_lb;
1537  PartitionRangeBound outer_ub;
1538  PartitionRangeBound inner_lb;
1539  PartitionRangeBound inner_ub;
1540  int next_index = 0;
1541  int default_index = -1;
1542  List *merged_datums = NIL;
1543  List *merged_kinds = NIL;
1544  List *merged_indexes = NIL;
1545 
1546  Assert(*outer_parts == NIL);
1547  Assert(*inner_parts == NIL);
1548  Assert(outer_bi->strategy == inner_bi->strategy &&
1549  outer_bi->strategy == PARTITION_STRATEGY_RANGE);
1550 
1551  init_partition_map(outer_rel, &outer_map);
1552  init_partition_map(inner_rel, &inner_map);
1553 
1554  /*
1555  * If the default partitions (if any) have been proven empty, deem them
1556  * non-existent.
1557  */
1558  if (outer_has_default && is_dummy_partition(outer_rel, outer_default))
1559  outer_has_default = false;
1560  if (inner_has_default && is_dummy_partition(inner_rel, inner_default))
1561  inner_has_default = false;
1562 
1563  /*
1564  * Merge partitions from both sides. In each iteration we compare a pair
1565  * of ranges, one from each side, and decide whether the corresponding
1566  * partitions match or not. If the two ranges overlap, move to the next
1567  * pair of ranges, otherwise move to the next range on the side with a
1568  * lower range. outer_lb_pos/inner_lb_pos keep track of the positions of
1569  * lower bounds in the datums arrays in the outer/inner
1570  * PartitionBoundInfos respectively.
1571  */
1572  outer_lb_pos = inner_lb_pos = 0;
1573  outer_index = get_range_partition(outer_rel, outer_bi, &outer_lb_pos,
1574  &outer_lb, &outer_ub);
1575  inner_index = get_range_partition(inner_rel, inner_bi, &inner_lb_pos,
1576  &inner_lb, &inner_ub);
1577  while (outer_index >= 0 || inner_index >= 0)
1578  {
1579  bool overlap;
1580  int ub_cmpval;
1581  int lb_cmpval;
1582  PartitionRangeBound merged_lb = {-1, NULL, NULL, true};
1583  PartitionRangeBound merged_ub = {-1, NULL, NULL, false};
1584  int merged_index = -1;
1585 
1586  /*
1587  * We run this loop till both sides finish. This allows us to avoid
1588  * duplicating code to handle the remaining ranges on the side which
1589  * finishes later. For that we set the comparison parameter cmpval in
1590  * such a way that it appears as if the side which finishes earlier
1591  * has an extra range higher than any other range on the unfinished
1592  * side. That way we advance the ranges on the unfinished side till
1593  * all of its ranges are exhausted.
1594  */
1595  if (outer_index == -1)
1596  {
1597  overlap = false;
1598  lb_cmpval = 1;
1599  ub_cmpval = 1;
1600  }
1601  else if (inner_index == -1)
1602  {
1603  overlap = false;
1604  lb_cmpval = -1;
1605  ub_cmpval = -1;
1606  }
1607  else
1608  overlap = compare_range_partitions(partnatts, partsupfuncs,
1609  partcollations,
1610  &outer_lb, &outer_ub,
1611  &inner_lb, &inner_ub,
1612  &lb_cmpval, &ub_cmpval);
1613 
1614  if (overlap)
1615  {
1616  /* Two ranges overlap; form a join pair. */
1617 
1618  PartitionRangeBound save_outer_ub;
1619  PartitionRangeBound save_inner_ub;
1620 
1621  /* Both partitions should not have been merged yet. */
1622  Assert(outer_index >= 0);
1623  Assert(outer_map.merged_indexes[outer_index] == -1 &&
1624  outer_map.merged[outer_index] == false);
1625  Assert(inner_index >= 0);
1626  Assert(inner_map.merged_indexes[inner_index] == -1 &&
1627  inner_map.merged[inner_index] == false);
1628 
1629  /*
1630  * Get the index of the merged partition. Both partitions aren't
1631  * merged yet, so the partitions should be merged successfully.
1632  */
1633  merged_index = merge_matching_partitions(&outer_map, &inner_map,
1634  outer_index, inner_index,
1635  &next_index);
1636  Assert(merged_index >= 0);
1637 
1638  /* Get the range bounds of the merged partition. */
1639  get_merged_range_bounds(partnatts, partsupfuncs,
1640  partcollations, jointype,
1641  &outer_lb, &outer_ub,
1642  &inner_lb, &inner_ub,
1643  lb_cmpval, ub_cmpval,
1644  &merged_lb, &merged_ub);
1645 
1646  /* Save the upper bounds of both partitions for use below. */
1647  save_outer_ub = outer_ub;
1648  save_inner_ub = inner_ub;
1649 
1650  /* Move to the next pair of ranges. */
1651  outer_index = get_range_partition(outer_rel, outer_bi, &outer_lb_pos,
1652  &outer_lb, &outer_ub);
1653  inner_index = get_range_partition(inner_rel, inner_bi, &inner_lb_pos,
1654  &inner_lb, &inner_ub);
1655 
1656  /*
1657  * If the range of a partition on one side overlaps the range of
1658  * the next partition on the other side, that will cause the
1659  * partition on one side to match at least two partitions on the
1660  * other side, which is the case that we currently don't support
1661  * partitioned join for; give up.
1662  */
1663  if (ub_cmpval > 0 && inner_index >= 0 &&
1664  compare_range_bounds(partnatts, partsupfuncs, partcollations,
1665  &save_outer_ub, &inner_lb) > 0)
1666  goto cleanup;
1667  if (ub_cmpval < 0 && outer_index >= 0 &&
1668  compare_range_bounds(partnatts, partsupfuncs, partcollations,
1669  &outer_lb, &save_inner_ub) < 0)
1670  goto cleanup;
1671 
1672  /*
1673  * A row from a non-overlapping portion (if any) of a partition on
1674  * one side might find its join partner in the default partition
1675  * (if any) on the other side, causing the same situation as
1676  * above; give up in that case.
1677  */
1678  if ((outer_has_default && (lb_cmpval > 0 || ub_cmpval < 0)) ||
1679  (inner_has_default && (lb_cmpval < 0 || ub_cmpval > 0)))
1680  goto cleanup;
1681  }
1682  else if (ub_cmpval < 0)
1683  {
1684  /* A non-overlapping outer range. */
1685 
1686  /* The outer partition should not have been merged yet. */
1687  Assert(outer_index >= 0);
1688  Assert(outer_map.merged_indexes[outer_index] == -1 &&
1689  outer_map.merged[outer_index] == false);
1690 
1691  /*
1692  * If the inner side has the default partition, or this is an
1693  * outer join, try to assign a merged partition to the outer
1694  * partition (see process_outer_partition()). Otherwise, the
1695  * outer partition will not contribute to the result.
1696  */
1697  if (inner_has_default || IS_OUTER_JOIN(jointype))
1698  {
1699  merged_index = process_outer_partition(&outer_map,
1700  &inner_map,
1701  outer_has_default,
1702  inner_has_default,
1703  outer_index,
1704  inner_default,
1705  jointype,
1706  &next_index,
1707  &default_index);
1708  if (merged_index == -1)
1709  goto cleanup;
1710  merged_lb = outer_lb;
1711  merged_ub = outer_ub;
1712  }
1713 
1714  /* Move to the next range on the outer side. */
1715  outer_index = get_range_partition(outer_rel, outer_bi, &outer_lb_pos,
1716  &outer_lb, &outer_ub);
1717  }
1718  else
1719  {
1720  /* A non-overlapping inner range. */
1721  Assert(ub_cmpval > 0);
1722 
1723  /* The inner partition should not have been merged yet. */
1724  Assert(inner_index >= 0);
1725  Assert(inner_map.merged_indexes[inner_index] == -1 &&
1726  inner_map.merged[inner_index] == false);
1727 
1728  /*
1729  * If the outer side has the default partition, or this is a FULL
1730  * join, try to assign a merged partition to the inner partition
1731  * (see process_inner_partition()). Otherwise, the inner
1732  * partition will not contribute to the result.
1733  */
1734  if (outer_has_default || jointype == JOIN_FULL)
1735  {
1736  merged_index = process_inner_partition(&outer_map,
1737  &inner_map,
1738  outer_has_default,
1739  inner_has_default,
1740  inner_index,
1741  outer_default,
1742  jointype,
1743  &next_index,
1744  &default_index);
1745  if (merged_index == -1)
1746  goto cleanup;
1747  merged_lb = inner_lb;
1748  merged_ub = inner_ub;
1749  }
1750 
1751  /* Move to the next range on the inner side. */
1752  inner_index = get_range_partition(inner_rel, inner_bi, &inner_lb_pos,
1753  &inner_lb, &inner_ub);
1754  }
1755 
1756  /*
1757  * If we assigned a merged partition, add the range bounds and index
1758  * of the merged partition if appropriate.
1759  */
1760  if (merged_index >= 0 && merged_index != default_index)
1761  add_merged_range_bounds(partnatts, partsupfuncs, partcollations,
1762  &merged_lb, &merged_ub, merged_index,
1763  &merged_datums, &merged_kinds,
1764  &merged_indexes);
1765  }
1766 
1767  /* Merge the default partitions if any. */
1768  if (outer_has_default || inner_has_default)
1769  merge_default_partitions(&outer_map, &inner_map,
1770  outer_has_default, inner_has_default,
1771  outer_default, inner_default,
1772  jointype, &next_index, &default_index);
1773  else
1774  Assert(default_index == -1);
1775 
1776  /* If we have merged partitions, create the partition bounds. */
1777  if (next_index > 0)
1778  {
1779  /*
1780  * Unlike the case of list partitioning, we wouldn't have re-merged
1781  * partitions, so did_remapping should be left alone.
1782  */
1783  Assert(!outer_map.did_remapping);
1784  Assert(!inner_map.did_remapping);
1785 
1786  /* Use maps to match partitions from inputs. */
1787  generate_matching_part_pairs(outer_rel, inner_rel,
1788  &outer_map, &inner_map,
1789  next_index,
1790  outer_parts, inner_parts);
1791  Assert(*outer_parts != NIL);
1792  Assert(*inner_parts != NIL);
1793  Assert(list_length(*outer_parts) == list_length(*inner_parts));
1794  Assert(list_length(*outer_parts) == next_index);
1795 
1796  /* Make a PartitionBoundInfo struct to return. */
1797  merged_bounds = build_merged_partition_bounds(outer_bi->strategy,
1798  merged_datums,
1799  merged_kinds,
1800  merged_indexes,
1801  -1,
1802  default_index);
1803  Assert(merged_bounds);
1804  }
1805 
1806 cleanup:
1807  /* Free local memory before returning. */
1808  list_free(merged_datums);
1809  list_free(merged_kinds);
1810  list_free(merged_indexes);
1811  free_partition_map(&outer_map);
1812  free_partition_map(&inner_map);
1813 
1814  return merged_bounds;
1815 }
bool did_remapping
Definition: partbounds.c:82
static void generate_matching_part_pairs(RelOptInfo *outer_rel, RelOptInfo *inner_rel, PartitionMap *outer_map, PartitionMap *inner_map, int nmerged, List **outer_parts, List **inner_parts)
Definition: partbounds.c:2450
#define NIL
Definition: pg_list.h:65
static void get_merged_range_bounds(int partnatts, FmgrInfo *partsupfuncs, Oid *partcollations, JoinType jointype, PartitionRangeBound *outer_lb, PartitionRangeBound *outer_ub, PartitionRangeBound *inner_lb, PartitionRangeBound *inner_ub, int lb_cmpval, int ub_cmpval, PartitionRangeBound *merged_lb, PartitionRangeBound *merged_ub)
Definition: partbounds.c:2719
static PartitionBoundInfo build_merged_partition_bounds(char strategy, List *merged_datums, List *merged_kinds, List *merged_indexes, int null_index, int default_index)
Definition: partbounds.c:2529
static int merge_matching_partitions(PartitionMap *outer_map, PartitionMap *inner_map, int outer_part, int inner_part, int *next_index)
Definition: partbounds.c:1873
static int process_inner_partition(PartitionMap *outer_map, PartitionMap *inner_map, bool outer_has_default, bool inner_has_default, int inner_index, int outer_default, JoinType jointype, int *next_index, int *default_index)
Definition: partbounds.c:2073
#define IS_OUTER_JOIN(jointype)
Definition: nodes.h:755
static bool compare_range_partitions(int partnatts, FmgrInfo *partsupfuncs, Oid *partcollations, PartitionRangeBound *outer_lb, PartitionRangeBound *outer_ub, PartitionRangeBound *inner_lb, PartitionRangeBound *inner_ub, int *lb_cmpval, int *ub_cmpval)
Definition: partbounds.c:2670
static void free_partition_map(PartitionMap *map)
Definition: partbounds.c:1843
static void add_merged_range_bounds(int partnatts, FmgrInfo *partsupfuncs, Oid *partcollations, PartitionRangeBound *merged_lb, PartitionRangeBound *merged_ub, int merged_index, List **merged_datums, List **merged_kinds, List **merged_indexes)
Definition: partbounds.c:2783
bool * merged
Definition: partbounds.c:80
static bool is_dummy_partition(RelOptInfo *rel, int part_index)
Definition: partbounds.c:1854
#define partition_bound_has_default(bi)
Definition: partbounds.h:97
static void cleanup(void)
Definition: bootstrap.c:867
struct PartitionBoundInfoData * boundinfo
Definition: pathnodes.h:759
static void init_partition_map(RelOptInfo *rel, PartitionMap *map)
Definition: partbounds.c:1822
#define Assert(condition)
Definition: c.h:804
static int list_length(const List *l)
Definition: pg_list.h:149
static void merge_default_partitions(PartitionMap *outer_map, PartitionMap *inner_map, bool outer_has_default, bool inner_has_default, int outer_default, int inner_default, JoinType jointype, int *next_index, int *default_index)
Definition: partbounds.c:2268
static int process_outer_partition(PartitionMap *outer_map, PartitionMap *inner_map, bool outer_has_default, bool inner_has_default, int outer_index, int inner_default, JoinType jointype, int *next_index, int *default_index)
Definition: partbounds.c:1991
#define PARTITION_STRATEGY_RANGE
Definition: parsenodes.h:816
void list_free(List *list)
Definition: list.c:1391
#define compare_range_bounds(partnatts, partsupfunc, partcollations, bound1, bound2)
Definition: partbounds.c:88
Definition: pg_list.h:50
int * merged_indexes
Definition: partbounds.c:79
static int get_range_partition(RelOptInfo *rel, PartitionBoundInfo bi, int *lb_pos, PartitionRangeBound *lb, PartitionRangeBound *ub)
Definition: partbounds.c:2589

◆ partition_bounds_copy()

PartitionBoundInfo partition_bounds_copy ( PartitionBoundInfo  src,
PartitionKey  key 
)

Definition at line 1010 of file partbounds.c.

References Assert, bms_copy(), datumCopy(), PartitionBoundInfoData::datums, PartitionBoundInfoData::default_index, generate_unaccent_rules::dest, i, PartitionBoundInfoData::indexes, PartitionBoundInfoData::interleaved_parts, PartitionBoundInfoData::kind, PartitionBoundInfoData::ndatums, PartitionBoundInfoData::nindexes, PartitionBoundInfoData::null_index, palloc(), PARTITION_RANGE_DATUM_VALUE, PARTITION_STRATEGY_HASH, PARTITION_STRATEGY_LIST, PARTITION_STRATEGY_RANGE, PartitionKeyData::partnatts, PartitionKeyData::parttypbyval, PartitionKeyData::parttyplen, PartitionKeyData::strategy, and PartitionBoundInfoData::strategy.

Referenced by RelationBuildPartitionDesc().

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

◆ partition_bounds_create()

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

Definition at line 303 of file partbounds.c.

References Assert, create_hash_bounds(), create_list_bounds(), create_range_bounds(), elog, ERROR, i, palloc(), PARTITION_STRATEGY_HASH, PARTITION_STRATEGY_LIST, PARTITION_STRATEGY_RANGE, and PartitionKeyData::strategy.

Referenced by RelationBuildPartitionDesc().

305 {
306  int i;
307 
308  Assert(nparts > 0);
309 
310  /*
311  * For each partitioning method, we first convert the partition bounds
312  * from their parser node representation to the internal representation,
313  * along with any additional preprocessing (such as de-duplicating range
314  * bounds). Resulting bound datums are then added to the 'datums' array
315  * in PartitionBoundInfo. For each datum added, an integer indicating the
316  * canonical partition index is added to the 'indexes' array.
317  *
318  * For each bound, we remember its partition's position (0-based) in the
319  * original list to later map it to the canonical index.
320  */
321 
322  /*
323  * Initialize mapping array with invalid values, this is filled within
324  * each sub-routine below depending on the bound type.
325  */
326  *mapping = (int *) palloc(sizeof(int) * nparts);
327  for (i = 0; i < nparts; i++)
328  (*mapping)[i] = -1;
329 
330  switch (key->strategy)
331  {
333  return create_hash_bounds(boundspecs, nparts, key, mapping);
334 
336  return create_list_bounds(boundspecs, nparts, key, mapping);
337 
339  return create_range_bounds(boundspecs, nparts, key, mapping);
340 
341  default:
342  elog(ERROR, "unexpected partition strategy: %d",
343  (int) key->strategy);
344  break;
345  }
346 
347  Assert(false);
348  return NULL; /* keep compiler quiet */
349 }
#define ERROR
Definition: elog.h:46
static PartitionBoundInfo create_hash_bounds(PartitionBoundSpec **boundspecs, int nparts, PartitionKey key, int **mapping)
Definition: partbounds.c:356
static PartitionBoundInfo create_list_bounds(PartitionBoundSpec **boundspecs, int nparts, PartitionKey key, int **mapping)
Definition: partbounds.c:471
#define PARTITION_STRATEGY_HASH
Definition: parsenodes.h:814
#define Assert(condition)
Definition: c.h:804
#define PARTITION_STRATEGY_LIST
Definition: parsenodes.h:815
#define PARTITION_STRATEGY_RANGE
Definition: parsenodes.h:816
static PartitionBoundInfo create_range_bounds(PartitionBoundSpec **boundspecs, int nparts, PartitionKey key, int **mapping)