8000 Lock table in DROP STATISTICS · postgres/postgres@28f84f7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 28f84f7

Browse files
committed
Lock table in DROP STATISTICS
The DROP STATISTICS code failed to properly lock the table, leading to ERROR: tuple concurrently deleted when executed concurrently with ANALYZE. Fixed by modifying RemoveStatisticsById() to acquire the same lock as ANALYZE. This function is called only by DROP STATISTICS, as ANALYZE calls RemoveStatisticsDataById() directly. Reported by Justin Pryzby, fix by me. Backpatch through 12. The code was like this since it was introduced in 10, but older releases are EOL. Reported-by: Justin Pryzby Reviewed-by: Tom Lane Backpatch-through: 12 Discussion: https://postgr.es/m/ZUuk-8CfbYeq6g_u@pryzbyj2023
1 parent b218fbb commit 28f84f7

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

src/backend/commands/statscmds.c

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -734,18 +734,11 @@ void
734734
RemoveStatisticsById(Oid statsOid)
735735
{
736736
Relation relation;
737+
Relation rel;
737738
HeapTuple tup;
738739
Form_pg_statistic_ext statext;
739740
Oid relid;
740741

741-
/*
742-
* First delete the pg_statistic_ext_data tuples holding the actual
743-
* statistical data. There might be data with/without inheritance, so
744-
* attempt deleting both.
745-
*/
746-
RemoveStatisticsDataById(statsOid, true);
747-
RemoveStatisticsDataById(statsOid, false);
748-
749742
/*
750743
* Delete the pg_statistic_ext tuple. Also send out a cache inval on the
751744
* associated table, so that dependent plans will be rebuilt.
@@ -760,12 +753,26 @@ RemoveStatisticsById(Oid statsOid)
760753
statext = (Form_pg_statistic_ext) GETSTRUCT(tup);
761754
relid = statext->stxrelid;
762755

756+
/*
757+
* Delete the pg_statistic_ext_data tuples holding the actual statistical
758+
* data. There might be data with/without inheritance, so attempt deleting
759+
* both. We lock the user table first, to prevent other processes (e.g.
760+
* DROP STATISTICS) from removing the row concurrently.
761+
*/
762+
rel = table_open(relid, ShareUpdateExclusiveLock);
763+
764+
RemoveStatisticsDataById(statsOid, true);
765+
RemoveStatisticsDataById(statsOid, false);
766+
763767
CacheInvalidateRelcacheByRelid(relid);
764768

765769
CatalogTupleDelete(relation, &tup->t_self);
766770

767771
ReleaseSysCache(tup);
768772

773+
/* Keep lock until the end of the transaction. */
774+
table_close(rel, NoLock);
775+
769776
table_close(relation, RowExclusiveLock);
770777
}
771778

0 commit comments

Comments
 (0)
0