Commit 9bb955c8 authored by Tom Lane's avatar Tom Lane

Update assorted TOAST-related documentation.

While working on documentation for expanded arrays, I noticed a number of
details in the TOAST-related documentation that were already inaccurate or
obsolete.  This should be fixed independently of whether expanded arrays
get in or not.  One issue is that the already existing indirect-pointer
facility was not documented at all.  Also, the documentation says that you
only need to use VARSIZE/SET_VARSIZE if you've made your variable-length
type TOAST-aware, but actually we've forced that business on all varlena
types even if they've opted out of TOAST by setting storage = plain.
Wordsmith a few other things too, like an amusingly archaic claim that
there are few 64-bit machines.

I thought about back-patching this, but since all this doco is oriented
to hackers and C-coded extension authors, fixing it in HEAD is probably
good enough.
parent 56a79a86
...@@ -329,15 +329,17 @@ CREATE TYPE <replaceable class="parameter">name</replaceable> ...@@ -329,15 +329,17 @@ CREATE TYPE <replaceable class="parameter">name</replaceable>
to <literal>VARIABLE</literal>. (Internally, this is represented to <literal>VARIABLE</literal>. (Internally, this is represented
by setting <literal>typlen</> to -1.) The internal representation of all by setting <literal>typlen</> to -1.) The internal representation of all
variable-length types must start with a 4-byte integer giving the total variable-length types must start with a 4-byte integer giving the total
length of this value of the type. length of this value of the type. (Note that the length field is often
encoded, as described in <xref linkend="storage-toast">; it's unwise
to access it directly.)
</para> </para>
<para> <para>
The optional flag <literal>PASSEDBYVALUE</literal> indicates that The optional flag <literal>PASSEDBYVALUE</literal> indicates that
values of this data type are passed by value, rather than by values of this data type are passed by value, rather than by
reference. You cannot pass by value types whose internal reference. Types passed by value must be fixed-length, and their internal
representation is larger than the size of the <type>Datum</> type representation cannot be larger than the size of the <type>Datum</> type
(4 bytes on most machines, 8 bytes on a few). (4 bytes on some machines, 8 bytes on others).
</para> </para>
<para> <para>
...@@ -367,6 +369,17 @@ CREATE TYPE <replaceable class="parameter">name</replaceable> ...@@ -367,6 +369,17 @@ CREATE TYPE <replaceable class="parameter">name</replaceable>
<literal>external</literal> items.) <literal>external</literal> items.)
</para> </para>
<para>
All <replaceable class="parameter">storage</replaceable> values other
than <literal>plain</literal> imply that the functions of the data type
can handle values that have been <firstterm>toasted</>, as described
in <xref linkend="storage-toast"> and <xref linkend="xtypes-toast">.
The specific other value given merely determines the default TOAST
storage strategy for columns of a toastable data type; users can pick
other strategies for individual columns using <literal>ALTER TABLE
SET STORAGE</>.
</para>
<para> <para>
The <replaceable class="parameter">like_type</replaceable> parameter The <replaceable class="parameter">like_type</replaceable> parameter
provides an alternative method for specifying the basic representation provides an alternative method for specifying the basic representation
...@@ -465,8 +478,8 @@ CREATE TYPE <replaceable class="parameter">name</replaceable> ...@@ -465,8 +478,8 @@ CREATE TYPE <replaceable class="parameter">name</replaceable>
identical things, and you want to allow these things to be accessed identical things, and you want to allow these things to be accessed
directly by subscripting, in addition to whatever operations you plan directly by subscripting, in addition to whatever operations you plan
to provide for the type as a whole. For example, type <type>point</> to provide for the type as a whole. For example, type <type>point</>
is represented as just two floating-point numbers, each can be accessed using is represented as just two floating-point numbers, which can be accessed
<literal>point[0]</> and <literal>point[1]</>. using <literal>point[0]</> and <literal>point[1]</>.
Note that Note that
this facility only works for fixed-length types whose internal form this facility only works for fixed-length types whose internal form
is exactly a sequence of identical fixed-length fields. A subscriptable is exactly a sequence of identical fixed-length fields. A subscriptable
......
This diff is collapsed.
...@@ -234,35 +234,49 @@ CREATE TYPE complex ( ...@@ -234,35 +234,49 @@ CREATE TYPE complex (
</para> </para>
<para> <para>
If the internal representation of the data type is variable-length, the
internal representation must follow the standard layout for variable-length
data: the first four bytes must be a <type>char[4]</type> field which is
never accessed directly (customarily named <structfield>vl_len_</>). You
must use the <function>SET_VARSIZE()</function> macro to store the total
size of the datum (including the length field itself) in this field
and <function>VARSIZE()</function> to retrieve it. (These macros exist
because the length field may be encoded depending on platform.)
</para>
<para>
For further details see the description of the
<xref linkend="sql-createtype"> command.
</para>
<sect2 id="xtypes-toast">
<title>TOAST Considerations</title>
<indexterm> <indexterm>
<primary>TOAST</primary> <primary>TOAST</primary>
<secondary>and user-defined types</secondary> <secondary>and user-defined types</secondary>
</indexterm> </indexterm>
If the values of your data type vary in size (in internal form), you should
make the data type <acronym>TOAST</>-able (see <xref <para>
linkend="storage-toast">). You should do this even if the data are always If the values of your data type vary in size (in internal form), it's
usually desirable to make the data type <acronym>TOAST</>-able (see <xref
linkend="storage-toast">). You should do this even if the values are always
too small to be compressed or stored externally, because too small to be compressed or stored externally, because
<acronym>TOAST</> can save space on small data too, by reducing header <acronym>TOAST</> can save space on small data too, by reducing header
overhead. overhead.
</para> </para>
<para> <para>
To do this, the internal representation must follow the standard layout for To support <acronym>TOAST</> storage, the C functions operating on the data
variable-length data: the first four bytes must be a <type>char[4]</type> type must always be careful to unpack any toasted values they are handed
field which is never accessed directly (customarily named by using <function>PG_DETOAST_DATUM</>. (This detail is customarily hidden
<structfield>vl_len_</>). You by defining type-specific <function>GETARG_DATATYPE_P</function> macros.)
must use <function>SET_VARSIZE()</function> to store the size of the datum Then, when running the <command>CREATE TYPE</command> command, specify the
in this field and <function>VARSIZE()</function> to retrieve it. The C internal length as <literal>variable</> and select some appropriate storage
functions operating on the data type must always be careful to unpack any option other than <literal>plain</>.
toasted values they are handed, by using <function>PG_DETOAST_DATUM</>.
(This detail is customarily hidden by defining type-specific
<function>GETARG_DATATYPE_P</function> macros.) Then, when running the
<command>CREATE TYPE</command> command, specify the internal length as
<literal>variable</> and select the appropriate storage option.
</para> </para>
<para> <para>
If the alignment is unimportant (either just for a specific function or If data alignment is unimportant (either just for a specific function or
because the data type specifies byte alignment anyway) then it's possible because the data type specifies byte alignment anyway) then it's possible
to avoid some of the overhead of <function>PG_DETOAST_DATUM</>. You can use to avoid some of the overhead of <function>PG_DETOAST_DATUM</>. You can use
<function>PG_DETOAST_DATUM_PACKED</> instead (customarily hidden by <function>PG_DETOAST_DATUM_PACKED</> instead (customarily hidden by
...@@ -286,8 +300,6 @@ CREATE TYPE complex ( ...@@ -286,8 +300,6 @@ CREATE TYPE complex (
</para> </para>
</note> </note>
<para> </sect2>
For further details see the description of the
<xref linkend="sql-createtype"> command.
</para>
</sect1> </sect1>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment