Re: UUID vs serial and currval('sequence_id')

From: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
To: Robert Stanford <rstanford(at)gmail(dot)com>
Cc: "pgsql-generallists(dot)postgresql(dot)org" <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: UUID vs serial and currval('sequence_id')
Date: 2022-05-02 22:39:05
Message-ID: CAKFQuwYD1niYAB1RRAMEJUrePqXxQ1tzgFnFdOjQ-xpLhqpzzg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Mon, May 2, 2022 at 3:33 PM Robert Stanford <rstanford(at)gmail(dot)com> wrote:

>
> --insert statement as single transaction returning contactid
> INSERT INTO contact(
> firstname, lastname)
> VALUES('John', 'Smith');
> INSERT INTO contactinterests(
> contactid, interest)
> VALUES (currval('contact_contactid_seq'),'Fishing')
> returning currval('contact_contactid_seq');
>
> Which is very nice as it gives us back the contactid.
>
> Is it possible to get similar functionality using gen_random_uuid() or
> uuid-ossp?
>
>
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.

David J.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Mladen Gogala 2022-05-02 22:41:38 Re: External psql editor
Previous Message Robert Stanford 2022-05-02 22:33:31 UUID vs serial and currval('sequence_id')