Home Explore Blog CI



postgresql

15th chunk of `doc/src/sgml/ref/create_type.sgml`
e227fb490b15fc40061d1289958f4af109be6531254900240000000100000d23
 versions.
   See <filename>src/backend/utils/fmgr/README</filename> for more
   information.
  </para>

 </refsect1>

 <refsect1>
  <title>Examples</title>

  <para>
   This example creates a composite type and uses it in
   a function definition:
<programlisting>
CREATE TYPE compfoo AS (f1 int, f2 text);

CREATE FUNCTION getfoo() RETURNS SETOF compfoo AS $$
    SELECT fooid, fooname FROM foo
$$ LANGUAGE SQL;
</programlisting>
  </para>

  <para>
   This example creates an enumerated type and uses it in
   a table definition:
<programlisting>
CREATE TYPE bug_status AS ENUM ('new', 'open', 'closed');

CREATE TABLE bug (
    id serial,
    description text,
    status bug_status
);
</programlisting>
  </para>

  <para>
   This example creates a range type:
<programlisting>
CREATE TYPE float8_range AS RANGE (subtype = float8, subtype_diff = float8mi);
</programlisting>
  </para>

  <para>
   This example creates the base data type <type>box</type> and then uses the
   type in a table definition:
<programlisting>
CREATE TYPE box;

CREATE FUNCTION my_box_in_function(cstring) RETURNS box AS ... ;
CREATE FUNCTION my_box_out_function(box) RETURNS cstring AS ... ;

CREATE TYPE box (
    INTERNALLENGTH = 16,
    INPUT = my_box_in_function,
    OUTPUT = my_box_out_function
);

CREATE TABLE myboxes (
    id integer,
    description box
);
</programlisting>
  </para>

  <para>
   If the internal structure of <type>box</type> were an array of four
   <type>float4</type> elements, we might instead use:
<programlisting>
CREATE TYPE box (
    INTERNALLENGTH = 16,
    INPUT = my_box_in_function,
    OUTPUT = my_box_out_function,
    ELEMENT = float4
);
</programlisting>
   which would allow a box value's component numbers to be accessed
   by subscripting.  Otherwise the type behaves the same as before.
  </para>

  <para>
   This example creates a large object type and uses it in
   a table definition:
<programlisting>
CREATE TYPE bigobj (
    INPUT = lo_filein, OUTPUT = lo_fileout,
    INTERNALLENGTH = VARIABLE
);
CREATE TABLE big_objs (
    id integer,
    obj bigobj
);
</programlisting>
  </para>

  <para>
   More examples, including suitable input and output functions, are
   in <xref linkend="xtypes"/>.
  </para>
 </refsect1>

 <refsect1 id="sql-createtype-compatibility">
  <title>Compatibility</title>

  <para>
   The first form of the <command>CREATE TYPE</command> command, which
   creates a composite type, conforms to the <acronym>SQL</acronym> standard.
   The other forms are <productname>PostgreSQL</productname>
   extensions.  The <command>CREATE TYPE</command> statement in
   the <acronym>SQL</acronym> standard also defines other forms that are not
   implemented in <productname>PostgreSQL</productname>.
  </para>

  <para>
   The ability to create a composite type with zero attributes is
   a <productname>PostgreSQL</productname>-specific deviation from the
   standard (analogous to the same case in <command>CREATE TABLE</command>).
  </para>
 </refsect1>

 <refsect1 id="sql-createtype-see-also">
  <title>See Also</title>

  <simplelist type="inline">
   <member><xref linkend="sql-altertype"/></member>
   <member><xref linkend="sql-createdomain"/></member>
   <member><xref linkend="sql-createfunction"/></member>
   <member><xref linkend="sql-droptype"/></member>
  </simplelist>
 </refsect1>

</refentry>

Title: CREATE TYPE - Examples, Large Objects, Compatibility, and See Also
Summary
This section provides examples of creating and using various data types in PostgreSQL, including composite, enumerated, range, base (box), and large object types. It also discusses the compatibility of the CREATE TYPE command with the SQL standard, noting PostgreSQL extensions. Finally, it lists related SQL commands like ALTER TYPE, CREATE DOMAIN, CREATE FUNCTION, and DROP TYPE.