Skip to content

Commit 5d1d8b3

Browse files
committed
Clarify error message and documentation related to typed tables.
We restrict typed tables (those declared as "OF composite_type") to be based on stand-alone composite types, not composite types that are the implicitly-created rowtypes of other tables. But if you tried to do that, you got the very confusing error message "type foo is not a composite type". Provide a more specific message for that case. Also clarify related documentation in the CREATE TABLE man page. Erik Wienhold and David G. Johnston, per complaint from Hannu Krosing. Discussion: https://postgr.es/m/CAMT0RQRysCb_Amy5CTENSc5GfsvXL1a4qX3mv_hx31_v74P==g@mail.gmail.com
1 parent c883453 commit 5d1d8b3

File tree

4 files changed

+26
-10
lines changed

4 files changed

+26
-10
lines changed

doc/src/sgml/ref/create_table.sgml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -249,18 +249,18 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
249249
<listitem>
250250
<para>
251251
Creates a <firstterm>typed table</firstterm>, which takes its
252-
structure from the specified composite type (name optionally
253-
schema-qualified). A typed table is tied to its type; for
254-
example the table will be dropped if the type is dropped
255-
(with <literal>DROP TYPE ... CASCADE</literal>).
252+
structure from the specified stand-alone composite type (that is,
253+
one created using <xref linkend="sql-createtype"/>) though it still
254+
produces a new composite type as well. The table will have a
255+
dependency on the referenced type, meaning that cascaded alter and
256+
drop actions on that type will propagate to the table.
256257
</para>
257258

258259
<para>
259-
When a typed table is created, then the data types of the
260-
columns are determined by the underlying composite type and are
261-
not specified by the <literal>CREATE TABLE</literal> command.
260+
A typed table always has the same column names and data types as the
261+
type it is derived from, so you cannot specify additional columns.
262262
But the <literal>CREATE TABLE</literal> command can add defaults
263-
and constraints to the table and can specify storage parameters.
263+
and constraints to the table, as well as specify storage parameters.
264264
</para>
265265
</listitem>
266266
</varlistentry>

src/backend/commands/tablecmds.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6962,8 +6962,15 @@ check_of_type(HeapTuple typetuple)
69626962
* the type before the typed table creation/conversion commits.
69636963
*/
69646964
relation_close(typeRelation, NoLock);
6965+
6966+
if (!typeOk)
6967+
ereport(ERROR,
6968+
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
6969+
errmsg("type %s is the row type of another table",
6970+
format_type_be(typ->oid)),
6971+
errdetail("A typed table must use a stand-alone composite type created with CREATE TYPE.")));
69656972
}
6966-
if (!typeOk)
6973+
else
69676974
ereport(ERROR,
69686975
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
69696976
errmsg("type %s is not a composite type",

src/test/regress/expected/typed_table.out

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,12 @@ drop cascades to function get_all_persons()
8989
drop cascades to table persons2
9090
drop cascades to table persons3
9191
CREATE TABLE persons5 OF stuff; -- only CREATE TYPE AS types may be used
92-
ERROR: type stuff is not a composite type
92+
ERROR: type stuff is the row type of another table
93+
DETAIL: A typed table must use a stand-alone composite type created with CREATE TYPE.
94+
CREATE TYPE tt_enum_type AS ENUM ('a');
95+
CREATE TABLE of_tt_enum_type OF tt_enum_type; -- not a composite type at all
96+
ERROR: type tt_enum_type is not a composite type
97+
DROP TYPE tt_enum_type;
9398
DROP TABLE stuff;
9499
-- implicit casting
95100
CREATE TYPE person_type AS (id int, name text);

src/test/regress/sql/typed_table.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ DROP TYPE person_type CASCADE;
4848

4949
CREATE TABLE persons5 OF stuff; -- only CREATE TYPE AS types may be used
5050

51+
CREATE TYPE tt_enum_type AS ENUM ('a');
52+
CREATE TABLE of_tt_enum_type OF tt_enum_type; -- not a composite type at all
53+
DROP TYPE tt_enum_type;
54+
5155
DROP TABLE stuff;
5256

5357

0 commit comments

Comments
 (0)