Skip to content

Commit fd0f651

Browse files
committed
Test IsInTransactionChain, not IsTransactionBlock, in vac_update_relstats.
As noted by Noah Misch, my initial cut at fixing bug #11638 didn't cover all cases where ANALYZE might be invoked in an unsafe context. We need to test the result of IsInTransactionChain not IsTransactionBlock; which is notationally a pain because IsInTransactionChain requires an isTopLevel flag, which would have to be passed down through several levels of callers. I chose to pass in_outer_xact (ie, the result of IsInTransactionChain) rather than isTopLevel per se, as that seemed marginally more apropos for the intermediate functions to know about.
1 parent 6057c21 commit fd0f651

File tree

4 files changed

+31
-20
lines changed

4 files changed

+31
-20
lines changed

src/backend/commands/analyze.c

+12-7
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ static BufferAccessStrategy vac_strategy;
8787

8888
static void do_analyze_rel(Relation onerel, VacuumStmt *vacstmt,
8989
AcquireSampleRowsFunc acquirefunc, BlockNumber relpages,
90-
bool inh, int elevel);
90+
bool inh, bool in_outer_xact, int elevel);
9191
static void BlockSampler_Init(BlockSampler bs, BlockNumber nblocks,
9292
int samplesize);
9393
static bool BlockSampler_HasMore(BlockSampler bs);
@@ -115,7 +115,8 @@ static Datum ind_fetch_func(VacAttrStatsP stats, int rownum, bool *isNull);
115115
* analyze_rel() -- analyze one relation
116116
*/
117117
void
118-
analyze_rel(Oid relid, VacuumStmt *vacstmt, BufferAccessStrategy bstrategy)
118+
analyze_rel(Oid relid, VacuumStmt *vacstmt,
119+
bool in_outer_xact, BufferAccessStrategy bstrategy)
119120
{
120121
Relation onerel;
121122
int elevel;
@@ -265,13 +266,15 @@ analyze_rel(Oid relid, VacuumStmt *vacstmt, BufferAccessStrategy bstrategy)
265266
/*
266267
* Do the normal non-recursive ANALYZE.
267268
*/
268-
do_analyze_rel(onerel, vacstmt, acquirefunc, relpages, false, elevel);
269+
do_analyze_rel(onerel, vacstmt, acquirefunc, relpages,
270+
false, in_outer_xact, elevel);
269271

270272
/*
271273
* If there are child tables, do recursive ANALYZE.
272274
*/
273275
if (onerel->rd_rel->relhassubclass)
274-
do_analyze_rel(onerel, vacstmt, acquirefunc, relpages, true, elevel);
276+
do_analyze_rel(onerel, vacstmt, acquirefunc, relpages,
277+
true, in_outer_xact, elevel);
275278

276279
/*
277280
* Close source relation now, but keep lock so that no one deletes it
@@ -301,7 +304,7 @@ analyze_rel(Oid relid, VacuumStmt *vacstmt, BufferAccessStrategy bstrategy)
301304
static void
302305
do_analyze_rel(Relation onerel, VacuumStmt *vacstmt,
303306
AcquireSampleRowsFunc acquirefunc, BlockNumber relpages,
304-
bool inh, int elevel)
307+
bool inh, bool in_outer_xact, int elevel)
305308
{
306309
int attr_cnt,
307310
tcnt,
@@ -584,7 +587,8 @@ do_analyze_rel(Relation onerel, VacuumStmt *vacstmt,
584587
visibilitymap_count(onerel),
585588
hasindex,
586589
InvalidTransactionId,
587-
InvalidMultiXactId);
590+
InvalidMultiXactId,
591+
in_outer_xact);
588592

589593
/*
590594
* Same for indexes. Vacuum always scans all indexes, so if we're part of
@@ -605,7 +609,8 @@ do_analyze_rel(Relation onerel, VacuumStmt *vacstmt,
605609
0,
606610
false,
607611
InvalidTransactionId,
608-
InvalidMultiXactId);
612+
InvalidMultiXactId,
613+
in_outer_xact);
609614
}
610615
}
611616

src/backend/commands/vacuum.c

+12-9
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@ vacuum(VacuumStmt *vacstmt, Oid relid, bool do_toast,
206206
*/
207207
if (use_own_xacts)
208208
{
209+
Assert(!in_outer_xact);
210+
209211
/* ActiveSnapshot is not set by autovacuum */
210212
if (ActiveSnapshotSet())
211213
PopActiveSnapshot();
@@ -251,7 +253,7 @@ vacuum(VacuumStmt *vacstmt, Oid relid, bool do_toast,
251253
PushActiveSnapshot(GetTransactionSnapshot());
252254
}
253255

254-
analyze_rel(relid, vacstmt, vac_strategy);
256+
analyze_rel(relid, vacstmt, in_outer_xact, vac_strategy);
255257

256258
if (use_own_xacts)
257259
{
@@ -665,13 +667,13 @@ vac_estimate_reltuples(Relation relation, bool is_analyze,
665667
* DDL flags such as relhasindex, by clearing them if no longer correct.
666668
* It's safe to do this in VACUUM, which can't run in parallel with
667669
* CREATE INDEX/RULE/TRIGGER and can't be part of a transaction block.
668-
* However, it's *not* safe to do it in an ANALYZE that's within a
669-
* transaction block, because for example the current transaction might
670+
* However, it's *not* safe to do it in an ANALYZE that's within an
671+
* outer transaction, because for example the current transaction might
670672
* have dropped the last index; then we'd think relhasindex should be
671673
* cleared, but if the transaction later rolls back this would be wrong.
672-
* So we refrain from updating the DDL flags if we're inside a
673-
* transaction block. This is OK since postponing the flag maintenance
674-
* is always allowable.
674+
* So we refrain from updating the DDL flags if we're inside an outer
675+
* transaction. This is OK since postponing the flag maintenance is
676+
* always allowable.
675677
*
676678
* This routine is shared by VACUUM and ANALYZE.
677679
*/
@@ -680,7 +682,8 @@ vac_update_relstats(Relation relation,
680682
BlockNumber num_pages, double num_tuples,
681683
BlockNumber num_all_visible_pages,
682684
bool hasindex, TransactionId frozenxid,
683-
MultiXactId minmulti)
685+
MultiXactId minmulti,
686+
bool in_outer_xact)
684687
{
685688
Oid relid = RelationGetRelid(relation);
686689
Relation rd;
@@ -716,9 +719,9 @@ vac_update_relstats(Relation relation,
716719
dirty = true;
717720
}
718721

719-
/* Apply DDL updates, but not inside a transaction block (see above) */
722+
/* Apply DDL updates, but not inside an outer transaction (see above) */
720723

721-
if (!IsTransactionBlock())
724+
if (!in_outer_xact)
722725
{
723726
/*
724727
* If we didn't find any indexes, reset relhasindex.

src/backend/commands/vacuumlazy.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,8 @@ lazy_vacuum_rel(Relation onerel, VacuumStmt *vacstmt,
309309
new_rel_allvisible,
310310
vacrelstats->hasindex,
311311
new_frozen_xid,
312-
new_min_multi);
312+
new_min_multi,
313+
false);
313314

314315
/* report results to the stats collector, too */
315316
new_live_tuples = new_rel_tuples - vacrelstats->new_dead_tuples;
@@ -1377,7 +1378,8 @@ lazy_cleanup_index(Relation indrel,
13771378
0,
13781379
false,
13791380
InvalidTransactionId,
1380-
InvalidMultiXactId);
1381+
InvalidMultiXactId,
1382+
false);
13811383

13821384
ereport(elevel,
13831385
(errmsg("index \"%s\" now contains %.0f row versions in %u pages",

src/include/commands/vacuum.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,8 @@ extern void vac_update_relstats(Relation relation,
156156
BlockNumber num_all_visible_pages,
157157
bool hasindex,
158158
TransactionId frozenxid,
159-
MultiXactId minmulti);
159+
MultiXactId minmulti,
160+
bool in_outer_xact);
160161
extern void vacuum_set_xid_limits(Relation rel,
161162
int freeze_min_age, int freeze_table_age,
162163
int multixact_freeze_min_age,
@@ -175,7 +176,7 @@ extern void lazy_vacuum_rel(Relation onerel, VacuumStmt *vacstmt,
175176

176177
/* in commands/analyze.c */
177178
extern void analyze_rel(Oid relid, VacuumStmt *vacstmt,
178-
BufferAccessStrategy bstrategy);
179+
bool in_outer_xact, BufferAccessStrategy bstrategy);
179180
extern bool std_typanalyze(VacAttrStats *stats);
180181
extern double anl_random_fract(void);
181182
extern double anl_init_selection_state(int n);

0 commit comments

Comments
 (0)