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 1nB7oT-0004xK-7F for pgsql-bugs@arkaria.postgresql.org; Sat, 22 Jan 2022 04:15:41 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nB7oR-00071M-QH for pgsql-bugs@arkaria.postgresql.org; Sat, 22 Jan 2022 04:15:39 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nB7oR-00071D-I9 for pgsql-bugs@lists.postgresql.org; Sat, 22 Jan 2022 04:15:39 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nB7oL-0008Qm-15 for pgsql-bugs@lists.postgresql.org; Sat, 22 Jan 2022 04:15:39 +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 20M4FPOV1318101; Fri, 21 Jan 2022 23:15:25 -0500 From: Tom Lane To: Japin Li cc: adrien.gilmore+pg@gmail.com, pgsql-bugs@lists.postgresql.org Subject: Re: BUG #17376: Adding unique column with a function() default results in "could not read block 0 in file" error In-reply-to: References: <17376-07b867ccc817828f@postgresql.org> <1196187.1642786604@sss.pgh.pa.us> Comments: In-reply-to Japin Li message dated "Sat, 22 Jan 2022 08:18:14 +0800" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1318099.1642824925.1@sss.pgh.pa.us> Date: Fri, 21 Jan 2022 23:15:25 -0500 Message-ID: <1318100.1642824925@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Japin Li writes: > On Sat, 22 Jan 2022 at 01:36, Tom Lane wrote: >> However, I fear we don't have adequate infrastructure to tell which >> table accesses *are* part of the ALTER TABLE machinery and which aren't. > Right. >> Maybe it'd be sufficient to check for an active ALTER TABLE in the >> parser, but I'm not sure. > Do you mean check the table is accessed by ALTER TABLE when calling SPI to > execute the function? How can we get the table that is be accessed by > ALTER TABLE in parser? The reason there's not a problem like this with respect to cross-session accesses is that the exclusive lock acquired by ALTER TABLE prevents other sessions from touching the table. Conversely, the reason we do have a problem here is that such locks don't prevent accesses within our own session. So a rough sketch for fixing this might be "at the point where we acquire a lock on some table for a new statement, also check to see if some internal-to-our-session operation has declared a need for exclusive access". The problem is that we don't have a well-defined line as to what's part of the ALTER and what is not. For example, I recall that ALTER TABLE handles index rebuilding by creating textual ALTER ADD INDEX commands, and parsing and executing those. How can we distinguish that behavior from a SQL function within an index or CHECK constraint trying to execute ALTER ADD INDEX? regards, tom lane