Skip to content

Commit a50f50a

Browse files
committed
logical decoding: old/newtuple in spooled UPDATE changes was switched around.
Somehow I managed to flip the order of restoring old & new tuples when de-spooling a change in a large transaction from disk. This happens to only take effect when a change is spooled to disk which has old/new versions of the tuple. That only is the case for UPDATEs where he primary key changed or where replica identity is changed to FULL. The tests didn't catch this because either spooled updates, or updates that changed primary keys, were tested; not both at the same time. Found while adding tests for the following commit. Backpatch: 9.4, where logical decoding was added
1 parent 465dd92 commit a50f50a

File tree

3 files changed

+37
-10
lines changed

3 files changed

+37
-10
lines changed

contrib/test_decoding/expected/ddl.out

+15
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,21 @@ ORDER BY 1,2;
196196
20467 | table public.tr_etoomuch: DELETE: id[integer]:1 | table public.tr_etoomuch: UPDATE: id[integer]:9999 data[integer]:-9999
197197
(3 rows)
198198

199+
-- check updates of primary keys work correctly
200+
BEGIN;
201+
CREATE TABLE spoolme AS SELECT g.i FROM generate_series(1, 5000) g(i);
202+
UPDATE tr_etoomuch SET id = -id WHERE id = 5000;
203+
DELETE FROM spoolme;
204+
DROP TABLE spoolme;
205+
COMMIT;
206+
SELECT data
207+
FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1')
208+
WHERE data ~ 'UPDATE';
209+
data
210+
-------------------------------------------------------------------------------------------------------------
211+
table public.tr_etoomuch: UPDATE: old-key: id[integer]:5000 new-tuple: id[integer]:-5000 data[integer]:5000
212+
(1 row)
213+
199214
/*
200215
* check whether we decode subtransactions correctly in relation with each
201216
* other

contrib/test_decoding/sql/ddl.sql

+12
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,18 @@ FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids',
114114
GROUP BY substring(data, 1, 24)
115115
ORDER BY 1,2;
116116

117+
-- check updates of primary keys work correctly
118+
BEGIN;
119+
CREATE TABLE spoolme AS SELECT g.i FROM generate_series(1, 5000) g(i);
120+
UPDATE tr_etoomuch SET id = -id WHERE id = 5000;
121+
DELETE FROM spoolme;
122+
DROP TABLE spoolme;
123+
COMMIT;
124+
125+
SELECT data
126+
FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1')
127+
WHERE data ~ 'UPDATE';
128+
117129
/*
118130
* check whether we decode subtransactions correctly in relation with each
119131
* other

src/backend/replication/logical/reorderbuffer.c

+10-10
Original file line numberDiff line numberDiff line change
@@ -2266,29 +2266,29 @@ ReorderBufferRestoreChange(ReorderBuffer *rb, ReorderBufferTXN *txn,
22662266
case REORDER_BUFFER_CHANGE_UPDATE:
22672267
/* fall through */
22682268
case REORDER_BUFFER_CHANGE_DELETE:
2269-
if (change->data.tp.newtuple)
2269+
if (change->data.tp.oldtuple)
22702270
{
22712271
Size len = offsetof(ReorderBufferTupleBuf, data)
22722272
+((ReorderBufferTupleBuf *) data)->tuple.t_len
22732273
- offsetof(HeapTupleHeaderData, t_bits);
22742274

2275-
change->data.tp.newtuple = ReorderBufferGetTupleBuf(rb);
2276-
memcpy(change->data.tp.newtuple, data, len);
2277-
change->data.tp.newtuple->tuple.t_data =
2278-
&change->data.tp.newtuple->header;
2275+
change->data.tp.oldtuple = ReorderBufferGetTupleBuf(rb);
2276+
memcpy(change->data.tp.oldtuple, data, len);
2277+
change->data.tp.oldtuple->tuple.t_data =
2278+
&change->data.tp.oldtuple->header;
22792279
data += len;
22802280
}
22812281

2282-
if (change->data.tp.oldtuple)
2282+
if (change->data.tp.newtuple)
22832283
{
22842284
Size len = offsetof(ReorderBufferTupleBuf, data)
22852285
+((ReorderBufferTupleBuf *) data)->tuple.t_len
22862286
- offsetof(HeapTupleHeaderData, t_bits);
22872287

2288-
change->data.tp.oldtuple = ReorderBufferGetTupleBuf(rb);
2289-
memcpy(change->data.tp.oldtuple, data, len);
2290-
change->data.tp.oldtuple->tuple.t_data =
2291-
&change->data.tp.oldtuple->header;
2288+
change->data.tp.newtuple = ReorderBufferGetTupleBuf(rb);
2289+
memcpy(change->data.tp.newtuple, data, len);
2290+
change->data.tp.newtuple->tuple.t_data =
2291+
&change->data.tp.newtuple->header;
22922292
data += len;
22932293
}
22942294
break;

0 commit comments

Comments
 (0)