pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
BUG #13844: Logical decoding bug with subxact + row locking
9+ messages / 4 participants
[nested] [flat]

* BUG #13844: Logical decoding bug with subxact + row locking
@ 2016-01-05 03:32 jarred@webriots.com
  2016-01-18 02:37 ` Re: BUG #13844: Logical decoding bug with subxact + row locking Bruce Momjian <bruce@momjian.us>
  2016-02-07 11:42 ` Re: BUG #13844: Logical decoding bug with subxact + row locking Andres Freund <andres@anarazel.de>
  2016-03-06 02:18 ` Re: BUG #13844: Logical decoding bug with subxact + row locking Andres Freund <andres@anarazel.de>
  0 siblings, 3 replies; 9+ messages in thread

From: jarred@webriots.com @ 2016-01-05 03:32 UTC (permalink / raw)
  To: pgsql-bugs

The following bug has been logged on the website:

Bug reference:      13844
Logged by:          Jarred Ward
Email address:      jarred@webriots.com
PostgreSQL version: 9.4.5
Operating system:   Linux
Description:        

This is a pretty serious logical decoding bug that bit us pretty hard
in production.

Run the following code:

   
----------------------------------------------------------------------------
    CREATE TABLE foo(a SERIAL PRIMARY KEY, b TEXT);
     
    SELECT pg_create_logical_replication_slot('regression_slot',
'test_decoding');
     
    INSERT INTO foo (b) VALUES ('john');
     
    BEGIN;
    SELECT * FROM foo WHERE b = 'john' FOR UPDATE;
    SAVEPOINT sp1;
    INSERT INTO foo (b) VALUES ('jane');
    ROLLBACK TO SAVEPOINT sp1;
    INSERT INTO foo (b) VALUES ('doe');
    END;
     
    SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL,
NULL);
   
----------------------------------------------------------------------------

The last select above should return the decoded WAL, but instead it
returns:

   
----------------------------------------------------------------------------
    ERROR:  subxact logged without previous toplevel record
   
----------------------------------------------------------------------------

And the process is disconnected with no way to recover the replication
slot without dropping and recreating.

If SELECT statement in the transaction is changed the following:

   
----------------------------------------------------------------------------
    SELECT * FROM foo WHERE b = 'john' FOR UPDATE;
   
----------------------------------------------------------------------------

to remove the row level locking the correct decoded change set is
returned.


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: BUG #13844: Logical decoding bug with subxact + row locking
  2016-01-05 03:32 BUG #13844: Logical decoding bug with subxact + row locking jarred@webriots.com
@ 2016-01-18 02:37 ` Bruce Momjian <bruce@momjian.us>
  2 siblings, 0 replies; 9+ messages in thread

From: Bruce Momjian @ 2016-01-18 02:37 UTC (permalink / raw)
  To: jarred@webriots.com; +Cc: pgsql-bugs

On Tue, Jan  5, 2016 at 03:32:49AM +0000, jarred@webriots.com wrote:
> The following bug has been logged on the website:
> 
> Bug reference:      13844
> Logged by:          Jarred Ward
> Email address:      jarred@webriots.com
> PostgreSQL version: 9.4.5
> Operating system:   Linux
> Description:        
> 
> This is a pretty serious logical decoding bug that bit us pretty hard
> in production.
> 
> Run the following code:
> 
>    
> ----------------------------------------------------------------------------
>     CREATE TABLE foo(a SERIAL PRIMARY KEY, b TEXT);
>      
>     SELECT pg_create_logical_replication_slot('regression_slot',
> 'test_decoding');
>      
>     INSERT INTO foo (b) VALUES ('john');
>      
>     BEGIN;
>     SELECT * FROM foo WHERE b = 'john' FOR UPDATE;
>     SAVEPOINT sp1;
>     INSERT INTO foo (b) VALUES ('jane');
>     ROLLBACK TO SAVEPOINT sp1;
>     INSERT INTO foo (b) VALUES ('doe');
>     END;
>      
>     SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL,
> NULL);
>    
> ----------------------------------------------------------------------------
> 
> The last select above should return the decoded WAL, but instead it
> returns:
> 
>    
> ----------------------------------------------------------------------------
>     ERROR:  subxact logged without previous toplevel record
>    
> ----------------------------------------------------------------------------
> 
> And the process is disconnected with no way to recover the replication
> slot without dropping and recreating.
> 
> If SELECT statement in the transaction is changed the following:
> 
>    
> ----------------------------------------------------------------------------
>     SELECT * FROM foo WHERE b = 'john' FOR UPDATE;
>    
> ----------------------------------------------------------------------------
> 
> to remove the row level locking the correct decoded change set is
> returned.

I can confirm this behavior.

-- 
  Bruce Momjian  <bruce@momjian.us>        http://momjian.us
  EnterpriseDB                             http://enterprisedb.com

+ As you are, so once was I. As I am, so you will be. +
+ Roman grave inscription                             +


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: BUG #13844: Logical decoding bug with subxact + row locking
  2016-01-05 03:32 BUG #13844: Logical decoding bug with subxact + row locking jarred@webriots.com
@ 2016-02-07 11:42 ` Andres Freund <andres@anarazel.de>
  2016-03-04 15:36   ` Re: BUG #13844: Logical decoding bug with subxact + row locking Alvaro Herrera <alvherre@2ndquadrant.com>
  2 siblings, 1 reply; 9+ messages in thread

From: Andres Freund @ 2016-02-07 11:42 UTC (permalink / raw)
  To: jarred@webriots.com; +Cc: pgsql-bugs

Hi,

On 2016-01-05 03:32:49 +0000, jarred@webriots.com wrote:
> This is a pretty serious logical decoding bug that bit us pretty hard
> in production.
> 
> Run the following code:
> 
>    
> ----------------------------------------------------------------------------
>     CREATE TABLE foo(a SERIAL PRIMARY KEY, b TEXT);
>      
>     SELECT pg_create_logical_replication_slot('regression_slot',
> 'test_decoding');
>      
>     INSERT INTO foo (b) VALUES ('john');
>      
>     BEGIN;
>     SELECT * FROM foo WHERE b = 'john' FOR UPDATE;
>     SAVEPOINT sp1;
>     INSERT INTO foo (b) VALUES ('jane');
>     ROLLBACK TO SAVEPOINT sp1;
>     INSERT INTO foo (b) VALUES ('doe');
>     END;
>      
>     SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL,
> NULL);
>    
> ----------------------------------------------------------------------------
> 
> The last select above should return the decoded WAL, but instead it
> returns:
> 
>    
> ----------------------------------------------------------------------------
>     ERROR:  subxact logged without previous toplevel record
>    
> ----------------------------------------------------------------------------

