<!--
-$Header: /cvsroot/pgsql/doc/src/sgml/ref/create_type.sgml,v 1.30 2002/07/24 19:11:07 petere Exp $
+$Header: /cvsroot/pgsql/doc/src/sgml/ref/create_type.sgml,v 1.31 2002/08/15 16:36:00 momjian Exp $
PostgreSQL documentation
-->
[ , ALIGNMENT = <replaceable class="parameter">alignment</replaceable> ]
[ , STORAGE = <replaceable class="parameter">storage</replaceable> ]
)
+
+CREATE TYPE <replaceable class="parameter">typename</replaceable> AS
+ ( <replaceable class="PARAMETER">column_definition_list</replaceable> )
+
+where <replaceable class="PARAMETER">column_definition_list</replaceable> can be:
+
+( <replaceable class="PARAMETER">column_name</replaceable> <replaceable class="PARAMETER">data_type</replaceable> [, ... ] )
</synopsis>
<refsect2 id="R2-SQL-CREATETYPE-1">
</para>
</listitem>
</varlistentry>
+
+ <varlistentry>
+ <term><replaceable class="PARAMETER">column_name</replaceable></term>
+ <listitem>
+ <para>
+ The name of a column of the composite type.
+ </para>
+ </listitem>
+ </varlistentry>
+
+ <varlistentry>
+ <term><replaceable class="PARAMETER">data_type</replaceable></term>
+ <listitem>
+ <para>
+ The name of an existing data type.
+ </para>
+ </listitem>
+ </varlistentry>
+
</variablelist>
</para>
</refsect2>
</para>
<para>
- <command>CREATE TYPE</command> requires the registration of two functions
- (using CREATE FUNCTION) before defining the type. The
- representation of a new base type is determined by
+ The first form of <command>CREATE TYPE</command> requires the
+ registration of two functions (using CREATE FUNCTION) before defining the
+ type. The representation of a new base type is determined by
<replaceable class="parameter">input_function</replaceable>, which
converts the type's external representation to an internal
representation usable by the
<literal>extended</literal> and <literal>external</literal> items.)
</para>
+ <para>
+ The second form of <command>CREATE TYPE</command> requires a column
+ definition list in the form ( <replaceable class="PARAMETER">column_name</replaceable>
+ <replaceable class="PARAMETER">data_type</replaceable> [, ... ] ). This
+ creates a composite type, similar to that of a TABLE or VIEW relation.
+ A stand-alone composite type is useful as the return type of FUNCTION.
+ </para>
+
<refsect2>
<title>Array Types</title>
CREATE TABLE big_objs (id int4, obj bigobj);
</programlisting>
</para>
+
+ <para>
+ This example creates a composite type and uses it in
+ a table function definition:
+<programlisting>
+CREATE TYPE compfoo AS (f1 int, f2 int);
+CREATE FUNCTION getfoo() RETURNS SETOF compfoo AS 'SELECT fooid, foorefid FROM foo' LANGUAGE SQL;
+</programlisting>
+ </para>
</refsect1>
<refsect1 id="SQL-CREATETYPE-compatibility">
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/catalog/heap.c,v 1.220 2002/08/11 21:17:34 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/catalog/heap.c,v 1.221 2002/08/15 16:36:00 momjian Exp $
*
*
* INTERFACE ROUTINES
/*
* first check for collision with system attribute names
*
- * Skip this for a view, since it doesn't have system attributes.
+ * Skip this for a view and type relation, since it doesn't have system
+ * attributes.
*/
- if (relkind != RELKIND_VIEW)
+ if (relkind != RELKIND_VIEW && relkind != RELKIND_COMPOSITE_TYPE)
{
for (i = 0; i < natts; i++)
{
/*
* Next we add the system attributes. Skip OID if rel has no OIDs.
- * Skip all for a view. We don't bother with making datatype
- * dependencies here, since presumably all these types are pinned.
+ * Skip all for a view or type relation. We don't bother with making
+ * datatype dependencies here, since presumably all these types are pinned.
*/
- if (relkind != RELKIND_VIEW)
+ if (relkind != RELKIND_VIEW && relkind != RELKIND_COMPOSITE_TYPE)
{
dpp = SysAtt;
for (i = 0; i < -1 - FirstLowInvalidHeapAttributeNumber; i++)
* physical disk file. (If we fail further down, it's the smgr's
* responsibility to remove the disk file again.)
*
- * NB: create a physical file only if it's not a view.
+ * NB: create a physical file only if it's not a view or type relation.
*/
new_rel_desc = heap_create(relname,
relnamespace,
tupdesc,
shared_relation,
- (relkind != RELKIND_VIEW),
+ (relkind != RELKIND_VIEW &&
+ relkind != RELKIND_COMPOSITE_TYPE),
allow_system_table_mods);
/* Fetch the relation OID assigned by heap_create */
/*
* unlink the relation's physical file and finish up.
*/
- if (rel->rd_rel->relkind != RELKIND_VIEW)
+ if (rel->rd_rel->relkind != RELKIND_VIEW &&
+ rel->rd_rel->relkind != RELKIND_COMPOSITE_TYPE)
smgrunlink(DEFAULT_SMGR, rel);
/*
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/catalog/namespace.c,v 1.30 2002/08/09 16:45:14 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/catalog/namespace.c,v 1.31 2002/08/15 16:36:01 momjian Exp $
*
*-------------------------------------------------------------------------
*/
case RELKIND_RELATION:
case RELKIND_SEQUENCE:
case RELKIND_VIEW:
+ case RELKIND_COMPOSITE_TYPE:
AssertTupleDescHasOid(pgclass->rd_att);
object.classId = RelOid_pg_class;
object.objectId = HeapTupleGetOid(tuple);
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/catalog/pg_type.c,v 1.77 2002/08/05 03:29:16 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/catalog/pg_type.c,v 1.78 2002/08/15 16:36:01 momjian Exp $
*
*-------------------------------------------------------------------------
*/
/*
* If the type is a rowtype for a relation, mark it as internally
- * dependent on the relation. This allows it to be auto-dropped
- * when the relation is, and not otherwise.
+ * dependent on the relation, *unless* it is a stand-alone composite
+ * type relation. For the latter case, we have to reverse the
+ * dependency.
+ *
+ * In the former case, this allows the type to be auto-dropped
+ * when the relation is, and not otherwise. And in the latter,
+ * of course we get the opposite effect.
*/
if (OidIsValid(relationOid))
{
+ Relation rel = relation_open(relationOid, AccessShareLock);
+ char relkind = rel->rd_rel->relkind;
+ relation_close(rel, AccessShareLock);
+
referenced.classId = RelOid_pg_class;
referenced.objectId = relationOid;
referenced.objectSubId = 0;
- recordDependencyOn(&myself, &referenced, DEPENDENCY_INTERNAL);
+
+ if (relkind != RELKIND_COMPOSITE_TYPE)
+ recordDependencyOn(&myself, &referenced, DEPENDENCY_INTERNAL);
+ else
+ recordDependencyOn(&referenced, &myself, DEPENDENCY_INTERNAL);
}
/*
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/commands/copy.c,v 1.162 2002/08/02 18:15:06 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/commands/copy.c,v 1.163 2002/08/15 16:36:02 momjian Exp $
*
*-------------------------------------------------------------------------
*/
if (rel->rd_rel->relkind == RELKIND_VIEW)
elog(ERROR, "You cannot copy view %s",
RelationGetRelationName(rel));
+ else if (rel->rd_rel->relkind == RELKIND_COMPOSITE_TYPE)
+ elog(ERROR, "You cannot copy type relation %s",
+ RelationGetRelationName(rel));
else if (rel->rd_rel->relkind == RELKIND_SEQUENCE)
elog(ERROR, "You cannot change sequence relation %s",
RelationGetRelationName(rel));
if (rel->rd_rel->relkind == RELKIND_VIEW)
elog(ERROR, "You cannot copy view %s",
RelationGetRelationName(rel));
+ else if (rel->rd_rel->relkind == RELKIND_COMPOSITE_TYPE)
+ elog(ERROR, "You cannot copy type relation %s",
+ RelationGetRelationName(rel));
else if (rel->rd_rel->relkind == RELKIND_SEQUENCE)
elog(ERROR, "You cannot copy sequence %s",
RelationGetRelationName(rel));
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/commands/tablecmds.c,v 1.28 2002/08/07 21:45:01 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/commands/tablecmds.c,v 1.29 2002/08/15 16:36:02 momjian Exp $
*
*-------------------------------------------------------------------------
*/
elog(ERROR, "TRUNCATE cannot be used on views. '%s' is a view",
RelationGetRelationName(rel));
+ if (rel->rd_rel->relkind == RELKIND_COMPOSITE_TYPE)
+ elog(ERROR, "TRUNCATE cannot be used on type relations. '%s' is a type",
+ RelationGetRelationName(rel));
+
if (!allowSystemTableMods && IsSystemRelation(rel))
elog(ERROR, "TRUNCATE cannot be used on system tables. '%s' is a system table",
RelationGetRelationName(rel));
case RELKIND_RELATION:
case RELKIND_INDEX:
case RELKIND_VIEW:
+ case RELKIND_COMPOSITE_TYPE:
case RELKIND_SEQUENCE:
case RELKIND_TOASTVALUE:
/* ok to change owner */
break;
default:
- elog(ERROR, "ALTER TABLE: relation \"%s\" is not a table, TOAST table, index, view, or sequence",
+ elog(ERROR, "ALTER TABLE: relation \"%s\" is not a table, TOAST table, index, view, type, or sequence",
NameStr(tuple_class->relname));
}
}
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/commands/typecmds.c,v 1.8 2002/07/24 19:11:09 petere Exp $
+ * $Header: /cvsroot/pgsql/src/backend/commands/typecmds.c,v 1.9 2002/08/15 16:36:02 momjian Exp $
*
* DESCRIPTION
* The "DefineFoo" routines take the parse tree and pick out the
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "commands/tablecmds.h"
#include "miscadmin.h"
#include "parser/parse_func.h"
#include "parser/parse_type.h"
static Oid findTypeIOFunction(List *procname, bool isOutput);
-
/*
* DefineType
* Registers a new type.
return procOid;
}
+
+/*-------------------------------------------------------------------
+ * DefineCompositeType
+ *
+ * Create a Composite Type relation.
+ * `DefineRelation' does all the work, we just provide the correct
+ * arguments!
+ *
+ * If the relation already exists, then 'DefineRelation' will abort
+ * the xact...
+ *
+ * DefineCompositeType returns relid for use when creating
+ * an implicit composite type during function creation
+ *-------------------------------------------------------------------
+ */
+Oid
+DefineCompositeType(const RangeVar *typevar, List *coldeflist)
+{
+ CreateStmt *createStmt = makeNode(CreateStmt);
+
+ if (coldeflist == NIL)
+ elog(ERROR, "attempted to define composite type relation with"
+ " no attrs");
+
+ /*
+ * now create the parameters for keys/inheritance etc. All of them are
+ * nil...
+ */
+ createStmt->relation = (RangeVar *) typevar;
+ createStmt->tableElts = coldeflist;
+ createStmt->inhRelations = NIL;
+ createStmt->constraints = NIL;
+ createStmt->hasoids = false;
+
+ /*
+ * finally create the relation...
+ */
+ return DefineRelation(createStmt, RELKIND_COMPOSITE_TYPE);
+}
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/executor/execMain.c,v 1.173 2002/08/07 21:45:02 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/executor/execMain.c,v 1.174 2002/08/15 16:36:02 momjian Exp $
*
*-------------------------------------------------------------------------
*/
elog(ERROR, "You can't change view relation %s",
RelationGetRelationName(resultRelationDesc));
break;
+ case RELKIND_COMPOSITE_TYPE:
+ elog(ERROR, "You can't change type relation %s",
+ RelationGetRelationName(resultRelationDesc));
+ break;
}
MemSet(resultRelInfo, 0, sizeof(ResultRelInfo));
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/nodes/copyfuncs.c,v 1.200 2002/08/04 19:48:09 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/backend/nodes/copyfuncs.c,v 1.201 2002/08/15 16:36:02 momjian Exp $
*
*-------------------------------------------------------------------------
*/
return newnode;
}
+static CompositeTypeStmt *
+_copyCompositeTypeStmt(CompositeTypeStmt *from)
+{
+ CompositeTypeStmt *newnode = makeNode(CompositeTypeStmt);
+
+ Node_Copy(from, newnode, typevar);
+ Node_Copy(from, newnode, coldeflist);
+
+ return newnode;
+}
+
static ViewStmt *
_copyViewStmt(ViewStmt *from)
{
case T_TransactionStmt:
retval = _copyTransactionStmt(from);
break;
+ case T_CompositeTypeStmt:
+ retval = _copyCompositeTypeStmt(from);
+ break;
case T_ViewStmt:
retval = _copyViewStmt(from);
break;
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/nodes/equalfuncs.c,v 1.149 2002/08/04 23:49:59 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/nodes/equalfuncs.c,v 1.150 2002/08/15 16:36:03 momjian Exp $
*
*-------------------------------------------------------------------------
*/
}
static bool
+_equalCompositeTypeStmt(CompositeTypeStmt *a, CompositeTypeStmt *b)
+{
+ if (!equal(a->typevar, b->typevar))
+ return false;
+ if (!equal(a->coldeflist, b->coldeflist))
+ return false;
+
+ return true;
+}
+
+static bool
_equalViewStmt(ViewStmt *a, ViewStmt *b)
{
if (!equal(a->view, b->view))
case T_TransactionStmt:
retval = _equalTransactionStmt(a, b);
break;
+ case T_CompositeTypeStmt:
+ retval = _equalCompositeTypeStmt(a, b);
+ break;
case T_ViewStmt:
retval = _equalViewStmt(a, b);
break;
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/parser/gram.y,v 2.358 2002/08/10 19:01:53 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/parser/gram.y,v 2.359 2002/08/15 16:36:03 momjian Exp $
*
* HISTORY
* AUTHOR DATE MAJOR EVENT
%type <list> stmtblock, stmtmulti,
OptTableElementList, TableElementList, OptInherit, definition,
- opt_distinct, opt_definition, func_args,
+ opt_distinct, opt_definition, func_args, rowdefinition
func_args_list, func_as, createfunc_opt_list
oper_argtypes, RuleActionList, RuleActionMulti,
opt_column_list, columnList, opt_name_list,
n->definition = $4;
$$ = (Node *)n;
}
+ | CREATE TYPE_P any_name AS rowdefinition
+ {
+ CompositeTypeStmt *n = makeNode(CompositeTypeStmt);
+ RangeVar *r = makeNode(RangeVar);
+
+ switch (length($3))
+ {
+ case 1:
+ r->catalogname = NULL;
+ r->schemaname = NULL;
+ r->relname = strVal(lfirst($3));
+ break;
+ case 2:
+ r->catalogname = NULL;
+ r->schemaname = strVal(lfirst($3));
+ r->relname = strVal(lsecond($3));
+ break;
+ case 3:
+ r->catalogname = strVal(lfirst($3));
+ r->schemaname = strVal(lsecond($3));
+ r->relname = strVal(lfirst(lnext(lnext($3))));
+ break;
+ default:
+ elog(ERROR,
+ "Improper qualified name "
+ "(too many dotted names): %s",
+ NameListToString($3));
+ break;
+ }
+ n->typevar = r;
+ n->coldeflist = $5;
+ $$ = (Node *)n;
+ }
| CREATE CHARACTER SET opt_as any_name GET definition opt_collate
{
DefineStmt *n = makeNode(DefineStmt);
}
;
+rowdefinition: '(' TableFuncElementList ')' { $$ = $2; }
+ ;
+
definition: '(' def_list ')' { $$ = $2; }
;
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/storage/buffer/bufmgr.c,v 1.129 2002/08/11 21:17:34 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/storage/buffer/bufmgr.c,v 1.130 2002/08/15 16:36:04 momjian Exp $
*
*-------------------------------------------------------------------------
*/
*/
if (relation->rd_rel->relkind == RELKIND_VIEW)
relation->rd_nblocks = 0;
+ else if (relation->rd_rel->relkind == RELKIND_COMPOSITE_TYPE)
+ relation->rd_nblocks = 0;
else if (!relation->rd_isnew && !relation->rd_istemp)
relation->rd_nblocks = smgrnblocks(DEFAULT_SMGR, relation);
return relation->rd_nblocks;
{
if (relation->rd_rel->relkind == RELKIND_VIEW)
relation->rd_nblocks = 0;
+ else if (relation->rd_rel->relkind == RELKIND_COMPOSITE_TYPE)
+ relation->rd_nblocks = 0;
else
relation->rd_nblocks = smgrnblocks(DEFAULT_SMGR, relation);
}
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/storage/smgr/smgr.c,v 1.58 2002/08/06 02:36:34 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/storage/smgr/smgr.c,v 1.59 2002/08/15 16:36:04 momjian Exp $
*
*-------------------------------------------------------------------------
*/
if (reln->rd_rel->relkind == RELKIND_VIEW)
return -1;
+ if (reln->rd_rel->relkind == RELKIND_COMPOSITE_TYPE)
+ return -1;
if ((fd = (*(smgrsw[which].smgr_open)) (reln)) < 0)
if (!failOK)
elog(ERROR, "cannot open %s: %m", RelationGetRelationName(reln));
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/tcop/postgres.c,v 1.281 2002/08/10 20:29:18 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/backend/tcop/postgres.c,v 1.282 2002/08/15 16:36:05 momjian Exp $
*
* NOTES
* this is the "main" module of the postgres backend and
if (!IsUnderPostmaster)
{
puts("\nPOSTGRES backend interactive interface ");
- puts("$Revision: 1.281 $ $Date: 2002/08/10 20:29:18 $\n");
+ puts("$Revision: 1.282 $ $Date: 2002/08/15 16:36:05 $\n");
}
/*
}
break;
+ case T_CompositeTypeStmt:
+ tag = "CREATE TYPE";
+ break;
+
case T_ViewStmt:
tag = "CREATE VIEW";
break;
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/tcop/utility.c,v 1.169 2002/08/07 21:45:02 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/tcop/utility.c,v 1.170 2002/08/15 16:36:05 momjian Exp $
*
*-------------------------------------------------------------------------
*/
{RELKIND_SEQUENCE, "a", "sequence", "SEQUENCE"},
{RELKIND_VIEW, "a", "view", "VIEW"},
{RELKIND_INDEX, "an", "index", "INDEX"},
+ {RELKIND_COMPOSITE_TYPE, "a", "type", "TYPE"},
{'\0', "a", "???", "???"}
};
}
break;
+ case T_CompositeTypeStmt: /* CREATE TYPE (composite) */
+ {
+ Oid relid;
+ CompositeTypeStmt *stmt = (CompositeTypeStmt *) parsetree;
+
+ /*
+ * DefineCompositeType returns relid for use when creating
+ * an implicit composite type during function creation
+ */
+ relid = DefineCompositeType(stmt->typevar, stmt->coldeflist);
+ }
+ break;
+
case T_ViewStmt: /* CREATE VIEW */
{
ViewStmt *stmt = (ViewStmt *) parsetree;
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/utils/adt/tid.c,v 1.32 2002/07/16 17:55:25 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/backend/utils/adt/tid.c,v 1.33 2002/08/15 16:36:05 momjian Exp $
*
* NOTES
* input routine largely stolen from boxin().
if (rel->rd_rel->relkind == RELKIND_VIEW)
return currtid_for_view(rel, tid);
+ if (rel->rd_rel->relkind == RELKIND_COMPOSITE_TYPE)
+ elog(ERROR, "currtid can't handle type relations");
+
ItemPointerCopy(tid, result);
heap_get_latest_tid(rel, SnapshotNow, result);
if (rel->rd_rel->relkind == RELKIND_VIEW)
return currtid_for_view(rel, tid);
+ if (rel->rd_rel->relkind == RELKIND_COMPOSITE_TYPE)
+ elog(ERROR, "currtid can't handle type relations");
+
result = (ItemPointer) palloc(sizeof(ItemPointerData));
ItemPointerCopy(tid, result);
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/bin/pg_dump/common.c,v 1.67 2002/07/30 21:56:04 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/bin/pg_dump/common.c,v 1.68 2002/08/15 16:36:06 momjian Exp $
*
*-------------------------------------------------------------------------
*/
for (i = 0; i < numTables; i++)
{
- /* Sequences and views never have parents */
+ /* Sequences, views, and types never have parents */
if (tblinfo[i].relkind == RELKIND_SEQUENCE ||
- tblinfo[i].relkind == RELKIND_VIEW)
+ tblinfo[i].relkind == RELKIND_VIEW ||
+ tblinfo[i].relkind == RELKIND_COMPOSITE_TYPE)
continue;
/* Don't bother computing anything for non-target tables, either */
for (i = 0; i < numTables; i++)
{
- /* Sequences and views never have parents */
+ /* Sequences, views, and types never have parents */
if (tblinfo[i].relkind == RELKIND_SEQUENCE ||
- tblinfo[i].relkind == RELKIND_VIEW)
+ tblinfo[i].relkind == RELKIND_VIEW ||
+ tblinfo[i].relkind == RELKIND_COMPOSITE_TYPE)
continue;
/* Don't bother computing anything for non-target tables, either */
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.281 2002/08/10 16:57:31 petere Exp $
+ * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.282 2002/08/15 16:36:06 momjian Exp $
*
*-------------------------------------------------------------------------
*/
FuncInfo *g_finfo, int numFuncs,
TypeInfo *g_tinfo, int numTypes);
static void dumpOneDomain(Archive *fout, TypeInfo *tinfo);
+static void dumpOneCompositeType(Archive *fout, TypeInfo *tinfo);
static void dumpOneTable(Archive *fout, TableInfo *tbinfo,
TableInfo *g_tblinfo);
static void dumpOneSequence(Archive *fout, TableInfo *tbinfo,
if (tblinfo[i].relkind == RELKIND_VIEW)
continue;
+ /* Skip TYPE relations */
+ if (tblinfo[i].relkind == RELKIND_COMPOSITE_TYPE)
+ continue;
+
if (tblinfo[i].relkind == RELKIND_SEQUENCE) /* already dumped */
continue;
int i_usename;
int i_typelem;
int i_typrelid;
+ int i_typrelkind;
int i_typtype;
int i_typisdefined;
appendPQExpBuffer(query, "SELECT pg_type.oid, typname, "
"typnamespace, "
"(select usename from pg_user where typowner = usesysid) as usename, "
- "typelem, typrelid, typtype, typisdefined "
+ "typelem, typrelid, "
+ "(select relkind from pg_class where oid = typrelid) as typrelkind, "
+ "typtype, typisdefined "
"FROM pg_type");
}
else
appendPQExpBuffer(query, "SELECT pg_type.oid, typname, "
"0::oid as typnamespace, "
"(select usename from pg_user where typowner = usesysid) as usename, "
- "typelem, typrelid, typtype, typisdefined "
+ "typelem, typrelid, "
+ "''::char as typrelkind, "
+ "typtype, typisdefined "
"FROM pg_type");
}
i_usename = PQfnumber(res, "usename");
i_typelem = PQfnumber(res, "typelem");
i_typrelid = PQfnumber(res, "typrelid");
+ i_typrelkind = PQfnumber(res, "typrelkind");
i_typtype = PQfnumber(res, "typtype");
i_typisdefined = PQfnumber(res, "typisdefined");
tinfo[i].usename = strdup(PQgetvalue(res, i, i_usename));
tinfo[i].typelem = strdup(PQgetvalue(res, i, i_typelem));
tinfo[i].typrelid = strdup(PQgetvalue(res, i, i_typrelid));
+ tinfo[i].typrelkind = *PQgetvalue(res, i, i_typrelkind);
tinfo[i].typtype = *PQgetvalue(res, i, i_typtype);
/*
appendPQExpBuffer(query,
"SELECT pg_class.oid, relname, relacl, relkind, "
"relnamespace, "
-
"(select usename from pg_user where relowner = usesysid) as usename, "
"relchecks, reltriggers, "
"relhasindex, relhasrules, relhasoids "
}
else if (g_fout->remoteVersion >= 70200)
{
+ /* before 7.3 there were no type relations with relkind 'c' */
appendPQExpBuffer(query,
"SELECT pg_class.oid, relname, relacl, relkind, "
"0::oid as relnamespace, "
if (tblinfo[i].relkind == RELKIND_SEQUENCE)
continue;
+ /* Don't bother to collect info for type relations */
+ if (tblinfo[i].relkind == RELKIND_COMPOSITE_TYPE)
+ continue;
+
/* Don't bother with uninteresting tables, either */
if (!tblinfo[i].interesting)
continue;
}
/*
+ * dumpOneCompositeType
+ * writes out to fout the queries to recreate a user-defined stand-alone
+ * composite type as requested by dumpTypes
+ */
+static void
+dumpOneCompositeType(Archive *fout, TypeInfo *tinfo)
+{
+ PQExpBuffer q = createPQExpBuffer();
+ PQExpBuffer delq = createPQExpBuffer();
+ PQExpBuffer query = createPQExpBuffer();
+ PGresult *res;
+ int ntups;
+ char *attname;
+ char *atttypdefn;
+ char *attbasetype;
+ const char *((*deps)[]);
+ int depIdx = 0;
+ int i;
+
+ deps = malloc(sizeof(char *) * 10);
+
+ /* Set proper schema search path so type references list correctly */
+ selectSourceSchema(tinfo->typnamespace->nspname);
+
+ /* Fetch type specific details */
+ /* We assume here that remoteVersion must be at least 70300 */
+
+ appendPQExpBuffer(query, "SELECT a.attname, "
+ "pg_catalog.format_type(a.atttypid, a.atttypmod) as atttypdefn, "
+ "a.atttypid as attbasetype "
+ "FROM pg_catalog.pg_type t, pg_catalog.pg_attribute a "
+ "WHERE t.oid = '%s'::pg_catalog.oid "
+ "AND a.attrelid = t.typrelid",
+ tinfo->oid);
+
+ res = PQexec(g_conn, query->data);
+ if (!res ||
+ PQresultStatus(res) != PGRES_TUPLES_OK)
+ {
+ write_msg(NULL, "query to obtain type information failed: %s", PQerrorMessage(g_conn));
+ exit_nicely();
+ }
+
+ /* Expecting at least a single result */
+ ntups = PQntuples(res);
+ if (ntups < 1)
+ {
+ write_msg(NULL, "Got no rows from: %s", query->data);
+ exit_nicely();
+ }
+
+ /* DROP must be fully qualified in case same name appears in pg_catalog */
+ appendPQExpBuffer(delq, "DROP TYPE %s.",
+ fmtId(tinfo->typnamespace->nspname, force_quotes));
+ appendPQExpBuffer(delq, "%s RESTRICT;\n",
+ fmtId(tinfo->typname, force_quotes));
+
+ appendPQExpBuffer(q,
+ "CREATE TYPE %s AS (",
+ fmtId(tinfo->typname, force_quotes));
+
+ for (i = 0; i < ntups; i++)
+ {
+ attname = PQgetvalue(res, i, PQfnumber(res, "attname"));
+ atttypdefn = PQgetvalue(res, i, PQfnumber(res, "atttypdefn"));
+ attbasetype = PQgetvalue(res, i, PQfnumber(res, "attbasetype"));
+
+ if (i > 0)
+ appendPQExpBuffer(q, ",\n\t %s %s", attname, atttypdefn);
+ else
+ appendPQExpBuffer(q, "%s %s", attname, atttypdefn);
+
+ /* Depends on the base type */
+ (*deps)[depIdx++] = strdup(attbasetype);
+ }
+ appendPQExpBuffer(q, ");\n");
+
+ (*deps)[depIdx++] = NULL; /* End of List */
+
+ ArchiveEntry(fout, tinfo->oid, tinfo->typname,
+ tinfo->typnamespace->nspname,
+ tinfo->usename, "TYPE", deps,
+ q->data, delq->data, NULL, NULL, NULL);
+
+ /*** Dump Type Comments ***/
+ resetPQExpBuffer(q);
+
+ appendPQExpBuffer(q, "TYPE %s", fmtId(tinfo->typname, force_quotes));
+ dumpComment(fout, q->data,
+ tinfo->typnamespace->nspname, tinfo->usename,
+ tinfo->oid, "pg_type", 0, NULL);
+
+ PQclear(res);
+ destroyPQExpBuffer(q);
+ destroyPQExpBuffer(delq);
+ destroyPQExpBuffer(query);
+}
+
+/*
* dumpTypes
* writes out to fout the queries to recreate all the user-defined types
*/
if (!tinfo[i].typnamespace->dump)
continue;
- /* skip relation types */
- if (atooid(tinfo[i].typrelid) != 0)
+ /* skip relation types for non-stand-alone type relations*/
+ if (atooid(tinfo[i].typrelid) != 0 && tinfo[i].typrelkind != 'c')
continue;
/* skip undefined placeholder types */
finfo, numFuncs, tinfo, numTypes);
else if (tinfo[i].typtype == 'd')
dumpOneDomain(fout, &tinfo[i]);
+ else if (tinfo[i].typtype == 'c')
+ dumpOneCompositeType(fout, &tinfo[i]);
}
}
if (tbinfo->relkind != RELKIND_SEQUENCE)
continue;
+
if (tbinfo->dump)
{
dumpOneSequence(fout, tbinfo, schemaOnly, dataOnly);
if (tbinfo->relkind == RELKIND_SEQUENCE) /* already dumped */
continue;
+ if (tbinfo->relkind == RELKIND_COMPOSITE_TYPE) /* dumped as a type */
+ continue;
if (tbinfo->dump)
{
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
- * $Id: pg_dump.h,v 1.94 2002/08/02 18:15:08 tgl Exp $
+ * $Id: pg_dump.h,v 1.95 2002/08/15 16:36:06 momjian Exp $
*
*-------------------------------------------------------------------------
*/
char *usename; /* name of owner, or empty string */
char *typelem; /* OID */
char *typrelid; /* OID */
+ char typrelkind; /* 'r', 'v', 'c', etc */
char typtype; /* 'b', 'c', etc */
bool isArray; /* true if user-defined array type */
bool isDefined; /* true if typisdefined */
*
* Copyright 2000-2002 by PostgreSQL Global Development Group
*
- * $Header: /cvsroot/pgsql/src/bin/psql/describe.c,v 1.60 2002/08/10 16:01:16 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/bin/psql/describe.c,v 1.61 2002/08/15 16:36:06 momjian Exp $
*/
#include "postgres_fe.h"
#include "describe.h"
/*
* do not include array types (start with underscore), do not include
- * user relations (typrelid!=0)
+ * user relations (typrelid!=0) unless they are type relations
*/
- appendPQExpBuffer(&buf, "WHERE t.typrelid = 0 AND t.typname !~ '^_'\n");
+ appendPQExpBuffer(&buf, "WHERE (t.typrelid = 0 ");
+ appendPQExpBuffer(&buf, "OR (SELECT c.relkind = 'c' FROM pg_class c "
+ "where c.oid = t.typrelid)) ");
+ appendPQExpBuffer(&buf, "AND t.typname !~ '^_'\n");
/* Match name pattern against either internal or external name */
processNamePattern(&buf, pattern, true, false,
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
- * $Id: pg_class.h,v 1.70 2002/08/02 18:15:09 tgl Exp $
+ * $Id: pg_class.h,v 1.71 2002/08/15 16:36:07 momjian Exp $
*
* NOTES
* the genbki.sh script reads this file and generates .bki
#define RELKIND_UNCATALOGED 'u' /* temporary heap */
#define RELKIND_TOASTVALUE 't' /* moved off huge values */
#define RELKIND_VIEW 'v' /* view */
+#define RELKIND_COMPOSITE_TYPE 'c' /* composite type */
#endif /* PG_CLASS_H */
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
- * $Id: defrem.h,v 1.43 2002/07/29 22:14:11 tgl Exp $
+ * $Id: defrem.h,v 1.44 2002/08/15 16:36:07 momjian Exp $
*
*-------------------------------------------------------------------------
*/
extern void RemoveTypeById(Oid typeOid);
extern void DefineDomain(CreateDomainStmt *stmt);
extern void RemoveDomain(List *names, DropBehavior behavior);
+extern Oid DefineCompositeType(const RangeVar *typevar, List *coldeflist);
extern void DefineOpClass(CreateOpClassStmt *stmt);
extern void RemoveOpClass(RemoveOpClassStmt *stmt);
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
- * $Id: nodes.h,v 1.114 2002/07/29 22:14:11 tgl Exp $
+ * $Id: nodes.h,v 1.115 2002/08/15 16:36:07 momjian Exp $
*
*-------------------------------------------------------------------------
*/
T_PrivTarget,
T_InsertDefault,
T_CreateOpClassItem,
+ T_CompositeTypeStmt,
/*
* TAGS FOR FUNCTION-CALL CONTEXT AND RESULTINFO NODES (see fmgr.h)
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
- * $Id: parsenodes.h,v 1.198 2002/08/04 19:48:10 momjian Exp $
+ * $Id: parsenodes.h,v 1.199 2002/08/15 16:36:07 momjian Exp $
*
*-------------------------------------------------------------------------
*/
} TransactionStmt;
/* ----------------------
+ * Create Type Statement, composite types
+ * ----------------------
+ */
+typedef struct CompositeTypeStmt
+{
+ NodeTag type;
+ RangeVar *typevar; /* the composite type to be created */
+ List *coldeflist; /* list of ColumnDef nodes */
+} CompositeTypeStmt;
+
+
+/* ----------------------
* Create View Statement
* ----------------------
*/
* procedural language
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/pl/plpgsql/src/pl_comp.c,v 1.45 2002/08/12 14:25:07 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/pl/plpgsql/src/pl_comp.c,v 1.46 2002/08/15 16:36:08 momjian Exp $
*
* This software is copyrighted by Jan Wieck - Hamburg.
*
}
/*
- * It must be a relation, sequence or view
+ * It must be a relation, sequence, view, or type
*/
classStruct = (Form_pg_class) GETSTRUCT(classtup);
if (classStruct->relkind != RELKIND_RELATION &&
classStruct->relkind != RELKIND_SEQUENCE &&
- classStruct->relkind != RELKIND_VIEW)
+ classStruct->relkind != RELKIND_VIEW &&
+ classStruct->relkind != RELKIND_COMPOSITE_TYPE)
{
ReleaseSysCache(classtup);
pfree(cp[0]);
classStruct = (Form_pg_class) GETSTRUCT(classtup);
relname = NameStr(classStruct->relname);
- /* accept relation, sequence, or view pg_class entries */
+ /* accept relation, sequence, view, or type pg_class entries */
if (classStruct->relkind != RELKIND_RELATION &&
classStruct->relkind != RELKIND_SEQUENCE &&
- classStruct->relkind != RELKIND_VIEW)
+ classStruct->relkind != RELKIND_VIEW &&
+ classStruct->relkind != RELKIND_COMPOSITE_TYPE)
elog(ERROR, "%s isn't a table", relname);
/*
zippo | 42
(1 row)
+-- Test stand-alone composite type
+CREATE TYPE default_test_row AS (f1 text_w_default, f2 int42);
+CREATE FUNCTION get_default_test() RETURNS SETOF default_test_row AS '
+ SELECT * FROM default_test;
+' LANGUAGE SQL;
+SELECT * FROM get_default_test();
+ f1 | f2
+-------+----
+ zippo | 42
+(1 row)
+
+DROP TYPE default_test_row CASCADE;
+NOTICE: Drop cascades to function get_default_test()
DROP TABLE default_test;
SELECT * FROM default_test;
+-- Test stand-alone composite type
+
+CREATE TYPE default_test_row AS (f1 text_w_default, f2 int42);
+
+CREATE FUNCTION get_default_test() RETURNS SETOF default_test_row AS '
+ SELECT * FROM default_test;
+' LANGUAGE SQL;
+
+SELECT * FROM get_default_test();
+
+DROP TYPE default_test_row CASCADE;
+
DROP TABLE default_test;