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:3504

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

2794 {
2795  int cmpval;
2796 
2797  if (!*merged_datums)
2798  {
2799  /* First merged partition */
2800  Assert(!*merged_kinds);
2801  Assert(!*merged_indexes);
2802  cmpval = 1;
2803  }
2804  else
2805  {
2806  PartitionRangeBound prev_ub;
2807 
2808  Assert(*merged_datums);
2809  Assert(*merged_kinds);
2810  Assert(*merged_indexes);
2811 
2812  /* Get the last upper bound. */
2813  prev_ub.index = llast_int(*merged_indexes);
2814  prev_ub.datums = (Datum *) llast(*merged_datums);
2815  prev_ub.kind = (PartitionRangeDatumKind *) llast(*merged_kinds);
2816  prev_ub.lower = false;
2817 
2818  /*
2819  * We pass lower1 = false to partition_rbound_cmp() to prevent it from
2820  * considering the last upper bound to be smaller than the lower bound
2821  * of the merged partition when the values of the two range bounds
2822  * compare equal.
2823  */
2824  cmpval = partition_rbound_cmp(partnatts, partsupfuncs, partcollations,
2825  merged_lb->datums, merged_lb->kind,
2826  false, &prev_ub);
2827  Assert(cmpval >= 0);
2828  }
2829 
2830  /*
2831  * If the lower bound is higher than the last upper bound, add the lower
2832  * bound with the index as -1 indicating that that is a lower bound; else,
2833  * the last upper bound will be reused as the lower bound of the merged
2834  * partition, so skip this.
2835  */
2836  if (cmpval > 0)
2837  {
2838  *merged_datums = lappend(*merged_datums, merged_lb->datums);
2839  *merged_kinds = lappend(*merged_kinds, merged_lb->kind);
2840  *merged_indexes = lappend_int(*merged_indexes, -1);
2841  }
2842 
2843  /* Add the upper bound and index of the merged partition. */
2844  *merged_datums = lappend(*merged_datums, merged_ub->datums);
2845  *merged_kinds = lappend(*merged_kinds, merged_ub->kind);
2846  *merged_indexes = lappend_int(*merged_indexes, merged_index);
2847 }
PartitionRangeDatumKind * kind
Definition: partbounds.c:68
PartitionRangeDatumKind
Definition: parsenodes.h:862
#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:3504
#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::interleaved_parts, 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  /* interleaved_parts is always NULL for join relations. */
2568  merged_bounds->interleaved_parts = NULL;
2569 
2570  Assert(list_length(merged_indexes) == ndatums);
2571  merged_bounds->nindexes = ndatums;
2572  merged_bounds->indexes = (int *) palloc(sizeof(int) * ndatums);
2573  pos = 0;
2574  foreach(lc, merged_indexes)
2575  merged_bounds->indexes[pos++] = lfirst_int(lc);
2576 
2577  merged_bounds->null_index = null_index;
2578  merged_bounds->default_index = default_index;
2579 
2580  return merged_bounds;
2581 }
#define NIL
Definition: pg_list.h:65
PartitionRangeDatumKind ** kind
Definition: partbounds.h:84
PartitionRangeDatumKind
Definition: parsenodes.h:862
Bitmapset * interleaved_parts
Definition: partbounds.h:87
#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:827
#define PARTITION_STRATEGY_RANGE
Definition: parsenodes.h:828
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 3267 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().

