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: account for identity columns in partitioned tables. #7785

Merged
merged 1 commit into from
Dec 18, 2024

Conversation

colm-mchugh
Copy link
Contributor

@colm-mchugh colm-mchugh commented Dec 9, 2024

PG17 added support for identity columns in partitioned tables: https://git.postgresql.org/gitweb/?p=postgresql.git;a=commitdiff;h=699586315 A consequence is that a table with an identity column cannot be attached as a partition. But Citus on Postgres 17 will generate identity column for the partitions if the parent table has one (or more) identity columns when propagating distributed table DDL to worker nodes, as happens in the generated_identity regress test in #7768:

 CREATE TABLE partitioned_table (
     a bigint CONSTRAINT myconname GENERATED BY DEFAULT AS IDENTITY (START WITH 10 INCREMENT BY 10),
     b bigint GENERATED ALWAYS AS IDENTITY (START WITH 10 INCREMENT BY 10),
     c int
 )
 PARTITION BY RANGE (c);
 CREATE TABLE partitioned_table_1_50 PARTITION OF partitioned_table FOR VALUES FROM (1) TO (50);
 CREATE TABLE partitioned_table_50_500 PARTITION OF partitioned_table FOR VALUES FROM (50) TO (1000);
 SELECT create_distributed_table('partitioned_table', 'a');
- create_distributed_table
----------------------------------------------------------------------
-
-(1 row)
-
+ERROR:  table "partitioned_table_1_50" being attached contains an identity column "a"
+DETAIL:  The new partition may not contain an identity column.

It is the Citus-generated ATTACH PARTITION statement that errors out, because the Citus-generated CREATE TABLE for the partitions included identity column definitions. The fix is straightforward - when propagating the CREATE TABLE ddl for a partition of a table with an identity column, don't include the identity column(s), they will be inherited on attaching the partition. In Citus on Postgres 16 (or less) partitions do not inherit identity; the partitions in the example would not have any identity columns so it was not an issue previously.

Copy link

codecov bot commented Dec 9, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Please upload report for BASE (release-13.0@89e0b53). Learn more about missing BASE report.

Additional details and impacted files
@@               Coverage Diff               @@
##             release-13.0    #7785   +/-   ##
===============================================
  Coverage                ?   89.64%           
===============================================
  Files                   ?      274           
  Lines                   ?    59595           
  Branches                ?     7436           
===============================================
  Hits                    ?    53425           
  Misses                  ?     4037           
  Partials                ?     2133           

@colm-mchugh colm-mchugh self-assigned this Dec 9, 2024
@colm-mchugh colm-mchugh force-pushed the cmchugh/pg17-generated_identity branch 2 times, most recently from 29c660e to 4cb15c5 Compare December 9, 2024 17:07
@colm-mchugh colm-mchugh force-pushed the cmchugh/pg17-generated_identity branch from 4cb15c5 to 1f10b06 Compare December 10, 2024 09:01
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.

This looks like a great fix. I don't see any immediate issues with previous PG version behavior. However, to be sure on PG17 behavior, we need to test on Citus all the cases in the PG commit:

  • A newly created partition inherits identity property (this is currently done in pg17.sql for a distributed table, Citus local table not yet)
  • Attached partition inherits identity column
  • Drop identity property when detaching partition
  • Partitions with their own identity columns are not allowed.
  • The usual ALTER operations (add identity column, add identity
    property to existing column, alter properties of an indentity
    column, drop identity property) are supported for partitioned
    tables. Changing a column only in a partitioned table or a
    partition is not allowed; the change needs to be applied to the
    whole partition hierarchy.

Also, we need to test both distributed and Citus local partitioned tables, as they follow some different code paths for specific behaviors.

Note: I didn't test these locally myself, though I hope they all work. If there is any of these that doesn't work, we need to error out properly.

@colm-mchugh colm-mchugh force-pushed the cmchugh/pg17-generated_identity branch from 1f10b06 to 5698bf1 Compare December 17, 2024 21:18
@colm-mchugh
Copy link
Contributor Author

This looks like a great fix. I don't see any immediate issues with previous PG version behavior. However, to be sure on PG17 behavior, we need to test on Citus all the cases in the PG commit:

  • A newly created partition inherits identity property (this is currently done in pg17.sql for a distributed table, Citus local table not yet)
  • Attached partition inherits identity column
  • Drop identity property when detaching partition
  • Partitions with their own identity columns are not allowed.
  • The usual ALTER operations (add identity column, add identity
    property to existing column, alter properties of an indentity
    column, drop identity property) are supported for partitioned
    tables. Changing a column only in a partitioned table or a
    partition is not allowed; the change needs to be applied to the
    whole partition hierarchy.

