Обсуждение: Couldn't cast to record[]
Hi, I created an table with composite type array as datatype for one column.
When I query that table I need the pg_typeof(column) as record[] instead of
composite_type[].
I tried creating a separate function and returning record[], but below error
is thrown,
ERROR: PL/pgSQL functions cannot return type record[]
Below is sample for my requirement.
pali=# create type address as (number bigint, city varchar);
CREATE TYPE
pali=# create table person(name varchar, addresses address[]);
CREATE TABLE
pali=# insert into person values ('Michael', array[(82, 'NYC'),(24,
'Dunkirk')]::address[]);
INSERT 0 1
pali=# select pg_typeof(addresses::record[]) from person ;
pg_typeof
-----------
address[]
Here I expect record[] instead of address[].
Thanks in advance.
When I query that table I need the pg_typeof(column) as record[] instead of
composite_type[].
I tried creating a separate function and returning record[], but below error
is thrown,
ERROR: PL/pgSQL functions cannot return type record[]
Below is sample for my requirement.
pali=# create type address as (number bigint, city varchar);
CREATE TYPE
pali=# create table person(name varchar, addresses address[]);
CREATE TABLE
pali=# insert into person values ('Michael', array[(82, 'NYC'),(24,
'Dunkirk')]::address[]);
INSERT 0 1
pali=# select pg_typeof(addresses::record[]) from person ;
pg_typeof
-----------
address[]
Here I expect record[] instead of address[].
Thanks in advance.
Hi
čt 3. 3. 2022 v 11:01 odesílatel Suresh Kumar R <suresh.arsenal29@gmail.com> napsal:
Hi, I created an table with composite type array as datatype for one column.
When I query that table I need the pg_typeof(column) as record[] instead of
composite_type[].
I tried creating a separate function and returning record[], but below error
is thrown,
ERROR: PL/pgSQL functions cannot return type record[]
Below is sample for my requirement.
pali=# create type address as (number bigint, city varchar);
CREATE TYPE
pali=# create table person(name varchar, addresses address[]);
CREATE TABLE
pali=# insert into person values ('Michael', array[(82, 'NYC'),(24,
'Dunkirk')]::address[]);
INSERT 0 1
pali=# select pg_typeof(addresses::record[]) from person ;
pg_typeof
-----------
address[]
Here I expect record[] instead of address[].
Thanks in advance.
record type is not real PostgreSQL's type. It is just PLpgSQL only type. You cannot use it outside PLpgSQL.
Regards
Pavel
Suresh Kumar R <suresh.arsenal29@gmail.com> writes: > When I query that table I need the pg_typeof(column) as record[] instead of > composite_type[]. Why? record[] is considered a pseudotype (polymorphic type), which is why casting to it doesn't do anything. regards, tom lane
On Thu, Mar 3, 2022 at 4:01 AM Suresh Kumar R <suresh.arsenal29@gmail.com> wrote: > > Hi, I created an table with composite type array as datatype for one column. > When I query that table I need the pg_typeof(column) as record[] instead of > composite_type[]. > I tried creating a separate function and returning record[], but below error > is thrown, > > ERROR: PL/pgSQL functions cannot return type record[] > > Below is sample for my requirement. > > pali=# create type address as (number bigint, city varchar); > CREATE TYPE > pali=# create table person(name varchar, addresses address[]); > CREATE TABLE > pali=# insert into person values ('Michael', array[(82, 'NYC'),(24, > 'Dunkirk')]::address[]); > INSERT 0 1 > pali=# select pg_typeof(addresses::record[]) from person ; > pg_typeof > ----------- > address[] > > Here I expect record[] instead of address[]. Why do you think you need this? If you need variant record storage, you probably want to go to jsonb, then use jsonb_populate_recordset to convert back to specific type. merlin
Hi Merlin,
With regards
I needed this because, I use postgres backend along with golang using pgx driver. When i query address i need to specify OID of address[](composite type). In my case i couldnt find the oid of that type since its dynamic. Needed generic type so needed to cast to record[] which has constant oid.
Suresh kumar R
On Fri, 4 Mar, 2022, 11:28 pm Merlin Moncure, <mmoncure@gmail.com> wrote:
On Thu, Mar 3, 2022 at 4:01 AM Suresh Kumar R
<suresh.arsenal29@gmail.com> wrote:
>
> Hi, I created an table with composite type array as datatype for one column.
> When I query that table I need the pg_typeof(column) as record[] instead of
> composite_type[].
> I tried creating a separate function and returning record[], but below error
> is thrown,
>
> ERROR: PL/pgSQL functions cannot return type record[]
>
> Below is sample for my requirement.
>
> pali=# create type address as (number bigint, city varchar);
> CREATE TYPE
> pali=# create table person(name varchar, addresses address[]);
> CREATE TABLE
> pali=# insert into person values ('Michael', array[(82, 'NYC'),(24,
> 'Dunkirk')]::address[]);
> INSERT 0 1
> pali=# select pg_typeof(addresses::record[]) from person ;
> pg_typeof
> -----------
> address[]
>
> Here I expect record[] instead of address[].
Why do you think you need this? If you need variant record storage,
you probably want to go to jsonb, then use jsonb_populate_recordset to
convert back to specific type.
merlin
Suresh Kumar R <suresh.arsenal29@gmail.com> writes: > I needed this because, I use postgres backend along with golang using > pgx driver. When i query address i need to specify OID of > address[](composite type). In my case i couldnt find the oid of that type > since its dynamic. The usual advice is to look it up on the fly. As an example: regression=# create type address as (f1 text, f2 text); CREATE TYPE regression=# select 'address[]'::regtype; regtype ----------- address[] (1 row) regression=# select 'address[]'::regtype::oid; oid -------- 127056 (1 row) If the client-side stack wants a type OID for a result column, it's unlikely to be satisfied with a pseudotype OID anyway. That provides next-to-no useful information about how to interpret values. Are you sure you can't just leave the type unspecified (e.g. zero) in whatever API is giving you trouble? regards, tom lane