Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e797c7a

Browse files
committed
BRIN: mask BRIN_EVACUATE_PAGE for WAL consistency checking
That bit is unlogged and therefore it's wrong to consider it in WAL page comparison. Add a test that tickles the case, as branch testing technology allows. This has been a problem ever since wal consistency checking was introduced (commit a507b86 for pg10), so backpatch to all supported branches. Author: 王海洋 (Haiyang Wang) <[email protected]> Reviewed-by: Kyotaro Horiguchi <[email protected]> Discussion: https://postgr.es/m/CACciXAD2UvLMOhc4jX9VvOKt7DtYLr3OYRBhvOZ-jRxtzc_7Jg@mail.gmail.com Discussion: https://postgr.es/m/CACciXADOfErX9Bx0nzE_SkdfXr6Bbpo5R=v_B6MUTEYW4ya+cg@mail.gmail.com
1 parent 2ffcb7d commit e797c7a

File tree

3 files changed

+76
-1
lines changed

3 files changed

+76
-1
lines changed

src/backend/access/brin/brin_pageops.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,12 @@ brin_start_evacuating_page(Relation idxRel, Buffer buf)
528528
lp = PageGetItemId(page, off);
529529
if (ItemIdIsUsed(lp))
530530
{
531-
/* prevent other backends from adding more stuff to this page */
531+
/*
532+
* Prevent other backends from adding more stuff to this page:
533+
* BRIN_EVACUATE_PAGE informs br_page_get_freespace that this page
534+
* can no longer be used to add new tuples. Note that this flag
535+
* is not WAL-logged, except accidentally.
536+
*/
532537
BrinPageFlags(page) |= BRIN_EVACUATE_PAGE;
533538
MarkBufferDirtyHint(buf, true);
534539

src/backend/access/brin/brin_xlog.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,4 +341,10 @@ brin_mask(char *pagedata, BlockNumber blkno)
341341
/* Regular brin pages contain unused space which needs to be masked. */
342342
mask_unused_space(page);
343343
}
344+
345+
/*
346+
* BRIN_EVACUATE_PAGE is not WAL-logged, since it's of no use in recovery.
347+
* Mask it. See brin_start_evacuating_page() for details.
348+
*/
349+
BrinPageFlags(page) &= ~BRIN_EVACUATE_PAGE;
344350
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Copyright (c) 2021-2022, PostgreSQL Global Development Group
2+
3+
# Verify WAL consistency
4+
5+
use strict;
6+
use warnings;
7+
8+
use PostgreSQL::Test::Utils;
9+
use Test::More;
10+
use PostgreSQL::Test::Cluster;
11+
12+
# Set up primary
13+
my $whiskey = PostgreSQL::Test::Cluster->new('whiskey');
14+
$whiskey->init(allows_streaming => 1);
15+
$whiskey->append_conf('postgresql.conf', 'wal_consistency_checking = brin');
16+
$whiskey->start;
17+
$whiskey->safe_psql('postgres', 'create extension pageinspect');
18+
is( $whiskey->psql(
19+
'postgres',
20+
qq[SELECT pg_create_physical_replication_slot('standby_1');]),
21+
0,
22+
'physical slot created on primary');
23+
24+
# Take backup
25+
my $backup_name = 'brinbkp';
26+
$whiskey->backup($backup_name);
27+
28+
# Create streaming standby linking to primary
29+
my $charlie = PostgreSQL::Test::Cluster->new('charlie');
30+
$charlie->init_from_backup($whiskey, $backup_name, has_streaming => 1);
31+
$charlie->append_conf('recovery.conf', 'primary_slot_name = standby_1');
32+
$charlie->start;
33+
34+
# Now write some WAL in the primary
35+
36+
$whiskey->safe_psql(
37+
'postgres', qq{
38+
create table tbl_timestamp0 (d1 timestamp(0) without time zone) with (fillfactor=10);
39+
create index on tbl_timestamp0 using brin (d1) with (pages_per_range = 1, autosummarize=false);
40+
});
41+
# Run a loop that will end when the second revmap page is created
42+
$whiskey->safe_psql(
43+
'postgres', q{
44+
do
45+
$$
46+
declare
47+
current timestamp with time zone := '2019-03-27 08:14:01.123456789 America/Punta_Arenas';
48+
begin
49+
loop
50+
insert into tbl_timestamp0 select i from
51+
generate_series(current, current + interval '1 day', '28 seconds') i;
52+
perform brin_summarize_new_values('tbl_timestamp0_d1_idx');
53+
if (brin_metapage_info(get_raw_page('tbl_timestamp0_d1_idx', 0))).lastrevmappage > 1 then
54+
exit;
55+
end if;
56+
current := current + interval '1 day';
57+
end loop;
58+
end
59+
$$;
60+
});
61+
62+
$whiskey->wait_for_catchup($charlie, 'replay', $whiskey->lsn('insert'));
63+
64+
done_testing();

0 commit comments

Comments
 (0)