3269 {
3270  List *new_part_constraints;
3271  List *def_part_constraints;
3272  List *all_parts;
3273  ListCell *lc;
3274 
3275  new_part_constraints = (new_spec->strategy == PARTITION_STRATEGY_LIST)
3276  ? get_qual_for_list(parent, new_spec)
3277  : get_qual_for_range(parent, new_spec, false);
3278  def_part_constraints =
3279  get_proposed_default_constraint(new_part_constraints);
3280 
3281  /*
3282  * Map the Vars in the constraint expression from parent's attnos to
3283  * default_rel's.
3284  */
3285  def_part_constraints =
3286  map_partition_varattnos(def_part_constraints, 1, default_rel,
3287  parent);
3288 
3289  /*
3290  * If the existing constraints on the default partition imply that it will
3291  * not contain any row that would belong to the new partition, we can
3292  * avoid scanning the default partition.
3293  */
3294  if (PartConstraintImpliedByRelConstraint(default_rel, def_part_constraints))
3295  {
3296  ereport(DEBUG1,
3297  (errmsg_internal("updated partition constraint for default partition \"%s\" is implied by existing constraints",
3298  RelationGetRelationName(default_rel))));
3299  return;
3300  }
3301 
3302  /*
3303  * Scan the default partition and its subpartitions, and check for rows
3304  * that do not satisfy the revised partition constraints.
3305  */
3306  if (default_rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
3307  all_parts = find_all_inheritors(RelationGetRelid(default_rel),
3308  AccessExclusiveLock, NULL);
3309  else
3310  all_parts = list_make1_oid(RelationGetRelid(default_rel));
3311 
3312  foreach(lc, all_parts)
3313  {
3314  Oid part_relid = lfirst_oid(lc);
3315  Relation part_rel;
3316  Expr *partition_constraint;
3317  EState *estate;
3318  ExprState *partqualstate = NULL;
3319  Snapshot snapshot;
3320  ExprContext *econtext;
3321  TableScanDesc scan;
3322  MemoryContext oldCxt;
3323  TupleTableSlot *tupslot;
3324 
3325  /* Lock already taken above. */
3326  if (part_relid != RelationGetRelid(default_rel))
3327  {
3328  part_rel = table_open(part_relid, NoLock);
3329 
3330  /*
3331  * Map the Vars in the constraint expression from default_rel's
3332  * the sub-partition's.
3333  */
3334  partition_constraint = make_ands_explicit(def_part_constraints);
3335  partition_constraint = (Expr *)
3336  map_partition_varattnos((List *) partition_constraint, 1,
3337  part_rel, default_rel);
3338 
3339  /*
3340  * If the partition constraints on default partition child imply
3341  * that it will not contain any row that would belong to the new
3342  * partition, we can avoid scanning the child table.
3343  */
3345  def_part_constraints))
3346  {
3347  ereport(DEBUG1,
3348  (errmsg_internal("updated partition constraint for default partition \"%s\" is implied by existing constraints",
3349  RelationGetRelationName(part_rel))));
3350 
3351  table_close(part_rel, NoLock);
3352  continue;
3353  }
3354  }
3355  else
3356  {
3357  part_rel = default_rel;
3358  partition_constraint = make_ands_explicit(def_part_constraints);
3359  }
3360 
3361  /*
3362  * Only RELKIND_RELATION relations (i.e. leaf partitions) need to be
3363  * scanned.
3364  */
3365  if (part_rel->rd_rel->relkind != RELKIND_RELATION)
3366  {
3367  if (part_rel->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
3368  ereport(WARNING,
3369  (errcode(ERRCODE_CHECK_VIOLATION),
3370  errmsg("skipped scanning foreign table \"%s\" which is a partition of default partition \"%s\"",
3371  RelationGetRelationName(part_rel),
3372  RelationGetRelationName(default_rel))));
3373 
3374  if (RelationGetRelid(default_rel) != RelationGetRelid(part_rel))
3375  table_close(part_rel, NoLock);
3376 
3377  continue;
3378  }
3379 
3380  estate = CreateExecutorState();
3381 
3382  /* Build expression execution states for partition check quals */
3383  partqualstate = ExecPrepareExpr(partition_constraint, estate);
3384 
3385  econtext = GetPerTupleExprContext(estate);
3386  snapshot = RegisterSnapshot(GetLatestSnapshot());
3387  tupslot = table_slot_create(part_rel, &estate->es_tupleTable);
3388  scan = table_beginscan(part_rel, snapshot, 0, NULL);
3389 
3390  /*
3391  * Switch to per-tuple memory context and reset it for each tuple
3392  * produced, so we don't leak memory.
3393  */
3395 
3396  while (table_scan_getnextslot(scan, ForwardScanDirection, tupslot))
3397  {
3398  econtext->ecxt_scantuple = tupslot;
3399 
3400  if (!ExecCheck(partqualstate, econtext))
3401  ereport(ERROR,
3402  (errcode(ERRCODE_CHECK_VIOLATION),
3403  errmsg("updated partition constraint for default partition \"%s\" would be violated by some row",
3404  RelationGetRelationName(default_rel)),
3405  errtable(default_rel)));
3406 
3407  ResetExprContext(econtext);
3409  }
3410 
3411  MemoryContextSwitchTo(oldCxt);
3412  table_endscan(scan);
3413  UnregisterSnapshot(snapshot);
3415  FreeExecutorState(estate);
3416 
3417  if (RelationGetRelid(default_rel) != RelationGetRelid(part_rel))
3418  table_close(part_rel, NoLock); /* keep the lock until commit */
3419  }
3420 }
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:825
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:867
#define WARNING
Definition: elog.h:40
List * es_tupleTable
Definition: execnodes.h:603
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:4291
TupleTableSlot * ecxt_scantuple
Definition: execnodes.h:227
#define PARTITION_STRATEGY_LIST
Definition: parsenodes.h:827
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:5746
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:4082
#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:17067

