proposal - operators ? and ->> for type record, and functions record_keys and record_each_text

Started by Pavel Stehuleover 5 years ago3 messageshackers
Beta feature

Hackorum builds and tests every patch posted to the lists, not only commitfest submissions. This is Hackorum's own CI rather than the PostgreSQL project's, and it is still under testing - please report anything that looks wrong.

won't retrysuccessCI history

You can run a PostgreSQL built from this patch straight from Docker, with no checkout and no build:

docker run --rm -p 5432:5432 ghcr.io/hackorum-dev/postgres-patch:t43818
psql -h localhost -U postgres

Built from patchset v1 (message #1), July 27, 2026 at 05:32 PM.

Every patchset is also pushed to a branch of our PostgreSQL fork, so you can check out the same tree CI built. Without a PostgreSQL checkout:

git clone --branch t43818_1 https://github.com/hackorum-dev/postgres.git

In a checkout you already have, add the fork once:

git remote add hackorum https://github.com/hackorum-dev/postgres.git

then, for this patchset and every later one:

git fetch hackorum t43818_1 && git checkout t43818_1

Patchset v1 (message #1) is on t43818_1

Jump to latest
#1Pavel Stehule
pavel.stehule@gmail.com

Hi

When I wrote an reply to questing

https://stackoverflow.com/questions/66523737/postgresql-10-pl-pgsql-test-if-column-exits-in-a-record-variable

I found an interesting idea to have some basic functions and operators for
record type (similar to json, jsonb or hstore).

Now we can do almost all tasks on record type by cast to jsonb type. But
this transformation has some overhead (and for some tasks is not
necessary), and it is not too intuitive too.

I don't think so we need full functionality like hstore or jsonb (minimally
because record type cannot be persistent and indexed), but some basic
functionality can be useful.

-- tests of basic helper functions for record type
do $$
declare
r record;
k text; v text; t text;
begin
select oid, relname, relnamespace, reltype from pg_class limit 1 into r;
if not r ? 'xxx' then
raise notice 'pg_class has not column xxx';
end if;

if r ? 'relname' then
raise notice 'pg_class has column relname';
end if;

foreach k in array record_keys_array(r)
loop
raise notice '% => %', k, r->>k;
end loop;

raise notice '---';

-- second (slower) variant
for k in select * from record_keys(r)
loop
raise notice '% => %', k, r->>k;
end loop;

raise notice '---';

-- complete unpacking
for k, v, t in select * from record_each_text(r)
loop
raise notice '% => %(%)', k, v, t;
end loop;
end;
$$;

What do you think about this proposal?

Comments, notes?

Regards

Pavel

Attachments:

t43818_1
basic-record-type-funcs-and-operators.patchtext/x-patch; charset=US-ASCII; name=basic-record-type-funcs-and-operators.patchDownload+489-0
#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Pavel Stehule (#1)
Re: proposal - operators ? and ->> for type record, and functions record_keys and record_each_text

Pavel Stehule <pavel.stehule@gmail.com> writes:

I found an interesting idea to have some basic functions and operators for
record type (similar to json, jsonb or hstore).

I think this is a pretty bad idea, because there's no way to know what
data type the result of -> should be. "Smash it all to text" is a hack,
not a solution --- and if you find that hack satisfactory, you might as
well be using json or hstore.

Most of the other things you mention are predicated on the assumption
that the field set will vary from one value to the next, which again
seems more like something you'd do with json or hstore than with SQL
composites.

regards, tom lane

#3Pavel Stehule
pavel.stehule@gmail.com
In reply to: Tom Lane (#2)
Re: proposal - operators ? and ->> for type record, and functions record_keys and record_each_text

po 8. 3. 2021 v 23:12 odesílatel Tom Lane <tgl@sss.pgh.pa.us> napsal:

Pavel Stehule <pavel.stehule@gmail.com> writes:

I found an interesting idea to have some basic functions and operators

for

record type (similar to json, jsonb or hstore).

I think this is a pretty bad idea, because there's no way to know what
data type the result of -> should be. "Smash it all to text" is a hack,
not a solution --- and if you find that hack satisfactory, you might as
well be using json or hstore.

I wrote (and sent) an implementation of generic type, that can hold any
type in binary form, and that can reduce IO casts. It can be more effective
than text, but an usability is the same like json or text, because you have
to use explicit casts everywhere. I think other solutions are not possible,
because you don't know the real type before an evaluation.

Most of the other things you mention are predicated on the assumption
that the field set will vary from one value to the next, which again
seems more like something you'd do with json or hstore than with SQL
composites.

I am thinking about effectiveness in triggers. NEW and OLD variables are of
record type, and sometimes you need to do operation just on tupledesc. When
I work with a record type, I can do it, without any overhead. When I need
to use jsonb or hstore, I have to pay, because all fields should be
transformated.

Minimally the operator "?" can be useful. It allows access to statically
specified fields without risk of exception. So I can write universal
trigger with

IF NEW ? 'fieldx' THEN
RAISE NOTICE '%', NEW.fieldx ;

and this operation can be fast and safe

Show quoted text

regards, tom lane