Skip to content

Commit a7b9ea3

Browse files
committed
make check constraint machinery aware of different TupleDescs (part_attno)
1 parent 39a2274 commit a7b9ea3

File tree

3 files changed

+33
-14
lines changed

3 files changed

+33
-14
lines changed

src/init.c

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,18 @@ static int cmp_range_entries(const void *p1, const void *p2, void *arg);
7777

7878
static bool validate_range_constraint(const Expr *expr,
7979
const PartRelationInfo *prel,
80+
const AttrNumber part_attno,
8081
Datum *min,
8182
Datum *max);
8283

8384
static bool validate_hash_constraint(const Expr *expr,
8485
const PartRelationInfo *prel,
86+
const AttrNumber part_attno,
8587
uint32 *part_hash);
8688

8789
static bool read_opexpr_const(const OpExpr *opexpr,
8890
const PartRelationInfo *prel,
91+
const AttrNumber part_attno,
8992
Datum *val);
9093

9194
static int oid_cmp(const void *p1, const void *p2);
@@ -347,6 +350,7 @@ init_shmem_config(void)
347350
void
348351
fill_prel_with_partitions(const Oid *partitions,
349352
const uint32 parts_count,
353+
const char *part_column_name,
350354
PartRelationInfo *prel)
351355
{
352356
uint32 i;
@@ -361,7 +365,18 @@ fill_prel_with_partitions(const Oid *partitions,
361365

362366
for (i = 0; i < PrelChildrenCount(prel); i++)
363367
{
364-
con_expr = get_partition_constraint_expr(partitions[i], prel->attnum);
368+
AttrNumber part_attno;
369+
370+
/* NOTE: Partitions may have different TupleDescs */
371+
part_attno = get_attnum(partitions[i], part_column_name);
372+
373+
/* Raise ERROR if there's no such column */
374+
if (part_attno == InvalidAttrNumber)
375+
elog(ERROR, "partition \"%s\" has no column \"%s\"",
376+
get_rel_name_or_relid(partitions[i]),
377+
part_column_name);
378+
379+
con_expr = get_partition_constraint_expr(partitions[i], part_attno);
365380

366381
/* Perform a partitioning_type-dependent task */
367382
switch (prel->parttype)
@@ -370,13 +385,13 @@ fill_prel_with_partitions(const Oid *partitions,
370385
{
371386
uint32 hash; /* hash value < parts_count */
372387

373-
if (validate_hash_constraint(con_expr, prel, &hash))
388+
if (validate_hash_constraint(con_expr, prel, part_attno, &hash))
374389
prel->children[hash] = partitions[i];
375390
else
376391
{
377392
DisablePathman(); /* disable pg_pathman since config is broken */
378393
ereport(ERROR,
379-
(errmsg("Wrong constraint format for HASH partition \"%s\"",
394+
(errmsg("wrong constraint format for HASH partition \"%s\"",
380395
get_rel_name_or_relid(partitions[i])),
381396
errhint(INIT_ERROR_HINT)));
382397
}
@@ -387,7 +402,7 @@ fill_prel_with_partitions(const Oid *partitions,
387402
{
388403
Datum range_min, range_max;
389404

390-
if (validate_range_constraint(con_expr, prel,
405+
if (validate_range_constraint(con_expr, prel, part_attno,
391406
&range_min, &range_max))
392407
{
393408
prel->ranges[i].child_oid = partitions[i];
@@ -398,7 +413,7 @@ fill_prel_with_partitions(const Oid *partitions,
398413
{
399414
DisablePathman(); /* disable pg_pathman since config is broken */
400415
ereport(ERROR,
401-
(errmsg("Wrong constraint format for RANGE partition \"%s\"",
416+
(errmsg("wrong constraint format for RANGE partition \"%s\"",
402417
get_rel_name_or_relid(partitions[i])),
403418
errhint(INIT_ERROR_HINT)));
404419
}
@@ -905,6 +920,7 @@ cmp_range_entries(const void *p1, const void *p2, void *arg)
905920
static bool
906921
validate_range_constraint(const Expr *expr,
907922
const PartRelationInfo *prel,
923+
const AttrNumber part_attno,
908924
Datum *min,
909925
Datum *max)
910926
{
@@ -928,23 +944,21 @@ validate_range_constraint(const Expr *expr,
928944

929945
if (strategy == BTGreaterEqualStrategyNumber)
930946
{
931-
if (!read_opexpr_const(opexpr, prel, min))
947+
if (!read_opexpr_const(opexpr, prel, part_attno, min))
932948
return false;
933949
}
934-
else
935-
return false;
950+
else return false;
936951

937952
/* check that right operand is < operator */
938953
opexpr = (OpExpr *) lsecond(boolexpr->args);
939954
strategy = get_op_opfamily_strategy(opexpr->opno, tce->btree_opf);
940955

941956
if (strategy == BTLessStrategyNumber)
942957
{
943-
if (!read_opexpr_const(opexpr, prel, max))
958+
if (!read_opexpr_const(opexpr, prel, part_attno, max))
944959
return false;
945960
}
946-
else
947-
return false;
961+
else return false;
948962

949963
return true;
950964
}
@@ -957,6 +971,7 @@ validate_range_constraint(const Expr *expr,
957971
static bool
958972
read_opexpr_const(const OpExpr *opexpr,
959973
const PartRelationInfo *prel,
974+
const AttrNumber part_attno,
960975
Datum *val)
961976
{
962977
const Node *left;
@@ -990,7 +1005,7 @@ read_opexpr_const(const OpExpr *opexpr,
9901005
else return false;
9911006

9921007
/* VAR.attno == partitioned attribute number */
993-
if (part_attr->varoattno != prel->attnum)
1008+
if (part_attr->varoattno != part_attno)
9941009
return false;
9951010

9961011
/* CONST is NOT NULL */
@@ -1026,6 +1041,7 @@ read_opexpr_const(const OpExpr *opexpr,
10261041
static bool
10271042
validate_hash_constraint(const Expr *expr,
10281043
const PartRelationInfo *prel,
1044+
const AttrNumber part_attno,
10291045
uint32 *part_hash)
10301046
{
10311047
const TypeCacheEntry *tce;
@@ -1079,7 +1095,7 @@ validate_hash_constraint(const Expr *expr,
10791095
var = (Var *) linitial(type_hash_proc_expr->args);
10801096

10811097
/* Check that 'var' is the partitioning key attribute */
1082-
if (var->varoattno != prel->attnum)
1098+
if (var->varoattno != part_attno)
10831099
return false;
10841100

10851101
/* Check that PARTITIONS_COUNT is equal to total amount of partitions */

src/init.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ void unload_config(void);
122122

123123
void fill_prel_with_partitions(const Oid *partitions,
124124
const uint32 parts_count,
125+
const char *part_column_name,
125126
PartRelationInfo *prel);
126127

127128
/* Result of find_inheritance_children_array() */

src/relation_info.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,9 @@ refresh_pathman_relation_info(Oid relid,
197197
* will try to refresh it again (and again), until the error is fixed
198198
* by user manually (i.e. invalid check constraints etc).
199199
*/
200-
fill_prel_with_partitions(prel_children, prel_children_count, prel);
200+
fill_prel_with_partitions(prel_children,
201+
prel_children_count,
202+
part_column_name, prel);
201203

202204
/* Peform some actions for each child */
203205
for (i = 0; i < prel_children_count; i++)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy