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 1nlfS1-0002UN-2B for pgsql-general@arkaria.postgresql.org; Mon, 02 May 2022 23:27:33 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nlfRz-0003oA-UT for pgsql-general@arkaria.postgresql.org; Mon, 02 May 2022 23:27:31 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nlfRz-0003o1-IX for pgsql-general@lists.postgresql.org; Mon, 02 May 2022 23:27:31 +0000 Received: from mail-ed1-x52e.google.com ([2a00:1450:4864:20::52e]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1nlfRu-0006vq-Iw for pgsql-general@lists.postgresql.org; Mon, 02 May 2022 23:27:31 +0000 Received: by mail-ed1-x52e.google.com with SMTP id be20so18194609edb.12 for ; Mon, 02 May 2022 16:27:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=fKjE7+5RT0cgMYzZ8TXhUc/JtTX7Ab3KXfrZvfDtGio=; b=Wx3o+LL0hpUVfje14W/VVI7QPSk/Y2MRdfQcoEnC1f7pXwaHRG66DXXIkLbC2/tYKn UmeANA3fenwRXHwPXTiLmT7g5JcBBC1Q1Ur2uh3h6bCKTDqhmAHUIVAl+OfU8kUPMQC4 mdsViMQ5TK/JwuUgYlX2gyvoUdC4pZfxXvSbUPXnDRAsX5Udyckp4LxyCyFsya817g/3 73MKBZ129O8m+CoqEkLazz32jdgGDUK2YhzAF86ddFhg05ih+OUlyk8k1eyv+Mgs1maJ fkHgDsNSvHlzZvxosV1Sh0+VI7n5Axm73QKtiAmYgbZyIXRq311/4ZmrGA+/JoLPGRtF QimQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=fKjE7+5RT0cgMYzZ8TXhUc/JtTX7Ab3KXfrZvfDtGio=; b=QOAHq/vsFt27ESY/TO8UQ7JrDCtSvz/u6tNyq4Spn7qx0sEMepeybNLay07xWoVXEM cSWvNySxOXIuc+eR3cnYS3bAHeeDOxleidg4CtHeGzeXj+kDRCldONRFBuxC2G+kv0+y ftVO4KIfysWobaGsB3m3XZ3MIMWNtLfMd1bLWyvYLc99Rt2h8mIRGdExPm0UCubxndR+ Wvg0xVzq1oxuWgJGdpwLgTQoBczuYOZ7f3l5Yjst9cI4qAJcxX+hKL7ykeK0v+4/jLTC qzm5Tcd7RvNpvr906zh+If1exYN3g5UMY4wPLizBobRcd94ihJs2msE0Q9jnmYV+nIOZ 62cQ== X-Gm-Message-State: AOAM532rDARFd7FMWmpI0vWZLmtmB9BN7qGTVkV4VzMsKbsHEu8Wdx8K 8LLexxdL1Hh3gnQB2aZBMp+i45N4FqsnwJht/i4= X-Google-Smtp-Source: ABdhPJx7fmfokPGUYoesQpwKAnJRhdY0hTDoiA/+CZrva8EzeWa1EDu7zY+XkTQENjEGF+o5e+AOaGrYtqccaW7KQNA= X-Received: by 2002:a05:6402:1941:b0:413:2b5f:9074 with SMTP id f1-20020a056402194100b004132b5f9074mr15230355edz.414.1651534045591; Mon, 02 May 2022 16:27:25 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: "David G. Johnston" Date: Mon, 2 May 2022 16:27:07 -0700 Message-ID: Subject: Re: UUID vs serial and currval('sequence_id') To: Robert Stanford Cc: "pgsql-generallists.postgresql.org" Content-Type: multipart/alternative; boundary="0000000000001f4ec205de0fbce6" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --0000000000001f4ec205de0fbce6 Content-Type: text/plain; charset="UTF-8" On Mon, May 2, 2022 at 4:24 PM Robert Stanford wrote: > On Tue, 3 May 2022 at 08:39, David G. Johnston > 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. --0000000000001f4ec205de0fbce6 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
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 variabl= es.=C2=A0 Which/how depends on the language you are writing in.=C2=A0 Pure = SQL without client involvement requires that you use chained CTEs of INSERT= ...RETURNING (or I suppose you could leverage set_config(), haven't tri= ed that way myself).=C2=A0 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=C2=A0 I can do:
alter table contact add column contactuuid uuid
alter table = contactinterests add column contactuuid uuid
alter table contactinterest= s drop column contactid

with thisuuid as (
= =C2=A0 =C2=A0 SELECT gen_random_uuid() as thisuuid
),
=C2=A0contactuu= id as(
=C2=A0 =C2=A0 INSERT INTO contact(
=C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0contactuuid,firstname, lastname)
=C2=A0 =C2=A0 VALUES(
= =C2=A0 =C2=A0 =C2=A0 =C2=A0 (select thisuuid =C2=A0from thisuuid ),'Joh= n', 'Smith') returning (select thisuuid =C2=A0from thisuuid ))
INSERT INTO contactinterests(
=C2=A0 =C2=A0 contactuuid, interest= )
=C2=A0 VALUES (
=C2=A0 =C2=A0 (select thisuuid =C2=A0from co= ntactuuid ),'Fishing')
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 return= ing (select thisuuid =C2=A0from 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 sugge= st avoiding naming the CTE query and the column(s) it produces the same thi= ng.

David J.

David J.
<= /div>
--0000000000001f4ec205de0fbce6--