pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Erik Wienhold <ewie@ewie.name>
To: Adrian Klaver <adrian.klaver@aklaver.com>
To: Mark Raynsford <co+org.postgresql@io7m.com>
Cc: David G. Johnston <david.g.johnston@gmail.com>
Cc: pgsql-general@lists.postgresql.org
Subject: Re: Column value derived from generated column in INSERT?
Date: Thu, 20 Oct 2022 00:09:09 +0200 (CEST)
Message-ID: <1406846786.124035.1666217349479@office.mailbox.org> (raw)
In-Reply-To: <82c73b61-3028-639d-5406-e50dbc998498@aklaver.com>
References: <20221019173225.53d939f2@sunflower.int.arc7.info>
	<CAKFQuwZpq-hZ-zBN8KhzF3Ke3bheHHpp_f=tEkH30+JTXtaMsg@mail.gmail.com>
	<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>

> On 19/10/2022 23:51 CEST Adrian Klaver <adrian.klaver@aklaver.com> 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 <adrian.klaver@aklaver.com> wrote:
> >>>
> >>> HINT:  There is an entry for table "t", but it cannot be referenced from
> >>> this part of the query.
> >>>
> >>> HINT:  There is a column named "x" in table "t", but it cannot be
> >>> 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 = 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 define
it as part of the database schema, if I understand it correctly.

--
Erik





view thread (9+ messages)  latest in thread

Message-ID: <1406846786.124035.1666217349479@office.mailbox.org>
Permalink:  ../1406846786.124035.1666217349479@office.mailbox.org/
Also on:    postgresql.org/message-id/1406846786.124035.1666217349479@office.mailbox.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: ewie@ewie.name, adrian.klaver@aklaver.com, co+org.postgresql@io7m.com, david.g.johnston@gmail.com
  Subject: Re: Column value derived from generated column in INSERT?
  In-Reply-To: <1406846786.124035.1666217349479@office.mailbox.org>
  References: <20221019173225.53d939f2@sunflower.int.arc7.info> <CAKFQuwZpq-hZ-zBN8KhzF3Ke3bheHHpp_f=tEkH30+JTXtaMsg@mail.gmail.com> <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> <1406846786.124035.1666217349479@office.mailbox.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox