Skip to content

Commit 2e0b5d2

Browse files
committed
Avoid scribbling of VACUUM options
This fixes two issues with the handling of VacuumParams in vacuum_rel(). This code path has the idea to change the passed-in pointer of VacuumParams for the "truncate" and "index_cleanup" options for the relation worked on, impacting the two following scenarios where incorrect options may be used because a VacuumParams pointer is shared across multiple relations: - Multiple relations in a single VACUUM command. - TOAST relations vacuumed with their main relation. The problem is avoided by providing to the two callers of vacuum_rel() copies of VacuumParams, before the pointer is updated for the "truncate" and "index_cleanup" options. The refactoring of the VACUUM option and parameters done in 0d83138 did not introduce an issue, but it has encouraged the problem we are dealing with in this commit, with b84dbc8 for "truncate" and a96c41f for "index_cleanup" that have been added a couple of years after the initial refactoring. HEAD will be improved with a different patch that hardens the uses of VacuumParams across the tree. This cannot be backpatched as it introduces an ABI breakage. The backend portion of the patch has been authored by Nathan, while I have implemented the tests. The tests rely on injection points to check the option values, making them faster, more reliable than the tests originally proposed by Shihao, and they also provide more coverage. This part can only be backpatched down to v17. Reported-by: Shihao Zhong <zhong950419@gmail.com> Author: Nathan Bossart <nathandbossart@gmail.com> Co-authored-by: Michael Paquier <michael@paquier.xyz> Discussion: https://postgr.es/m/CAGRkXqTo+aK=GTy5pSc-9cy8H2F2TJvcrZ-zXEiNJj93np1UUw@mail.gmail.com Backpatch-through: 13
1 parent 9f33300 commit 2e0b5d2

File tree

5 files changed

+175
-5
lines changed

5 files changed

+175
-5
lines changed

