Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1m1kNH-00052p-T7 for pgsql-hackers@arkaria.postgresql.org; Fri, 09 Jul 2021 06:52:36 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1m1kNG-0002pp-E5 for pgsql-hackers@arkaria.postgresql.org; Fri, 09 Jul 2021 06:52:34 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1m1kNG-0002o7-2a for pgsql-hackers@lists.postgresql.org; Fri, 09 Jul 2021 06:52:34 +0000 Received: from smtp-fw-9102.amazon.com ([207.171.184.29]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1m1kN8-0007iV-ED for pgsql-hackers@lists.postgresql.org; Fri, 09 Jul 2021 06:52:33 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1625813547; x=1657349547; h=to:cc:from:subject:message-id:date:mime-version; bh=8D6IEX6UhsPQkiVxtefquJm2PSG4aUbEZLJ2Mm7tD1I=; b=uABm+jk7yj+gLVe09zpZc+pae4VJi7lEA8REXEFWZzOMhsMBrl5E29HN 2ks85oAs4k7p5z6lctD6VZn1L00Ws+cASJTjeDHmNVGfP/7cHnRM6IwO4 YQNDf9Ab1Zh36m1OwtJ0pqDyuB3Fw4q/YXBAyNV0KhGNRDsJ4kQ9gC2q8 8=; X-Amazon-filename: v1-0001-toast-rewrite.patch X-IronPort-AV: E=Sophos;i="5.84,226,1620691200"; d="scan'208,217";a="144517943" Received: from pdx4-co-svc-p1-lb2-vlan3.amazon.com (HELO email-inbound-relay-2b-4e24fd92.us-west-2.amazon.com) ([10.25.36.214]) by smtp-border-fw-9102.sea19.amazon.com with ESMTP; 09 Jul 2021 06:51:43 +0000 Received: from EX13D03EUC003.ant.amazon.com (pdx1-ws-svc-p6-lb9-vlan2.pdx.amazon.com [10.236.137.194]) by email-inbound-relay-2b-4e24fd92.us-west-2.amazon.com (Postfix) with ESMTPS id D9C23A07F6; Fri, 9 Jul 2021 06:51:40 +0000 (UTC) Received: from 38f9d36c4abb.ant.amazon.com (10.43.160.85) by EX13D03EUC003.ant.amazon.com (10.43.164.192) with Microsoft SMTP Server (TLS) id 15.0.1497.18; Fri, 9 Jul 2021 06:51:38 +0000 To: PostgreSQL Hackers CC: Amit Kapila , "Schneider (AWS), Jeremy" From: "Drouvot, Bertrand" Subject: [bug] Logical Decoding of relation rewrite with toast does not reset toast_hash Message-ID: Date: Fri, 9 Jul 2021 08:51:34 +0200 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Thunderbird/78.11.0 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="------------22A5DB197C6CBD50E990D3A7" Content-Language: en-US X-Originating-IP: [10.43.160.85] X-ClientProxiedBy: EX13D37UWC004.ant.amazon.com (10.43.162.212) To EX13D03EUC003.ant.amazon.com (10.43.164.192) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --------------22A5DB197C6CBD50E990D3A7 Content-Type: multipart/alternative; boundary="------------A2EA60988BD48073FE3EA4EE" --------------A2EA60988BD48073FE3EA4EE Content-Type: text/plain; charset="utf-8"; format=flowed Content-Transfer-Encoding: 8bit Hi hackers, During logical decoding, we have recently observed (and reported in [1]) errors like: ERROR:  could not open relation with OID 0 After investigating a recent issue on a PostgreSQL database that was encountering this error, we found that the logical decoding of relation rewrite with toast could produce this error without resetting the toast_hash. We were able to create this repro of the error: postgres=# \! cat bdt_repro.sql select pg_create_logical_replication_slot('bdt_slot','test_decoding'); CREATE TABLE tbl1 (a INT, b TEXT); CREATE TABLE tbl2 (a INT); ALTER TABLE tbl1 ALTER COLUMN b SET STORAGE EXTERNAL; BEGIN; INSERT INTO tbl1 VALUES(1, repeat('a', 4000)) ; ALTER TABLE tbl1 ADD COLUMN id serial primary key; INSERT INTO tbl2 VALUES(1); commit; select * from pg_logical_slot_get_changes('bdt_slot', null, null); That ends up on 12.5 with: ERROR:  could not open relation with OID 0 And on current master with: server closed the connection unexpectedly     This probably means the server terminated abnormally     before or while processing the request. The connection to the server was lost. Attempting reset: Failed. The issue has been introduced by 325f2ec555 (and more precisely by its change in reorderbuffer.c), so it does affect pre v11 versions: git branch -r --contains 325f2ec555   origin/HEAD -> origin/master   origin/REL_11_STABLE   origin/REL_12_STABLE   origin/REL_13_STABLE   origin/REL_14_STABLE   origin/master The fact that current master is producing a different behavior than 12.5 (for example) is due to 4daa140a2f that generates a failed assertion in such a case (after going to the code path that should print out the ERROR): #2  0x0000000000b29fab in ExceptionalCondition (conditionName=0xce6850 "(rb->size >= sz) && (txn->size >= sz)", errorType=0xce5f84 "FailedAssertion", fileName=0xce5fd0 "reorderbuffer.c", lineNumber=3141) at assert.c:69 #3  0x00000000008ff1fb in ReorderBufferChangeMemoryUpdate (rb=0x11a7a40, change=0x11c94b8, addition=false) at reorderbuffer.c:3141 #4  0x00000000008fab27 in ReorderBufferReturnChange (rb=0x11a7a40, change=0x11c94b8, upd_mem=true) at reorderbuffer.c:477 #5  0x0000000000902ec1 in ReorderBufferToastReset (rb=0x11a7a40, txn=0x11b1998) at reorderbuffer.c:4799 #6  0x00000000008faaa2 in ReorderBufferReturnTXN (rb=0x11a7a40, txn=0x11b1998) at reorderbuffer.c:448 #7  0x00000000008fc95b in ReorderBufferCleanupTXN (rb=0x11a7a40, txn=0x11b1998) at reorderbuffer.c:1540 I am adding Amit to this thread to make him aware than this is related to the recent issue Jeremy and I were talking about in [1] - which we now believe is not linked to the logical decoding and speculative insert bug fixed in 4daa140a2f but likely to this new toast rewrite bug. Please find enclosed a patch proposal to: * Avoid the failed assertion on current master and generate the error message instead (should the code reach that stage). * Reset the toast_hash in case of relation rewrite with toast (so that the logical decoding in the above repro is working). I am adding this patch to the next commitfest. Thanks Bertrand [1]: https://www.postgresql.org/message-id/CAA4eK1KcUPwwhDVhJmdQExc09AzEBZMGbOa-u3DYaJs1zzfEnA%40mail.gmail.com --------------A2EA60988BD48073FE3EA4EE Content-Type: text/html; charset="utf-8" Content-Transfer-Encoding: 8bit

Hi hackers,

During logical decoding, we have recently observed (and reported in [1]) errors like:

ERROR:  could not open relation with OID 0

After investigating a recent issue on a PostgreSQL database that was encountering this error, we found that the logical decoding of relation rewrite with toast could produce this error without resetting the toast_hash.

We were able to create this repro of the error:

postgres=# \! cat bdt_repro.sql
select pg_create_logical_replication_slot('bdt_slot','test_decoding');
CREATE TABLE tbl1 (a INT, b TEXT);
CREATE TABLE tbl2 (a INT);
ALTER TABLE tbl1 ALTER COLUMN b SET STORAGE EXTERNAL;
BEGIN;
INSERT INTO tbl1 VALUES(1, repeat('a', 4000)) ;
ALTER TABLE tbl1 ADD COLUMN id serial primary key;
INSERT INTO tbl2 VALUES(1);
commit;
select * from pg_logical_slot_get_changes('bdt_slot', null, null);

That ends up on 12.5 with:

ERROR:  could not open relation with OID 0

And on current master with:

server closed the connection unexpectedly
    This probably means the server terminated abnormally

    before or while processing the request.
The connection to the server was lost. Attempting reset: Failed.

The issue has been introduced by 325f2ec555 (and more precisely by its change in reorderbuffer.c), so it does affect pre v11 versions:

git branch -r --contains 325f2ec555
  origin/HEAD -> origin/master
  origin/REL_11_STABLE
  origin/REL_12_STABLE
  origin/REL_13_STABLE
  origin/REL_14_STABLE
  origin/master

The fact that current master is producing a different behavior than 12.5 (for example) is due to 4daa140a2f that generates a failed assertion in such a case (after going to the code path that should print out the ERROR):

#2  0x0000000000b29fab in ExceptionalCondition (conditionName=0xce6850 "(rb->size >= sz) && (txn->size >= sz)", errorType=0xce5f84 "FailedAssertion", fileName=0xce5fd0 "reorderbuffer.c", lineNumber=3141) at assert.c:69
#3  0x00000000008ff1fb in ReorderBufferChangeMemoryUpdate (rb=0x11a7a40, change=0x11c94b8, addition=false) at reorderbuffer.c:3141
#4  0x00000000008fab27 in ReorderBufferReturnChange (rb=0x11a7a40, change=0x11c94b8, upd_mem=true) at reorderbuffer.c:477
#5  0x0000000000902ec1 in ReorderBufferToastReset (rb=0x11a7a40, txn=0x11b1998) at reorderbuffer.c:4799
#6  0x00000000008faaa2 in ReorderBufferReturnTXN (rb=0x11a7a40, txn=0x11b1998) at reorderbuffer.c:448
#7  0x00000000008fc95b in ReorderBufferCleanupTXN (rb=0x11a7a40, txn=0x11b1998) at reorderbuffer.c:1540

I am adding Amit to this thread to make him aware than this is related to the recent issue Jeremy and I were talking about in [1] - which we now believe is not linked to the logical decoding and speculative insert bug fixed in 4daa140a2f but likely to this new toast rewrite bug.

Please find enclosed a patch proposal to:

* Avoid the failed assertion on current master and generate the error message instead (should the code reach that stage).
* Reset the toast_hash in case of relation rewrite with toast (so that the logical decoding in the above repro is working).

I am adding this patch to the next commitfest.

Thanks
Bertrand

[1]: https://www.postgresql.org/message-id/CAA4eK1KcUPwwhDVhJmdQExc09AzEBZMGbOa-u3DYaJs1zzfEnA%40mail.gmail.com
--------------A2EA60988BD48073FE3EA4EE-- --------------22A5DB197C6CBD50E990D3A7 Content-Type: text/plain; charset="UTF-8"; x-mac-type=0; x-mac-creator=0; name="v1-0001-toast-rewrite.patch" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="v1-0001-toast-rewrite.patch" ZGlmZiAtLWdpdCBhL3NyYy9iYWNrZW5kL3JlcGxpY2F0aW9uL2xvZ2ljYWwvcmVvcmRlcmJ1 ZmZlci5jIGIvc3JjL2JhY2tlbmQvcmVwbGljYXRpb24vbG9naWNhbC9yZW9yZGVyYnVmZmVy LmMKaW5kZXggMWI0ZjRhNTI4YS4uZjFhODlhM2Q4NSAxMDA2NDQKLS0tIGEvc3J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