Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1ffYdy-0006v2-S5 for pgsql-hackers@arkaria.postgresql.org; Tue, 17 Jul 2018 22:40:31 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1ffYdu-0000dG-He for pgsql-hackers@arkaria.postgresql.org; Tue, 17 Jul 2018 22:40:26 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1ffYdu-0000d8-8L for pgsql-hackers@lists.postgresql.org; Tue, 17 Jul 2018 22:40:26 +0000 Received: from smtp-fw-4101.amazon.com ([72.21.198.25]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1ffYdl-0004G1-Qv for pgsql-hackers@postgresql.org; Tue, 17 Jul 2018 22:40:25 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1531867217; x=1563403217; h=from:to:subject:date:message-id:mime-version; bh=L8HRmRwD3ChaASh5OL8Ay8gPn8D3z8YGB0oHPYFYRIw=; b=cPQF0iRuqGqd33+Fb0/RraJbA65Wc8HZXEaXF+j8pERQn2NncrVeBmac P55UYBa3r2WhabMfwK6YqgaiPyqFN7K7yNP9gNsEHX9+qxGw1lNMPby5V cAI7sxMi3v3NoNIRVYSygZZDmfR0tHgsVg5wI5D3R4+uaNufB5fNJwpFj U=; X-Amazon-filename: skip-compression-of-pre-9.4-empty-gin-data-leaf-page-replay_v1.patch X-IronPort-AV: E=Sophos;i="5.51,367,1526342400"; d="scan'208,223";a="729300476" Received: from iad6-co-svc-p1-lb1-vlan3.amazon.com (HELO email-inbound-relay-2a-f14f4a47.us-west-2.amazon.com) ([10.124.125.6]) by smtp-border-fw-out-4101.iad4.amazon.com with ESMTP/TLS/DHE-RSA-AES256-SHA; 17 Jul 2018 22:40:14 +0000 Received: from EX13MTAUWB001.ant.amazon.com (pdx1-ws-svc-p6-lb9-vlan3.pdx.amazon.com [10.236.137.198]) by email-inbound-relay-2a-f14f4a47.us-west-2.amazon.com (8.14.7/8.14.7) with ESMTP id w6HMeDfc096013 (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=FAIL) for ; Tue, 17 Jul 2018 22:40:14 GMT Received: from EX13D08UWB001.ant.amazon.com (10.43.161.104) by EX13MTAUWB001.ant.amazon.com (10.43.161.207) with Microsoft SMTP Server (TLS) id 15.0.1367.3; Tue, 17 Jul 2018 22:40:13 +0000 Received: from EX13D08UWB001.ant.amazon.com (10.43.161.104) by EX13D08UWB001.ant.amazon.com (10.43.161.104) with Microsoft SMTP Server (TLS) id 15.0.1367.3; Tue, 17 Jul 2018 22:40:12 +0000 Received: from EX13D08UWB001.ant.amazon.com ([10.43.161.104]) by EX13D08UWB001.ant.amazon.com ([10.43.161.104]) with mapi id 15.00.1367.000; Tue, 17 Jul 2018 22:40:12 +0000 From: "R, Siva" To: pgsql-hackers Subject: Bug in gin insert redo code path during re-compression of empty gin data leaf pages Thread-Topic: Bug in gin insert redo code path during re-compression of empty gin data leaf pages Thread-Index: AQHUHhriTtCi9KoAzkiIHCifB95wmw== Date: Tue, 17 Jul 2018 22:40:12 +0000 Message-ID: <1531867212836.63354@amazon.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: yes X-MS-TNEF-Correlator: x-ms-exchange-transport-fromentityheader: Hosted x-originating-ip: [10.43.162.37] Content-Type: multipart/mixed; boundary="_002_153186721283663354amazoncom_" MIME-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --_002_153186721283663354amazoncom_ Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Hi,=0A= We came across an issue during replay of a gin insert record on a pre-9.4 u= ncompressed data leaf page that does not have any items in it. The engine v= ersion where the replay is done is 9.6.3. The redo logic attempts to compre= ss the existing page items before trying to insert new items from the WAL = record[1]. In this particular case, we noticed that if the data leaf page d= oes not have any items in it (see below on how such a page can be present),= the compression should not be attempted as the algorithm expects the page = to have at least one item [2]. This will lead to incorrect data size set on= the page and also makes the assertion that expects npacked and nuncompress= ed to be equal, false [3].=0A= =0A= In Postgres 9.3, when the gin posting tree is vacuumed, the pages that are = in the leftmost and rightmost branches are not deleted [4]. These empty pag= es can be part of the database after upgrading to 9.6.3. We verified this b= y doing the following test:=0A= =0A= Step 1: On Postgresql 9.3, create a table with gin index, insert and delete= some data followed by vacuum.=0A= =0A= * CREATE EXTENSION IF NOT EXISTS pg_trgm;=0A= * CREATE TABLE gin_test (first_name text, last_name text);=0A= * INSERT INTO gin_test SELECT md5(random()::text), md5(random()::text) FROM= (SELECT * FROM generate_series(1,100000) AS id) AS x;=0A= * CREATE INDEX gin_t_idx ON gin_test USING gin (first_name gin_trgm_ops, la= st_name gin_trgm_ops) WITH (fastupdate =3D OFF);=0A= * DELETE FROM gin_test;=0A= * VACUUM gin_test;=0A= =0A= =0A= Step 2: Upgrade the database to 9.6.3 using pg_upgrade=0A= =0A= * pg_upgrade -b $93_POSTGRES_DIR/bin -B $96_POSTGRES_DIR/bin -d $93_DATADIR= -D $96_DATADIR=0A= =0A= =0A= Step 3: Check the gin page opaque information of a uncompressed data leaf p= age to see if it has no items=0A= =0A= * CREATE EXTENSION IF NOT EXISTS pageinspect;=0A= * SELECT * FROM gin_metapage_info(get_raw_page('gin_t_idx', 0)); >>> Get n= umber of data pages=0A= * SELECT * FROM gin_page_opaque_info(get_raw_page('gin_t_idx', 8000)); >>> = some block number close to total number of blocks=0A= * This will return a page that has rightlink 2^32-1, maxoff 0 and flags= {data,leaf}.=0A= =0A= Attached is a patch that will skip compression of the posting list in the c= ase that the data leaf page is empty.=0A= Please let us know any comments or feedback. Thanks!=0A= =0A= Best=0A= Siva=0A= =0A= =0A= *References:*=0A= [1] - https://git.postgresql.org/gitweb/?p=3Dpostgresql.git;a=3Dblob;f=3Dsr= c/backend/access/gin/ginxlog.c;h=3Da40f1683dd80e4620ba6ccd93ae0e178e9616cf7= ;hb=3Drefs/heads/REL9_6_STABLE#l147=0A= [2] - https://git.postgresql.org/gitweb/?p=3Dpostgresql.git;a=3Dblob;f=3Dsr= c/backend/access/gin/ginpostinglist.c;h=3D54d5f6f630c4638474d3208489c5fb6eb= 1ca1eeb;hb=3Drefs/heads/REL9_6_STABLE#l201=0A= [3] - https://git.postgresql.org/gitweb/?p=3Dpostgresql.git;a=3Dblob;f=3Dsr= c/backend/access/gin/ginxlog.c;h=3Da40f1683dd80e4620ba6ccd93ae0e178e9616cf7= ;hb=3Drefs/heads/REL9_6_STABLE#l163=0A= [4] - https://git.postgresql.org/gitweb/?p=3Dpostgresql.git;a=3Dblob;f=3Dsr= c/backend/access/gin/ginvacuum.c;h=3D31b765287f6ac9da798d2637046e706f67bdac= 7a;hb=3Drefs/heads/REL9_3_STABLE#l430= --_002_153186721283663354amazoncom_ Content-Type: application/octet-stream; name="skip-compression-of-pre-9.4-empty-gin-data-leaf-page-replay_v1.patch" Content-Description: skip-compression-of-pre-9.4-empty-gin-data-leaf-page-replay_v1.patch Content-Disposition: attachment; filename="skip-compression-of-pre-9.4-empty-gin-data-leaf-page-replay_v1.patch"; size=1766; creation-date="Tue, 17 Jul 2018 22:37:40 GMT"; modification-date="Tue, 17 Jul 2018 22:37:40 GMT" Content-Transfer-Encoding: base64 RnJvbSA5NTZhNTMzYzVlYzMxNzE0ODU2MjQ0NjFjYzEzZjc4NmRiZmMxNWU1IE1vbiBTZXAgMTcg MDA6MDA6MDAgMjAwMQpGcm9tOiBTaXZhIFIgPHNpdmFzdWJyQGFtYXpvbi5jb20+CkRhdGU6IFR1 ZSwgMTcgSnVsIDIwMT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