diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 0ab43deb71..1126e7a585 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -30,6 +30,7 @@ #include "commands/defrem.h" #include "miscadmin.h" #include "statistics/statistics.h" +#include "storage/lmgr.h" #include "utils/builtins.h" #include "utils/fmgroids.h" #include "utils/inval.h" @@ -533,6 +534,21 @@ RemoveStatisticsById(Oid statsOid) Form_pg_statistic_ext statext; Oid relid; + /* Lookup and lock relation the statistics is defined on. */ + tup = SearchSysCache1(STATEXTOID, ObjectIdGetDatum(statsOid)); + + if (!HeapTupleIsValid(tup)) + elog(ERROR, "cache lookup failed for statistics object %u", statsOid); + + statext = (Form_pg_statistic_ext) GETSTRUCT(tup); + relid = statext->stxrelid; + + /* lock the relation in access exclusive mode */ + LockRelationOid(relid, AccessExclusiveLock); + + ReleaseSysCache(tup); + + /* * First delete the pg_statistic_ext_data tuple holding the actual * statistical data.