Skip to content

Commit 23ddadf

Browse files
committed
BRIN autosummarization may need a snapshot
It's possible to define BRIN indexes on functions that require a snapshot to run, but the autosummarization feature introduced by commit 7526e10 fails to provide one. This causes autovacuum to leave a BRIN placeholder tuple behind after a failed work-item execution, making such indexes less efficient. Repair by obtaining a snapshot prior to running the task, and add a test to verify this behavior. Author: Álvaro Herrera <alvherre@kurilemu.de> Reported-by: Giovanni Fabris <giovanni.fabris@icon.it> Reported-by: Arthur Nascimento <tureba@gmail.com> Backpatch-through: 13 Discussion: https://postgr.es/m/202511031106.h4fwyuyui6fz@alvherre.pgsql
1 parent da5ea6c commit 23ddadf

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

src/backend/postmaster/autovacuum.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2615,7 +2615,9 @@ do_autovacuum(void)
26152615
workitem->avw_active = true;
26162616
LWLockRelease(AutovacuumLock);
26172617

2618+
PushActiveSnapshot(GetTransactionSnapshot());
26182619
perform_work_item(workitem);
2620+
PopActiveSnapshot();
26192621

26202622
/*
26212623
* Check for config changes before acquiring lock for further jobs.

src/test/modules/brin/t/01_workitems.pl

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,54 @@
2424
create index brin_wi_idx on brin_wi using brin (a) with (pages_per_range=1, autosummarize=on);
2525
'
2626
);
27+
# Another table with an index that requires a snapshot to run
28+
$node->safe_psql(
29+
'postgres',
30+
'create table journal (d timestamp) with (fillfactor = 10);
31+
create function packdate(d timestamp) returns text language plpgsql
32+
as $$ begin return to_char(d, \'yyyymm\'); end; $$
33+
returns null on null input immutable;
34+
create index brin_packdate_idx on journal using brin (packdate(d))
35+
with (autosummarize = on, pages_per_range = 1);
36+
'
37+
);
38+
2739
my $count = $node->safe_psql('postgres',
2840
"select count(*) from brin_page_items(get_raw_page('brin_wi_idx', 2), 'brin_wi_idx'::regclass)"
2941
);
30-
is($count, '1', "initial index state is correct");
42+
is($count, '1', "initial brin_wi_index index state is correct");
43+
$count = $node->safe_psql('postgres',
44+
"select count(*) from brin_page_items(get_raw_page('brin_packdate_idx', 2), 'brin_packdate_idx'::regclass)"
45+
);
46+
is($count, '1', "initial brin_packdate_idx index state is correct");
3147

3248
$node->safe_psql('postgres',
3349
'insert into brin_wi select * from generate_series(1, 100)');
50+
$node->safe_psql('postgres',
51+
"insert into journal select * from generate_series(timestamp '1976-08-01', '1976-10-28', '1 day')");
3452

3553
$node->poll_query_until(
3654
'postgres',
3755
"select count(*) > 1 from brin_page_items(get_raw_page('brin_wi_idx', 2), 'brin_wi_idx'::regclass)",
3856
't');
3957

4058
$count = $node->safe_psql('postgres',
41-
"select count(*) > 1 from brin_page_items(get_raw_page('brin_wi_idx', 2), 'brin_wi_idx'::regclass)"
59+
"select count(*) from brin_page_items(get_raw_page('brin_wi_idx', 2), 'brin_wi_idx'::regclass)
60+
where not placeholder;"
4261
);
43-
is($count, 't', "index got summarized");
62+
cmp_ok($count, '>', '1', "$count brin_wi_idx ranges got summarized");
63+
64+
$node->poll_query_until(
65+
'postgres',
66+
"select count(*) > 1 from brin_page_items(get_raw_page('brin_packdate_idx', 2), 'brin_packdate_idx'::regclass)",
67+
't');
68+
69+
$count = $node->safe_psql('postgres',
70+
"select count(*) from brin_page_items(get_raw_page('brin_packdate_idx', 2), 'brin_packdate_idx'::regclass)
71+
where not placeholder;"
72+
);
73+
cmp_ok($count, '>', '1', "$count brin_packdate_idx ranges got summarized");
74+
4475
$node->stop;
4576

4677
done_testing();

0 commit comments

Comments
 (0)