Also, we need to test both distributed and Citus local partitioned tables, as they follow some different code paths for specific behaviors.

The PR has been updated with tests for inheriting identity (create and attach partition) and drop identity (detach) for distributed table and local tables - thanks for the comprehensive list! Independently of partitioned tables, it looks like adding an identity column to a citus table is unsupported (there is a test for this in generated_identity, via #6738) as also is adding identity to or altering the identity of an existing column, and dropping identity property from an existing column; alter table preprocessing currently rejects Alter Table - Set Identity and Alter Table - Drop Identity commands. The PR has been updated with tests for ALTER operations, with the corresponding errors in the goldfile, to show the current status, and perhaps they can be amended if and when support is added in Citus ?

@naisila
Copy link
Member

naisila commented Dec 18, 2024

@colm-mchugh

The PR has been updated with tests for inheriting identity (create and attach partition) and drop identity (detach) for distributed table and local tables - thanks for the comprehensive list! Independently of partitioned tables, it looks like adding an identity column to a citus table is unsupported (there is a test for this in generated_identity, via #6738) as also is adding identity to or altering the identity of an existing column, and dropping identity property from an existing column; alter table preprocessing currently rejects Alter Table - Set Identity and Alter Table - Drop Identity commands. The PR has been updated with tests for ALTER operations, with the corresponding errors in the goldfile, to show the current status, and perhaps they can be amended if and when support is added in Citus ?

Thanks for addressing all of these cases, I had forgotten some of the Citus limitations. What you are saying makes total sense, and the current test suite is comprehensive for what we currently support.

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.

LGTM, thanks!

Reminder to rebase to release-13.0 and drop the enable configure ... commit before merging.

@naisila naisila force-pushed the naisila/pg17_support branch from 1c39f95 to e8352a8 Compare December 18, 2024 11:45
@colm-mchugh colm-mchugh changed the base branch from naisila/pg17_support to release-13.0 December 18, 2024 12:44
PG17 added support for identity columns in partitioned tables:
https://git.postgresql.org/gitweb/?p=postgresql.git;a=commitdiff;h=699586315
A table with an identity column cannot be attached as a partition.

In Citus on Postgres 17+, when propagating the CREATE TABLE ddl for a
partition of a table with an identity column, the coordinator does not
need to include identity definitions in the ddl. The identity will be
inherited on attaching the partition to the parent table. With Postgres
16 (or less) partitions do not inherit identity.
@colm-mchugh colm-mchugh force-pushed the cmchugh/pg17-generated_identity branch from 5698bf1 to 37c9253 Compare December 18, 2024 12:50
@colm-mchugh colm-mchugh merged commit e91ee24 into release-13.0 Dec 18, 2024
120 of 121 checks passed
@colm-mchugh colm-mchugh deleted the cmchugh/pg17-generated_identity branch December 18, 2024 13:18
naisila added a commit that referenced this pull request Dec 24, 2024
This is the final commit that adds
PG17 compatibility with Citus's current capabilities.

You can use Citus community, release-13.0 branch, with PG17.1.

---------

Specifically, this commit:

- Enables PG17 in the configure script.

- Adds PG17 tests to CI using test images that have 17.1

- Fixes an upgrade test: see below for details
In `citus_prepare_upgrade()`, don't drop any_value when upgrading from
PG16+, because PG16+ has its own any_value function. Attempting to do so
results in the error seen in [pg16-pg17
upgrade](https://github.com/citusdata/citus/actions/runs/11768444117/job/32778340003?pr=7661):
```
ERROR:  cannot drop function any_value(anyelement) because it is required by the database system
CONTEXT:  SQL statement "DROP AGGREGATE IF EXISTS pg_catalog.any_value(anyelement)"
```
When 16 becomes the minimum supported Postgres version, the drop
statements can be removed.

---------

Several PG17 Compatibility commits have been merged before this final one.
All these subtasks are done #7653

See the list below:

Compilation PR: #7699
Ruleutils PR: #7725
Sister PR for tests: citusdata/the-process#159

Helpful smaller PRs:
- #7714
- #7726
- #7731
- #7732
- #7733
- #7738
- #7745
- #7747
- #7748
- #7749
- #7752
- #7755
- #7757
- #7759
- #7760
- #7761
- #7762
- #7765
- #7766
- #7768
- #7769
- #7771
- #7774
- #7776
- #7780
- #7781
- #7785
- #7788
- #7793
- #7796

---------

Co-authored-by: Colm <[email protected]>
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