Bug in gin insert redo code path during re-compression of empty gin data leaf pages
Hi,
We came across an issue during replay of a gin insert record on a pre-9.4 uncompressed data leaf page that does not have any items in it. The engine version where the replay is done is 9.6.3. The redo logic attempts to compress the existing page items before trying to insert new items from the WAL record[1]https://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/backend/access/gin/ginxlog.c;h=a40f1683dd80e4620ba6ccd93ae0e178e9616cf7;hb=refs/heads/REL9_6_STABLE#l147. In this particular case, we noticed that if the data leaf page does 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]https://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/backend/access/gin/ginpostinglist.c;h=54d5f6f630c4638474d3208489c5fb6eb1ca1eeb;hb=refs/heads/REL9_6_STABLE#l201. This will lead to incorrect data size set on the page and also makes the assertion that expects npacked and nuncompressed to be equal, false [3]https://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/backend/access/gin/ginxlog.c;h=a40f1683dd80e4620ba6ccd93ae0e178e9616cf7;hb=refs/heads/REL9_6_STABLE#l163.
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]https://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/backend/access/gin/ginvacuum.c;h=31b765287f6ac9da798d2637046e706f67bdac7a;hb=refs/heads/REL9_3_STABLE#l430. These empty pages can be part of the database after upgrading to 9.6.3. We verified this by doing the following test:
Step 1: On Postgresql 9.3, create a table with gin index, insert and delete some data followed by vacuum.
* CREATE EXTENSION IF NOT EXISTS pg_trgm;
* CREATE TABLE gin_test (first_name text, last_name text);
* INSERT INTO gin_test SELECT md5(random()::text), md5(random()::text) FROM (SELECT * FROM generate_series(1,100000) AS id) AS x;
* CREATE INDEX gin_t_idx ON gin_test USING gin (first_name gin_trgm_ops, last_name gin_trgm_ops) WITH (fastupdate = OFF);
* DELETE FROM gin_test;
* VACUUM gin_test;
Step 2: Upgrade the database to 9.6.3 using pg_upgrade
* pg_upgrade -b $93_POSTGRES_DIR/bin -B $96_POSTGRES_DIR/bin -d $93_DATADIR -D $96_DATADIR
Step 3: Check the gin page opaque information of a uncompressed data leaf page to see if it has no items
* CREATE EXTENSION IF NOT EXISTS pageinspect;
* SELECT * FROM gin_metapage_info(get_raw_page('gin_t_idx', 0)); >>> Get number of data pages
* SELECT * FROM gin_page_opaque_info(get_raw_page('gin_t_idx', 8000)); >>> some block number close to total number of blocks
* This will return a page that has rightlink 2^32-1, maxoff 0 and flags {data,leaf}.
Attached is a patch that will skip compression of the posting list in the case that the data leaf page is empty.
Please let us know any comments or feedback. Thanks!
Best
Siva
*References:*
[1]: https://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/backend/access/gin/ginxlog.c;h=a40f1683dd80e4620ba6ccd93ae0e178e9616cf7;hb=refs/heads/REL9_6_STABLE#l147
[2]: https://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/backend/access/gin/ginpostinglist.c;h=54d5f6f630c4638474d3208489c5fb6eb1ca1eeb;hb=refs/heads/REL9_6_STABLE#l201
[3]: https://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/backend/access/gin/ginxlog.c;h=a40f1683dd80e4620ba6ccd93ae0e178e9616cf7;hb=refs/heads/REL9_6_STABLE#l163
[4]: https://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/backend/access/gin/ginvacuum.c;h=31b765287f6ac9da798d2637046e706f67bdac7a;hb=refs/heads/REL9_3_STABLE#l430
Attachments:
skip-compression-of-pre-9.4-empty-gin-data-leaf-page-replay_v1.patchapplication/octet-stream; name=skip-compression-of-pre-9.4-empty-gin-data-leaf-page-replay_v1.patchDownload+19-7
Hi!
On Wed, Jul 18, 2018 at 1:40 AM R, Siva <sivasubr@amazon.com> wrote:
We came across an issue during replay of a gin insert record on a pre-9.4
uncompressed data leaf page that does not have any items in it. The engine
version where the replay is done is 9.6.3. The redo logic attempts to
compress 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 does 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 nuncompressed to be equal, false [3].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
pages can be part of the database after upgrading to 9.6.3. We verified
this by doing the following test:
Thank you for reporting this bug. At the first glance it seems that your
proposed fix is correct. I'll review it in more details and commit.
------
Alexander Korotkov
Postgres Professional: http://www.postgrespro.com
The Russian Postgres Company
Hi!
On Wed, Jul 18, 2018 at 11:59 AM Alexander Korotkov <
a.korotkov@postgrespro.ru> wrote:
On Wed, Jul 18, 2018 at 1:40 AM R, Siva <sivasubr@amazon.com> wrote:
We came across an issue during replay of a gin insert record on a pre-9.4
uncompressed data leaf page that does not have any items in it. The engine
version where the replay is done is 9.6.3. The redo logic attempts to
compress 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 does 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 nuncompressed to be equal, false [3].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
pages can be part of the database after upgrading to 9.6.3. We verified
this by doing the following test:Thank you for reporting this bug. At the first glance it seems that your
proposed fix is correct. I'll review it in more details and commit.
I've managed to reproduce assertion failure while upgrading from 9.3 to
master. The steps to reproduction are following.
1. On 9.3 run following.
* create table t as (select array[1] as a from generate_series(1,2000));
* create index t_idx on t using gin(a) with (fastupdate= off);
* delete from t;
* vacuum t;
2. pg_upgrade from 9.3 to master
3. On upgraded master set full_page_writes = off and run it
4. On master run following
* insert into t (select array[1] as a from generate_series(1,2000));
5. kill postmaster with -9
2018-07-19 14:30:11.437 MSK [57946] CONTEXT: WAL redo at 0/7000190 for
Gin/INSERT: isdata: T isleaf: T 1 segments: 0 (replace)
TRAP: FailedAssertion("!(npacked == nuncompressed)", File: "ginxlog.c",
Line: 161)
After applying your patch, I still have following assertion failure.
2018-07-19 14:40:34.449 MSK [60372] LOG: redo starts at 0/7000140
TRAP: FailedAssertion("!(a_action == 2)", File: "ginxlog.c", Line: 262)
So, the issue is still persists. I'm going to investigate this bug more
and come up with updated patch.
------
Alexander Korotkov
Postgres Professional: http://www.postgrespro.com
The Russian Postgres Company
On Thu, Jul 19, 2018 at 2:43 PM Alexander Korotkov <
a.korotkov@postgrespro.ru> wrote:
On Wed, Jul 18, 2018 at 11:59 AM Alexander Korotkov <
a.korotkov@postgrespro.ru> wrote:On Wed, Jul 18, 2018 at 1:40 AM R, Siva <sivasubr@amazon.com> wrote:
We came across an issue during replay of a gin insert record on a
pre-9.4 uncompressed data leaf page that does not have any items in it. The
engine version where the replay is done is 9.6.3. The redo logic attempts
to compress 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 does 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 nuncompressed to be equal, false [3].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
pages can be part of the database after upgrading to 9.6.3. We verified
this by doing the following test:Thank you for reporting this bug. At the first glance it seems that your
proposed fix is correct. I'll review it in more details and commit.I've managed to reproduce assertion failure while upgrading from 9.3 to
master. The steps to reproduction are following.1. On 9.3 run following.
* create table t as (select array[1] as a from generate_series(1,2000));
* create index t_idx on t using gin(a) with (fastupdate= off);
* delete from t;
* vacuum t;2. pg_upgrade from 9.3 to master
3. On upgraded master set full_page_writes = off and run it
4. On master run following
* insert into t (select array[1] as a from generate_series(1,2000));
5. kill postmaster with -92018-07-19 14:30:11.437 MSK [57946] CONTEXT: WAL redo at 0/7000190 for
Gin/INSERT: isdata: T isleaf: T 1 segments: 0 (replace)
TRAP: FailedAssertion("!(npacked == nuncompressed)", File: "ginxlog.c",
Line: 161)After applying your patch, I still have following assertion failure.
2018-07-19 14:40:34.449 MSK [60372] LOG: redo starts at 0/7000140
TRAP: FailedAssertion("!(a_action == 2)", File: "ginxlog.c", Line: 262)So, the issue is still persists. I'm going to investigate this bug more
and come up with updated patch.
It appears that we need to handle empty posting list pages also
in disassembleLeaf(). Updated patch is attached. I'm going to commit it.
BTW, what is your full name for the commit message?
------
Alexander Korotkov
Postgres Professional: http://www.postgrespro.com
The Russian Postgres Company
Attachments:
skip-compression-of-pre-9.4-empty-gin-data-leaf-page-replay_v2.patchapplication/octet-stream; name=skip-compression-of-pre-9.4-empty-gin-data-leaf-page-replay_v2.patchDownload+33-14
Hi Alexander!
On Thu, Jul 19, 2018 at 2:43 PM Alexander Korotkov <a.korotkov@postgrespro.ru> wrote:
It appears that we need to handle empty posting list pages also in disassembleLeaf(). Updated patch is attached. I'm
going to commit it.
Thank you so much for reviewing the patch! You are right, we need to handle the regular insert/vacuum path for the empty page as well, thanks for fixing that. The updated patch looks good to me.
BTW, what is your full name for the commit message?
My full name is "Sivasubramanian Ramasubramanian".
Email : sivasubr@amazon.com
Thanks again!
Best
Siva
On Thu, Jul 19, 2018 at 6:17 PM R, Siva <sivasubr@amazon.com> wrote:
On Thu, Jul 19, 2018 at 2:43 PM Alexander Korotkov <
a.korotkov@postgrespro.ru> wrote:
It appears that we need to handle empty posting list pages also
in disassembleLeaf(). Updated patch is attached. I'm
going to commit it.
Thank you so much for reviewing the patch! You are right, we need to
handle the regular insert/vacuum path for the empty page as well, thanks
for fixing that. The updated patch looks good to me.BTW, what is your full name for the commit message?
My full name is "Sivasubramanian Ramasubramanian".
Email : sivasubr@amazon.com
Pushed, thanks!
------
Alexander Korotkov
Postgres Professional: http://www.postgrespro.com
The Russian Postgres Company
On 2018-Jul-19, Alexander Korotkov wrote:
Pushed, thanks!
I think you missed branch REL_11_STABLE ...
--
�lvaro Herrera https://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
Thu, Jul 19, 2018 at 9:29 PM Alvaro Herrera <alvherre@2ndquadrant.com>
wrote:
On 2018-Jul-19, Alexander Korotkov wrote:
Pushed, thanks!
I think you missed branch REL_11_STABLE ...
Thank you for noticing! I also have just discovered that while looking at
buildfarm. Fixed.
------
Alexander Korotkov
Postgres Professional: http://www.postgrespro.com
The Russian Postgres Company