Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fxamH-0002Pe-J5 for pgsql-general@arkaria.postgresql.org; Wed, 05 Sep 2018 16:35: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 1fxamF-0001AP-8c for pgsql-general@arkaria.postgresql.org; Wed, 05 Sep 2018 16:35: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_SHA384:256) (Exim 4.89) (envelope-from ) id 1fxamE-0001AI-UJ for pgsql-general@lists.postgresql.org; Wed, 05 Sep 2018 16:35:35 +0000 Received: from mail-pg1-x542.google.com ([2607:f8b0:4864:20::542]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1fxamB-0000go-B7 for pgsql-general@postgresql.org; Wed, 05 Sep 2018 16:35:34 +0000 Received: by mail-pg1-x542.google.com with SMTP id d19-v6so3717380pgv.1 for ; Wed, 05 Sep 2018 09:35:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=illuminatedcomputing-com.20150623.gappssmtp.com; s=20150623; h=from:subject:to:message-id:date:user-agent:mime-version :content-language:content-transfer-encoding; bh=772lb89xxOmGhFtv2h3MQJIZr3nikAqzKwPUsNhOl/o=; b=K7lLzV6rkI5dIlo2V/r1TYpB1C9IpisXdKFivUWLsporGbfBL9z7g0ewCCY8Y0ESjg CmljJFOoBj5flBtWXdxwFFsuQZHC9BlaMXdby0fYJBGRzWNmVDZNF15q33hs3ZRRiW7P Il9qxVCk6WWWB09wZQzgERE4lZkEUkz8Rqu2NeHNcwQdF8R7CgbFl7aQHom93fkrt6Sy kElMNRvEQPsZDAZvMDZEf7AWZ9XcR3rd7xDmdW3d98V494aMD7PaVeT/VFoExjugCAcs 86joZxOwKdfBYgkXFtAQh+kKKOdyIYygPbVnilxdjn6i66HZNUPuPiC3Xab61RhokmmN LFMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:subject:to:message-id:date:user-agent :mime-version:content-language:content-transfer-encoding; bh=772lb89xxOmGhFtv2h3MQJIZr3nikAqzKwPUsNhOl/o=; b=bZL36bb5wPGOSPSq3gBxNT/yB8IRNk8YNwpuwTEp+Eogi2/DiB1DOUiK0kphrtzxHW fjqS8yVTy4UwrxzptPqp6l0HM1JzA+veN7sjWSA5layG66CJsU6evZSLwItuqw0wvRBn q0fm5TyvsQNOOLf71pFsfl4QHHKSqaWh+UttwhCfbSO4Pp/ZOIfRdKKfFHmy6Jt/umW2 Gtqfj6WGKFeLF4BBehCViVaJrQ6d20G6Kco0L44AErbV6j7r4h2OuQQEPnBQv+bIudhO TGoOaAqdbF3vMSOX6tNVnef5qB61yA2gxxtb0fJLLT58f8CkBm0wKzffzMZCN41ePwS8 t2FA== X-Gm-Message-State: APzg51CWnsPJYW3gnVSgjVUrsSkSzYonZa0Esi76C2gTHLk460/kDdPK ysOmNshQgfWejgJenPOy3SVFEmh5lI4= X-Google-Smtp-Source: ANB0VdbHNFFwp5kfKxkjhm1BrxAGlxIgK1kogJGiY2FGtGH/0NDQFIhxQt62wLO7jn8m65MKkuiVvg== X-Received: by 2002:a63:df04:: with SMTP id u4-v6mr37689677pgg.434.1536165328583; Wed, 05 Sep 2018 09:35:28 -0700 (PDT) Received: from [192.168.2.201] (static-50-53-109-254.bvtn.or.frontiernet.net. [50.53.109.254]) by smtp.googlemail.com with ESMTPSA id v72-v6sm7085807pfj.22.2018.09.05.09.35.27 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 05 Sep 2018 09:35:28 -0700 (PDT) From: Paul Jungwirth Subject: Full table lock dropping a foreign key To: "pgsql-general@postgresql.org" Message-ID: <5cfcdc5c-79db-98ed-6596-bc1fbae3ac10@illuminatedcomputing.com> Date: Wed, 5 Sep 2018 09:35:27 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hello, I noticed that Postgres takes an AccessExclusiveLock (a lock on the whole table) against the *referenced* table when dropping a foreign key. I wasn't expecting that, and some experimentation showed it does *not* take one when creating the FK. For example: pjtest=# create table parent (id integer primary key); CREATE TABLE pjtest=# create table child (id integer primary key, parent_id integer); CREATE TABLE pjtest=# begin; BEGIN pjtest=# alter table child add constraint pfk foreign key (parent_id) references parent (id); ALTER TABLE And now pg_locks has this: pjtest=# select locktype, relation::regclass, mode from pg_locks; locktype | relation | mode ---------------+-------------+----------------------- relation | parent_pkey | AccessShareLock relation | child_pkey | AccessShareLock virtualxid | | ExclusiveLock relation | pg_locks | AccessShareLock virtualxid | | ExclusiveLock relation | parent | AccessShareLock relation | parent | RowShareLock relation | parent | ShareRowExclusiveLock transactionid | | ExclusiveLock relation | child | AccessShareLock relation | child | ShareRowExclusiveLock (11 rows) But after dropping it: pjtest=# commit; COMMIT pjtest=# begin; BEGIN pjtest=# alter table child drop constraint pfk; ALTER TABLE Now my locks are: pjtest=# select locktype, relation::regclass, mode from pg_locks; locktype | relation | mode ---------------+----------+--------------------- virtualxid | | ExclusiveLock relation | pg_locks | AccessShareLock virtualxid | | ExclusiveLock relation | parent | AccessExclusiveLock relation | child | AccessExclusiveLock transactionid | | ExclusiveLock object | | AccessExclusiveLock object | | AccessExclusiveLock object | | AccessExclusiveLock object | | AccessExclusiveLock object | | AccessExclusiveLock (11 rows) I noticed this on 9.5 but confirmed it on 10.5. I was surprised because the docs give a pretty short list of things that take AccessExclusiveLocks (https://www.postgresql.org/docs/current/static/explicit-locking.html). It mentions ALTER TABLE, and it makes sense when I recall that foreign keys are implemented by putting triggers on *both* referencing & referenced tables, but still it caught me off guard. Also I don't understand why the lock is not necessary when adding a foreign key? Anyway I don't have much of a question, although I wouldn't mind adding a note to the docs that dropping an FK takes this lock on both tables, if others agree that is a good idea. Yours, -- Paul ~{:-) pj@illuminatedcomputing.com