pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: David G. Johnston <david.g.johnston@gmail.com>
To: Robert Stanford <rstanford@gmail.com>
Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
Subject: Re: UUID vs serial and currval('sequence_id')
Date: Mon, 2 May 2022 16:27:07 -0700
Message-ID: <CAKFQuwaNO9XBtDPYgrg1ntQd---QuO2fQEodOKVe5cFuqizdOA@mail.gmail.com> (raw)
In-Reply-To: <CAC1FqCFY23SmQJPohSA-qRO=fNR-286fHKsvuMQ-u6NzxFopZw@mail.gmail.com>
References: <CAC1FqCE54E6_OHkAmHq6MO_=u87-0ON875A52=Ji9WLwieSTUw@mail.gmail.com>
	<CAKFQuwYD1niYAB1RRAMEJUrePqXxQ1tzgFnFdOjQ-xpLhqpzzg@mail.gmail.com>
	<CAC1FqCFY23SmQJPohSA-qRO=fNR-286fHKsvuMQ-u6NzxFopZw@mail.gmail.com>

On Mon, May 2, 2022 at 4:24 PM Robert Stanford <rstanford@gmail.com> wrote:

> On Tue, 3 May 2022 at 08:39, David G. Johnston <david.g.johnston@gmail.com>
> wrote:
>
>> You basically have to use "INSERT ... RETURNING" or variables.  Which/how
>> depends on the language you are writing in.  Pure SQL without client
>> involvement requires that you use chained CTEs of INSERT...RETURNING (or I
>> suppose you could leverage set_config(), haven't tried that way myself).
>> In pl/pgsql you can also use variables, and the same goes for psql - though
>> that requires client involvement and so isn't generally that great a choice.
>>
>>
> Thanks, so  I can do:
>
> alter table contact add column contactuuid uuid
> alter table contactinterests add column contactuuid uuid
> alter table contactinterests drop column contactid
>
> with thisuuid as (
>     SELECT gen_random_uuid() as thisuuid
> ),
>  contactuuid as(
>     INSERT INTO contact(
>          contactuuid,firstname, lastname)
>     VALUES(
>         (select thisuuid  from thisuuid ),'John', 'Smith') returning
> (select thisuuid  from thisuuid )
> )
> INSERT INTO contactinterests(
>     contactuuid, interest)
>   VALUES (
>     (select thisuuid  from contactuuid ),'Fishing')
>           returning (select thisuuid  from contactuuid );
>
>
It works but "returning contactuuid" is considerably easier to understand
and probably cheaper to execute.

If you are going to pre-compute the uuid the returning clause becomes
pointless though, as your example demonstrates - you never actually use the
returned value.

I suggest avoiding naming the CTE query and the column(s) it produces the
same thing.

David J.

David J.

view thread (4+ messages)

Message-ID: <CAKFQuwaNO9XBtDPYgrg1ntQd---QuO2fQEodOKVe5cFuqizdOA@mail.gmail.com>
Permalink:  ../CAKFQuwaNO9XBtDPYgrg1ntQd---QuO2fQEodOKVe5cFuqizdOA@mail.gmail.com/
Also on:    postgresql.org/message-id/CAKFQuwaNO9XBtDPYgrg1ntQd---QuO2fQEodOKVe5cFuqizdOA@mail.gmail.com

 · 

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: david.g.johnston@gmail.com, rstanford@gmail.com
  Subject: Re: UUID vs serial and currval('sequence_id')
  In-Reply-To: <CAKFQuwaNO9XBtDPYgrg1ntQd---QuO2fQEodOKVe5cFuqizdOA@mail.gmail.com>
  References: <CAC1FqCE54E6_OHkAmHq6MO_=u87-0ON875A52=Ji9WLwieSTUw@mail.gmail.com> <CAKFQuwYD1niYAB1RRAMEJUrePqXxQ1tzgFnFdOjQ-xpLhqpzzg@mail.gmail.com> <CAC1FqCFY23SmQJPohSA-qRO=fNR-286fHKsvuMQ-u6NzxFopZw@mail.gmail.com> <CAKFQuwaNO9XBtDPYgrg1ntQd---QuO2fQEodOKVe5cFuqizdOA@mail.gmail.com>

* 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