Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PG17 compatibility: Fix Test Failure in multi_alter_table_add_const #7733

Draft
wants to merge 1 commit into
base: m3hm3t/pg17_support
Choose a base branch
from

Conversation

m3hm3t
Copy link
Contributor

@m3hm3t m3hm3t commented Nov 11, 2024

In earlier versions of PostgreSQL, exclusion constraints were not allowed on partitioned tables. This is why the error in your regression test (ERROR: exclusion constraints are not supported on partitioned tables) was raised in PostgreSQL 16. In PostgreSQL 17, exclusion constraints are now allowed on partitioned tables, which is why the error no longer appears when you attempt to add an exclusion constraint.

The constraint exclusion mechanism, described in the documentation, relies on CHECK constraints to decide which partitions or child tables need to be queried.

CHECK constraints

 -- Check "ADD EXCLUDE" errors out for partitioned table since the postgres does not allow it
 ALTER TABLE AT_AddConstNoName.citus_local_partitioned_table ADD EXCLUDE(partition_col WITH =);
-ERROR:  exclusion constraints are not supported on partitioned tables
 -- Check "ADD CHECK"
 SET client_min_messages TO DEBUG1;
 ALTER TABLE AT_AddConstNoName.citus_local_partitioned_table ADD CHECK (dist_col > 0);
 DEBUG:  the constraint name on the shards of the partition is too long, switching to sequential and local execution mode to prevent self deadlocks: longlonglonglonglonglonglonglonglonglonglonglo_537570f5_5_check
 DEBUG:  verifying table "longlonglonglonglonglonglonglonglonglonglonglonglonglonglongabc"
 DEBUG:  verifying table "p1"
 RESET client_min_messages;
 SELECT con.conname
     FROM pg_catalog.pg_constraint con
       INNER JOIN pg_catalog.pg_class rel ON rel.oid = con.conrelid
       INNER JOIN pg_catalog.pg_namespace nsp ON nsp.oid = connamespace
           WHERE rel.relname = 'citus_local_partitioned_table';
                      conname                      
 --------------------------------------------------
+ citus_local_partitioned_table_partition_col_excl
  citus_local_partitioned_table_check
-(1 row)
+(2 rows)

@m3hm3t m3hm3t self-assigned this Nov 11, 2024
Copy link

codecov bot commented Nov 11, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Please upload report for BASE (m3hm3t/pg17_support@56595dd). Learn more about missing BASE report.

Additional details and impacted files
@@                  Coverage Diff                   @@
##             m3hm3t/pg17_support    #7733   +/-   ##
======================================================
  Coverage                       ?   89.56%           
======================================================
  Files                          ?      274           
  Lines                          ?    59689           
  Branches                       ?     7446           
======================================================
  Hits                           ?    53458           
  Misses                         ?     4087           
  Partials                       ?     2144           

@m3hm3t m3hm3t marked this pull request as ready for review November 11, 2024 20:48
@m3hm3t m3hm3t changed the title Fix Test Failure in multi_alter_table_add_const in PG17 PG17 compatibility: Fix Test Failure in multi_alter_table_add_const in PG17 Nov 12, 2024
Copy link
Member

@naisila naisila left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that we lack in documentation right now, let me introduce this concept here as it's a good PR for it.
For new things that PG allows, like an exclusion constraint on partitioned tables, first we make sure that it works as it is supposed to in Citus.
If it does, our general practice is to create a new file to test it, namely pg17.sql where we add all such things. We will populate this file with other things as we go on with the support. (For this reason we have pg16.sql pg15.sql tests etc.) I will include this in the documentation for new PG support.

With that in mind, could you adjust this PR accordingly? I don't think we need to add a new output file for multi_alter_table_add_constraints. Instead, we can create pg17.sql file which will have pg17.out and pg17_0.out, and include this particular test case there. In this case, the pg17_0.out file will have the error exclusion constraints are not supported on partitioned tables.

@naisila naisila changed the title PG17 compatibility: Fix Test Failure in multi_alter_table_add_const in PG17 PG17 compatibility: Fix Test Failure in multi_alter_table_add_const Nov 12, 2024
@m3hm3t m3hm3t marked this pull request as draft November 15, 2024 08:39
@m3hm3t m3hm3t force-pushed the m3hm3t/multi_alter_table_add_const branch 2 times, most recently from fa4854a to 765d397 Compare November 15, 2024 15:35
@m3hm3t
Copy link
Contributor Author

m3hm3t commented Nov 15, 2024

Given that we lack in documentation right now, let me introduce this concept here as it's a good PR for it. For new things that PG allows, like an exclusion constraint on partitioned tables, first we make sure that it works as it is supposed to in Citus. If it does, our general practice is to create a new file to test it, namely pg17.sql where we add all such things. We will populate this file with other things as we go on with the support. (For this reason we have pg16.sql pg15.sql tests etc.) I will include this in the documentation for new PG support.

With that in mind, could you adjust this PR accordingly? I don't think we need to add a new output file for multi_alter_table_add_constraints. Instead, we can create pg17.sql file which will have pg17.out and pg17_0.out, and include this particular test case there. In this case, the pg17_0.out file will have the error exclusion constraints are not supported on partitioned tables.

@naisila Can you review the solution? If it is acceptable, I will update the PR to prepare it for merging into the release branch.

