Re: Do you know a json_populate_record alternative method to create a ROW with a named field notation?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Stéphane Klein <contact(at)stephane-klein(dot)info>
Cc: pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: Do you know a json_populate_record alternative method to create a ROW with a named field notation?
Date: 2022-03-06 18:54:58
Message-ID: 16489.1646592898@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

=?UTF-8?Q?St=C3=A9phane_Klein?= <contact(at)stephane-klein(dot)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

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Brent Wood 2022-03-07 01:35:37 Re: FDW to postGIS Database
Previous Message Stéphane Klein 2022-03-06 18:41:44 Do you know a json_populate_record alternative method to create a ROW with a named field notation?