◆ check_new_partition_bound()

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

Definition at line 2908 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().

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

References compare_range_bounds.

Referenced by merge_range_bounds().

2680 {
2681  /*
2682  * Check if the outer partition's upper bound is lower than the inner
2683  * partition's lower bound; if so the partitions aren't overlapping.
2684  */
2685  if (compare_range_bounds(partnatts, partsupfuncs, partcollations,
2686  outer_ub, inner_lb) < 0)
2687  {
2688  *lb_cmpval = -1;
2689  *ub_cmpval = -1;
2690  return false;
2691  }
2692 
2693  /*
2694  * Check if the outer partition's lower bound is higher than the inner
2695  * partition's upper bound; if so the partitions aren't overlapping.
2696  */
2697  if (compare_range_bounds(partnatts, partsupfuncs, partcollations,
2698  outer_lb, inner_ub) > 0)
2699  {
2700  *lb_cmpval = 1;
2701  *ub_cmpval = 1;
2702  return false;
2703  }
2704 
2705  /* All other cases indicate overlapping partitions. */
2706  *lb_cmpval = compare_range_bounds(partnatts, partsupfuncs, partcollations,
2707  outer_lb, inner_lb);
2708  *ub_cmpval = compare_range_bounds(partnatts, partsupfuncs, partcollations,
2709  outer_ub, inner_ub);
2710  return true;
2711 }
#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 4743 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().

4745 {
4746  int i;
4747  uint64 rowHash = 0;
4749 
4750  for (i = 0; i < partnatts; i++)
4751  {
4752  /* Nulls are just ignored */
4753  if (!isnull[i])
4754  {
4755  Datum hash;
4756 
4757  Assert(OidIsValid(partsupfunc[i].fn_oid));
4758 
4759  /*
4760  * Compute hash for each datum value by calling respective
4761  * datatype-specific hash functions of each partition key
4762  * attribute.
4763  */
4764  hash = FunctionCall2Coll(&partsupfunc[i], partcollation[i],
4765  values[i], seed);
4766 
4767  /* Form a single 64-bit hash value */
4768  rowHash = hash_combine64(rowHash, DatumGetUInt64(hash));
4769  }
4770  }
4771 
4772  return rowHash;
4773 }
#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:156
int i
static unsigned hash(unsigned *uv, int n)
Definition: rege_dfa.c:715

◆ 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:84
static int32 qsort_partition_hbound_cmp(const void *a, const void *b)
Definition: partbounds.c:3794
Bitmapset * interleaved_parts
Definition: partbounds.h:87
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:826
#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:505

