proposal - operators ? and ->> for type record, and functions record_keys and record_each_text
Hi
When I wrote an reply to questing
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:
basic-record-type-funcs-and-operators.patchtext/x-patch; charset=US-ASCII; name=basic-record-type-funcs-and-operators.patchDownload+489-0
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
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