What's the benefit (or usage scenario) of a "typed table"?

Started by Thomas Kellererover 9 years ago6 messagesgeneral
Jump to latest
#1Thomas Kellerer
spam_eater@gmx.net

I recently stumbled over "typed tables" in Postgres
(there were several questions containing this on stackoverflow recently)

create type some_type as (id integer, data text);
create table some_table of some_type;

I wonder what the benefit of a typed table is and when this would be useful?

Regards
Thomas

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

#2David G. Johnston
david.g.johnston@gmail.com
In reply to: Thomas Kellerer (#1)
Re: What's the benefit (or usage scenario) of a "typed table"?

On Saturday, December 31, 2016, Thomas Kellerer <spam_eater@gmx.net> wrote:

I recently stumbled over "typed tables" in Postgres
(there were several questions containing this on stackoverflow recently)

create type some_type as (id integer, data text);
create table some_table of some_type;

I wonder what the benefit of a typed table is and when this would be
useful?

Given that I'd frown upon having two types with identical structure I'd say
this is could be harmfull if swallowed. Thoguh it might find use in
refactoring and maintaining backward compatibility. It does setup an
object dependency which is the only visible difference from just copy the
type definition into the CREATE TABLE. But I'd say if you want a table
with said structure you should plan on droppign the original type after
you've altered all references to it to point to the new implicit type
created with the table.

David J.

#3Thomas Kellerer
spam_eater@gmx.net
In reply to: David G. Johnston (#2)
Re: What's the benefit (or usage scenario) of a "typed table"?

David G. Johnston schrieb am 31.12.2016 um 16:51:

I wonder what the benefit of a typed table is and when this would be useful?

But I'd say if you want a table with said structure you should plan on
droppign the original type after you've altered all references to it to
point to the new implicit type created with the table.

I am not planning to use it.

I am just wondering _if_ there is an advantage to this setup

Apparently at some point someone thought it would be useful, otherwise
it wouldn't have been implemented I guess.

Thomas

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

#4Tom Lane
tgl@sss.pgh.pa.us
In reply to: Thomas Kellerer (#1)
Re: What's the benefit (or usage scenario) of a "typed table"?

Thomas Kellerer <spam_eater@gmx.net> writes:

I recently stumbled over "typed tables" in Postgres
(there were several questions containing this on stackoverflow recently)

create type some_type as (id integer, data text);
create table some_table of some_type;

I wonder what the benefit of a typed table is and when this would be useful?

AFAIK we implemented that only because it's in the SQL standard.
Otherwise you might as well use, for instance, CREATE TABLE ... LIKE.

regards, tom lane

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

#5Adrian Klaver
adrian.klaver@aklaver.com
In reply to: Thomas Kellerer (#3)
Re: What's the benefit (or usage scenario) of a "typed table"?

On 12/31/2016 08:25 AM, Thomas Kellerer wrote:

David G. Johnston schrieb am 31.12.2016 um 16:51:

I wonder what the benefit of a typed table is and when this would be
useful?

But I'd say if you want a table with said structure you should plan on
droppign the original type after you've altered all references to it to
point to the new implicit type created with the table.

I am not planning to use it.

I am just wondering _if_ there is an advantage to this setup

Apparently at some point someone thought it would be useful, otherwise
it wouldn't have been implemented I guess.

I see it as completing the loop. A table creates a composite type, a
composite type creates a table. Not sure where that falls on the useful
scale.

Thomas

--
Adrian Klaver
adrian.klaver@aklaver.com

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

#6Peter Eisentraut
peter_e@gmx.net
In reply to: Thomas Kellerer (#1)
Re: What's the benefit (or usage scenario) of a "typed table"?

On 12/31/16 10:34 AM, Thomas Kellerer wrote:

I recently stumbled over "typed tables" in Postgres
(there were several questions containing this on stackoverflow recently)

create type some_type as (id integer, data text);
create table some_table of some_type;

I wonder what the benefit of a typed table is and when this would be useful?

One use is with PL/Proxy. You create the type on the proxy, thus
allowing you to define functions using the type. Then create the table
on the backend from the type, thus ensuring they are the same.

--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general