Ugh, that's an annoying bug. Working on producing a fix.

The problem is that logical decoding expects to know about toplevel xids
before subtransaction/savepoint xids. Which is enforced in the WAL
logging code, so that's ok. But the problem is that right now the WAL
produced looks like (abbreviated):
rmgr: Heap        tx:        585, lsn: 0/01A304A8, desc: LOCK off 1: xid 585 LOCK_ONLY EXCL_LOCK KEYS_UPDATED , blkref #0: rel 1663/12385/24607 blk 0
rmgr: Heap        tx:        586, lsn: 0/01A30658, desc: INSERT off 2, blkref #0: rel 1663/12385/24607 blk 0 FPW
rmgr: Transaction tx:        586, lsn: 0/01A30798, desc: ABORT 2016-02-07 12:21:18.624045 CET
rmgr: Standby     tx:          0, lsn: 0/01A307C0, desc: RUNNING_XACTS nextXid 587 latestCompletedXid 586 oldestRunningXid 585; 1 xacts: 585
rmgr: Heap        tx:        587, lsn: 0/01A307F8, desc: INSERT off 3, blkref #0: rel 1663/12385/24607 blk 0
rmgr: Btree       tx:        587, lsn: 0/01A30838, desc: INSERT_LEAF off 3, blkref #0: rel 1663/12385/24614 blk 1
rmgr: Transaction tx:        585, lsn: 0/01A30878, desc: COMMIT 2016-02-07 12:21:28.062978 CET; subxacts: 587

and decode.c doesn't care about LOCK records so far. Which means
reorderbuffer.c doesn't know about the relevant xid. Hm.

Greetings,

