Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jMmCh-0005JW-9h for pgsql-general@arkaria.postgresql.org; Fri, 10 Apr 2020 05:27:47 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jMmCg-0000VK-3V for pgsql-general@arkaria.postgresql.org; Fri, 10 Apr 2020 05:27:46 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jMmCf-0000VD-Oa for pgsql-general@lists.postgresql.org; Fri, 10 Apr 2020 05:27:45 +0000 Received: from mail-wr1-x441.google.com ([2a00:1450:4864:20::441]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1jMmCa-0001Ya-Iq for pgsql-general@postgresql.org; Fri, 10 Apr 2020 05:27:44 +0000 Received: by mail-wr1-x441.google.com with SMTP id v5so1035295wrp.12 for ; Thu, 09 Apr 2020 22:27:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20150623.gappssmtp.com; s=20150623; h=message-id:subject:from:to:date:in-reply-to:references:user-agent :mime-version:content-transfer-encoding; bh=hwb93PwdQhe73XWrZlX3IAfdR4rM/gVIJfzFT0zQjg4=; b=SXALJUbT/5A78EQuMmgLk5nEyKC/OlyPdKM1VipzQoxj1Q83smBLq7V15ekDj3nxi/ MBoRsxp9ZPzMITTdvUgepwVBuJYEKoVSuGNQG/wrEajbA2G544jZeI/B8t+Y3SSvNu8r hqWXNCDShIxhljcBJ2Qj5xStHsN9GM7waXZyZ8JWkYDHyKeKSPdZ/oGmEFkt5dGPdK+v KYotTdY126aJFhlCqRnf1RjigiQ6nZ3naMB+5DP2zdiN+/iJKXdyOPSHo8n+z+KBjWG5 LcJ2X3wwcpDDCuOuSzSGu50K2JBaLEc7PpGxdm0E0YHCNGI588l4Awids00FRRPkROxi eXFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:subject:from:to:date:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=hwb93PwdQhe73XWrZlX3IAfdR4rM/gVIJfzFT0zQjg4=; b=ldqOq8gHQBbE6ItjEaN/8Ny2la16w1reNaTaOBoX/w8ie8fYjSeWowY7YaXKDkOnC3 TFQ4HTGcH4GI2151N3ikJTBmkql6oTMe6fnXzHPxXFU8GWdA1gruYjZrg6lfmJv/uFyj So0HmW5YR0IEY7PNw8pTuygBGB1KGXd5VrfMfxNB/0Wcrq3S1dSMqbaIyRPGpAmimwdL zuiZqnUUMNFpJAjrEAFcs52qfVDNkdMuvZ9+x2jxFSy2+/47rkVmMpMRCh0XmJv8hq+l RsAi4Hk/tGghmhRsBgE7ps9AZ5nWv5pR+6cBvPYTauvbBl/bj195sZkXXsUpMsqSdR/c OBbA== X-Gm-Message-State: AGi0PuaiUQLu/yyLy3rpHxqQgwzqMTBVKVNQBG11zkUOktqJgSqq1bsl PUrT2x0L0qTF7uH/aORsyuY0Vg== X-Google-Smtp-Source: APiQypJE5U8tpo2vuDCSKgVl2Sn1xGYxysChKyCrFB89fxRAmchDkvQiYW584dWRU+kAbXWzf+ocFw== X-Received: by 2002:a5d:5230:: with SMTP id i16mr2700294wra.15.1586496458653; Thu, 09 Apr 2020 22:27:38 -0700 (PDT) Received: from dynamic-em36j457sllv9hwse-pd01.res.v6.highway.a1.net (dynamic-em36j457sllv9hwse-pd01.res.v6.highway.a1.net. [2001:871:60:346b:2b57:cfcd:9dfc:d53e]) by smtp.gmail.com with ESMTPSA id i9sm1439189wrn.25.2020.04.09.22.27.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Apr 2020 22:27:38 -0700 (PDT) Message-ID: <3a4e7019df93301b82a98ddbfa73d31170d03a45.camel@cybertec.at> Subject: Re: Different Lock Behavior With Create and Drop Foreign Key From: Laurenz Albe To: Virendra Kumar , Pgsql-general General Date: Fri, 10 Apr 2020 07:27:37 +0200 In-Reply-To: <834603375.3470346.1586482852542@mail.yahoo.com> References: <834603375.3470346.1586482852542.ref@mail.yahoo.com> <834603375.3470346.1586482852542@mail.yahoo.com> Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.32.5 (3.32.5-1.fc30) MIME-Version: 1.0 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Fri, 2020-04-10 at 01:40 +0000, Virendra Kumar wrote: > Here is test case. > ---- > create table testx > ( > v_code character varying(32), > client_id bigint > ); > alter table testx add constraint testx_pkey primary key (v_code); > > create table testy > ( > dxid bigint, > v_code character varying(32) > ); > alter table testy add constraint testy_pkey primary key (dxid); > create index on testy (v_code); > > Let's begin two session, in session1 I am going begin a transaction and run select on parent table: > > Session1 > ---- > begin; > select * from testx; > > > On another session let's call session2, I am running create FK on second table > > Session2 > --- > alter table testy add constraint testy_fkey foreign key (v_code) references testx(v_code); <--This works. > alter table testy drop constraint testy_fkey; <--Hangs That is because foreign keys are implemented with system triggers, some of which are defined on the target table. Now CREATE TRIGGER does not require an ACCESS EXCLUSIVE lock, but DROP TRIGGER does. Session 1 holds an ACCESS SHARE lock on the table, which conflicts only with ACCESS EXCLUSIVE. So creating the foreign key works, but dropping it hangs when the triggers are dropped. Yours, Laurenz Albe -- Cybertec | https://www.cybertec-postgresql.com