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 1tp8VJ-000vc5-F8 for pgsql-hackers@arkaria.postgresql.org; Mon, 03 Mar 2025 16:18:53 +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 1tp8VH-00B5cK-Cp for pgsql-hackers@arkaria.postgresql.org; Mon, 03 Mar 2025 16:18:51 +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 1tp8VH-00B5Z9-2U for pgsql-hackers@lists.postgresql.org; Mon, 03 Mar 2025 16:18:51 +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.96) (envelope-from ) id 1tp8VD-000jaO-1e for pgsql-hackers@postgresql.org; Mon, 03 Mar 2025 16:18:50 +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 523GIkb93664076; Mon, 3 Mar 2025 11:18:46 -0500 From: Tom Lane To: jian he cc: PostgreSQL-development Subject: Re: bug when apply fast default mechanism for adding new column over domain with default value In-reply-to: References: <2901599.1740811424@sss.pgh.pa.us> <3086966.1740873261@sss.pgh.pa.us> <3519325.1740953319@sss.pgh.pa.us> Comments: In-reply-to jian he message dated "Mon, 03 Mar 2025 16:45:21 +0800" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <3664074.1741018726.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Mon, 03 Mar 2025 11:18:46 -0500 Message-ID: <3664075.1741018726@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk jian he writes: > within ATExecAddColumn, we can > if (!missingIsNull) > StoreAttrMissingVal(rel, attribute->attnum, missingval, missingIsNull= ); > to save some cycles? Probably not really worth it: surely that's going to be a very infrequent edge case. We already eliminated cases with a simple null-Constant default, so we'd only get here with a more complex expression that evaluates to null. > + /* ... and store it. If it's null, nothing is stored. */ > + StoreAttrMissingVal(rel, attribute->attnum, > + missingval, missingIsNull); > + has_missing =3D !missingIsNull; > + FreeExecutorState(estate); > do we need pfree missingval? I don't see the point. It's not like this code leaks nothing else, and we shouldn't be running in a long-lived memory context. > maybe here add comments mentioning that the identity column needs table = rewrite. > since we removed ``tab->rewrite |=3D AT_REWRITE_DEFAULT_VAL;`` > in the if (colDef->identity) branch. We don't need that because it's not a special case anymore: the NextValueExpr will correctly be seen as a volatile default. The existing code needs that hack because it creates the NextValueExpr after the point at which expression volatility is checked, but that's just poor design. > looking at DefineRelation comments: > * We can set the atthasdef flags now in the tuple descriptor; this = just > * saves StoreAttrDefault from having to do an immediate update of t= he > * pg_attribute rows. > this seems not right? > DefineRelation->heap_create_with_catalog->heap_create->RelationBuildLoca= lRelation->CreateTupleDescCopy > don't copy atthasdef. > RelationBuildLocalRelation later didn't touch atthasdef. > populate_compact_attribute didn't touch atthasdef. > so StoreAttrDefault has to update that pg_attribute row. You are right: that code has no effect, and if it did have an effect it would be wrong in the case where somebody writes "DEFAULT NULL" explicitly. We'd end with atthasdef set and nothing in pg_attrdef, which would upset various places later. So we should remove that comment and also the adjustments of atthasdef. That seems like an independent patch though. regards, tom lane