REPACK (CONCURRENTLY) loses missing values of columns added without a rewrite
Hi,
REPACK (CONCURRENTLY) is not correctly accounting for missing values when
applying decoded changes to the transient relation (NewHeap). This is
because it is using the NewHeap descriptor, which intentionally has no
missing values when it is formed in make_new_heap().
This can leave a column with NULL instead of its default value, even if the
column is NOT NULL. It only happens today when an UPDATE during the repack
fires a BEFORE UPDATE trigger that returns OLD, so the new row version
reuses a tuple written before the ALTER TABLE ... ADD COLUMN ... DEFAULT.
```
# session 1
CREATE EXTENSION injection_points;
CREATE TABLE t(i int primary key, j int);
INSERT INTO t VALUES (1,1),(2,2),(3,3);
ALTER TABLE t ADD COLUMN c int NOT NULL DEFAULT 42;
CREATE FUNCTION retold() RETURNS trigger LANGUAGE plpgsql AS 'BEGIN
RETURN OLD; END';
CREATE TRIGGER tr BEFORE UPDATE ON t FOR EACH ROW EXECUTE FUNCTION retold();
SELECT injection_points_attach('repack-concurrently-before-lock', 'wait');
REPACK (CONCURRENTLY) t;
# session 2
UPDATE t SET j = j WHERE i IN (1,2);
SELECT injection_points_wakeup('repack-concurrently-before-lock');
SELECT injection_points_detach('repack-concurrently-before-lock');
SELECT * FROM t;
```
The final SELECT * FROM t; results in
```
i | j | c
---+---+----
3 | 3 | 42
1 | 1 |
2 | 2 |
(3 rows)
```
where i = 1 and 2 are missing the value of the default column.
The same issue has been found previously in different cases. 16828d5c027
made every consumer of a stored tuple responsible for the missing values,
ba9f18abd36 worked around one such case by expanding the tuple in
MaterializeTupleForTrigger(), and 20d3fe9009d replaced that workaround by
using the real tuple descriptor instead.
The attached patch takes the same approach as 20d3fe9009d, which is to use
the real tuple descriptor rather than expand the tuple. It deforms the decoded
tuples with the descriptor of the relation they were decoded from, which is
the only one that has the missing values. Tuples formed with it are still
valid for the transient relation, whose attributes are a copy of the source
relation ones. An isolation test is included.
My first thought was to expand the decoded tuple with heap_expand_tuple(),
but that is the workaround of ba9f18abd which 20d3fe9009d got rid of.
heap_expand_tuple() has had no caller in the tree since then, and
minimal_expand_tuple() has had none since 4da597edf1b, so as a separate
cleanup for v20 it may be worth removing both.
I will add this to the v19 open items, as I think it should be backpatched.
--
Sami Imseih
Amazon Web Services
Hi,
v2- just simplifies the test a bit; otherwise same as v1.
--
Sami Imseih
Amazon Web Services
Hi Sami,
v2 fixes the issue you mentioned.
I think It also fixes another case. If the added column is in the replica
identity index, REPACK looks up the row with a NULL key. That fails the
Assert in find_target_tuple(), or gives "could not find target tuple"
without assertions. Adding this to the test covers it:
CREATE UNIQUE INDEX repack_test_i_c_idx ON repack_test (i, c);
ALTER TABLE repack_test REPLICA IDENTITY USING INDEX
repack_test_i_c_idx;
Thanks,
Shihao
v2 fixes the issue you mentioned.
Thanks for checking!
I think It also fixes another case. If the added column is in the replica
identity index, REPACK looks up the row with a NULL key. That fails the
Assert in find_target_tuple(), or gives "could not find target tuple"
without assertions. Adding this to the test covers it:CREATE UNIQUE INDEX repack_test_i_c_idx ON repack_test (i, c);
ALTER TABLE repack_test REPLICA IDENTITY USING INDEX repack_test_i_c_idx;
ahh, good repro. It's worse for a pass-by-reference column, i.e. TEXT.
find_target_tuple() only sets sk_argument, never SK_ISNULL, so the
comparison function gets a NULL pointer rather than a NULL key.
So a non assert build will actually segfault.
v3 uses a TEXT column in the identity index, which covers the int case
too since both go through the same sk_argument.
--
Sami Imseih
Amazon Web Services (AWS)
Hi Sami,
v3 looks good to me.
Move to RFC state
Thanks,
Shihao
On Wed, 23 Sept 2026 at 23:41, Sami Imseih <samimseih.pg@gmail.com> wrote:
v2 fixes the issue you mentioned.
Thanks for checking!
I think It also fixes another case. If the added column is in the replica
identity index, REPACK looks up the row with a NULL key. That fails the
Assert in find_target_tuple(), or gives "could not find target tuple"
without assertions. Adding this to the test covers it:CREATE UNIQUE INDEX repack_test_i_c_idx ON repack_test (i, c);
ALTER TABLE repack_test REPLICA IDENTITY USING INDEX repack_test_i_c_idx;ahh, good repro. It's worse for a pass-by-reference column, i.e. TEXT.
find_target_tuple() only sets sk_argument, never SK_ISNULL, so the
comparison function gets a NULL pointer rather than a NULL key.
So a non assert build will actually segfault.v3 uses a TEXT column in the identity index, which covers the int case
too since both go through the same sk_argument.--
Sami Imseih
Amazon Web Services (AWS)
Hi!
I reproduced this while stress testing REPACK (CONCURRENTLY) on HEAD.
Whats worse, this issue makes the database unrestorable via pg_dump/COPY.
I also checked DROP COLUMN, ALTER TYPE, SET STORAGE, SET COMPRESSION,
SET NOT NULL, ADD/DROP CONSTRAINT NOT VALID/VALIDATE, virtual
generated columns and found no related issue (both with and without
v3).
So patch LGTM
--
Best regards,
Kirill Reshke
On 2026-Sep-22, Sami Imseih wrote:
REPACK (CONCURRENTLY) is not correctly accounting for missing values when
applying decoded changes to the transient relation (NewHeap). This is
because it is using the NewHeap descriptor, which intentionally has no
missing values when it is formed in make_new_heap().
Hah, interesting, thanks.
I wonder why in your fix we keep a pointer to the whole relation instead
of just to its tupledesc. What about the attached v4?
I considered using CreateTupleDescCopyConstr, but it seems pointless:
the Relation pointer cannot go away while repack is running anyway. I
ran your test with only CreateTupleDescCopy() to see how would your new
test would fail (because such a descriptor wouldn't have the missing
attrs), but it failed differently, because attnotnull is not set.
Anyway, this was just a perhaps pointless experiment.
--
Álvaro Herrera 48°01'N 7°57'E — https://www.EnterpriseDB.com/
"Los cuentos de hadas no dan al niño su primera idea sobre los monstruos.
Lo que le dan es su primera idea de la posible derrota del monstruo."
(G. K. Chesterton)
Thanks!
I wonder why in your fix we keep a pointer to the whole relation instead
of just to its tupledesc. What about the attached v4?
It was expedient. The tupledesc is all we need, so v4 is better. This LGTM.
I ran your test with only CreateTupleDescCopy() to see how would your new
test would fail (because such a descriptor wouldn't have the missing
attrs), but it failed differently, because attnotnull is not set.
Anyway, this was just a perhaps pointless experiment.
Thanks for checking. Right, CreateTupleDescCopy() would just reintroduce
the problem we're fixing.
v5 attached fixes indentation.
--
Sami Imseih
Amazon Web Services (AWS)
Thank you! I have pushed this with some other cosmetic changes --
in particular I added comments in a bunch of places to clarify the role
of the Relation entries being used and which descriptors we use for
each.
--
Álvaro Herrera PostgreSQL Developer — https://www.EnterpriseDB.com/
"La virtud es el justo medio entre dos defectos" (Aristóteles)