◆ 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:84
Bitmapset * interleaved_parts
Definition: partbounds.h:87
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:99
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:98
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:827
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:3809
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:84
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:862
Bitmapset * interleaved_parts
Definition: partbounds.h:87
struct cursor * cur
Definition: ecpg.c:28
static int32 qsort_partition_rbound_cmp(const void *a, const void *b, void *arg)
Definition: partbounds.c:3826
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:3444
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:828
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:761
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:768
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 3430 of file partbounds.c.

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

3431 {
3432  Assert(bound && bound->strategy == PARTITION_STRATEGY_HASH);
3433  return bound->nindexes;
3434 }
#define PARTITION_STRATEGY_HASH
Definition: parsenodes.h:826
#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 2722 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().

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

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

Referenced by make_partition_op_expr().

3850 {
3851  Oid operoid;
3852 
3853  /*
3854  * Get the operator in the partitioning opfamily using the opclass'
3855  * declared input type as both left- and righttype.
3856  */
3857  operoid = get_opfamily_member(key->partopfamily[col],
3858  key->partopcintype[col],
3859  key->partopcintype[col],
3860  strategy);
3861  if (!OidIsValid(operoid))
3862  elog(ERROR, "missing operator %d(%u,%u) in partition opfamily %u",
3863  strategy, key->partopcintype[col], key->partopcintype[col],
3864  key->partopfamily[col]);
3865 
3866  /*
3867  * If the partition key column is not of the same type as the operator
3868  * class and not polymorphic, tell caller to wrap the non-Const expression
3869  * in a RelabelType. This matches what parse_coerce.c does.
3870  */
3871  *need_relabel = (key->parttypid[col] != key->partopcintype[col] &&
3872  key->partopcintype[col] != RECORDOID &&
3873  !IsPolymorphicType(key->partopcintype[col]));
3874 
3875  return operoid;
3876 }
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 3999 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().

