Skip to content

Fix Publication for partitioned tables #550

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

raguidu
Copy link

@raguidu raguidu commented Jun 5, 2025

Currently there is a bug in handling Postgres Publications for partitioned tables.

When a partitioned table is added to a publication, Postgres internally tracks only the child partitions in pg_publication_tables, not the parent table itself. This causes an issue in Terraform’s postgresql_publication resource: even though the parent table was added, a subsequent Terraform plan sees only the child partitions and assumes the parent table is missing. It then attempts to re-add the parent table, resulting in an error like:

pq: relation "my_partitioned_table" is already member of publication "my_publication"

This makes it impossible to manage publications declaratively for partitioned tables using Terraform without running into persistent apply errors.

What this PR does:

This PR updates the postgresql_publication resource logic to:

  • Detect when child partitions listed in pg_publication_tables belong to a common parent table (via pg_inherits and pg_class)
  • Report the parent table (e.g. public.my_partitioned_table) in place of individual partitions, ensuring the Terraform state matches the declared configuration
  • Preserve behaviour for non-partitioned tables, avoiding any regressions

Compatibility:

Although not tested in all versions, this fix should be compatible with Postgres 10+ (which is the same compatibility of the current query) since I'm only adding old/established catalog views (pg_class, pg_inherits, pg_namespace).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant