· Synx Data Labs
One INSERT, Two Rows, One Primary Key
A unique constraint on append-optimized tables that holds against every committed row and misses what the current statement just wrote. Inherited across the Greenplum 7 lineage, fixed in SynxDB 4.8.1, and offered to anyone downstream.
This is the second time this year we have traced a duplicate primary key down to inherited code. The first was a heap table and a snapshot missing three fields. This one is append-optimized storage, and the mechanism is entirely different — but the shape of the discovery is the same. Old code, a latent defect, nobody downstream noticed, and a customer found it in production.
A customer came to us with duplicate rows under a primary key on an append-optimized, column-oriented table. The schema was textbook: the primary key column was also the distribution key and also the partition key. There is no distribution-key mismatch to explain away here. Every index was valid. The duplicates were there anyway.
gp_segment_id | partition | ctid | id
---------------+----------------------+------------------+------------
0 | events_1_prt_36 | (33554540,16775) | 3443086210
0 | events_1_prt_36 | (33554540,17324) | 3443086210
Same segment. Same partition. Two tuples, one key.
The uniqueness check never reads the data
Enforcing a unique index on an append-optimized table does not work the way it does on a heap table.
On a heap, the index entry points at a tuple and the check follows the pointer. On AO and AOCO tables, physical row locations are indirected through the block directory — a catalog structure mapping ranges of row numbers to physical file offsets. The uniqueness check never looks at the data. It asks a much narrower question:
Is there a visible block directory entry that covers this tid?
The call chain is aoco_index_unique_check() → AppendOnlyBlockDirectory_CoversTuple() → blkdir_entry_exists(). If no visible entry covers the tid, the row is treated as absent and the insert proceeds.
That design has an obvious problem, and the original authors knew it: rows the current command is in the middle of inserting are not in the block directory yet. So the command writes a placeholder entry at the start — an entry with rowCount = AOTupleId_MaxRowNum, deliberately covering everything the command is about to write. While that placeholder is visible, the check can see the command’s own rows.
It is a reasonable design. It just does not survive contact with a second minipage.
The placeholder gets overwritten by the thing it was protecting against
Block directory entries are batched into minipages. When a minipage fills, it is flushed and a new one starts. The flush runs through write_minipage(), which chooses between insert and update on exactly one condition:
if (ItemPointerIsValid(&minipageInfo->tupleTid))
CatalogTupleUpdateWithInfo(...); /* have a tid -> this column group already has a row */
else
CatalogTupleInsertWithInfo(...);
When the first minipage fills, tupleTid is valid — because it points at the placeholder. So the flush takes the UPDATE branch and overwrites it.
The placeholder said I cover everything this command writes. The UPDATE rewrites it to say I cover the rows that were just flushed to disk. From the second minipage onward, the rows still in flight have no visible covering entry. The check looks, finds nothing, and lets the duplicate through.
The constraint works perfectly against everything already committed. It is blind only to what the current statement wrote moments earlier. Which is exactly why it appears to work right up until you load at volume.
There was a FIXME
The function carried a comment anticipating this:
we would need to flush a placeholder row at the beginning of each new in-memory minipage
The assumption at the time was that only CREATE INDEX CONCURRENTLY could reach it, through SNAPSHOT_SELF. The ordinary INSERT path, using SNAPSHOT_DIRTY, turns out to reach it identically. The note was right about the mechanism and wrong about the blast radius, and it sat there.
What actually triggers it
One condition, narrower than it sounds but broader than it was first reported:
A single INSERT that produces more block directory entries than one minipage holds.
Minipage capacity is gp_blockdirectory_minipage_size, default 161.
It is not specific to column-oriented storage. Not specific to partitioned tables. Not related to compression. We measured:
| Storage | Minipage capacity | Rows in one INSERT | Result |
|---|---|---|---|
ao_column | 1 / 2 / 4 | 200,000 | duplicate admitted |
ao_column | 161 (default) | 200,000 | correctly rejected |
ao_row | 1 / 2 / 4 | 200,000 | duplicate admitted |
ao_row | 161 (default) | 200,000 | duplicate admitted |
Row-oriented append-optimized tables leak at two hundred thousand rows in a single statement, at stock settings. That is an unremarkable batch size.
Column-oriented tables need far more — tens of millions — and the reason is worth knowing. The check inspects only the first non-dropped column. With an 8-byte column and a large blocksize, one block directory entry covers an enormous number of rows, so it takes a very large statement to generate 161 entries. The case that found this needed 20 million rows in one statement. The threshold is a function of your first column’s width and your block size, not a row count you can look up.
Raising gp_blockdirectory_minipage_size does not help. 161 is both the default and the maximum, and any statement large enough still crosses it.
The fix
In the page-full branch of insert_new_entry(), immediately after clear_minipage(), write a fresh placeholder row.
The ordering is the whole fix. clear_minipage() has just invalidated tupleTid, so the write_minipage() that follows takes the INSERT branch rather than the UPDATE branch — writing a new row instead of overwriting the real entry that was just flushed.
The invariant moves from the first minipage has a covering row to there is a covering row for the entire duration of the command.
Verified in both directions on a three-segment assert build:
before: INSERT 0 20000001 -- 20000001 rows in the table
after: ERROR: duplicate key value violates unique constraint "events_1_prt_2_pkey"
DETAIL: Key (id)=(20000000) already exists.
We then removed the patch, rebuilt, and confirmed the fault returns — ruling out anything environmental. A regression test covering both storage types at 10,000 rows each, about 1.5 seconds, now guards it. It fails on unpatched binaries and passes on patched ones.
Test your own build
A few minutes. Adjust the storage clause to match what you run.
DROP TABLE IF EXISTS t_dupcheck;
CREATE TABLE t_dupcheck (id bigint NOT NULL, pad text, PRIMARY KEY (id))
USING ao_row
DISTRIBUTED BY (id);
-- one statement, one deliberate collision
INSERT INTO t_dupcheck
SELECT CASE WHEN i = 200000 THEN 199999 ELSE i END, 'x'
FROM generate_series(1, 200000) i;
If that returns INSERT 0 200000 instead of a unique violation, your build has it.
To check tables you already have, one warning carried over from our last advisory — set optimizer = off first:
SET optimizer = off;
SELECT id, count(*)
FROM your_table
GROUP BY id
HAVING count(*) > 1;
With the optimizer on, GPORCA knows the column is a primary key, knows a primary key is unique, and plans a GroupAggregate that cannot produce a group larger than one. It will correctly answer the question it was asked and reassuringly return nothing. The database was told the table is unique, so it believes the table is unique. Same trap as last time, different underlying bug.
Mitigation before you are on a fixed build
Two things work, and one does not.
Does not work: raising the minipage size, for the reason above.
Works: splitting very large loads into several smaller statements. The check is reliable across separate statements — the failure is confined to what one statement wrote. We would rather not recommend this as a lasting pattern, because large bulk loads are what an analytical warehouse should be doing, but it removes the condition entirely while you wait for a build.
Also works: verifying after each load with the grouped count above, rather than relying on the constraint alone.
One point that catches people: a fixed build stops new duplicates being created. It does not remove duplicates already on disk. Those rows are there and need a deduplication pass of their own.
Where this leaves the ecosystem
Unique indexes on append-optimized tables arrived in the Greenplum 7 generation, and the block directory code underneath them is shared ancestry. Every distribution descended from that lineage — Apache Cloudberry, Greenplum 7, WarehousePG, Arenadata DB, and our own SynxDB — inherits the same approach to this check.
We are not going to publish a verdict on anyone else’s build that we have not actually run. What we can say is that the code path is common inheritance and the reproduction above takes minutes. If you maintain or operate one of these distributions, run it and find out.
We are contributing the fix back to Apache Cloudberry. If you are downstream of that lineage and would rather have the patch directly than wait for it to flow through, ask us and we will send it. There is nothing proprietary in it — a few lines in the block directory write path, plus the regression test.
The honest framing is the same as last time. This is old, widely inherited open-source infrastructure, carrying a comment that predicted the failure. Nobody downstream caught it, ourselves included, until a customer’s table produced two rows for one key. We would rather the rest of the lineage had the fix than that we were the only ones holding it.
We publish a standalone duplicate-key check with the detection sweep, the single-statement reproduction above, and remediation guidance for both defects in this series. If you would rather have a second opinion on your own exposure, talk to us.
FAQ
Why did a unique constraint allow duplicate rows on an append-optimized table? The uniqueness check on append-optimized tables does not read the data. It asks the block directory whether a visible entry covers the tuple. Rows inserted by the statement in progress are covered by a placeholder entry written at the start of the command, but when the first minipage fills, the flush overwrites that placeholder instead of leaving it in place. From the second minipage onward the statement’s own rows have no visible covering entry, so the check finds nothing and admits the duplicate.
Which tables and workloads are affected?
Any append-optimized table with a unique index or primary key, where a single INSERT produces more block directory entries than one minipage holds. Minipage capacity is gp_blockdirectory_minipage_size, default 161. It is not specific to column-oriented storage, partitioned tables, or compression. Row-oriented append-optimized tables can leak at around 200,000 rows in one statement at stock settings; column-oriented tables typically need tens of millions.
Can I avoid the problem by raising gp_blockdirectory_minipage_size?
No. 161 is both the default and the maximum value, and any statement large enough will still cross it. Until you are on a fixed build, splitting very large loads into smaller statements avoids the condition, and a GROUP BY with HAVING count(*) > 1 verifies the result independently of the constraint.
Why does checking for duplicates with GROUP BY sometimes return nothing?
Because GPORCA knows the column is a primary key, knows a primary key is unique, and plans a GroupAggregate that cannot produce a group larger than one. It correctly answers the question it was asked and returns no rows. Set optimizer = off before running a duplicate check on a table with a primary key.
Does this affect Apache Cloudberry, Greenplum, WarehousePG or Arenadata DB? Unique indexes on append-optimized tables arrived in the Greenplum 7 generation and the block directory code underneath is shared ancestry across that lineage. We have not run the reproduction against every distribution and will not publish a verdict we have not verified. The test in this post takes a few minutes on any build. We are contributing the fix to Apache Cloudberry and will send the patch directly to anyone downstream who asks.
Found, root-caused and fixed by the SynxDB engineering team, October 2026. The fix ships in SynxDB 4.8.1 and is being contributed to Apache Cloudberry (Incubating).
The first post in this series covers an unrelated duplicate-key defect in heap storage, caused by a snapshot copy that dropped its subtransaction fields: Two rows, one primary key.