From: Guancheng Luo <prajnamort@gmail.com>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: pgsql-hackers@lists.postgresql.org
Subject: Re: [PATCH] Check operator when creating unique index on partition table
Date: Sat, 28 Mar 2020 11:28:41 +0800
Message-ID: <B63F443B-C5CC-4590-8011-EF3E5BB17823@gmail.com> (raw)
In-Reply-To: <4731.1585155636@sss.pgh.pa.us>
References: <D9C3CEF7-04E8-47A1-8300-CA1DCD5ED40D@gmail.com>
<4731.1585155636@sss.pgh.pa.us>
> On Mar 26, 2020, at 01:00, Tom Lane <tgl@sss.pgh.pa.us> wrote:
>
> Guancheng Luo <prajnamort@gmail.com> writes:
>> I found that things could go wrong in some cases, when the unique index and the partition key use different opclass.
>
> I agree that this is an oversight, but it seems like your solution is
> overcomplicated and probably still too forgiving. Should we not just
> insist that the index opfamily match the partition key opfamily?
> It looks to me like that would reduce the code change to about like
> this:
>
> - if (key->partattrs[i] == indexInfo->ii_IndexAttrNumbers[j])
> + if (key->partattrs[i] == indexInfo->ii_IndexAttrNumbers[j] &&
> + key->partopfamily[i] == get_opclass_family(classObjectId[j]))
>
> which is a lot more straightforward and provides a lot more certainty
> that the index will act as the partition constraint demands.
>
> This would reject, for example, a hash index associated with a btree-based
> partition constraint, but I'm not sure we're losing anything much thereby.
> (I do not think your patch is correct for the case where the opfamilies
> belong to different AMs, anyway.)
Since unique index cannot be using HASH, I think we only need to consider BTREE index here.
There is cases when a BTREE index associated with a HASH partition key, but I think we should allow them,
as long as their equality operators consider the same value as equal.
I’ve added some more test for this case.
> I'm not really on board with adding a whole new test script for this,
> either.
Indeed, I think `indexing.sql` might be more apporiate. I moved these tests in my new patch.
Best Regards,
Guancheng Luo
Attachments:0001-Check-operator-when-creating-UNIQUE-index-on-PARTITI.patch (application/octet-stream, 13.8K) download
From 29b6590e5946a09e2cc04ecb8c8bfaa8d9b0288e Mon Sep 17 00:00:00 2001
From: prajnamort <prajnamort@gmail.com>
Date: Wed, 25 Mar 2020 15:50:01 +0800
Subject: [PATCH] Check operator when creating UNIQUE index on PARTITION table
Currently, we only check if every column in the partition key
is included in the unique index. But this is not enough when
custom opclass exists. Since there can be cases, that some
values belong to different partitions should be considered
equal according to unique index's opclass.
So, we should check the compatiblity of equality operator
between partition key's opclass and index column's opclass.
Only if they are the same operator should we consider them as
compatible.
---
src/backend/commands/indexcmds.c | 34 +++++++++-
src/test/regress/expected/indexing.out | 114 +++++++++++++++++++++++++++++++++
src/test/regress/sql/indexing.sql | 107 +++++++++++++++++++++++++++++++
3 files changed, 253 insertions(+), 2 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.cindex 4e8263af4b..e7c3960011 100644--- a/src/backend/commands/indexcmds.c+++ b/src/backend/commands/indexcmds.c@@ -877,8 +877,38 @@ DefineIndex(Oid relationId,
{
if (key->partattrs[i] == indexInfo->ii_IndexAttrNumbers[j])
{
- found = true;- break;+ /*+ * Check the equality operator in both the index's operator+ * class and the partition key's operator class. They must+ * be the same operator for us to consider them compatible.+ */+ int ptkey_eq_strategy;+ Oid ptkey_eqop;+ Oid idxcol_opclass;+ Oid idxcol_opfamily;+ Oid idxcol_opclass_intype;+ Oid idxcol_eqop;++ if (key->strategy == PARTITION_STRATEGY_HASH)+ ptkey_eq_strategy = HTEqualStrategyNumber;+ else+ ptkey_eq_strategy = BTEqualStrategyNumber;+ ptkey_eqop = get_opfamily_member(key->partopfamily[i],+ key->partopcintype[i],+ key->partopcintype[i],+ ptkey_eq_strategy);+ idxcol_opclass = classObjectId[j];+ idxcol_opfamily = get_opclass_family(idxcol_opclass);+ idxcol_opclass_intype = get_opclass_input_type(idxcol_opclass);+ idxcol_eqop = get_opfamily_member(idxcol_opfamily,+ idxcol_opclass_intype,+ idxcol_opclass_intype,+ BTEqualStrategyNumber);+ if (ptkey_eqop == idxcol_eqop)+ {+ found = true;+ break;+ }
}
}
if (!found)
diff --git a/src/test/regress/expected/indexing.out b/src/test/regress/expected/indexing.outindex f78865ef81..ffba7fbd83 100644--- a/src/test/regress/expected/indexing.out+++ b/src/test/regress/expected/indexing.out@@ -1336,3 +1336,117 @@ Indexes:
"parted_index_col_drop11_b_idx" btree (b)
drop table parted_index_col_drop;
+--+-- Test for checking operator class compatibility between partition key and+-- unique index. If they are imcompatible, the unique index should not be+-- allowed to created.+--+CREATE FUNCTION abscmp(int, int) RETURNS INT AS $$+begin return btint4cmp(abs($1),abs($2)); end;+$$ LANGUAGE plpgsql STRICT IMMUTABLE;+CREATE FUNCTION abshashint4(int) RETURNS INT AS $$+begin return hashint4(abs($1)); end;+$$ LANGUAGE plpgsql STRICT IMMUTABLE;+CREATE FUNCTION abshashint4extended(int, bigint) RETURNS BIGINT AS $$+begin return hashint4extended(abs($1),$2); end;+$$ LANGUAGE plpgsql STRICT IMMUTABLE;+CREATE FUNCTION abslt(int, int) RETURNS BOOL AS $$+begin return abscmp($1,$2) < 0; end;+$$ LANGUAGE plpgsql STRICT IMMUTABLE;+CREATE FUNCTION abslte(int, int) RETURNS BOOL AS $$+begin return abscmp($1,$2) <= 0; end;+$$ LANGUAGE plpgsql STRICT IMMUTABLE;+CREATE FUNCTION abseq(int, int) RETURNS BOOL AS $$+begin return abscmp($1,$2) = 0; end;+$$ LANGUAGE plpgsql STRICT IMMUTABLE;+CREATE FUNCTION absgte(int, int) RETURNS BOOL AS $$+begin return abscmp($1,$2) >= 0; end;+$$ LANGUAGE plpgsql STRICT IMMUTABLE;+CREATE FUNCTION absgt(int, int) RETURNS BOOL AS $$+begin return abscmp($1,$2) > 0; end;+$$ LANGUAGE plpgsql STRICT IMMUTABLE;+CREATE OPERATOR |<| (PROCEDURE = abslt, LEFTARG = int, RIGHTARG = int, COMMUTATOR = |>|, HASHES);+CREATE OPERATOR |<=| (PROCEDURE = abslt, LEFTARG = int, RIGHTARG = int, COMMUTATOR = |>=|, HASHES);+CREATE OPERATOR |=| (PROCEDURE = abseq, LEFTARG = int, RIGHTARG = int, COMMUTATOR = |=|, HASHES, MERGES);+CREATE OPERATOR |>=| (PROCEDURE = abslt, LEFTARG = int, RIGHTARG = int, COMMUTATOR = |<=|, HASHES);+CREATE OPERATOR |>| (PROCEDURE = abslt, LEFTARG = int, RIGHTARG = int, COMMUTATOR = |<|, HASHES);+CREATE OPERATOR CLASS abs_int_btree_ops FOR TYPE int4+ USING btree AS+ OPERATOR 1 |<|,+ OPERATOR 2 |<=|,+ OPERATOR 3 |=|,+ OPERATOR 4 |>=|,+ OPERATOR 5 |>|,+ FUNCTION 1 abscmp(int, int);+CREATE OPERATOR CLASS abs_int_hash_ops FOR TYPE int4+ USING hash AS+ OPERATOR 1 |=|,+ FUNCTION 1 abshashint4(int),+ FUNCTION 2 abshashint4extended(int, bigint);+-- default BTREE partition & default BTREE index+CREATE TABLE ptop_test (a int, b int, c int) PARTITION BY LIST (a);+CREATE TABLE ptop_test_0 PARTITION OF ptop_test FOR VALUES IN ('0');+CREATE TABLE ptop_test_p1 PARTITION OF ptop_test FOR VALUES IN ('1');+CREATE TABLE ptop_test_m1 PARTITION OF ptop_test FOR VALUES IN ('-1');+CREATE UNIQUE INDEX ptop_test_unq_abs_a ON ptop_test (a abs_int_btree_ops); -- imcompatible+ERROR: insufficient columns in UNIQUE constraint definition+DETAIL: UNIQUE constraint on table "ptop_test" lacks column "a" which is part of the partition key.+CREATE UNIQUE INDEX ptop_test_unq_a ON ptop_test (a); -- compatible+DROP INDEX ptop_test_unq_a;+DROP TABLE ptop_test;+-- custom BTREE partition & custom BTREE index+CREATE TABLE ptop_test (a int, b int, c int) PARTITION BY LIST (a abs_int_btree_ops);+CREATE TABLE ptop_test_0 PARTITION OF ptop_test FOR VALUES IN ('0');+CREATE TABLE ptop_test_p1 PARTITION OF ptop_test FOR VALUES IN ('1', '-1');+CREATE UNIQUE INDEX ptop_test_unq_a ON ptop_test (a); -- imcompatible+ERROR: insufficient columns in UNIQUE constraint definition+DETAIL: UNIQUE constraint on table "ptop_test" lacks column "a" which is part of the partition key.+CREATE UNIQUE INDEX ptop_test_unq_abs_a ON ptop_test (a abs_int_btree_ops); -- compatible+DROP INDEX ptop_test_unq_abs_a;+DROP TABLE ptop_test;+-- default HASH partition & default BTREE index+CREATE TABLE ptop_test (a int, b int, c int) PARTITION BY HASH (a);+CREATE TABLE ptop_test_p0 PARTITION OF ptop_test+ FOR VALUES WITH (MODULUS 3, REMAINDER 0);+CREATE TABLE ptop_test_p1 PARTITION OF ptop_test+ FOR VALUES WITH (MODULUS 3, REMAINDER 1);+CREATE TABLE ptop_test_p2 PARTITION OF ptop_test+ FOR VALUES WITH (MODULUS 3, REMAINDER 2);+CREATE UNIQUE INDEX ptop_test_unq_abs_a ON ptop_test (a abs_int_btree_ops); -- imcompatible+ERROR: insufficient columns in UNIQUE constraint definition+DETAIL: UNIQUE constraint on table "ptop_test" lacks column "a" which is part of the partition key.+CREATE UNIQUE INDEX ptop_test_unq_a ON ptop_test (a); -- compatible+DROP INDEX ptop_test_unq_a;+DROP TABLE ptop_test;+-- custom HASH partition & custom BTREE index+CREATE TABLE ptop_test (a int, b int, c int) PARTITION BY HASH (a abs_int_hash_ops);+CREATE TABLE ptop_test_p0 PARTITION OF ptop_test+ FOR VALUES WITH (MODULUS 3, REMAINDER 0);+CREATE TABLE ptop_test_p1 PARTITION OF ptop_test+ FOR VALUES WITH (MODULUS 3, REMAINDER 1);+CREATE TABLE ptop_test_p2 PARTITION OF ptop_test+ FOR VALUES WITH (MODULUS 3, REMAINDER 2);+CREATE UNIQUE INDEX ptop_test_unq_a ON ptop_test (a); -- imcompatible+ERROR: insufficient columns in UNIQUE constraint definition+DETAIL: UNIQUE constraint on table "ptop_test" lacks column "a" which is part of the partition key.+CREATE UNIQUE INDEX ptop_test_unq_abs_a ON ptop_test (a abs_int_btree_ops); -- compatible+DROP INDEX ptop_test_unq_abs_a;+DROP TABLE ptop_test;+-- Clean up+DROP OPERATOR CLASS abs_int_btree_ops USING btree;+DROP OPERATOR FAMILY abs_int_btree_ops USING btree;+DROP OPERATOR CLASS abs_int_hash_ops USING hash;+DROP OPERATOR FAMILY abs_int_hash_ops USING hash;+DROP OPERATOR |<| (int, int);+DROP OPERATOR |<=| (int, int);+DROP OPERATOR |=| (int, int);+DROP OPERATOR |>=| (int, int);+DROP OPERATOR |>| (int, int);+DROP FUNCTION abscmp(int, int);+DROP FUNCTION abslt(int, int);+DROP FUNCTION abslte(int, int);+DROP FUNCTION abseq(int, int);+DROP FUNCTION absgte(int, int);+DROP FUNCTION absgt(int, int);+DROP FUNCTION abshashint4(int);+DROP FUNCTION abshashint4extended(int, bigint);diff --git a/src/test/regress/sql/indexing.sql b/src/test/regress/sql/indexing.sqlindex 35d159f41b..8dcf3d9c92 100644--- a/src/test/regress/sql/indexing.sql+++ b/src/test/regress/sql/indexing.sql@@ -734,3 +734,110 @@ alter table parted_index_col_drop drop column c;
\d parted_index_col_drop2
\d parted_index_col_drop11
drop table parted_index_col_drop;
++--+-- Test for checking operator class compatibility between partition key and+-- unique index. If they are imcompatible, the unique index should not be+-- allowed to created.+--+CREATE FUNCTION abscmp(int, int) RETURNS INT AS $$+begin return btint4cmp(abs($1),abs($2)); end;+$$ LANGUAGE plpgsql STRICT IMMUTABLE;+CREATE FUNCTION abshashint4(int) RETURNS INT AS $$+begin return hashint4(abs($1)); end;+$$ LANGUAGE plpgsql STRICT IMMUTABLE;+CREATE FUNCTION abshashint4extended(int, bigint) RETURNS BIGINT AS $$+begin return hashint4extended(abs($1),$2); end;+$$ LANGUAGE plpgsql STRICT IMMUTABLE;+CREATE FUNCTION abslt(int, int) RETURNS BOOL AS $$+begin return abscmp($1,$2) < 0; end;+$$ LANGUAGE plpgsql STRICT IMMUTABLE;+CREATE FUNCTION abslte(int, int) RETURNS BOOL AS $$+begin return abscmp($1,$2) <= 0; end;+$$ LANGUAGE plpgsql STRICT IMMUTABLE;+CREATE FUNCTION abseq(int, int) RETURNS BOOL AS $$+begin return abscmp($1,$2) = 0; end;+$$ LANGUAGE plpgsql STRICT IMMUTABLE;+CREATE FUNCTION absgte(int, int) RETURNS BOOL AS $$+begin return abscmp($1,$2) >= 0; end;+$$ LANGUAGE plpgsql STRICT IMMUTABLE;+CREATE FUNCTION absgt(int, int) RETURNS BOOL AS $$+begin return abscmp($1,$2) > 0; end;+$$ LANGUAGE plpgsql STRICT IMMUTABLE;+CREATE OPERATOR |<| (PROCEDURE = abslt, LEFTARG = int, RIGHTARG = int, COMMUTATOR = |>|, HASHES);+CREATE OPERATOR |<=| (PROCEDURE = abslt, LEFTARG = int, RIGHTARG = int, COMMUTATOR = |>=|, HASHES);+CREATE OPERATOR |=| (PROCEDURE = abseq, LEFTARG = int, RIGHTARG = int, COMMUTATOR = |=|, HASHES, MERGES);+CREATE OPERATOR |>=| (PROCEDURE = abslt, LEFTARG = int, RIGHTARG = int, COMMUTATOR = |<=|, HASHES);+CREATE OPERATOR |>| (PROCEDURE = abslt, LEFTARG = int, RIGHTARG = int, COMMUTATOR = |<|, HASHES);+CREATE OPERATOR CLASS abs_int_btree_ops FOR TYPE int4+ USING btree AS+ OPERATOR 1 |<|,+ OPERATOR 2 |<=|,+ OPERATOR 3 |=|,+ OPERATOR 4 |>=|,+ OPERATOR 5 |>|,+ FUNCTION 1 abscmp(int, int);+CREATE OPERATOR CLASS abs_int_hash_ops FOR TYPE int4+ USING hash AS+ OPERATOR 1 |=|,+ FUNCTION 1 abshashint4(int),+ FUNCTION 2 abshashint4extended(int, bigint);+-- default BTREE partition & default BTREE index+CREATE TABLE ptop_test (a int, b int, c int) PARTITION BY LIST (a);+CREATE TABLE ptop_test_0 PARTITION OF ptop_test FOR VALUES IN ('0');+CREATE TABLE ptop_test_p1 PARTITION OF ptop_test FOR VALUES IN ('1');+CREATE TABLE ptop_test_m1 PARTITION OF ptop_test FOR VALUES IN ('-1');+CREATE UNIQUE INDEX ptop_test_unq_abs_a ON ptop_test (a abs_int_btree_ops); -- imcompatible+CREATE UNIQUE INDEX ptop_test_unq_a ON ptop_test (a); -- compatible+DROP INDEX ptop_test_unq_a;+DROP TABLE ptop_test;+-- custom BTREE partition & custom BTREE index+CREATE TABLE ptop_test (a int, b int, c int) PARTITION BY LIST (a abs_int_btree_ops);+CREATE TABLE ptop_test_0 PARTITION OF ptop_test FOR VALUES IN ('0');+CREATE TABLE ptop_test_p1 PARTITION OF ptop_test FOR VALUES IN ('1', '-1');+CREATE UNIQUE INDEX ptop_test_unq_a ON ptop_test (a); -- imcompatible+CREATE UNIQUE INDEX ptop_test_unq_abs_a ON ptop_test (a abs_int_btree_ops); -- compatible+DROP INDEX ptop_test_unq_abs_a;+DROP TABLE ptop_test;+-- default HASH partition & default BTREE index+CREATE TABLE ptop_test (a int, b int, c int) PARTITION BY HASH (a);+CREATE TABLE ptop_test_p0 PARTITION OF ptop_test+ FOR VALUES WITH (MODULUS 3, REMAINDER 0);+CREATE TABLE ptop_test_p1 PARTITION OF ptop_test+ FOR VALUES WITH (MODULUS 3, REMAINDER 1);+CREATE TABLE ptop_test_p2 PARTITION OF ptop_test+ FOR VALUES WITH (MODULUS 3, REMAINDER 2);+CREATE UNIQUE INDEX ptop_test_unq_abs_a ON ptop_test (a abs_int_btree_ops); -- imcompatible+CREATE UNIQUE INDEX ptop_test_unq_a ON ptop_test (a); -- compatible+DROP INDEX ptop_test_unq_a;+DROP TABLE ptop_test;+-- custom HASH partition & custom BTREE index+CREATE TABLE ptop_test (a int, b int, c int) PARTITION BY HASH (a abs_int_hash_ops);+CREATE TABLE ptop_test_p0 PARTITION OF ptop_test+ FOR VALUES WITH (MODULUS 3, REMAINDER 0);+CREATE TABLE ptop_test_p1 PARTITION OF ptop_test+ FOR VALUES WITH (MODULUS 3, REMAINDER 1);+CREATE TABLE ptop_test_p2 PARTITION OF ptop_test+ FOR VALUES WITH (MODULUS 3, REMAINDER 2);+CREATE UNIQUE INDEX ptop_test_unq_a ON ptop_test (a); -- imcompatible+CREATE UNIQUE INDEX ptop_test_unq_abs_a ON ptop_test (a abs_int_btree_ops); -- compatible+DROP INDEX ptop_test_unq_abs_a;+DROP TABLE ptop_test;+-- Clean up+DROP OPERATOR CLASS abs_int_btree_ops USING btree;+DROP OPERATOR FAMILY abs_int_btree_ops USING btree;+DROP OPERATOR CLASS abs_int_hash_ops USING hash;+DROP OPERATOR FAMILY abs_int_hash_ops USING hash;+DROP OPERATOR |<| (int, int);+DROP OPERATOR |<=| (int, int);+DROP OPERATOR |=| (int, int);+DROP OPERATOR |>=| (int, int);+DROP OPERATOR |>| (int, int);+DROP FUNCTION abscmp(int, int);+DROP FUNCTION abslt(int, int);+DROP FUNCTION abslte(int, int);+DROP FUNCTION abseq(int, int);+DROP FUNCTION absgte(int, int);+DROP FUNCTION absgt(int, int);+DROP FUNCTION abshashint4(int);+DROP FUNCTION abshashint4extended(int, bigint);--
2.11.0
=
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-hackers@postgresql.org
Cc: prajnamort@gmail.com, tgl@sss.pgh.pa.us
Subject: Re: [PATCH] Check operator when creating unique index on partition table
In-Reply-To: <B63F443B-C5CC-4590-8011-EF3E5BB17823@gmail.com>
References: <D9C3CEF7-04E8-47A1-8300-CA1DCD5ED40D@gmail.com> <4731.1585155636@sss.pgh.pa.us> <B63F443B-C5CC-4590-8011-EF3E5BB17823@gmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox