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 1qprnN-007MDV-HB for pgsql-bugs@arkaria.postgresql.org; Mon, 09 Oct 2023 15:03:45 +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 1qprnK-00FD1G-CJ for pgsql-bugs@arkaria.postgresql.org; Mon, 09 Oct 2023 15:03:43 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qprnK-00FD0F-2t for pgsql-bugs@lists.postgresql.org; Mon, 09 Oct 2023 15:03:43 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qprnH-0016Xx-Nd for pgsql-bugs@lists.postgresql.org; Mon, 09 Oct 2023 15:03:42 +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 399F3bQI143484; Mon, 9 Oct 2023 11:03:37 -0400 From: Tom Lane To: "David G. Johnston" cc: "liam@morland.ca" , "pgsql-bugs@lists.postgresql.org" Subject: Re: BUG #18151: pg_upgradecluster fails when column default refers to column In-reply-to: References: <18151-cdc2191b130172c7@postgresql.org> Comments: In-reply-to "David G. Johnston" message dated "Mon, 09 Oct 2023 07:31:32 -0700" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <143482.1696863817.1@sss.pgh.pa.us> Date: Mon, 09 Oct 2023 11:03:37 -0400 Message-ID: <143483.1696863817@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk "David G. Johnston" writes: > On Sunday, October 8, 2023, PG Bug reporting form > wrote: >> The reason is the the default value for column "event_id" of table "event" >> is "event_id_nextval()". That function is: >> SELECT COALESCE(MAX(event_id) + 1, 1) FROM event; > You are expressly forbidden to have the default value expression of a > column reference a subquery. You must use a trigger. Well, you're not allowed to do it directly: regression=# create table foo (f1 int default ((select max(f1) from foo))); ERROR: cannot use subquery in DEFAULT expression LINE 1: create table foo (f1 int default ((select max(f1) from foo))... ^ but that's just a minor implementation restriction. Doing it through a function is fine. (Whether it's a good idea is another question.) Unlike, say, CHECK constraints, there's not any expectation that a default expression be immutable or avoid dependence on database state --- if there was, "default nextval(...)" would be problematic. In any case, dump/restore ought not fail like this. regards, tom lane