Andres Freund


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: BUG #13844: Logical decoding bug with subxact + row locking
  2016-01-05 03:32 BUG #13844: Logical decoding bug with subxact + row locking jarred@webriots.com
  2016-02-07 11:42 ` Re: BUG #13844: Logical decoding bug with subxact + row locking Andres Freund <andres@anarazel.de>
@ 2016-03-04 15:36   ` Alvaro Herrera <alvherre@2ndquadrant.com>
  2016-03-04 18:12     ` Re: BUG #13844: Logical decoding bug with subxact + row locking Andres Freund <andres@anarazel.de>
  0 siblings, 1 reply; 9+ messages in thread

From: Alvaro Herrera @ 2016-03-04 15:36 UTC (permalink / raw)
  To: Andres Freund <andres@anarazel.de>; +Cc: jarred@webriots.com; pgsql-bugs

Andres Freund wrote:

> Ugh, that's an annoying bug. Working on producing a fix.
> 
> The problem is that logical decoding expects to know about toplevel xids
> before subtransaction/savepoint xids. Which is enforced in the WAL
> logging code, so that's ok. But the problem is that right now the WAL
> produced looks like (abbreviated):
> rmgr: Heap        tx:        585, lsn: 0/01A304A8, desc: LOCK off 1: xid 585 LOCK_ONLY EXCL_LOCK KEYS_UPDATED , blkref #0: rel 1663/12385/24607 blk 0
> rmgr: Heap        tx:        586, lsn: 0/01A30658, desc: INSERT off 2, blkref #0: rel 1663/12385/24607 blk 0 FPW
> rmgr: Transaction tx:        586, lsn: 0/01A30798, desc: ABORT 2016-02-07 12:21:18.624045 CET
> rmgr: Standby     tx:          0, lsn: 0/01A307C0, desc: RUNNING_XACTS nextXid 587 latestCompletedXid 586 oldestRunningXid 585; 1 xacts: 585
> rmgr: Heap        tx:        587, lsn: 0/01A307F8, desc: INSERT off 3, blkref #0: rel 1663/12385/24607 blk 0
> rmgr: Btree       tx:        587, lsn: 0/01A30838, desc: INSERT_LEAF off 3, blkref #0: rel 1663/12385/24614 blk 1
> rmgr: Transaction tx:        585, lsn: 0/01A30878, desc: COMMIT 2016-02-07 12:21:28.062978 CET; subxacts: 587
> 
> and decode.c doesn't care about LOCK records so far. Which means
> reorderbuffer.c doesn't know about the relevant xid. Hm.

Hi,

Any progress with this?

-- 
Álvaro Herrera                http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: BUG #13844: Logical decoding bug with subxact + row locking
  2016-01-05 03:32 BUG #13844: Logical decoding bug with subxact + row locking jarred@webriots.com
  2016-02-07 11:42 ` Re: BUG #13844: Logical decoding bug with subxact + row locking Andres Freund <andres@anarazel.de>
  2016-03-04 15:36   ` Re: BUG #13844: Logical decoding bug with subxact + row locking Alvaro Herrera <alvherre@2ndquadrant.com>
@ 2016-03-04 18:12     ` Andres Freund <andres@anarazel.de>
  2016-03-04 18:28       ` Re: BUG #13844: Logical decoding bug with subxact + row locking Alvaro Herrera <alvherre@2ndquadrant.com>
  0 siblings, 1 reply; 9+ messages in thread

From: Andres Freund @ 2016-03-04 18:12 UTC (permalink / raw)
  To: Alvaro Herrera <alvherre@2ndquadrant.com>; +Cc: jarred@webriots.com; pgsql-bugs

On 2016-03-04 12:36:32 -0300, Alvaro Herrera wrote:
> Andres Freund wrote:
> 
> > Ugh, that's an annoying bug. Working on producing a fix.
> > 
> > The problem is that logical decoding expects to know about toplevel xids
> > before subtransaction/savepoint xids. Which is enforced in the WAL
> > logging code, so that's ok. But the problem is that right now the WAL
> > produced looks like (abbreviated):
> > rmgr: Heap        tx:        585, lsn: 0/01A304A8, desc: LOCK off 1: xid 585 LOCK_ONLY EXCL_LOCK KEYS_UPDATED , blkref #0: rel 1663/12385/24607 blk 0
> > rmgr: Heap        tx:        586, lsn: 0/01A30658, desc: INSERT off 2, blkref #0: rel 1663/12385/24607 blk 0 FPW
> > rmgr: Transaction tx:        586, lsn: 0/01A30798, desc: ABORT 2016-02-07 12:21:18.624045 CET
> > rmgr: Standby     tx:          0, lsn: 0/01A307C0, desc: RUNNING_XACTS nextXid 587 latestCompletedXid 586 oldestRunningXid 585; 1 xacts: 585
> > rmgr: Heap        tx:        587, lsn: 0/01A307F8, desc: INSERT off 3, blkref #0: rel 1663/12385/24607 blk 0
> > rmgr: Btree       tx:        587, lsn: 0/01A30838, desc: INSERT_LEAF off 3, blkref #0: rel 1663/12385/24614 blk 1
> > rmgr: Transaction tx:        585, lsn: 0/01A30878, desc: COMMIT 2016-02-07 12:21:28.062978 CET; subxacts: 587
> > 
> > and decode.c doesn't care about LOCK records so far. Which means
> > reorderbuffer.c doesn't know about the relevant xid. Hm.
> 
> Any progress with this?

Yes. The attached WIP patch fixes this, and some other issues. I'm
working on splitting it up, and adding some more testcases.

Andres

-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs

Attachments:

logical.diff (text/x-diff, 23.6K) download diff --git a/contrib/test_decoding/expected/toast.out b/contrib/test_decoding/expected/toast.out index 735b14c..389748f 100644 --- a/contrib/test_decoding/expected/toast.out +++ b/contrib/test_decoding/expected/toast.out @@ -292,6 +292,64 @@ SELECT substr(data, 1, 200) FROM pg_logical_slot_get_changes('regression_slot', COMMIT (235 rows) +-- test we can decode "old" tuples bigger than the max heap tuple size correctly +DROP TABLE IF EXISTS toasted_several; +NOTICE: table "toasted_several" does not exist, skipping +CREATE TABLE toasted_several ( + id serial unique not null, + toasted_key text primary key, + toasted_col1 text, + toasted_col2 text +); +ALTER TABLE toasted_several REPLICA IDENTITY FULL; +ALTER TABLE toasted_several ALTER COLUMN toasted_key SET STORAGE EXTERNAL; +ALTER TABLE toasted_several ALTER COLUMN toasted_col1 SET STORAGE EXTERNAL; +ALTER TABLE toasted_several ALTER COLUMN toasted_col2 SET STORAGE EXTERNAL; +INSERT INTO toasted_several(toasted_key) VALUES(repeat('9876543210', 2000)); +SELECT regexp_replace(data, '^(.{100}).*(.{100})$', '\1..\2') FROM pg_logical_slot_peek_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1'); + regexp_replace +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ + BEGIN + table public.toasted_several: INSERT: id[integer]:1 toasted_key[text]:'98765432109876543210987654321..098765432109876543210987654321098765432109876543210' toasted_col1[text]:null toasted_col2[text]:null + COMMIT +(3 rows) + +-- test update of a toasted key without changing it +UPDATE toasted_several SET toasted_col1 = toasted_key; +UPDATE toasted_several SET toasted_col2 = toasted_col1; +SELECT regexp_replace(data, '^(.{100}).*(.{100})$', '\1..\2') FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1'); + regexp_replace +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ + BEGIN + table public.toasted_several: INSERT: id[integer]:1 toasted_key[text]:'98765432109876543210987654321..098765432109876543210987654321098765432109876543210' toasted_col1[text]:null toasted_col2[text]:null + COMMIT + BEGIN + table public.toasted_several: UPDATE: old-key: id[integer]:1 toasted_key[text]:'98765432109876543210..432109876543210987654321098765432109876543210987654321098765432109876543210' toasted_col2[text]:null + COMMIT + BEGIN + table public.toasted_several: UPDATE: old-key: id[integer]:1 toasted_key[text]:'98765432109876543210..876543210987654321098765432109876543210987654321098765432109876543210987654321098765432109876543210' + COMMIT +(9 rows) + +/* + * update with large tuplebuf, in a transaction large enough to force to spool to disk + */ +BEGIN; +INSERT INTO toasted_several(toasted_key) SELECT * FROM generate_series(1, 10234); +UPDATE toasted_several SET toasted_col1 = toasted_col2 WHERE id = 1; +DELETE FROM toasted_several WHERE id = 1; +COMMIT; +DROP TABLE toasted_several; +SELECT regexp_replace(data, '^(.{100}).*(.{100})$', '\1..\2') FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1') +WHERE data NOT LIKE '%INSERT: %'; + regexp_replace +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ + BEGIN + table public.toasted_several: UPDATE: old-key: id[integer]:1 toasted_key[text]:'98765432109876543210..7654321098765432109876543210987654321098765432109876543210' toasted_col2[text]:unchanged-toast-datum + table public.toasted_several: DELETE: id[integer]:1 toasted_key[text]:'98765432109876543210987654321..876543210987654321098765432109876543210987654321098765432109876543210987654321098765432109876543210' + COMMIT +(4 rows) + SELECT pg_drop_replication_slot('regression_slot'); pg_drop_replication_slot -------------------------- diff --git a/contrib/test_decoding/sql/toast.sql b/contrib/test_decoding/sql/toast.sql index 26d6b4f..dcb74e3 100644 --- a/contrib/test_decoding/sql/toast.sql +++ b/contrib/test_decoding/sql/toast.sql @@ -265,4 +265,41 @@ ALTER TABLE toasted_copy ALTER COLUMN data SET STORAGE EXTERNAL; 203 untoasted200 \. SELECT substr(data, 1, 200) FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1'); + +-- test we can decode "old" tuples bigger than the max heap tuple size correctly +DROP TABLE IF EXISTS toasted_several; +CREATE TABLE toasted_several ( + id serial unique not null, + toasted_key text primary key, + toasted_col1 text, + toasted_col2 text +); +ALTER TABLE toasted_several REPLICA IDENTITY FULL; +ALTER TABLE toasted_several ALTER COLUMN toasted_key SET STORAGE EXTERNAL; +ALTER TABLE toasted_several ALTER COLUMN toasted_col1 SET STORAGE EXTERNAL; +ALTER TABLE toasted_several ALTER COLUMN toasted_col2 SET STORAGE EXTERNAL; + +INSERT INTO toasted_several(toasted_key) VALUES(repeat('9876543210', 2000)); + +SELECT regexp_replace(data, '^(.{100}).*(.{100})$', '\1..\2') FROM pg_logical_slot_peek_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1'); + +-- test update of a toasted key without changing it +UPDATE toasted_several SET toasted_col1 = toasted_key; +UPDATE toasted_several SET toasted_col2 = toasted_col1; + +SELECT regexp_replace(data, '^(.{100}).*(.{100})$', '\1..\2') FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1'); + +/* + * update with large tuplebuf, in a transaction large enough to force to spool to disk + */ +BEGIN; +INSERT INTO toasted_several(toasted_key) SELECT * FROM generate_series(1, 10234); +UPDATE toasted_several SET toasted_col1 = toasted_col2 WHERE id = 1; +DELETE FROM toasted_several WHERE id = 1; +COMMIT; + +DROP TABLE toasted_several; + +SELECT regexp_replace(data, '^(.{100}).*(.{100})$', '\1..\2') FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1') +WHERE data NOT LIKE '%INSERT: %'; SELECT pg_drop_replication_slot('regression_slot'); diff --git a/src/backend/replication/logical/decode.c b/src/backend/replication/logical/decode.c index 56be1ed..c62e453 100644 --- a/src/backend/replication/logical/decode.c +++ b/src/backend/replication/logical/decode.c @@ -78,6 +78,14 @@ static void DecodeXLogTuple(char *data, Size len, ReorderBufferTupleBuf *tup); * Take every XLogReadRecord()ed record and perform the actions required to * decode it using the output plugin already setup in the logical decoding * context. + * + * NB: Note that every record's xid needs to be processed by reorderbuffer + * (xids contained in the content of records are not relevant for this rule). + * That means that for records which'd otherwise not go through the + * reorderbuffer ReorderBufferProcessXid() has to be called. We don't want to + * call ReorderBufferProcessXid for each record type by default, because + * e.g. empty xacts can be handled more efficiently if there's no previous + * state for them. */ void LogicalDecodingProcessRecord(LogicalDecodingContext *ctx, XLogReaderState *record) @@ -135,6 +143,9 @@ LogicalDecodingProcessRecord(LogicalDecodingContext *ctx, XLogReaderState *recor case RM_BRIN_ID: case RM_COMMIT_TS_ID: case RM_REPLORIGIN_ID: + /* just deal with xid, and done */ + ReorderBufferProcessXid(ctx->reorder, XLogRecGetXid(record), + buf.origptr); break; case RM_NEXT_ID: elog(ERROR, "unexpected RM_NEXT_ID rmgr_id: %u", (RmgrIds) XLogRecGetRmid(buf.record)); @@ -150,6 +161,9 @@ DecodeXLogOp(LogicalDecodingContext *ctx, XLogRecordBuffer *buf) SnapBuild *builder = ctx->snapshot_builder; uint8 info = XLogRecGetInfo(buf->record) & ~XLR_INFO_MASK; + ReorderBufferProcessXid(ctx->reorder, XLogRecGetXid(buf->record), + buf->origptr); + switch (info) { /* this is also used in END_OF_RECOVERY checkpoints */ @@ -260,6 +274,7 @@ DecodeXactOp(LogicalDecodingContext *ctx, XLogRecordBuffer *buf) * transactions in the changestream allowing for a kind of * distributed 2PC. */ + ReorderBufferProcessXid(reorder, XLogRecGetXid(r), buf->origptr); break; default: elog(ERROR, "unexpected RM_XACT_ID record type: %u", info); @@ -276,6 +291,8 @@ DecodeStandbyOp(LogicalDecodingContext *ctx, XLogRecordBuffer *buf) XLogReaderState *r = buf->record; uint8 info = XLogRecGetInfo(r) & ~XLR_INFO_MASK; + ReorderBufferProcessXid(ctx->reorder, XLogRecGetXid(r), buf->origptr); + switch (info) { case XLOG_RUNNING_XACTS: @@ -313,6 +330,8 @@ DecodeHeap2Op(LogicalDecodingContext *ctx, XLogRecordBuffer *buf) TransactionId xid = XLogRecGetXid(buf->record); SnapBuild *builder = ctx->snapshot_builder; + ReorderBufferProcessXid(ctx->reorder, xid, buf->origptr); + /* no point in doing anything yet */ if (SnapBuildCurrentState(builder) < SNAPBUILD_FULL_SNAPSHOT) return; @@ -366,6 +385,8 @@ DecodeHeapOp(LogicalDecodingContext *ctx, XLogRecordBuffer *buf) TransactionId xid = XLogRecGetXid(buf->record); SnapBuild *builder = ctx->snapshot_builder; + ReorderBufferProcessXid(ctx->reorder, xid, buf->origptr); + /* no point in doing anything yet */ if (SnapBuildCurrentState(builder) < SNAPBUILD_FULL_SNAPSHOT) return; @@ -584,7 +605,8 @@ DecodeInsert(LogicalDecodingContext *ctx, XLogRecordBuffer *buf) Size tuplelen; char *tupledata = XLogRecGetBlockData(r, 0, &tuplelen); - change->data.tp.newtuple = ReorderBufferGetTupleBuf(ctx->reorder); + change->data.tp.newtuple = + ReorderBufferGetTupleBuf(ctx->reorder, tuplelen); DecodeXLogTuple(tupledata, tuplelen, change->data.tp.newtuple); } @@ -630,7 +652,8 @@ DecodeUpdate(LogicalDecodingContext *ctx, XLogRecordBuffer *buf) { data = XLogRecGetBlockData(r, 0, &datalen); - change->data.tp.newtuple = ReorderBufferGetTupleBuf(ctx->reorder); + change->data.tp.newtuple = + ReorderBufferGetTupleBuf(ctx->reorder, datalen); DecodeXLogTuple(data, datalen, change->data.tp.newtuple); } @@ -641,7 +664,8 @@ DecodeUpdate(LogicalDecodingContext *ctx, XLogRecordBuffer *buf) data = XLogRecGetData(r) + SizeOfHeapUpdate; datalen = XLogRecGetDataLen(r) - SizeOfHeapUpdate; - change->data.tp.oldtuple = ReorderBufferGetTupleBuf(ctx->reorder); + change->data.tp.oldtuple = + ReorderBufferGetTupleBuf(ctx->reorder, datalen); DecodeXLogTuple(data, datalen, change->data.tp.oldtuple); } @@ -691,13 +715,15 @@ DecodeDelete(LogicalDecodingContext *ctx, XLogRecordBuffer *buf) /* old primary key stored */ if (xlrec->flags & XLH_DELETE_CONTAINS_OLD) { + Size len = XLogRecGetDataLen(r) - SizeOfHeapDelete; + Assert(XLogRecGetDataLen(r) > (SizeOfHeapDelete + SizeOfHeapHeader)); - change->data.tp.oldtuple = ReorderBufferGetTupleBuf(ctx->reorder); + change->data.tp.oldtuple = + ReorderBufferGetTupleBuf(ctx->reorder, len); DecodeXLogTuple((char *) xlrec + SizeOfHeapDelete, - XLogRecGetDataLen(r) - SizeOfHeapDelete, - change->data.tp.oldtuple); + len, change->data.tp.oldtuple); } change->data.tp.clear_toast_afterwards = true; @@ -757,35 +783,36 @@ DecodeMultiInsert(LogicalDecodingContext *ctx, XLogRecordBuffer *buf) */ if (xlrec->flags & XLH_INSERT_CONTAINS_NEW_TUPLE) { - change->data.tp.newtuple = ReorderBufferGetTupleBuf(ctx->reorder); + xlhdr = (xl_multi_insert_tuple *) SHORTALIGN(data); + data = ((char *) xlhdr) + SizeOfMultiInsertTuple; + datalen = xlhdr->datalen; + + change->data.tp.newtuple = + ReorderBufferGetTupleBuf(ctx->reorder, datalen); tuple = change->data.tp.newtuple; /* not a disk based tuple */ ItemPointerSetInvalid(&tuple->tuple.t_self); - xlhdr = (xl_multi_insert_tuple *) SHORTALIGN(data); - data = ((char *) xlhdr) + SizeOfMultiInsertTuple; - datalen = xlhdr->datalen; - /* * We can only figure this out after reassembling the * transactions. */ tuple->tuple.t_tableOid = InvalidOid; - tuple->tuple.t_data = &tuple->t_data.header; + tuple->tuple.t_data = &tuple->header; tuple->tuple.t_len = datalen + SizeofHeapTupleHeader; - memset(&tuple->t_data.header, 0, SizeofHeapTupleHeader); + memset(&tuple->header, 0, SizeofHeapTupleHeader); - memcpy((char *) &tuple->t_data.header + SizeofHeapTupleHeader, + memcpy((char *) &tuple->header + SizeofHeapTupleHeader, (char *) data, datalen); data += datalen; - tuple->t_data.header.t_infomask = xlhdr->t_infomask; - tuple->t_data.header.t_infomask2 = xlhdr->t_infomask2; - tuple->t_data.header.t_hoff = xlhdr->t_hoff; + tuple->header.t_infomask = xlhdr->t_infomask; + tuple->header.t_infomask2 = xlhdr->t_infomask2; + tuple->header.t_hoff = xlhdr->t_hoff; } /* @@ -853,7 +880,6 @@ DecodeXLogTuple(char *data, Size len, ReorderBufferTupleBuf *tuple) int datalen = len - SizeOfHeapHeader; Assert(datalen >= 0); - Assert(datalen <= MaxHeapTupleSize); tuple->tuple.t_len = datalen + SizeofHeapTupleHeader; @@ -862,20 +888,20 @@ DecodeXLogTuple(char *data, Size len, ReorderBufferTupleBuf *tuple) /* we can only figure this out after reassembling the transactions */ tuple->tuple.t_tableOid = InvalidOid; - tuple->tuple.t_data = &tuple->t_data.header; + tuple->tuple.t_data = &tuple->header; /* data is not stored aligned, copy to aligned storage */ memcpy((char *) &xlhdr, data, SizeOfHeapHeader); - memset(&tuple->t_data.header, 0, SizeofHeapTupleHeader); + memset(&tuple->header, 0, SizeofHeapTupleHeader); - memcpy((char *) &tuple->t_data.header + SizeofHeapTupleHeader, + memcpy((char *) &tuple->header + SizeofHeapTupleHeader, data + SizeOfHeapHeader, datalen); - tuple->t_data.header.t_infomask = xlhdr.t_infomask; - tuple->t_data.header.t_infomask2 = xlhdr.t_infomask2; - tuple->t_data.header.t_hoff = xlhdr.t_hoff; + tuple->header.t_infomask = xlhdr.t_infomask; + tuple->header.t_infomask2 = xlhdr.t_infomask2; + tuple->header.t_hoff = xlhdr.t_hoff; } diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c index 78acced..8d7c2f6 100644 --- a/src/backend/replication/logical/reorderbuffer.c +++ b/src/backend/replication/logical/reorderbuffer.c @@ -444,13 +444,31 @@ ReorderBufferReturnChange(ReorderBuffer *rb, ReorderBufferChange *change) /* - * Get an unused, possibly preallocated, ReorderBufferTupleBuf + * Get an unused, possibly preallocated, ReorderBufferTupleBuf fitting at + * least a tuple of size tuplelen. */ ReorderBufferTupleBuf * -ReorderBufferGetTupleBuf(ReorderBuffer *rb) +ReorderBufferGetTupleBuf(ReorderBuffer *rb, Size tuplelen) { ReorderBufferTupleBuf *tuple; + tuplelen += SizeofHeapTupleHeader; + + /* + * Most tuples are below MaxHeapTupleSize, but the ones generated for + * oldtuples can be bigger, as they don't have out-of-line toast + * columns. These we allocate individually. + */ + if (tuplelen > MaxHeapTupleSize) + { + tuple = (ReorderBufferTupleBuf *) + MemoryContextAlloc(rb->context, + SizeofReorderBufferTupleBuf + + tuplelen); + tuple->alloc_tuple_size = tuplelen; + return tuple; + } + /* check the slab cache */ if (rb->nr_cached_tuplebufs) { @@ -458,13 +476,17 @@ ReorderBufferGetTupleBuf(ReorderBuffer *rb) tuple = slist_container(ReorderBufferTupleBuf, node, slist_pop_head_node(&rb->cached_tuplebufs)); #ifdef USE_ASSERT_CHECKING - memset(tuple, 0xa9, sizeof(ReorderBufferTupleBuf)); + memset(&tuple->tuple, 0xa9, sizeof(HeapTupleData)); + memset(&tuple->header, 0xa9, MaxHeapTupleSize); #endif } else { tuple = (ReorderBufferTupleBuf *) - MemoryContextAlloc(rb->context, sizeof(ReorderBufferTupleBuf)); + MemoryContextAlloc(rb->context, + SizeofReorderBufferTupleBuf + + MaxHeapTupleSize); + tuple->alloc_tuple_size = MaxHeapTupleSize; } return tuple; @@ -479,13 +501,22 @@ ReorderBufferGetTupleBuf(ReorderBuffer *rb) void ReorderBufferReturnTupleBuf(ReorderBuffer *rb, ReorderBufferTupleBuf *tuple) { + /* never use the slab cache for oversized tuples */ + if (tuple->alloc_tuple_size > MaxHeapTupleSize) + { + pfree(tuple); + return; + } + /* check whether to put into the slab cache */ if (rb->nr_cached_tuplebufs < max_cached_tuplebufs) { rb->nr_cached_tuplebufs++; slist_push_head(&rb->cached_tuplebufs, &tuple->node); - VALGRIND_MAKE_MEM_UNDEFINED(tuple, sizeof(ReorderBufferTupleBuf)); + VALGRIND_MAKE_MEM_UNDEFINED(&tuple->header, tuple->alloc_tuple_size); + VALGRIND_MAKE_MEM_UNDEFINED(&tuple, SizeofReorderBufferTupleBuf); VALGRIND_MAKE_MEM_DEFINED(&tuple->node, sizeof(tuple->node)); + VALGRIND_MAKE_MEM_DEFINED(&tuple->alloc_tuple_size, sizeof(tuple->alloc_tuple_size)); } else { @@ -1739,18 +1770,15 @@ ReorderBufferForget(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn) ReorderBufferCleanupTXN(rb, txn); } - /* - * Check whether a transaction is already known in this module.xs + * */ -bool -ReorderBufferIsXidKnown(ReorderBuffer *rb, TransactionId xid) +void +ReorderBufferProcessXid(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn) { - ReorderBufferTXN *txn; - - txn = ReorderBufferTXNByXid(rb, xid, false, NULL, InvalidXLogRecPtr, - false); - return txn != NULL; + /* many records won't have an xid assigned, centralize check here */ + if (xid != InvalidTransactionId) + ReorderBufferTXNByXid(rb, xid, true, NULL, lsn, true); } /* @@ -2087,11 +2115,11 @@ ReorderBufferSerializeChange(ReorderBuffer *rb, ReorderBufferTXN *txn, newtup = change->data.tp.newtuple; if (oldtup) - oldlen = offsetof(ReorderBufferTupleBuf, t_data) + + oldlen = SizeofReorderBufferTupleBuf + oldtup->tuple.t_len; if (newtup) - newlen = offsetof(ReorderBufferTupleBuf, t_data) + + newlen = SizeofReorderBufferTupleBuf + newtup->tuple.t_len; sz += oldlen; @@ -2330,29 +2358,34 @@ ReorderBufferRestoreChange(ReorderBuffer *rb, ReorderBufferTXN *txn, case REORDER_BUFFER_CHANGE_UPDATE: case REORDER_BUFFER_CHANGE_DELETE: case REORDER_BUFFER_CHANGE_INTERNAL_SPEC_INSERT: - if (change->data.tp.newtuple) - { - Size len = offsetof(ReorderBufferTupleBuf, t_data) + - ((ReorderBufferTupleBuf *) data)->tuple.t_len; - - change->data.tp.newtuple = ReorderBufferGetTupleBuf(rb); - memcpy(change->data.tp.newtuple, data, len); - change->data.tp.newtuple->tuple.t_data = - &change->data.tp.newtuple->t_data.header; - data += len; - } - if (change->data.tp.oldtuple) { - Size len = offsetof(ReorderBufferTupleBuf, t_data) + - ((ReorderBufferTupleBuf *) data)->tuple.t_len; + Size tuplelen = + ((ReorderBufferTupleBuf *) data)->tuple.t_len; + Size len = SizeofReorderBufferTupleBuf + tuplelen; - change->data.tp.oldtuple = ReorderBufferGetTupleBuf(rb); + change->data.tp.oldtuple = + ReorderBufferGetTupleBuf(rb, tuplelen - SizeofHeapTupleHeader); memcpy(change->data.tp.oldtuple, data, len); change->data.tp.oldtuple->tuple.t_data = - &change->data.tp.oldtuple->t_data.header; + &change->data.tp.oldtuple->header; data += len; } + + if (change->data.tp.newtuple) + { + Size tuplelen = + ((ReorderBufferTupleBuf *) data)->tuple.t_len; + Size len = SizeofReorderBufferTupleBuf + tuplelen; + + change->data.tp.newtuple = + ReorderBufferGetTupleBuf(rb, tuplelen - SizeofHeapTupleHeader); + memcpy(change->data.tp.newtuple, data, len); + change->data.tp.newtuple->tuple.t_data = + &change->data.tp.newtuple->header; + data += len; + } + break; case REORDER_BUFFER_CHANGE_INTERNAL_SNAPSHOT: { @@ -2729,7 +2762,7 @@ ReorderBufferToastReplace(ReorderBuffer *rb, ReorderBufferTXN *txn, */ tmphtup = heap_form_tuple(desc, attrs, isnull); Assert(newtup->tuple.t_len <= MaxHeapTupleSize); - Assert(&newtup->t_data.header == newtup->tuple.t_data); + Assert(&newtup->header == newtup->tuple.t_data); memcpy(newtup->tuple.t_data, tmphtup->t_data, tmphtup->t_len); newtup->tuple.t_len = tmphtup->t_len; diff --git a/src/backend/replication/logical/snapbuild.c b/src/backend/replication/logical/snapbuild.c index ed823ec..179b85a 100644 --- a/src/backend/replication/logical/snapbuild.c +++ b/src/backend/replication/logical/snapbuild.c @@ -635,8 +635,6 @@ SnapBuildClearExportedSnapshot(void) bool SnapBuildProcessChange(SnapBuild *builder, TransactionId xid, XLogRecPtr lsn) { - bool is_old_tx; - /* * We can't handle data in transactions if we haven't built a snapshot * yet, so don't store them. @@ -657,9 +655,7 @@ SnapBuildProcessChange(SnapBuild *builder, TransactionId xid, XLogRecPtr lsn) * If the reorderbuffer doesn't yet have a snapshot, add one now, it will * be needed to decode the change we're currently processing. */ - is_old_tx = ReorderBufferIsXidKnown(builder->reorder, xid); - - if (!is_old_tx || !ReorderBufferXidHasBaseSnapshot(builder->reorder, xid)) + if (!ReorderBufferXidHasBaseSnapshot(builder->reorder, xid)) { /* only build a new snapshot if we don't have a prebuilt one */ if (builder->snapshot == NULL) diff --git a/src/include/replication/reorderbuffer.h b/src/include/replication/reorderbuffer.h index d33ea27..f7e4c51 100644 --- a/src/include/replication/reorderbuffer.h +++ b/src/include/replication/reorderbuffer.h @@ -23,16 +23,18 @@ typedef struct ReorderBufferTupleBuf /* position in preallocated list */ slist_node node; + /* pre-allocated size of tuple buffer, different from tuple size */ + Size alloc_tuple_size; + /* tuple, stored sequentially */ HeapTupleData tuple; - union - { - HeapTupleHeaderData header; - char data[MaxHeapTupleSize]; - double align_it; /* ensure t_data is MAXALIGN'd */ - } t_data; + HeapTupleHeaderData header; + + /* actual tuple data follows */ } ReorderBufferTupleBuf; +#define SizeofReorderBufferTupleBuf offsetof(ReorderBufferTupleBuf, header) + /* * Types of the change passed to a 'change' callback. * @@ -341,7 +343,7 @@ struct ReorderBuffer ReorderBuffer *ReorderBufferAllocate(void); void ReorderBufferFree(ReorderBuffer *); -ReorderBufferTupleBuf *ReorderBufferGetTupleBuf(ReorderBuffer *); +ReorderBufferTupleBuf *ReorderBufferGetTupleBuf(ReorderBuffer *, Size len); void ReorderBufferReturnTupleBuf(ReorderBuffer *, ReorderBufferTupleBuf *tuple); ReorderBufferChange *ReorderBufferGetChange(ReorderBuffer *); void ReorderBufferReturnChange(ReorderBuffer *, ReorderBufferChange *); @@ -366,7 +368,7 @@ void ReorderBufferAddNewTupleCids(ReorderBuffer *, TransactionId, XLogRecPtr lsn CommandId cmin, CommandId cmax, CommandId combocid); void ReorderBufferAddInvalidations(ReorderBuffer *, TransactionId, XLogRecPtr lsn, Size nmsgs, SharedInvalidationMessage *msgs); -bool ReorderBufferIsXidKnown(ReorderBuffer *, TransactionId xid); +void ReorderBufferProcessXid(ReorderBuffer *, TransactionId xid, XLogRecPtr lsn); void ReorderBufferXidSetCatalogChanges(ReorderBuffer *, TransactionId xid, XLogRecPtr lsn); bool ReorderBufferXidHasCatalogChanges(ReorderBuffer *, TransactionId xid); bool ReorderBufferXidHasBaseSnapshot(ReorderBuffer *, TransactionId xid);
^ permalink raw reply [nested|flat] 9+ messages in thread

* Re: BUG #13844: Logical decoding bug with subxact + row locking
  2016-01-05 03:32 BUG #13844: Logical decoding bug with subxact + row locking jarred@webriots.com
  2016-02-07 11:42 ` Re: BUG #13844: Logical decoding bug with subxact + row locking Andres Freund <andres@anarazel.de>
  2016-03-04 15:36   ` Re: BUG #13844: Logical decoding bug with subxact + row locking Alvaro Herrera <alvherre@2ndquadrant.com>
  2016-03-04 18:12     ` Re: BUG #13844: Logical decoding bug with subxact + row locking Andres Freund <andres@anarazel.de>
