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 1iVOXy-0006W5-Hw for pgsql-hackers@arkaria.postgresql.org; Thu, 14 Nov 2019 23:29:06 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iVOXw-0003om-Qa for pgsql-hackers@arkaria.postgresql.org; Thu, 14 Nov 2019 23:29:04 +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 1iVOXw-0003oe-Hx for pgsql-hackers@lists.postgresql.org; Thu, 14 Nov 2019 23:29:04 +0000 Received: from mail-wr1-x442.google.com ([2a00:1450:4864:20::442]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iVOXs-0000fE-NZ for pgsql-hackers@lists.postgresql.org; Thu, 14 Nov 2019 23:29:03 +0000 Received: by mail-wr1-x442.google.com with SMTP id b3so8830950wrs.13 for ; Thu, 14 Nov 2019 15:29:00 -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=mNBieS+WLeTYzQ2FIFkGx0aUEeEQHGG92pNkGK3lovI=; b=xTxhAQUQtZGnz7GJvZOQcR1mWjKQpcJ3KzaKOrt++5LvvuZox98kkIv3969QhYD3JC M90DkynuLo811kM16J0QumRwWpSz78iADBXbOhgrGzutRpQJVZl9rVGbp3qWRB1Mp9Qg Sf4MQdzmFRRlzBDp9V06iAUyuYiccpvXFztOqACafXHcgcNnNXXgbZOS62zy+QNBztzN ubEhNx3kce0DAr+RRK3F3k9BdmYs8Nr7/HTfQpd/BZ1D+4/2aot+4M+G1xHRM8Nf89FS bhyIuE7SG3sMTb2zaNCK4dOQkSnyRNTPpOkZJAbqkAEwtZqDkgjbjTNxgcOkf7dOLkKJ /sFQ== 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=mNBieS+WLeTYzQ2FIFkGx0aUEeEQHGG92pNkGK3lovI=; b=WyQcRGKF1RdeDF3O5HC45x/tdB5V15xApSGDlt+3jMtdPfC7FJiZd2p264UsXYs5ul gjWlG/ysJD3ElL6NIcT1yw+YdAf0YzAaKgMqDWsA3Y2fmUSAHJtii/74XJLsC+gh7BPU S7MJjaUffAyaTJqv8aNizEbi7HbLR2tk6zb/PnCQmO3gS5W7FGjFQppfxeEQgDy9wqzU zl8wuVmv581r/R1Gkml2whSl3jk77DBixWMuSKHaLNP2Qo81vPSS/lbWNdpgeNpgiP6r NJ24NgQEr9VHuSW3lYc6go5GLp+U1kX47aGSMfsio/SJViNJOILKIYVCdLqOaujiM2oK BR6A== X-Gm-Message-State: APjAAAUIB1B6jv23iqeb/koAK0co0QWtWGNUiQoKwlaMCUTdqQwmjgiZ 1KHdu0/kQrwGxdmow+VLfCTz5w== X-Google-Smtp-Source: APXvYqy/muBXRlXBWaSwEOIiuKpx4RaePIAuwrxixIyRrrEJWOO2+vzqbfDke56/gAzLyH2Drm0V8Q== X-Received: by 2002:adf:ec44:: with SMTP id w4mr10994380wrn.274.1573774139570; Thu, 14 Nov 2019 15:28:59 -0800 (PST) Received: from localhost (ip-86-49-253-92.net.upcbroadband.cz. [86.49.253.92]) by smtp.gmail.com with ESMTPSA id m3sm8717447wrw.20.2019.11.14.15.28.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Nov 2019 15:28:58 -0800 (PST) Date: Fri, 15 Nov 2019 00:28:57 +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: <20191114232857.fhnbuxs4bgcy7llm@development> References: <18427.1573508501@sss.pgh.pa.us> <997cbcae-7739-f871-84a0-1192380abb54@gmail.com> <15228.1573695479@sss.pgh.pa.us> <20191114213124.i6xwx24y26iklzds@development> <14155.1573767414@sss.pgh.pa.us> <20191114222232.o76u6jdvf53ozjjk@development> <17008.1573770906@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="q2qpedieetz2bclv" Content-Disposition: inline In-Reply-To: <17008.1573770906@sss.pgh.pa.us> User-Agent: NeoMutt/20180716 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --q2qpedieetz2bclv Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline On Thu, Nov 14, 2019 at 05:35:06PM -0500, Tom Lane wrote: >Tomas Vondra writes: >> On Thu, Nov 14, 2019 at 04:36:54PM -0500, Tom Lane wrote: >>> Hm. No, it's not enough, unless you add more logic to deal with the >>> possibility that the stats object is gone by the time you have the >>> table lock. Consider e.g. two concurrent DROP STATISTICS commands, >>> or a statistics drop that's cascading from something like a drop >>> of a relevant function and so has no earlier table lock. > >> Isn't that solved by RemoveObjects() doing this? > >> /* Get an ObjectAddress for the object. */ >> address = get_object_address(stmt->removeType, >> object, >> &relation, >> AccessExclusiveLock, >> stmt->missing_ok); > >Ah, I see, we already have AEL on the stats object itself. So that >eliminates my concern about a race between two RemoveStatisticsById >calls, but what we have instead is fear of deadlock. A DROP STATISTICS >command will acquire AEL on the stats object but then AEL on the table, >the opposite of what will happen during DROP TABLE, so concurrent >executions of those will deadlock. That might be better than the >failures Mark is seeing now, but not by much. > Hmmm, yeah :-( >A correct fix I think is that the planner ought to acquire AccessShareLock >on a stats object it's trying to use (and then recheck whether the object >is still there). That seems rather expensive, but there may be no other >way. Yes, so something like for indexes, although we don't need the recheck in that case. I think the attached patch does that (but it's 1AM here). regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services --q2qpedieetz2bclv Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="stats-locking-v2.patch" diff --git a/src/backend/optimizer/util/plancat.c b/src/backend/optimizer/util/plancat.c index 5e889d1861..a1e325615c 100644 --- a/src/backend/optimizer/util/plancat.c +++ b/src/backend/optimizer/util/plancat.c @@ -46,6 +46,7 @@ #include "rewrite/rewriteManip.h" #include "statistics/statistics.h" #include "storage/bufmgr.h" +#include "storage/lmgr.h" #include "utils/builtins.h" #include "utils/lsyscache.h" #include "utils/partcache.h" @@ -1305,9 +1306,13 @@ get_relation_statistics(RelOptInfo *rel, Relation relation) Bitmapset *keys = NULL; int i; + LockDatabaseObject(StatisticExtRelationId, statOid, 0, AccessShareLock); + + /* we may get invalid tuple, if the statistic just got dropped */ htup = SearchSysCache1(STATEXTOID, ObjectIdGetDatum(statOid)); if (!HeapTupleIsValid(htup)) - elog(ERROR, "cache lookup failed for statistics object %u", statOid); + continue; + staForm = (Form_pg_statistic_ext) GETSTRUCT(htup); dtup = SearchSysCache1(STATEXTDATASTXOID, ObjectIdGetDatum(statOid)); --q2qpedieetz2bclv--