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 1olHFW-0005eV-GH for pgsql-general@arkaria.postgresql.org; Wed, 19 Oct 2022 22:09:18 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1olHFV-00033C-14 for pgsql-general@arkaria.postgresql.org; Wed, 19 Oct 2022 22:09:17 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1olHFU-00032v-Ml for pgsql-general@lists.postgresql.org; Wed, 19 Oct 2022 22:09:16 +0000 Received: from mout-u-204.mailbox.org ([80.241.59.204]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1olHFS-00021z-7Y for pgsql-general@lists.postgresql.org; Wed, 19 Oct 2022 22:09:15 +0000 Received: from smtp102.mailbox.org (smtp102.mailbox.org [IPv6:2001:67c:2050:b231:465::102]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-u-204.mailbox.org (Postfix) with ESMTPS id 4Mt4dL08G5z9sSl; Thu, 20 Oct 2022 00:09:10 +0200 (CEST) Date: Thu, 20 Oct 2022 00:09:09 +0200 (CEST) From: Erik Wienhold To: Adrian Klaver , Mark Raynsford Cc: "David G. Johnston" , pgsql-general@lists.postgresql.org Message-ID: <1406846786.124035.1666217349479@office.mailbox.org> In-Reply-To: <82c73b61-3028-639d-5406-e50dbc998498@aklaver.com> References: <20221019173225.53d939f2@sunflower.int.arc7.info> <20221019193057.39aff1c1@sunflower.int.arc7.info> <82c54487-2f99-7829-6f76-a266afb8890a@aklaver.com> <20221019194805.79d0b2ed@sunflower.int.arc7.info> <507b68d6-db0d-6cc5-f9d3-2dbd7bc8ad97@aklaver.com> <82c73b61-3028-639d-5406-e50dbc998498@aklaver.com> Subject: Re: Column value derived from generated column in INSERT? MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Priority: 3 Importance: Normal X-Rspamd-Queue-Id: 4Mt4dL08G5z9sSl List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > On 19/10/2022 23:51 CEST Adrian Klaver wrote: > > On 10/19/22 12:58 PM, Adrian Klaver wrote: > > On 10/19/22 12:48, Mark Raynsford wrote: > >> On 2022-10-19T12:43:31 -0700 > >> Adrian Klaver wrote: > >>> > >>> HINT:=C2=A0 There is an entry for table "t", but it cannot be referen= ced from > >>> this part of the query. > >>> > >>> HINT:=C2=A0 There is a column named "x" in table "t", but it cannot b= e > >>> referenced from this part of the query. > >> > >> Yes, I saw those, hence asking on the list if there was a way to do it= . > > > > Using a trigger. > > To expand: > > create table t ( > x integer not null generated always as identity, > y integer not null > ); > insert into t(y) values (1); > > select * from t; > > x | y > ---+--- > 1 | 1 > (1 row) > > > > CREATE FUNCTION identity_test( ) > RETURNS trigger > LANGUAGE plpgsql > AS $function$ > BEGIN > NEW.y =3D NEW.x * 2; > RETURN NEW; > END; > > $function$ > ; > > create trigger identity_trg before insert on t for each row execute > function identity_test(); > > insert into t(y) values (0); > > select * from t; > > x | y > ---+--- > 1 | 1 > 2 | 4 > (2 rows) Make t.y a generated column and avoid the trigger: create table t ( x int not null generated always as identity, y int not null generated always as (x * 2) stored; ); insert into t (x) values (default), (default); select * from t; x | y ---+--- 1 | 2 2 | 4 (2 rows) But I think Mark wants to specify the expression in the INSERT and not defi= ne it as part of the database schema, if I understand it correctly. -- Erik