Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iVMiH-0002S4-El for pgsql-hackers@arkaria.postgresql.org; Thu, 14 Nov 2019 21:31:37 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iVMiF-0008PM-Qt for pgsql-hackers@arkaria.postgresql.org; Thu, 14 Nov 2019 21:31:35 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iVMiF-0008PE-Ci for pgsql-hackers@lists.postgresql.org; Thu, 14 Nov 2019 21:31:35 +0000 Received: from mail-wm1-x344.google.com ([2a00:1450:4864:20::344]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iVMiA-000869-U1 for pgsql-hackers@lists.postgresql.org; Thu, 14 Nov 2019 21:31:34 +0000 Received: by mail-wm1-x344.google.com with SMTP id z19so7932743wmk.3 for ; Thu, 14 Nov 2019 13:31:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=7PoLdTdK6GWpNRAhGxNRUPfgNUR3N3i6bTcIRSnVXrc=; b=1Y54bX4PznDJ1Ddqu+hB+277y37pQvvODfRY+qkFpVBWPC1tgpBzd36f3phGXTjO/p hkoko5iFOSxCCcb758NFplNsI3WGzhaf+RyvBH/7Op/RCm89ORUYwbmba9kgFIISVY2x 5UQbvCmDgbvMemMB+7zKRuI+3/CJYSRH/xkTY08/H8f8BvCtPQI7ZL0XfrI552SjMDGL FlKp6I9wDDteVF1qlAYaRM76raF+FSMNiWaQopXimUVs92S25qUM/KfjzXBK4rs+CAz8 tRoS3IvGmLLzHTnRvlyZ6MG7KQxlO4wbjS+w9bgyc8U6u+105El0u2uV6nkVeWqrZJ70 l3Yg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=7PoLdTdK6GWpNRAhGxNRUPfgNUR3N3i6bTcIRSnVXrc=; b=uEUbzaLwsEEwTldzbJDDHcQk9u40sm5nlyvAXDhfk96ypqCoJ/mD3DfuDgBPRMhpnU /GkSFOTL3BBKbTFlaXBBWg3ALK9a0XgjvYHa3Z2nBNnMHoZBm73t9bmKlaumjgm2lYW1 smyM9brVhPrP2YkGaBNKhshwntddk3Kt2vWGQvlag5ss4E+fVFuWG7UlNxsTUmBlm6yX WvNXVIAl+mX5y8cNY04Ply/09m7btwt+J2xko13vnH5yBE+wv8L8LlPBAIT1O3f43Kf0 cvZmVSR+/l1tvthMxpaViEGXve1NJYVfYdjANBCOvgJZGIqMTlcZ5QVFlgUg+gJU4mUD Fj6g== X-Gm-Message-State: APjAAAW5p2FFuRCr6C3hhoL98MkhWS+JrESM7P0afKmZlHl/RFPNAVTl XH/G4tqyx/beOiC52NFDQRXBzg== X-Google-Smtp-Source: APXvYqzqkZF2Ihg2cdPCOSdD4nQEWTRG7NRkElMDcG+usPUtHkPyBZ5irriPH1QZyuf2l0qFoYAaQw== X-Received: by 2002:a1c:544e:: with SMTP id p14mr10092211wmi.17.1573767088645; Thu, 14 Nov 2019 13:31:28 -0800 (PST) Received: from localhost (ip-86-49-253-92.net.upcbroadband.cz. [86.49.253.92]) by smtp.gmail.com with ESMTPSA id z6sm9237592wro.18.2019.11.14.13.31.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Nov 2019 13:31:25 -0800 (PST) Date: Thu, 14 Nov 2019 22:31:24 +0100 From: Tomas Vondra To: Tom Lane Cc: Mark Dilger , pgsql-hackers@lists.postgresql.org Subject: Re: Missing dependency tracking for TableFunc nodes Message-ID: <20191114213124.i6xwx24y26iklzds@development> References: <18427.1573508501@sss.pgh.pa.us> <997cbcae-7739-f871-84a0-1192380abb54@gmail.com> <15228.1573695479@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="nev5ied3k6kuxcrp" Content-Disposition: inline In-Reply-To: <15228.1573695479@sss.pgh.pa.us> User-Agent: NeoMutt/20180716 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --nev5ied3k6kuxcrp Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline On Wed, Nov 13, 2019 at 08:37:59PM -0500, Tom Lane wrote: >Mark Dilger writes: >> On 11/11/19 1:41 PM, Tom Lane wrote: >>> I happened to notice that find_expr_references_walker has not >>> been taught anything about TableFunc nodes, which means it will >>> miss the type and collation OIDs embedded in such a node. > >> I can consistently generate errors like the following in master: >> ERROR: cache lookup failed for statistics object 31041 > >This is surely a completely different issue --- there are not, >one hopes, any extended-stats OIDs embedded in views or other >query trees. > >I concur with Tomas' suspicion that this must be a race condition >during DROP STATISTICS. If we're going to allow people to do that >separately from dropping the table(s), there has to be some kind of >locking around it, and it sounds like there's not :-( > I think the right thing to do is simply acquire AE lock on the relation in RemoveStatisticsById, per the attached patch. It's possible we'll need to do something more complicated once join stats are added, but for now this should be enough (and backpatchable). regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services --nev5ied3k6kuxcrp Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="stats-locking.patch" 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. --nev5ied3k6kuxcrp--