pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Couldn't cast to record[]
6+ messages / 4 participants
[nested] [flat]

* Couldn't cast to record[]
@ 2022-03-03 10:02 Suresh Kumar R <suresh.arsenal29@gmail.com>
  2022-03-03 10:16 ` Re: Couldn't cast to record[] Pavel Stehule <pavel.stehule@gmail.com>
  2022-03-03 15:48 ` Re: Couldn't cast to record[] Tom Lane <tgl@sss.pgh.pa.us>
  2022-03-04 17:58 ` Re: Couldn't cast to record[] Merlin Moncure <mmoncure@gmail.com>
  0 siblings, 3 replies; 6+ messages in thread

From: Suresh Kumar R @ 2022-03-03 10:02 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

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.

^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Couldn't cast to record[]
  2022-03-03 10:02 Couldn't cast to record[] Suresh Kumar R <suresh.arsenal29@gmail.com>
@ 2022-03-03 10:16 ` Pavel Stehule <pavel.stehule@gmail.com>
  2 siblings, 0 replies; 6+ messages in thread

From: Pavel Stehule @ 2022-03-03 10:16 UTC (permalink / raw)
  To: Suresh Kumar R <suresh.arsenal29@gmail.com>; +Cc: pgsql-general <pgsql-general@lists.postgresql.org>

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

^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Couldn't cast to record[]
  2022-03-03 10:02 Couldn't cast to record[] Suresh Kumar R <suresh.arsenal29@gmail.com>
@ 2022-03-03 15:48 ` Tom Lane <tgl@sss.pgh.pa.us>
  2 siblings, 0 replies; 6+ messages in thread

From: Tom Lane @ 2022-03-03 15:48 UTC (permalink / raw)
  To: Suresh Kumar R <suresh.arsenal29@gmail.com>; +Cc: pgsql-general@lists.postgresql.org

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





^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Couldn't cast to record[]
  2022-03-03 10:02 Couldn't cast to record[] Suresh Kumar R <suresh.arsenal29@gmail.com>
@ 2022-03-04 17:58 ` Merlin Moncure <mmoncure@gmail.com>
  2022-03-05 07:33   ` Re: Couldn't cast to record[] Suresh Kumar R <suresh.arsenal29@gmail.com>
  2 siblings, 1 reply; 6+ messages in thread

From: Merlin Moncure @ 2022-03-04 17:58 UTC (permalink / raw)
  To: Suresh Kumar R <suresh.arsenal29@gmail.com>; +Cc: pgsql-general <pgsql-general@lists.postgresql.org>

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





^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Couldn't cast to record[]
  2022-03-03 10:02 Couldn't cast to record[] Suresh Kumar R <suresh.arsenal29@gmail.com>
  2022-03-04 17:58 ` Re: Couldn't cast to record[] Merlin Moncure <mmoncure@gmail.com>
@ 2022-03-05 07:33   ` Suresh Kumar R <suresh.arsenal29@gmail.com>
  2022-03-05 15:29     ` Re: Couldn't cast to record[] Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 6+ messages in thread

From: Suresh Kumar R @ 2022-03-05 07:33 UTC (permalink / raw)
  To: Merlin Moncure <mmoncure@gmail.com>; +Cc: pgsql-general <pgsql-general@lists.postgresql.org>

Hi Merlin,
   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.

With regards
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
>

^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Couldn't cast to record[]
  2022-03-03 10:02 Couldn't cast to record[] Suresh Kumar R <suresh.arsenal29@gmail.com>
  2022-03-04 17:58 ` Re: Couldn't cast to record[] Merlin Moncure <mmoncure@gmail.com>
  2022-03-05 07:33   ` Re: Couldn't cast to record[] Suresh Kumar R <suresh.arsenal29@gmail.com>
@ 2022-03-05 15:29     ` Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 6+ messages in thread

From: Tom Lane @ 2022-03-05 15:29 UTC (permalink / raw)
  To: Suresh Kumar R <suresh.arsenal29@gmail.com>; +Cc: Merlin Moncure <mmoncure@gmail.com>; pgsql-general <pgsql-general@lists.postgresql.org>

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





^ permalink  raw  reply  [nested|flat] 6+ messages in thread


end of thread, other threads:[~2022-03-05 15:29 UTC | newest]

Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2022-03-03 10:02 Couldn't cast to record[] Suresh Kumar R <suresh.arsenal29@gmail.com>
2022-03-03 10:16 ` Pavel Stehule <pavel.stehule@gmail.com>
2022-03-03 15:48 ` Tom Lane <tgl@sss.pgh.pa.us>
2022-03-04 17:58 ` Merlin Moncure <mmoncure@gmail.com>
2022-03-05 07:33   ` Suresh Kumar R <suresh.arsenal29@gmail.com>
2022-03-05 15:29     ` Tom Lane <tgl@sss.pgh.pa.us>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox