pg.ddx.io pgsql-hackers@postgresql.org mailing list archive
help / color / mirror / Atom feed[PATCH] Check operator when creating unique index on partition table
4+ messages / 2 participants
[nested] [flat]
Commits from this thread:
2020-04-01 501b018799 76ea606968 76ddc88f4d Check equality semantics for unique indexes on partitioned tables.
* [PATCH] Check operator when creating unique index on partition table
@ 2020-03-25 10:43 Guancheng Luo <prajnamort@gmail.com>
2020-03-25 17:00 ` Re: [PATCH] Check operator when creating unique index on partition table Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 4+ messages in thread
From: Guancheng Luo @ 2020-03-25 10:43 UTC (permalink / raw)
To: pgsql-hackers@lists.postgresql.org
Hi,
I found that things could go wrong in some cases, when the unique index and the partition key use different opclass.
For example:
```
CREATE TABLE ptop_test (a int, b int, c int) PARTITION BY LIST (a);
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); -- this should fail
```
In this example, `abs_int_btree_ops` is a opclass whose equality operator is customized to consider ‘-1’ and ‘1’ as equal.
So the unique index should not be allowed to create, since ‘-1’ and ‘1’ will be put in different partition.
The attached patch should fix this problem.
Best Regards,
Guancheng Luo
Attachments:
0001-Check-operator-when-creating-UNIQUE-index-on-PARTITI.patch (application/octet-stream, 10.6K) download
From ec60e592dc653651f410d5cc7927d110a90d3b92 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/partition_opclass.out | 69 ++++++++++++++++++++++++
src/test/regress/parallel_schedule | 2 +-
src/test/regress/serial_schedule | 1 +
src/test/regress/sql/partition_opclass.sql | 70 +++++++++++++++++++++++++
5 files changed, 173 insertions(+), 3 deletions(-)
create mode 100644 src/test/regress/expected/partition_opclass.out
create mode 100644 src/test/regress/sql/partition_opclass.sql
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 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/partition_opclass.out b/src/test/regress/expected/partition_opclass.out
new file mode 100644
index 0000000000..27e0664de3
--- /dev/null
+++ b/src/test/regress/expected/partition_opclass.out
@@ -0,0 +1,69 @@
+--
+-- 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 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 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); -- should fail
+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);
+DROP INDEX ptop_test_unq_a;
+DROP TABLE ptop_test;
+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); -- should fail
+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);
+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 |<| (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);
diff --git a/src/test/regress/parallel_schedule b/src/test/regress/parallel_schedule
index d2b17dd3ea..ac6919a8bc 100644
--- a/src/test/regress/parallel_schedule
+++ b/src/test/regress/parallel_schedule
@@ -112,7 +112,7 @@ test: plancache limit plpgsql copy2 temp domain rangefuncs prepare conversion tr
# ----------
# Another group of parallel tests
# ----------
-test: partition_join partition_prune reloptions hash_part indexing partition_aggregate partition_info tuplesort explain
+test: partition_join partition_prune reloptions hash_part indexing partition_aggregate partition_info tuplesort explain partition_opclass
# event triggers cannot run concurrently with any test that runs DDL
test: event_trigger
diff --git a/src/test/regress/serial_schedule b/src/test/regress/serial_schedule
index acba391332..b63bff2a05 100644
--- a/src/test/regress/serial_schedule
+++ b/src/test/regress/serial_schedule
@@ -197,3 +197,4 @@ test: explain
test: event_trigger
test: fast_default
test: stats
+test: partition_opclass
\ No newline at end of file
diff --git a/src/test/regress/sql/partition_opclass.sql b/src/test/regress/sql/partition_opclass.sql
new file mode 100644
index 0000000000..1ad279f531
--- /dev/null
+++ b/src/test/regress/sql/partition_opclass.sql
@@ -0,0 +1,70 @@
+--
+-- 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 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 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); -- should fail
+CREATE UNIQUE INDEX ptop_test_unq_a ON ptop_test (a);
+DROP INDEX ptop_test_unq_a;
+DROP TABLE ptop_test;
+
+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); -- should fail
+CREATE UNIQUE INDEX ptop_test_unq_abs_a ON ptop_test (a abs_int_btree_ops);
+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 |<| (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);
--
2.11.0
=
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: [PATCH] Check operator when creating unique index on partition table
2020-03-25 10:43 [PATCH] Check operator when creating unique index on partition table Guancheng Luo <prajnamort@gmail.com>
@ 2020-03-25 17:00 ` Tom Lane <tgl@sss.pgh.pa.us>
2020-03-28 03:28 ` Re: [PATCH] Check operator when creating unique index on partition table Guancheng Luo <prajnamort@gmail.com>
0 siblings, 1 reply; 4+ messages in thread
From: Tom Lane @ 2020-03-25 17:00 UTC (permalink / raw)
To: Guancheng Luo <prajnamort@gmail.com>; +Cc: pgsql-hackers@lists.postgresql.org
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.)
I'm not really on board with adding a whole new test script for this,
either.
regards, tom lane
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: [PATCH] Check operator when creating unique index on partition table
2020-03-25 10:43 [PATCH] Check operator when creating unique index on partition table Guancheng Luo <prajnamort@gmail.com>
2020-03-25 17:00 ` Re: [PATCH] Check operator when creating unique index on partition table Tom Lane <tgl@sss.pgh.pa.us>
@ 2020-03-28 03:28 ` Guancheng Luo <prajnamort@gmail.com>
2020-04-01 18:53 ` Re: [PATCH] Check operator when creating unique index on partition table Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 4+ messages in thread
From: Guancheng Luo @ 2020-03-28 03:28 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-hackers@lists.postgresql.org
> 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.c
index 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.out
index 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.sql
index 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
=
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: [PATCH] Check operator when creating unique index on partition table
2020-03-25 10:43 [PATCH] Check operator when creating unique index on partition table Guancheng Luo <prajnamort@gmail.com>
2020-03-25 17:00 ` Re: [PATCH] Check operator when creating unique index on partition table Tom Lane <tgl@sss.pgh.pa.us>
2020-03-28 03:28 ` Re: [PATCH] Check operator when creating unique index on partition table Guancheng Luo <prajnamort@gmail.com>
@ 2020-04-01 18:53 ` Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 0 replies; 4+ messages in thread
From: Tom Lane @ 2020-04-01 18:53 UTC (permalink / raw)
To: Guancheng Luo <prajnamort@gmail.com>; +Cc: pgsql-hackers@lists.postgresql.org
Guancheng Luo <prajnamort@gmail.com> writes:
> On Mar 26, 2020, at 01:00, Tom Lane <tgl@sss.pgh.pa.us> wrote:
>> 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.
> 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.
Ah, yeah, I see we already have regression test cases that require that.
I pushed the patch with some cosmetic revisions. I left out the
regression test case though; it seemed pretty expensive considering
that the code is already being exercised by existing cases.
Thanks for the report and patch!
regards, tom lane
^ permalink raw reply [nested|flat] 4+ messages in thread
end of thread, other threads:[~2020-04-01 18:53 UTC | newest]
Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2020-03-25 10:43 [PATCH] Check operator when creating unique index on partition table Guancheng Luo <prajnamort@gmail.com>
2020-03-25 17:00 ` Tom Lane <tgl@sss.pgh.pa.us>
2020-03-28 03:28 ` Guancheng Luo <prajnamort@gmail.com>
2020-04-01 18:53 ` Tom Lane <tgl@sss.pgh.pa.us>
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