4000 {
4002  FuncExpr *fexpr;
4003  Node *relidConst;
4004  Node *modulusConst;
4005  Node *remainderConst;
4006  List *args;
4007  ListCell *partexprs_item;
4008  int i;
4009 
4010  /* Fixed arguments. */
4011  relidConst = (Node *) makeConst(OIDOID,
4012  -1,
4013  InvalidOid,
4014  sizeof(Oid),
4016  false,
4017  true);
4018 
4019  modulusConst = (Node *) makeConst(INT4OID,
4020  -1,
4021  InvalidOid,
4022  sizeof(int32),
4023  Int32GetDatum(spec->modulus),
4024  false,
4025  true);
4026 
4027  remainderConst = (Node *) makeConst(INT4OID,
4028  -1,
4029  InvalidOid,
4030  sizeof(int32),
4031  Int32GetDatum(spec->remainder),
4032  false,
4033  true);
4034 
4035  args = list_make3(relidConst, modulusConst, remainderConst);
4036  partexprs_item = list_head(key->partexprs);
4037 
4038  /* Add an argument for each key column. */
4039  for (i = 0; i < key->partnatts; i++)
4040  {
4041  Node *keyCol;
4042 
4043  /* Left operand */
4044  if (key->partattrs[i] != 0)
4045  {
4046  keyCol = (Node *) makeVar(1,
4047  key->partattrs[i],
4048  key->parttypid[i],
4049  key->parttypmod[i],
4050  key->parttypcoll[i],
4051  0);
4052  }
4053  else
4054  {
4055  keyCol = (Node *) copyObject(lfirst(partexprs_item));
4056  partexprs_item = lnext(key->partexprs, partexprs_item);
4057  }
4058 
4059  args = lappend(args, keyCol);
4060  }
4061 
4062  fexpr = makeFuncExpr(F_SATISFIES_HASH_PARTITION,
4063  BOOLOID,
4064  args,
4065  InvalidOid,
4066  InvalidOid,
4068 
4069  return list_make1(fexpr);
4070 }
#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
Var * makeVar(int varno, AttrNumber varattno, Oid vartype, int32 vartypmod, Oid varcollid, Index varlevelsup)
Definition: makefuncs.c:66
Definition: nodes.h:538
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
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:654
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 4082 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().

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

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

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

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

◆ get_range_nulltest()

static List * get_range_nulltest ( PartitionKey  key)
static

Definition at line 4697 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().

4698 {
4699  List *result = NIL;
4700  NullTest *nulltest;
4701  ListCell *partexprs_item;
4702  int i;
4703 
4704  partexprs_item = list_head(key->partexprs);
4705  for (i = 0; i < key->partnatts; i++)
4706  {
4707  Expr *keyCol;
4708 
4709  if (key->partattrs[i] != 0)
4710  {
4711  keyCol = (Expr *) makeVar(1,
4712  key->partattrs[i],
4713  key->parttypid[i],
4714  key->parttypmod[i],
4715  key->parttypcoll[i],
4716  0);
4717  }
4718  else
4719  {
4720  if (partexprs_item == NULL)
4721  elog(ERROR, "wrong number of partition key expressions");
4722  keyCol = copyObject(lfirst(partexprs_item));
4723  partexprs_item = lnext(key->partexprs, partexprs_item);
4724  }
4725 
4726  nulltest = makeNode(NullTest);
4727  nulltest->arg = keyCol;
4728  nulltest->nulltesttype = IS_NOT_NULL;
4729  nulltest->argisrow = false;
4730  nulltest->location = -1;
4731  result = lappend(result, nulltest);
4732  }
4733 
4734  return result;
4735 }
#define NIL
Definition: pg_list.h:65
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:322
Var * makeVar(int varno, AttrNumber varattno, Oid vartype, int32 vartypmod, Oid varcollid, Index varlevelsup)
Definition: makefuncs.c:66
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
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:586
#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:654
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 2592 of file partbounds.c.

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

Referenced by merge_range_bounds().

2597 {
2598  int part_index;
2599 
2601 
2602  do
2603  {
2604  part_index = get_range_partition_internal(bi, lb_pos, lb, ub);
2605  if (part_index == -1)
2606  return -1;
2607  } while (is_dummy_partition(rel, part_index));
2608 
2609  return part_index;
2610 }
static int get_range_partition_internal(PartitionBoundInfo bi, int *lb_pos, PartitionRangeBound *lb, PartitionRangeBound *ub)
Definition: partbounds.c:2613
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:828

◆ get_range_partition_internal()

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

Definition at line 2613 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().

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

◆ make_one_partition_rbound()

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

Definition at line 3444 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().

3445 {
3446  PartitionRangeBound *bound;
3447  ListCell *lc;
3448  int i;
3449 
3450  Assert(datums != NIL);
3451 
3452  bound = (PartitionRangeBound *) palloc0(sizeof(PartitionRangeBound));
3453  bound->index = index;
3454  bound->datums = (Datum *) palloc0(key->partnatts * sizeof(Datum));
3455  bound->kind = (PartitionRangeDatumKind *) palloc0(key->partnatts *
3456  sizeof(PartitionRangeDatumKind));
3457  bound->lower = lower;
3458 
3459  i = 0;
3460  foreach(lc, datums)
3461  {
3463 
3464  /* What's contained in this range datum? */
3465  bound->kind[i] = datum->kind;
3466 
3467  if (datum->kind == PARTITION_RANGE_DATUM_VALUE)
3468  {
3469  Const *val = castNode(Const, datum->value);
3470 
3471  if (val->constisnull)
3472  elog(ERROR, "invalid range bound datum");
3473  bound->datums[i] = val->constvalue;
3474  }
3475 
3476  i++;
3477  }
3478 
3479  return bound;
3480 }
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:607
PartitionRangeDatumKind
Definition: parsenodes.h:862
PartitionRangeDatumKind kind
Definition: parsenodes.h:873
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 3884 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().

3886 {
3887  Oid operoid;
3888  bool need_relabel = false;
3889  Expr *result = NULL;
3890 
3891  /* Get the correct btree operator for this partitioning column */
3892  operoid = get_partition_operator(key, keynum, strategy, &need_relabel);
3893 
3894  /*
3895  * Chosen operator may be such that the non-Const operand needs to be
3896  * coerced, so apply the same; see the comment in
3897  * get_partition_operator().
3898  */
3899  if (!IsA(arg1, Const) &&
3900  (need_relabel ||
3901  key->partcollation[keynum] != key->parttypcoll[keynum]))
3902  arg1 = (Expr *) makeRelabelType(arg1,
3903  key->partopcintype[keynum],
3904  -1,
3905  key->partcollation[keynum],
3907 
3908  /* Generate the actual expression */
3909  switch (key->strategy)
3910  {
3912  {
3913  List *elems = (List *) arg2;
3914  int nelems = list_length(elems);
3915 
3916  Assert(nelems >= 1);
3917  Assert(keynum == 0);
3918 
3919  if (nelems > 1 &&
3920  !type_is_array(key->parttypid[keynum]))
3921  {
3922  ArrayExpr *arrexpr;
3923  ScalarArrayOpExpr *saopexpr;
3924 
3925  /* Construct an ArrayExpr for the right-hand inputs */
3926  arrexpr = makeNode(ArrayExpr);
3927  arrexpr->array_typeid =
3928  get_array_type(key->parttypid[keynum]);
3929  arrexpr->array_collid = key->parttypcoll[keynum];
3930  arrexpr->element_typeid = key->parttypid[keynum];
3931  arrexpr->elements = elems;
3932  arrexpr->multidims = false;
3933  arrexpr->location = -1;
3934 
3935  /* Build leftop = ANY (rightop) */
3936  saopexpr = makeNode(ScalarArrayOpExpr);
3937  saopexpr->opno = operoid;
3938  saopexpr->opfuncid = get_opcode(operoid);
3939  saopexpr->hashfuncid = InvalidOid;
3940  saopexpr->negfuncid = InvalidOid;
3941  saopexpr->useOr = true;
3942  saopexpr->inputcollid = key->partcollation[keynum];
3943  saopexpr->args = list_make2(arg1, arrexpr);
3944  saopexpr->location = -1;
3945 
3946  result = (Expr *) saopexpr;
3947  }
3948  else
3949  {
3950  List *elemops = NIL;
3951  ListCell *lc;
3952 
3953  foreach(lc, elems)
3954  {
3955  Expr *elem = lfirst(lc),
3956  *elemop;
3957 
3958  elemop = make_opclause(operoid,
3959  BOOLOID,
3960  false,
3961  arg1, elem,
3962  InvalidOid,
3963  key->partcollation[keynum]);
3964  elemops = lappend(elemops, elemop);
3965  }
3966 
3967  result = nelems > 1 ? makeBoolExpr(OR_EXPR, elemops, -1) : linitial(elemops);
3968  }
3969  break;
3970  }
3971 
3973  result = make_opclause(operoid,
3974  BOOLOID,
3975  false,
3976  arg1, arg2,
3977  InvalidOid,
3978  key->partcollation[keynum]);
3979  break;
3980 
3981  default:
3982  elog(ERROR, "invalid partitioning strategy");
3983  break;
3984  }
3985 
3986  return result;
3987 }
#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:589
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:3848
#define InvalidOid
Definition: postgres_ext.h:36
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1256
#define makeNode(_type_)
Definition: nodes.h:586
#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:827
Oid element_typeid
Definition: primnodes.h:1036
#define PARTITION_STRATEGY_RANGE
Definition: parsenodes.h:828
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:84
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:99
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:697
struct PartitionBoundInfoData * boundinfo
Definition: pathnodes.h:764
#define partition_bound_accepts_nulls(bi)
Definition: partbounds.h:98
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:827
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:2722
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:2673
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:2786
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:99
static void cleanup(void)
Definition: bootstrap.c:697
struct PartitionBoundInfoData * boundinfo
Definition: pathnodes.h:764
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:828
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:2592

◆ 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:84
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:74
PartitionRangeDatumKind
Definition: parsenodes.h:862
Bitmapset * interleaved_parts
Definition: partbounds.h:87
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:826
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:827
#define PARTITION_STRATEGY_RANGE
Definition: parsenodes.h:828
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:826
#define Assert(condition)
Definition: c.h:804