Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
--
-- A composite datum that is incompatible with the current definition of its
-- type can be stored in a table (nothing deforms it on the way in).  Logical
-- decoding of the transaction that stored it then fails, as the output plugin
-- deforms the row with the current descriptor, and the slot cannot advance
-- past that transaction.  This shows that behaviour.
--
 
-- predictability
SET synchronous_commit = on;
 
-- the decoding errors' CONTEXT includes an LSN
\set VERBOSITY terse
 
CREATE TYPE ld_type AS (a bytea, i int);
SELECT 'init' FROM pg_create_logical_replication_slot('regression_slot', 'test_decoding');
 
-- Form a datum, alter the type underneath it in a later transaction, then
-- store the stale datum.
CREATE PROCEDURE ld_store() LANGUAGE plpgsql AS $$
DECLARE v ld_type;
BEGIN
  v := '(\\x4649584d45,1)'::ld_type;
  COMMIT;
  ALTER TYPE ld_type ALTER ATTRIBUTE a TYPE text;
  CREATE TABLE ld_tbl (id int, c ld_type);
  COMMIT;
  INSERT INTO ld_tbl VALUES (1, v);
END $$;
CALL ld_store();
 
-- Reading the stored datum fails ...
SELECT (c).a FROM ld_tbl;
 
-- ... and so does decoding the transaction that stored it.
SELECT count(*) FROM pg_logical_slot_peek_changes('regression_slot', NULL, NULL, 'skip-empty-xacts', '1');
 
-- Fixing the row does not help: The change record is already in the WAL.
UPDATE ld_tbl SET c = ROW('fixed', 2)::ld_type;
SELECT count(*) FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'skip-empty-xacts', '1');
 
-- Advancing the slot past the transaction is the way out.
SELECT pg_replication_slot_advance('regression_slot', pg_current_wal_lsn()) IS NOT NULL;
SELECT count(*) FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'skip-empty-xacts', '1');
 
SELECT pg_drop_replication_slot('regression_slot');
DROP TABLE ld_tbl;
DROP PROCEDURE ld_store();
DROP TYPE ld_type;