src/backend/commands/vacuum.c

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
#include "utils/fmgroids.h"
5757
#include "utils/guc.h"
5858
#include "utils/guc_hooks.h"
59+
#include "utils/injection_point.h"
5960
#include "utils/memutils.h"
6061
#include "utils/snapmgr.h"
6162
#include "utils/syscache.h"
@@ -619,7 +620,15 @@ vacuum(List *relations, VacuumParams *params, BufferAccessStrategy bstrategy,
619620

620621
if (params->options & VACOPT_VACUUM)
621622
{
622-
if (!vacuum_rel(vrel->oid, vrel->relation, params, bstrategy))
623+
VacuumParams params_copy;
624+
625+
/*
626+
* vacuum_rel() scribbles on the parameters, so give it a copy
627+
* to avoid affecting other relations.
628+
*/
629+
memcpy(&params_copy, params, sizeof(VacuumParams));
630+
631+
if (!vacuum_rel(vrel->oid, vrel->relation, &params_copy, bstrategy))
623632
continue;
624633
}
625634

@@ -1972,9 +1981,16 @@ vacuum_rel(Oid relid, RangeVar *relation, VacuumParams *params,
19721981
Oid save_userid;
19731982
int save_sec_context;
19741983
int save_nestlevel;
1984+
VacuumParams toast_vacuum_params;
19751985

19761986
Assert(params != NULL);
19771987

1988+
/*
1989+
* This function scribbles on the parameters, so make a copy early to
1990+
* avoid affecting the TOAST table (if we do end up recursing to it).
1991+
*/
1992+
memcpy(&toast_vacuum_params, params, sizeof(VacuumParams));
1993+
19781994
/* Begin a transaction for vacuuming this relation */
19791995
StartTransactionCommand();
19801996

@@ -2155,6 +2171,15 @@ vacuum_rel(Oid relid, RangeVar *relation, VacuumParams *params,
21552171
}
21562172
}
21572173

2174+
#ifdef USE_INJECTION_POINTS
2175+
if (params->index_cleanup == VACOPTVALUE_AUTO)
2176+
INJECTION_POINT("vacuum-index-cleanup-auto");
2177+
else if (params->index_cleanup == VACOPTVALUE_DISABLED)
2178+
INJECTION_POINT("vacuum-index-cleanup-disabled");
2179+
else if (params->index_cleanup == VACOPTVALUE_ENABLED)
2180+
INJECTION_POINT("vacuum-index-cleanup-enabled");
2181+
#endif
2182+
21582183
/*
21592184
* Set truncate option based on truncate reloption if it wasn't specified
21602185
* in VACUUM command, or when running in an autovacuum worker
@@ -2168,6 +2193,15 @@ vacuum_rel(Oid relid, RangeVar *relation, VacuumParams *params,
21682193
params->truncate = VACOPTVALUE_DISABLED;
21692194
}
21702195

2196+
#ifdef USE_INJECTION_POINTS
2197+
if (params->truncate == VACOPTVALUE_AUTO)
2198+
INJECTION_POINT("vacuum-truncate-auto");
2199+
else if (params->truncate == VACOPTVALUE_DISABLED)
2200+
INJECTION_POINT("vacuum-truncate-disabled");
2201+
else if (params->truncate == VACOPTVALUE_ENABLED)
2202+
INJECTION_POINT("vacuum-truncate-enabled");
2203+
#endif
2204+
21712205
/*
21722206
* Remember the relation's TOAST relation for later, if the caller asked
21732207
* us to process it. In VACUUM FULL, though, the toast table is
@@ -2247,15 +2281,12 @@ vacuum_rel(Oid relid, RangeVar *relation, VacuumParams *params,
22472281
*/
22482282
if (toast_relid != InvalidOid)
22492283
{
2250-
VacuumParams toast_vacuum_params;
2251-
22522284
/*
22532285
* Force VACOPT_PROCESS_MAIN so vacuum_rel() processes it. Likewise,
22542286
* set toast_parent so that the privilege checks are done on the main
22552287
* relation. NB: This is only safe to do because we hold a session
22562288
* lock on the main relation that prevents concurrent deletion.
22572289
*/
2258-
memcpy(&toast_vacuum_params, params, sizeof(VacuumParams));
22592290
toast_vacuum_params.options |= VACOPT_PROCESS_MAIN;
22602291
toast_vacuum_params.toast_parent = relid;
22612292

src/test/modules/injection_points/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ EXTENSION = injection_points
99
DATA = injection_points--1.0.sql
1010
PGFILEDESC = "injection_points - facility for injection points"
1111

12-
REGRESS = injection_points reindex_conc
12+
REGRESS = injection_points reindex_conc vacuum
1313
REGRESS_OPTS = --dlpath=$(top_builddir)/src/test/regress
1414

1515
ISOLATION = inplace syscache-update-pruned
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
-- Tests for VACUUM
2+
CREATE EXTENSION injection_points;
3+
SELECT injection_points_set_local();
4+
injection_points_set_local
5+
----------------------------
6+
7+
(1 row)
8+
9+
SELECT injection_points_attach('vacuum-index-cleanup-auto', 'notice');
10+
injection_points_attach
11+
-------------------------
12+
13+
(1 row)
14+
15+
SELECT injection_points_attach('vacuum-index-cleanup-disabled', 'notice');
16+
injection_points_attach
17+
-------------------------
18+
19+
(1 row)
20+
21+
SELECT injection_points_attach('vacuum-index-cleanup-enabled', 'notice');
22+
injection_points_attach
23+
-------------------------
24+
25+
(1 row)
26+
27+
SELECT injection_points_attach('vacuum-truncate-auto', 'notice');
28+
injection_points_attach
29+
-------------------------
30+
31+
(1 row)
32+
33+
SELECT injection_points_attach('vacuum-truncate-disabled', 'notice');
34+
injection_points_attach
35+
-------------------------
36+
37+
(1 row)
38+
39+
SELECT injection_points_attach('vacuum-truncate-enabled', 'notice');
40+
injection_points_attach
41+
-------------------------
42+
43+
(1 row)
44+
45+
-- Check state of index_cleanup and truncate in VACUUM.
46+
CREATE TABLE vac_tab_on_toast_off(i int, j text) WITH
47+
(autovacuum_enabled=false,
48+
vacuum_index_cleanup=true, toast.vacuum_index_cleanup=false,
49+
vacuum_truncate=true, toast.vacuum_truncate=false);
50+
CREATE TABLE vac_tab_off_toast_on(i int, j text) WITH
51+
(autovacuum_enabled=false,
52+
vacuum_index_cleanup=false, toast.vacuum_index_cleanup=true,
53+
vacuum_truncate=false, toast.vacuum_truncate=true);
54+
-- Multiple relations should use their options in isolation.
55+
VACUUM vac_tab_on_toast_off, vac_tab_off_toast_on;
56+
NOTICE: notice triggered for injection point vacuum-index-cleanup-enabled
57+
NOTICE: notice triggered for injection point vacuum-truncate-enabled
58+
NOTICE: notice triggered for injection point vacuum-index-cleanup-disabled
59+
NOTICE: notice triggered for injection point vacuum-truncate-disabled
60+
NOTICE: notice triggered for injection point vacuum-index-cleanup-disabled
61+
NOTICE: notice triggered for injection point vacuum-truncate-disabled
62+
NOTICE: notice triggered for injection point vacuum-index-cleanup-enabled
63+
NOTICE: notice triggered for injection point vacuum-truncate-enabled
64+
DROP TABLE vac_tab_on_toast_off;
65+
DROP TABLE vac_tab_off_toast_on;
66+
-- Cleanup
67+
SELECT injection_points_detach('vacuum-index-cleanup-auto');
68+
injection_points_detach
69+
-------------------------
70+
71+
(1 row)
72+
73+
SELECT injection_points_detach('vacuum-index-cleanup-disabled');
74+
injection_points_detach
75+
-------------------------
76+
77+
(1 row)
78+
79+
SELECT injection_points_detach('vacuum-index-cleanup-enabled');
80+
injection_points_detach
81+
-------------------------
82+
83+
(1 row)
84+
85+
SELECT injection_points_detach('vacuum-truncate-auto');
86+
injection_points_detach
87+
-------------------------
88+
89+
(1 row)
90+
91+
SELECT injection_points_detach('vacuum-truncate-disabled');
92+
injection_points_detach
93+
-------------------------
94+
95+
(1 row)
96+
97+
SELECT injection_points_detach('vacuum-truncate-enabled');
98+
injection_points_detach
99+
-------------------------
100+
101+
(1 row)
102+
103+
DROP EXTENSION injection_points;

src/test/modules/injection_points/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ tests += {
3434
'sql': [
3535
'injection_points',
3636
'reindex_conc',
37+
'vacuum',
3738
],
3839
'regress_args': ['--dlpath', meson.build_root() / 'src/test/regress'],
3940
# The injection points are cluster-wide, so disable installcheck
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
-- Tests for VACUUM
2+
3+
CREATE EXTENSION injection_points;
4+
5+
SELECT injection_points_set_local();
6+
SELECT injection_points_attach('vacuum-index-cleanup-auto', 'notice');
7+
SELECT injection_points_attach('vacuum-index-cleanup-disabled', 'notice');
8+
SELECT injection_points_attach('vacuum-index-cleanup-enabled', 'notice');
9+
SELECT injection_points_attach('vacuum-truncate-auto', 'notice');
10+
SELECT injection_points_attach('vacuum-truncate-disabled', 'notice');
11+
SELECT injection_points_attach('vacuum-truncate-enabled', 'notice');
12+
13+
-- Check state of index_cleanup and truncate in VACUUM.
14+
CREATE TABLE vac_tab_on_toast_off(i int, j text) WITH
15+
(autovacuum_enabled=false,
16+
vacuum_index_cleanup=true, toast.vacuum_index_cleanup=false,
17+
vacuum_truncate=true, toast.vacuum_truncate=false);
18+
CREATE TABLE vac_tab_off_toast_on(i int, j text) WITH
19+
(autovacuum_enabled=false,
20+
vacuum_index_cleanup=false, toast.vacuum_index_cleanup=true,
21+
vacuum_truncate=false, toast.vacuum_truncate=true);
22+
-- Multiple relations should use their options in isolation.
23+
VACUUM vac_tab_on_toast_off, vac_tab_off_toast_on;
24+
25+
DROP TABLE vac_tab_on_toast_off;
26+
DROP TABLE vac_tab_off_toast_on;
27+
28+
-- Cleanup
29+
SELECT injection_points_detach('vacuum-index-cleanup-auto');
30+
SELECT injection_points_detach('vacuum-index-cleanup-disabled');
31+
SELECT injection_points_detach('vacuum-index-cleanup-enabled');
32+
SELECT injection_points_detach('vacuum-truncate-auto');
33+
SELECT injection_points_detach('vacuum-truncate-disabled');
34+
SELECT injection_points_detach('vacuum-truncate-enabled');
35+
DROP EXTENSION injection_points;

0 commit comments

Comments
 (0)