@m3hm3t m3hm3t marked this pull request as ready for review November 15, 2024 16:35
@@ -785,38 +785,6 @@ SELECT con.conname
\c - - :master_host :master_port
ALTER TABLE AT_AddConstNoName.citus_local_partitioned_table DROP CONSTRAINT citus_local_partitioned_table_partition_col_key;

-- Check "ADD EXCLUDE" errors out for partitioned table since the postgres does not allow it
ALTER TABLE AT_AddConstNoName.citus_local_partitioned_table ADD EXCLUDE(partition_col WITH =);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that in this test, we only need to remove the line that adds the exclusion constraint, not the rest.


\c - - :master_host :master_port
-- Check "ADD EXCLUDE" errors out for partitioned table since the postgres does not allow it before PG 17
ALTER TABLE AT_AddConstNoName.citus_local_partitioned_table ADD EXCLUDE(partition_col WITH =);
Copy link
Member

@naisila naisila Nov 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that this is now something new that Citus supports, we don't necessarily need to add the exact same test that was in multi_alter_table_add_constraints_without_name.sql. We should include that particular test case for sure, i.e. ALTER TABLE ADD EXCLUDE CONSTRAINT, but we can (should) do more to test this feature.

If we consider this as a new feature in Citus (even though it doesnt need extra code work, apparently), we can create a "simpler/more readable" test, e.g. with the following steps:

Create distributed partitioned table dist_blabla
create partitioned citus local table citus_local_blabla
add exclude constraint with name in dist_blabla
check that its propagated to the workers properly
add exclude constraint with name in citus_local_blabla
check that it was added correctly
add exclusin constraints without name in both tables
verify previous command worked
alter the constraints (if meaningful?)
verify previous command worked
drop constraints
verify previous command worked
something else you can think of?

A test like the above is actually what we run when we want to make sure that something works as it should in Citus. (i.e. something is distributed properly)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@naisila

I’ve prepared a draft of the test based on this plan. Could you review it, please?
Should this test be in the pg17.sql file?

-- Test for exclusion constraints on partitioned and distributed partitioned tables in Citus environment

-- Step 1: Create a distributed partitioned table
CREATE TABLE distributed_partitioned_table (
    id serial PRIMARY KEY,
    partition_col int NOT NULL
) DISTRIBUTED BY (id)
PARTITION BY RANGE (partition_col);

-- Step 2: Create a partitioned Citus local table
CREATE TABLE local_partitioned_table (
    id serial PRIMARY KEY,
    partition_col int NOT NULL
) PARTITION BY RANGE (partition_col);

-- Step 3: Add an exclusion constraint with a name to the distributed partitioned table
ALTER TABLE distributed_partitioned_table ADD CONSTRAINT dist_exclude_named EXCLUDE USING gist (partition_col WITH =);

-- Step 4: Verify propagation of exclusion constraint to worker nodes
\c - - :public_worker_1_host :worker_1_port
SELECT * FROM pg_dist_shard_exclude WHERE constraint_name = dist_exclude_named;

-- Step 5: Add an exclusion constraint with a name to the Citus local partitioned table
\c - - :master_host :master_port
ALTER TABLE local_partitioned_table ADD CONSTRAINT local_exclude_named EXCLUDE USING gist (partition_col WITH =);

-- Step 6: Verify the exclusion constraint on the local partitioned table
SELECT conname FROM pg_constraint WHERE conname = 'local_exclude_named';

-- Step 7: Add exclusion constraints without names to both tables
ALTER TABLE distributed_partitioned_table ADD EXCLUDE USING gist (partition_col WITH =);
ALTER TABLE local_partitioned_table ADD EXCLUDE USING gist (partition_col WITH =);

-- Step 8: Verify the unnamed exclusion constraints were added
SELECT * FROM pg_constraint WHERE conrelid = 'local_partitioned_table'::regclass;
\c - - :public_worker_1_host :worker_1_port
SELECT * FROM pg_constraint WHERE conrelid = 'distributed_partitioned_table'::regclass;

-- Step 9: Drop the exclusion constraints from both tables
\c - - :master_host :master_port
ALTER TABLE distributed_partitioned_table DROP CONSTRAINT dist_exclude_named;
ALTER TABLE local_partitioned_table DROP CONSTRAINT local_exclude_named;

-- Step 10: Verify the constraints were dropped
SELECT * FROM pg_constraint WHERE conname = 'dist_exclude_named';
SELECT * FROM pg_constraint WHERE conname = 'local_exclude_named';

-- Step 11: Clean up - Drop the tables
DROP TABLE IF EXISTS distributed_partitioned_table;
DROP TABLE IF EXISTS local_partitioned_table;

@naisila naisila force-pushed the naisila/pg17_support branch 9 times, most recently from e108bb8 to c396ce6 Compare November 22, 2024 13:27
@m3hm3t m3hm3t marked this pull request as draft November 28, 2024 15:02
update

update

.

move the tests

update

update

update

style
@m3hm3t m3hm3t force-pushed the m3hm3t/multi_alter_table_add_const branch from 765d397 to bb75e64 Compare November 28, 2024 15:03
@m3hm3t m3hm3t changed the base branch from naisila/pg17_support to m3hm3t/pg17_support November 28, 2024 15:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants