From: Tom Lane <tgl@sss.pgh.pa.us>
To: Stéphane Klein <contact@stephane-klein.info>
Cc: pgsql-general@lists.postgresql.org
Subject: Re: Do you know a json_populate_record alternative method to create a ROW with a named field notation?
Date: Sun, 06 Mar 2022 13:54:58 -0500
Message-ID: <16489.1646592898@sss.pgh.pa.us> (raw)
In-Reply-To: <CADKxhpesteVU7GiMWqUbMKsTiRzbeXxoNGR42Hv9K7L0cduFqQ@mail.gmail.com>
References: <CADKxhpesteVU7GiMWqUbMKsTiRzbeXxoNGR42Hv9K7L0cduFqQ@mail.gmail.com>
=?UTF-8?Q?St=C3=A9phane_Klein?= <contact@stephane-klein.info> writes:
> **Question:** do you know a method like json_populate_record (
> https://www.postgresql.org/docs/13/functions-json.html), which allows
> creating a `ROW` with named field notation without using a json format?
You mean you want to give the field names explicitly in the expression?
The closest thing I can think of is to make a constructor function and
invoke it with named-argument notation:
regression=# CREATE TYPE contact AS (
regression(# firstname VARCHAR,
regression(# lastname VARCHAR
regression(# );
CREATE TYPE
regression=# create function make_contact(firstname varchar, lastname varchar)
regression-# returns contact as 'select row(firstname, lastname)::contact'
regression-# language sql;
CREATE FUNCTION
regression=# select make_contact(firstname => 'John', lastname => 'Doe');
make_contact
--------------
(John,Doe)
(1 row)
A bit tedious to set up, but it would have some advantages, eg you
could provide default values.
regards, tom lane
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: tgl@sss.pgh.pa.us, contact@stephane-klein.info
Subject: Re: Do you know a json_populate_record alternative method to create a ROW with a named field notation?
In-Reply-To: <16489.1646592898@sss.pgh.pa.us>
References: <CADKxhpesteVU7GiMWqUbMKsTiRzbeXxoNGR42Hv9K7L0cduFqQ@mail.gmail.com> <16489.1646592898@sss.pgh.pa.us>
* 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