fix(subset): deduplicate CTE names when a table is reachable via multiple FK paths - #430
Open
joshuabaird wants to merge 1 commit into
Open
joshuabaird wants to merge 1 commit into
joshuabaird wants to merge 1 commit into
Conversation
…iple FK paths
When the FK graph contains a diamond pattern — a shared dependency
reachable from the subset root via two or more distinct FK paths —
`cteQuery.addItem` was called multiple times with the same CTE name.
This produced a WITH clause with duplicate names, which PostgreSQL
rejects with error 42712 ("WITH query name ... specified more than once").
Example: subsetting `djstripe_customer` where `djstripe_paymentmethod`
is reachable both directly (via `paymentmethod.customer_id`) and
indirectly (via `invoice.default_payment_method_id`) causes
`public__djstripe_paymentmethod__ids` to be added to the CTE list twice.
Fix: track seen CTE names in a `map[string]struct{}` on `cteQuery` and
skip `addItem` if the name is already registered. The first definition
wins, which is correct since both paths produce equivalent content for
the same table.
Fixes: GreenmaskIO#265
Made-with: Cursor
wwoytenko
self-requested a review
April 14, 2026 19:57
wwoytenko
reviewed
Apr 14, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
When the FK graph contains a diamond pattern — a shared dependency reachable from the subset root via two or more distinct FK paths —
cteQuery.addItemwas called multiple times with the same CTE name. This produced aWITHclause with duplicate names, which PostgreSQL rejects with error 42712:Reproduction
Any schema where a single table is reachable via multiple FK paths from the subset root triggers this. A concrete real-world example using dj-stripe:
djstripe_customerwithsubset_condsdjstripe_paymentmethod.customer_id→djstripe_customer(direct path)djstripe_invoice.customer_id→djstripe_customer(direct path)djstripe_invoice.default_payment_method_id→djstripe_paymentmethod(indirect path)When Greenmask walks the graph from
djstripe_customer, it encountersdjstripe_paymentmethodvia two routes and callsaddItem("public__djstripe_paymentmethod__ids", ...)twice, producing an invalidWITHclause.I believe this is related to #265.
Fix
Track seen CTE names in a
map[string]struct{}oncteQueryand skipaddItemif the name is already registered. The first definition wins — both paths produce equivalent content for the same table.Test plan
greenmask dumpagainst a schema with diamond-shaped FK dependencies andsubset_condson the shared ancestor — confirm no 42712 error