Re: create type with %type or %rowtype

From: Post Gresql <postgresql(at)taljaren(dot)se>
To: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
Cc: PostgreSQL General <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: create type with %type or %rowtype
Date: 2020-11-18 07:34:05
Message-ID: 82b59d98-bc35-7212-b950-45ff585f3b12@taljaren.se
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


On 2020-11-18 04:37, David G. Johnston wrote:
> (resending to include the list)
>
> On Tue, Nov 17, 2020 at 3:12 PM Post Gresql <postgresql(at)taljaren(dot)se
> <mailto:postgresql(at)taljaren(dot)se>> wrote:
>
> create type my_type as my_table%rowtype;
>
>
> This would be redundant with existing behavior - all tables have a
> corresponding type already
>
> create type my_type as my_table.my_column%type;
>
>
> What does the indirection get us?
>
> Correct? It seems to be a feature for plpgsql programing only, right?
>
>
> Correct
>
>
> But wouldn't that be a good thing to be able to do?
>
>
> You are the one proposing it - why would it be a good thing to do?

My idea, that I did not explain properly, sorry for that, is that when I
write plpgsql functions I sometime need to have a certain column type as
return value, or even a complete table row as return type.

Then it would be great if I could just refer to the column or row type
when delcaring the return type.

It would also be handy if I could reference types when declaring other
types,

for example

create type my_type (a int, b my_table.my_column%type);

The real reason: you will be sure you are using the same type
everywhere.  And it is easier to change type later on, then only one
column has to be changed, not many and in a lot of different places.

I hope that explains my idea.

> David J.
>
>
> On Tue, Nov 17, 2020 at 3:12 PM Post Gresql <postgresql(at)taljaren(dot)se
> <mailto:postgresql(at)taljaren(dot)se>> wrote:
>
> Hello.
>
> It seems that I can not create a type with
>
> create type my_type as my_table%rowtype;
>
> or
>
> create type my_type as my_table.my_column%type;
>
> Correct? It seems to be a feature for plpgsql programing only, right?
>
> But wouldn't that be a good thing to be able to do? Or would it cause
> too many problems?
>
>
> Best regards
>
>
>
>

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Hemil Ruparel 2020-11-18 07:56:56 Postgresql13-devel fails to install on centos 7
Previous Message Marcin Giedz 2020-11-18 07:10:52 Re: pg_upgrade from 12 to 13 failes with plpython2