Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tDptw-003W2g-Ao for pgsql-general@arkaria.postgresql.org; Wed, 20 Nov 2024 18:58:08 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1tDptu-008Swo-8j for pgsql-general@arkaria.postgresql.org; Wed, 20 Nov 2024 18:58:06 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tDptt-008Swg-Tb for pgsql-general@lists.postgresql.org; Wed, 20 Nov 2024 18:58:05 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tDptn-002vJn-B8 for pgsql-general@lists.postgresql.org; Wed, 20 Nov 2024 18:58:04 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 4AKIvvqo232552; Wed, 20 Nov 2024 13:57:57 -0500 From: Tom Lane To: Istvan Soos cc: pgsql-general@lists.postgresql.org Subject: Re: A table lock inside a transaction depends on query protocol being used? In-reply-to: References: Comments: In-reply-to Istvan Soos message dated "Wed, 20 Nov 2024 19:04:44 +0100" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <232550.1732129077.1@sss.pgh.pa.us> Date: Wed, 20 Nov 2024 13:57:57 -0500 Message-ID: <232551.1732129077@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Istvan Soos writes: > The following minimal reproduction case can be used locally: > Setup: > CREATE TABLE a ( > a_id INTEGER PRIMARY KEY NOT NULL, > a_other_id INTEGER NOT NULL > ); > CREATE TABLE b (other_id INTEGER PRIMARY KEY NOT NULL); > BEGIN; > SELECT * FROM a; > ALTER TABLE a ADD CONSTRAINT fk_other FOREIGN KEY (a_other_id) > REFERENCES b(other_id); > At which point we get '55006: cannot ALTER TABLE "a" because it is > being used by active queries in this session'. It makes sense, > however, if we change the SELECT to simple query protocol, the error > is not present and the transaction completes. Your message trace isn't too clear (it's not apparent where you're issuing the ALTER TABLE), but I wonder if you could be failing to close out the SELECT statement before issuing ALTER. The error message implies that something is still holding a reference count on "a"'s relcache entry, and it's hard to see what that could be except a still-open Portal for the SELECT. regards, tom lane