Home Explore Blog CI



postgresql

9th chunk of `doc/src/sgml/ref/create_type.sgml`
0deab9315970b8a4318aa73585a7dd8d74f222884d3ed3a30000000100000fa6
 be overridden by an explicit <literal>DEFAULT</literal>
   clause attached to a particular column.)
  </para>

  <para>
   To indicate that a type is a fixed-length array type,
   specify the type of the array
   elements using the <literal>ELEMENT</literal> key word.  For example, to
   define an array of 4-byte integers (<type>int4</type>), specify
   <literal>ELEMENT = int4</literal>.  For more details,
   see <xref linkend="sql-createtype-array"/> below.
  </para>

  <para>
   To indicate the delimiter to be used between values in the external
   representation of arrays of this type, <replaceable
   class="parameter">delimiter</replaceable> can be
   set to a specific character.  The default delimiter is the comma
   (<literal>,</literal>).  Note that the delimiter is associated
   with the array element type, not the array type itself.
  </para>

  <para>
   If the optional Boolean
   parameter <replaceable class="parameter">collatable</replaceable>
   is true, column definitions and expressions of the type may carry
   collation information through use of
   the <literal>COLLATE</literal> clause.  It is up to the
   implementations of the functions operating on the type to actually
   make use of the collation information; this does not happen
   automatically merely by marking the type collatable.
  </para>
  </refsect2>

  <refsect2 id="sql-createtype-array" xreflabel="Array Types">
   <title>Array Types</title>

   <para>
    Whenever a user-defined type is created,
    <productname>PostgreSQL</productname> automatically creates an
    associated array type, whose name consists of the element type's
    name prepended with an underscore, and truncated if necessary to keep
    it less than <symbol>NAMEDATALEN</symbol> bytes long.  (If the name
    so generated collides with an existing type name, the process is
    repeated until a non-colliding name is found.)
    This implicitly-created array type is variable length and uses the
    built-in input and output functions <literal>array_in</literal> and
    <literal>array_out</literal>.  Furthermore, this type is what the system
    uses for constructs such as <literal>ARRAY[]</literal> over the
    user-defined type.  The array type tracks any changes in its
    element type's owner or schema, and is dropped if the element type is.
   </para>

   <para>
    You might reasonably ask why there is an <option>ELEMENT</option>
    option, if the system makes the correct array type automatically.
    The main case where it's useful to use <option>ELEMENT</option> is when you are
    making a fixed-length type that happens to be internally an array of a number of
    identical things, and you want to allow these things to be accessed
    directly by subscripting, in addition to whatever operations you plan
    to provide for the type as a whole.  For example, type <type>point</type>
    is represented as just two floating-point numbers, which can be accessed
    using <literal>point[0]</literal> and <literal>point[1]</literal>.
    Note that
    this facility only works for fixed-length types whose internal form
    is exactly a sequence of identical fixed-length fields.
    For historical reasons (i.e., this is clearly wrong but it's far too
    late to change it), subscripting of fixed-length array types starts from
    zero, rather than from one as for variable-length arrays.
   </para>

   <para>
    Specifying the <option>SUBSCRIPT</option> option allows a data type to
    be subscripted, even though the system does not otherwise regard it as
    an array type.  The behavior just described for fixed-length arrays is
    actually implemented by the <option>SUBSCRIPT</option> handler
    function <function>raw_array_subscript_handler</function>, which is
    used automatically if you specify <option>ELEMENT</option> for a
    fixed-length type without also writing <option>SUBSCRIPT</option>.
   </para>

   <para>
    When specifying a custom <option>SUBSCRIPT</option> function,

Title: CREATE TYPE - Array Types and the ELEMENT and SUBSCRIPT Options
Summary
This section explains how PostgreSQL automatically creates array types for user-defined types, and the purpose of the ELEMENT option for fixed-length arrays. It also discusses the SUBSCRIPT option, which allows a data type to be subscripted even if it's not an array type, and how it's used with fixed-length arrays.