@ 2016-03-04 18:28       ` Alvaro Herrera <alvherre@2ndquadrant.com>
  2016-03-04 18:30         ` Re: BUG #13844: Logical decoding bug with subxact + row locking Andres Freund <andres@anarazel.de>
  0 siblings, 1 reply; 9+ messages in thread

From: Alvaro Herrera @ 2016-03-04 18:28 UTC (permalink / raw)
  To: Andres Freund <andres@anarazel.de>; +Cc: jarred@webriots.com; pgsql-bugs

Andres Freund wrote:
> On 2016-03-04 12:36:32 -0300, Alvaro Herrera wrote:

> > Any progress with this?
> 
> Yes. The attached WIP patch fixes this, and some other issues. I'm
> working on splitting it up, and adding some more testcases.

Great, thanks.  Looks interesting.  I started to write a patch today
before posting and in doing so I started to realize it was a bit bigger
than what I was doing.  I'll give it a look later.

Do you think you can get this before the next set of releases?  If you
think I can help you get this done, please let me know.

-- 
Álvaro Herrera                http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: BUG #13844: Logical decoding bug with subxact + row locking
  2016-01-05 03:32 BUG #13844: Logical decoding bug with subxact + row locking jarred@webriots.com
  2016-02-07 11:42 ` Re: BUG #13844: Logical decoding bug with subxact + row locking Andres Freund <andres@anarazel.de>
  2016-03-04 15:36   ` Re: BUG #13844: Logical decoding bug with subxact + row locking Alvaro Herrera <alvherre@2ndquadrant.com>
  2016-03-04 18:12     ` Re: BUG #13844: Logical decoding bug with subxact + row locking Andres Freund <andres@anarazel.de>
  2016-03-04 18:28       ` Re: BUG #13844: Logical decoding bug with subxact + row locking Alvaro Herrera <alvherre@2ndquadrant.com>
