Fix Publication for partitioned tables #550
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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:
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).