Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit54a9406

Browse files
committed
Do not return NULL for error cases in satisfies_hash_partition().
Since this function is used as a CHECK constraint condition,returning NULL is tantamount to returning TRUE, which would have theeffect of letting in a row that doesn't satisfy the hash condition.Admittedly, the cases for which this is done should be unreachablein practice, but that doesn't make it any less a bad idea. It alsoseems like a dartboard was used to decide which error cases shouldthrow errors as opposed to returning NULL.For the checks for NULL input values, I just switched it to returningfalse. There's some argument that an error would be better; but thecase really should be can't-happen in a generated hash constraint,so it's likely not worth more code for.For the parent-relation-open-failure case, it seems like we mightas well let relation_open throw an error, instead of having animpossible-to-diagnose constraint failure.Back-patch to v11 where this code came in.Discussion:https://postgr.es/m/24067.1605134819@sss.pgh.pa.us
1 parent029fa66 commit54a9406

File tree

2 files changed

+10
-15
lines changed

2 files changed

+10
-15
lines changed

‎src/backend/partitioning/partbounds.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2778,6 +2778,8 @@ compute_partition_hash_value(int partnatts, FmgrInfo *partsupfunc, Oid *partcoll
27782778
*
27792779
* Returns true if remainder produced when this computed single hash value is
27802780
* divided by the given modulus is equal to given remainder, otherwise false.
2781+
* NB: it's important that this never return null, as the constraint machinery
2782+
* would consider that to be a "pass".
27812783
*
27822784
* See get_qual_for_hash() for usage.
27832785
*/
@@ -2802,9 +2804,9 @@ satisfies_hash_partition(PG_FUNCTION_ARGS)
28022804
ColumnsHashData*my_extra;
28032805
uint64rowHash=0;
28042806

2805-
/* Returnnull if the parent OID, modulus, or remainder is NULL. */
2807+
/* Returnfalse if the parent OID, modulus, or remainder is NULL. */
28062808
if (PG_ARGISNULL(0)||PG_ARGISNULL(1)||PG_ARGISNULL(2))
2807-
PG_RETURN_NULL();
2809+
PG_RETURN_BOOL(false);
28082810
parentId=PG_GETARG_OID(0);
28092811
modulus=PG_GETARG_INT32(1);
28102812
remainder=PG_GETARG_INT32(2);
@@ -2833,15 +2835,12 @@ satisfies_hash_partition(PG_FUNCTION_ARGS)
28332835
PartitionKeykey;
28342836
intj;
28352837

2836-
/* Open parent relation and fetch partition keyinfo */
2837-
parent=try_relation_open(parentId,AccessShareLock);
2838-
if (parent==NULL)
2839-
PG_RETURN_NULL();
2838+
/* Open parent relation and fetch partition key info */
2839+
parent=relation_open(parentId,AccessShareLock);
28402840
key=RelationGetPartitionKey(parent);
28412841

28422842
/* Reject parent table that is not hash-partitioned. */
2843-
if (parent->rd_rel->relkind!=RELKIND_PARTITIONED_TABLE||
2844-
key->strategy!=PARTITION_STRATEGY_HASH)
2843+
if (key==NULL||key->strategy!=PARTITION_STRATEGY_HASH)
28452844
ereport(ERROR,
28462845
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
28472846
errmsg("\"%s\" is not a hash partitioned table",

‎src/test/regress/expected/hash_part.out

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,7 @@ CREATE TABLE mchash1
1010
PARTITION OF mchash FOR VALUES WITH (MODULUS 4, REMAINDER 0);
1111
-- invalid OID, no such table
1212
SELECT satisfies_hash_partition(0, 4, 0, NULL);
13-
satisfies_hash_partition
14-
--------------------------
15-
16-
(1 row)
17-
13+
ERROR: could not open relation with OID 0
1814
-- not partitioned
1915
SELECT satisfies_hash_partition('tenk1'::regclass, 4, 0, NULL);
2016
ERROR: "tenk1" is not a hash partitioned table
@@ -34,14 +30,14 @@ ERROR: remainder for hash partition must be less than modulus
3430
SELECT satisfies_hash_partition('mchash'::regclass, NULL, 0, NULL);
3531
satisfies_hash_partition
3632
--------------------------
37-
33+
f
3834
(1 row)
3935

4036
-- remainder is null
4137
SELECT satisfies_hash_partition('mchash'::regclass, 4, NULL, NULL);
4238
satisfies_hash_partition
4339
--------------------------
44-
40+
f
4541
(1 row)
4642

4743
-- too many arguments

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp