automatic cast of anonymous record array type to the array type of a named composite type in PostgreSQL?
1
vote
0
answers
920
views
From PostgreSQL documentation , it is possible to cast an anonymous record type to a named composite type at a function call site. Quoting the example there:
CREATE TABLE mytable(f1 int, f2 float, f3 text);
CREATE FUNCTION getf1(mytable) RETURNS int AS 'SELECT $1.f1' LANGUAGE SQL;
-- No cast needed since only one getf1() exists
SELECT getf1(ROW(1,2.5,'this is a test'));
However, the implicit cast of record type does not seem to work for an array of anonymous records. If I slightly change the above example to work on an an array function taking
getf1a(mytable[])
as below, I get an error:
CREATE FUNCTION getf1a(mytable[]) RETURNS int AS 'SELECT $1.f1' LANGUAGE SQL;
SELECT getf1a(ARRAY[ROW(1,2.5,'this is a test')]);
The error says:
=> ERROR: function getf1a(record[]) does not exist
LINE 1: SELECT getf1a(ARRAY[ROW(1,2.5,'this is a test')]);
The anonymous and named array record types are compatible and there is no ambiguity. It would be nice if the cast is implicit. My question is:
Is there a way to make the implicit cast work for array record types too?
(This is with PostgreSQL 12.x)
Asked by tinlyx
(3820 rep)
Apr 15, 2020, 05:02 AM
Last activity: Apr 16, 2020, 02:45 AM
Last activity: Apr 16, 2020, 02:45 AM