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 1iug40-0006Ik-19 for pgsql-bugs@arkaria.postgresql.org; Thu, 23 Jan 2020 17:14:40 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iug3y-000434-I8 for pgsql-bugs@arkaria.postgresql.org; Thu, 23 Jan 2020 17:14:38 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iug3y-00042x-6e for pgsql-bugs@lists.postgresql.org; Thu, 23 Jan 2020 17:14:38 +0000 Received: from mail-qt1-x842.google.com ([2607:f8b0:4864:20::842]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1iug3v-0004h3-L7 for pgsql-bugs@lists.postgresql.org; Thu, 23 Jan 2020 17:14:36 +0000 Received: by mail-qt1-x842.google.com with SMTP id e25so3002121qtr.13 for ; Thu, 23 Jan 2020 09:14:35 -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:mime-version:content-disposition :content-transfer-encoding:in-reply-to:user-agent; bh=fViUpvZEB7AhfimPMtqKGYaLaVF86OeNaB/nyWcdaLg=; b=THJ6GX9yyWy/2MEg8g/7uADTEl1yVtRcUP4TWpgKLyjYVzgkNtyvu1d2IVBySRZ1ji /vBNg4RLl1claM+W3gxOzXke/9oHeTXz8l2TAoD3IsJiYbsNDPFpSAEzwvtQEDNXACTh yZriKibcYGJgIy7vPh2c5vqp5WAZXwC0Sm0+vyiTjNhAkMzBEVQ+vwsLpH+r0eHVcmQI emPK1oIloXrkcz4HUE7r+TdxA+3AnX1sP6ioWcyp521WEb9S5d+xmxOeT1lTqh0dqB79 n3blHSIumPbqi2ptvID2UysxxuX6qCTuuJxc/ujJimEIrqy3fdaQ9NoK8V4GSfDAmTK2 Qy0g== 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:mime-version :content-disposition:content-transfer-encoding:in-reply-to :user-agent; bh=fViUpvZEB7AhfimPMtqKGYaLaVF86OeNaB/nyWcdaLg=; b=itqKshL4WzW4msu89e9p6WqYg/XLSWJSax1o5Z5bcMCNlwgr4T4QXpkmYSihZiSW64 rEkb+S7Kyvd1PW3WZfwiCrRb6uEh3TuN4OXfZRVz5s/hkU3YV9SHpcorcEYsZKxa6wMA 3ctksNlO4zSJ+Plmu+vEWh+pO6R+shALqNUESnZoAjY41RO/DUuDU6F1jgavojZUoYtt 8eRbacZZSYqj43TJs5I8fYbFrJk2ccR7dVXr48bstwQdH1ieM8yTWu2gobLlB1zGsTfI q04hwovt5BAKhRVesW88b9+wht09XU6rTHEwk3hgk2EPLUpQ1PN/O2/kyXXUWd/THT1/ S68g== X-Gm-Message-State: APjAAAXejQldm22mfXMTpU7wOvl+SJrQU3o7Wz9rZaXuMnOqb9vCdck1 TTnKAyIsZX4edz0FTPPcAaucWQ== X-Google-Smtp-Source: APXvYqzAew54nZn+0VFyWP7GWTqkM1Z4vvdZWXMnxTpv875BRs492ScB3QYpLQhVXVIzXtzoMc9FRg== X-Received: by 2002:ac8:43ce:: with SMTP id w14mr11232519qtn.256.1579799673212; Thu, 23 Jan 2020 09:14:33 -0800 (PST) Received: from nimloth.alvh.no-ip.org ([179.56.96.77]) by smtp.gmail.com with ESMTPSA id k9sm1226031qtq.75.2020.01.23.09.14.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Jan 2020 09:14:32 -0800 (PST) Received: by nimloth.alvh.no-ip.org (Postfix, from userid 1000) id 2EFDC300A9E; Thu, 23 Jan 2020 14:14:23 -0300 (-03) Date: Thu, 23 Jan 2020 14:14:23 -0300 From: Alvaro Herrera To: Tom Lane Cc: Mithun Cy , PostgreSQL Hackers , PostgreSQL mailing lists Subject: Re: DROP OWNED CASCADE vs Temp tables Message-ID: <20200123171423.GA25918@alvherre.pgsql> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="NzB8fVQJ5HfG6fxh" Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20200114215942.GA18482@alvherre.pgsql> User-Agent: Mutt/1.10.1 (2018-07-13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --NzB8fVQJ5HfG6fxh Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit On 2020-Jan-14, Alvaro Herrera wrote: > On 2020-Jan-13, Tom Lane wrote: > > > That seems fundamentally wrong. By the time we've queued an object for > > deletion in dependency.c, we have a lock on it, and we've verified that > > the object is still there (cf. systable_recheck_tuple calls). > > If shdepDropOwned is doing it differently, I'd say shdepDropOwned is > > doing it wrong. > > Hmm, it seems to be doing it differently. Maybe it should be acquiring > locks on all objects in that nested loop and verified them for > existence, so that when it calls performMultipleDeletions the objects > are already locked, as you say. Yeah, this solves the reported bug. This is not a 100% solution: there's the cases when the user is removed from an ACL and from a policy, and those deletions are done directly instead of accumulating to the end for a mass deletion. I had to export AcquireDeletionLock (previously a static in dependency.c). I wonder if I should export ReleaseDeletionLock too, for symmetry. -- Álvaro Herrera https://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services --NzB8fVQJ5HfG6fxh Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="lock-shdepDrop.patch" diff --git a/src/backend/catalog/dependency.c b/src/backend/catalog/dependency.c index c4a4df25b8..2ec3e39694 100644 --- a/src/backend/catalog/dependency.c +++ b/src/backend/catalog/dependency.c @@ -199,7 +199,6 @@ static void reportDependentObjects(const ObjectAddresses *targetObjects, static void deleteOneObject(const ObjectAddress *object, Relation *depRel, int32 flags); static void doDeletion(const ObjectAddress *object, int flags); -static void AcquireDeletionLock(const ObjectAddress *object, int flags); static void ReleaseDeletionLock(const ObjectAddress *object); static bool find_expr_references_walker(Node *node, find_expr_references_context *context); @@ -1530,7 +1529,7 @@ doDeletion(const ObjectAddress *object, int flags) * else. Note that dependency.c is not concerned with deleting any kind of * shared-across-databases object, so we have no need for LockSharedObject. */ -static void +void AcquireDeletionLock(const ObjectAddress *object, int flags) { if (object->classId == RelationRelationId) diff --git a/src/backend/catalog/pg_shdepend.c b/src/backend/catalog/pg_shdepend.c index 2ef792dbd7..20d7ee7d87 100644 --- a/src/backend/catalog/pg_shdepend.c +++ b/src/backend/catalog/pg_shdepend.c @@ -1319,12 +1319,16 @@ shdepDropOwned(List *roleids, DropBehavior behavior) elog(ERROR, "unexpected dependency type"); break; case SHARED_DEPENDENCY_ACL: + /* XXX need to lock+accumulate everything and drop later? */ RemoveRoleFromObjectACL(roleid, sdepForm->classid, sdepForm->objid); break; case SHARED_DEPENDENCY_POLICY: - /* If unable to remove role from policy, remove policy. */ + /* + * Try to remove role from policy; if unable to, remove + * policy. + */ if (!RemoveRoleFromObjectPolicy(roleid, sdepForm->classid, sdepForm->objid)) @@ -1332,6 +1336,15 @@ shdepDropOwned(List *roleids, DropBehavior behavior) obj.classId = sdepForm->classid; obj.objectId = sdepForm->objid; obj.objectSubId = sdepForm->objsubid; + /* + * Acquire lock on object, then verify this dependency + * is still relevant. If not, the object might have + * been dropped or the policy modified. Ignore the + * object in that case. + */ + AcquireDeletionLock(&obj, 0); + if (!systable_recheck_tuple(scan, tuple)) + break; add_exact_object_address(&obj, deleteobjs); } break; @@ -1342,6 +1355,10 @@ shdepDropOwned(List *roleids, DropBehavior behavior) obj.classId = sdepForm->classid; obj.objectId = sdepForm->objid; obj.objectSubId = sdepForm->objsubid; + /* as above */ + AcquireDeletionLock(&obj, 0); + if (!systable_recheck_tuple(scan, tuple)) + break; add_exact_object_address(&obj, deleteobjs); } break; diff --git a/src/include/catalog/dependency.h b/src/include/catalog/dependency.h index 0cd6fcf027..fa9252b07c 100644 --- a/src/include/catalog/dependency.h +++ b/src/include/catalog/dependency.h @@ -142,6 +142,8 @@ typedef enum ObjectClass /* in dependency.c */ +extern void AcquireDeletionLock(const ObjectAddress *object, int flags); + extern void performDeletion(const ObjectAddress *object, DropBehavior behavior, int flags); --NzB8fVQJ5HfG6fxh--