@ 2016-03-04 18:30         ` Andres Freund <andres@anarazel.de>
  2016-03-05 07:47           ` Re: BUG #13844: Logical decoding bug with subxact + row locking Andres Freund <andres@anarazel.de>
  0 siblings, 1 reply; 9+ messages in thread

From: Andres Freund @ 2016-03-04 18:30 UTC (permalink / raw)
  To: Alvaro Herrera <alvherre@2ndquadrant.com>; +Cc: jarred@webriots.com; pgsql-bugs

On 2016-03-04 15:28:40 -0300, Alvaro Herrera wrote:
> Do you think you can get this before the next set of releases?  If you
> think I can help you get this done, please let me know.

Yes, I hope to get it done in the next few hours.

Andres


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: BUG #13844: Logical decoding bug with subxact + row locking
  2016-01-05 03:32 BUG #13844: Logical decoding bug with subxact + row locking jarred@webriots.com
  2016-02-07 11:42 ` Re: BUG #13844: Logical decoding bug with subxact + row locking Andres Freund <andres@anarazel.de>
  2016-03-04 15:36   ` Re: BUG #13844: Logical decoding bug with subxact + row locking Alvaro Herrera <alvherre@2ndquadrant.com>
  2016-03-04 18:12     ` Re: BUG #13844: Logical decoding bug with subxact + row locking Andres Freund <andres@anarazel.de>
  2016-03-04 18:28       ` Re: BUG #13844: Logical decoding bug with subxact + row locking Alvaro Herrera <alvherre@2ndquadrant.com>
  2016-03-04 18:30         ` Re: BUG #13844: Logical decoding bug with subxact + row locking Andres Freund <andres@anarazel.de>
@ 2016-03-05 07:47           ` Andres Freund <andres@anarazel.de>
  0 siblings, 0 replies; 9+ messages in thread

From: Andres Freund @ 2016-03-05 07:47 UTC (permalink / raw)
  To: Alvaro Herrera <alvherre@2ndquadrant.com>; +Cc: jarred@webriots.com; pgsql-bugs

On 2016-03-04 10:30:02 -0800, Andres Freund wrote:
> On 2016-03-04 15:28:40 -0300, Alvaro Herrera wrote:
> > Do you think you can get this before the next set of releases?  If you
> > think I can help you get this done, please let me know.
> 
> Yes, I hope to get it done in the next few hours.

Here's updated and separated versions of patches addressing all the open
logical decoding issues that I know of. I plan to take another look over
them tomorrow and then push.  0001 is the patch addressing this specific
issue.

Regards,

Andres

Attachments:

  0001-logical-decoding-Tell-reorderbuffer-about-all-xids.patch (text/x-patch, 0B) download

^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: BUG #13844: Logical decoding bug with subxact + row locking
  2016-01-05 03:32 BUG #13844: Logical decoding bug with subxact + row locking jarred@webriots.com
@ 2016-03-06 02:18 ` Andres Freund <andres@anarazel.de>
  2 siblings, 0 replies; 9+ messages in thread

From: Andres Freund @ 2016-03-06 02:18 UTC (permalink / raw)
  To: jarred@webriots.com; +Cc: pgsql-bugs

Hi Jared,

On 2016-01-05 03:32:49 +0000, jarred@webriots.com wrote:
> The following bug has been logged on the website:
> 
> Bug reference:      13844
> Logged by:          Jarred Ward
> Email address:      jarred@webriots.com
> PostgreSQL version: 9.4.5
> Operating system:   Linux
> Description:        
> 
> This is a pretty serious logical decoding bug that bit us pretty hard
> in production.
> 
> Run the following code:
> 
>    
> ----------------------------------------------------------------------------
>     CREATE TABLE foo(a SERIAL PRIMARY KEY, b TEXT);
>      
>     SELECT pg_create_logical_replication_slot('regression_slot',
> 'test_decoding');
>      
>     INSERT INTO foo (b) VALUES ('john');
>      
>     BEGIN;
>     SELECT * FROM foo WHERE b = 'john' FOR UPDATE;
>     SAVEPOINT sp1;
>     INSERT INTO foo (b) VALUES ('jane');
>     ROLLBACK TO SAVEPOINT sp1;
>     INSERT INTO foo (b) VALUES ('doe');
>     END;
>      
>     SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL,
> NULL);
>    
> ----------------------------------------------------------------------------
> 
> The last select above should return the decoded WAL, but instead it
> returns:
> 
>    
> ----------------------------------------------------------------------------
>     ERROR:  subxact logged without previous toplevel record
>    
> ----------------------------------------------------------------------------
> 
> And the process is disconnected with no way to recover the replication
> slot without dropping and recreating.
> 
> If SELECT statement in the transaction is changed the following:
> 
>    
> ----------------------------------------------------------------------------
>     SELECT * FROM foo WHERE b = 'john' FOR UPDATE;
>    
> ----------------------------------------------------------------------------
> 
> to remove the row level locking the correct decoded change set is
> returned.

I pushed a fix for this; it'll be included in the next set of
maintenance releases which are coming up soon-ish. Sorry that it took so
long.

Thanks for the report!

Regards,

Andres


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 9+ messages in thread


end of thread, other threads:[~2016-03-06 02:18 UTC | newest]

Thread overview: 9+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2016-01-05 03:32 BUG #13844: Logical decoding bug with subxact + row locking jarred@webriots.com
2016-01-18 02:37 ` Bruce Momjian <bruce@momjian.us>
2016-02-07 11:42 ` Andres Freund <andres@anarazel.de>
2016-03-04 15:36   ` Alvaro Herrera <alvherre@2ndquadrant.com>
2016-03-04 18:12     ` Andres Freund <andres@anarazel.de>
2016-03-04 18:28       ` Alvaro Herrera <alvherre@2ndquadrant.com>
2016-03-04 18:30         ` Andres Freund <andres@anarazel.de>
2016-03-05 07:47           ` Andres Freund <andres@anarazel.de>
2016-03-06 02:18 ` Andres Freund <andres@anarazel.de>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox