On Fri, 2008-10-31 at 16:03 +0200, Peter Eisentraut wrote:
> Here is an implementation of distinct types, known from SQL99 and
> beyond.  They are like domains, except that they don't have defaults or
> constraints and they do not allow implicit casting to their base type.
> The latter aspect is what makes them distinct types.  They are useful to
> create more type-safe database schemas, to prevent using generic types
> such as text or int for everything and then mixing them in inappropriate
> ways.  This is something domains are not useful for.  Much of the
> internals are shared with domains nevertheless.  The difference is
> really only the casting behavior.
> 
> To create a distinct type, just run
> 
> CREATE TYPE mystring AS text;
> 

It needs documentation, and I included a quick patch for that (if that's
helpful).

It builds fine for me and appears to do everything as advertised. 

I skimmed the code and the relevant parts of the SQL standard, but it
may need further review by someone who knows the type system and the SQL
standard better than I.

Regards,
        Jeff Davis




diff --git a/doc/src/sgml/ref/create_domain.sgml b/doc/src/sgml/ref/create_domain.sgml
index b26d575..3805c86 100644
--- a/doc/src/sgml/ref/create_domain.sgml
+++ b/doc/src/sgml/ref/create_domain.sgml
@@ -57,6 +57,12 @@ where <replaceable class="PARAMETER">constraint</replaceable> is:
    Define a domain rather than setting up each table's constraint
    individually.
   </para>
+
+  <para>
+   Domains are similar to distinct types, described in <xref
+   linkend="sql-createtype">, except that you can specify defaults or
+   constraints, and a domain can be implicitly case to its base type.
+  </para>
  </refsect1>
 
  <refsect1>
diff --git a/doc/src/sgml/ref/create_type.sgml b/doc/src/sgml/ref/create_type.sgml
index 70ddae6..db9408a 100644
--- a/doc/src/sgml/ref/create_type.sgml
+++ b/doc/src/sgml/ref/create_type.sgml
@@ -26,6 +26,8 @@ CREATE TYPE <replaceable class="parameter">name</replaceable> AS
 CREATE TYPE <replaceable class="parameter">name</replaceable> AS ENUM
     ( '<replaceable class="parameter">label</replaceable>' [, ... ] )
 
+CREATE TYPE <replaceable class="parameter">name</replaceable> AS <replaceable class="parameter">data_type</replaceable>
+
 CREATE TYPE <replaceable class="parameter">name</replaceable> (
     INPUT = <replaceable class="parameter">input_function</replaceable>,
     OUTPUT = <replaceable class="parameter">output_function</replaceable>
@@ -95,10 +97,25 @@ CREATE TYPE <replaceable class="parameter">name</replaceable>
   </refsect2>
 
   <refsect2>
+   <title>Distinct Types</title>
+
+   <para>
+    The third form of <command>CREATE TYPE</command> creates a
+    distinct type. Distinct types are similar to domains, as described
+    in <xref linkend="sql-createdomain">, except that they do not have
+    defaults or constraints, and they cannot be implicitly cast to
+    their base type. Distinct types are useful to avoid making
+    mistakes by comparing two unrelated values that happen to be the
+    same type, such as two integers representing supplier number and
+    part number.
+   </para>
+  </refsect2>
+
+  <refsect2>
    <title>Base Types</title>
 
   <para>
-   The third form of <command>CREATE TYPE</command> creates a new base type
+   The fourth form of <command>CREATE TYPE</command> creates a new base type
    (scalar type).  To create a new base type, you must be a superuser.
    (This restriction is made because an erroneous type definition could
    confuse or even crash the server.)
-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Reply via email to