2019-04-04 02:37:00 +02:00
|
|
|
<!-- doc/src/sgml/tableam.sgml -->
|
|
|
|
|
|
|
|
<chapter id="tableam">
|
|
|
|
<title>Table Access Method Interface Definition</title>
|
|
|
|
|
|
|
|
<indexterm>
|
|
|
|
<primary>Table Access Method</primary>
|
|
|
|
</indexterm>
|
|
|
|
<indexterm>
|
|
|
|
<primary>tableam</primary>
|
|
|
|
<secondary>Table Access Method</secondary>
|
|
|
|
</indexterm>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
This chapter explains the interface between the core
|
|
|
|
<productname>PostgreSQL</productname> system and <firstterm>table access
|
|
|
|
methods</firstterm>, which manage the storage for tables. The core system
|
|
|
|
knows little about these access methods beyond what is specified here, so
|
|
|
|
it is possible to develop entirely new access method types by writing
|
|
|
|
add-on code.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
Each table access method is described by a row in the <link
|
|
|
|
linkend="catalog-pg-am"><structname>pg_am</structname></link> system
|
|
|
|
catalog. The <structname>pg_am</structname> entry specifies a name and a
|
|
|
|
<firstterm>handler function</firstterm> for the table access method. These
|
|
|
|
entries can be created and deleted using the <xref
|
|
|
|
linkend="sql-create-access-method"/> and <xref
|
|
|
|
linkend="sql-drop-access-method"/> SQL commands.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
A table access method handler function must be declared to accept a single
|
|
|
|
argument of type <type>internal</type> and to return the pseudo-type
|
|
|
|
<type>table_am_handler</type>. The argument is a dummy value that simply
|
|
|
|
serves to prevent handler functions from being called directly from SQL commands.
|
|
|
|
|
|
|
|
The result of the function must be a pointer to a struct of type
|
|
|
|
<structname>TableAmRoutine</structname>, which contains everything that the
|
|
|
|
core code needs to know to make use of the table access method. The return
|
|
|
|
value needs to be of server lifetime, which is typically achieved by
|
|
|
|
defining it as a <literal>static const</literal> variable in global
|
|
|
|
scope. The <structname>TableAmRoutine</structname> struct, also called the
|
|
|
|
access method's <firstterm>API struct</firstterm>, defines the behavior of
|
|
|
|
the access method using callbacks. These callbacks are pointers to plain C
|
|
|
|
functions and are not visible or callable at the SQL level. All the
|
|
|
|
callbacks and their behavior is defined in the
|
|
|
|
<structname>TableAmRoutine</structname> structure (with comments inside the
|
|
|
|
struct defining the requirements for callbacks). Most callbacks have
|
2019-04-05 18:45:59 +02:00
|
|
|
wrapper functions, which are documented from the point of view of a user
|
|
|
|
(rather than an implementor) of the table access method. For details,
|
2019-04-04 02:37:00 +02:00
|
|
|
please refer to the <ulink url="https://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/include/access/tableam.h;hb=HEAD">
|
|
|
|
<filename>src/include/access/tableam.h</filename></ulink> file.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2019-04-05 18:45:59 +02:00
|
|
|
To implement an access method, an implementor will typically need to
|
|
|
|
implement an AM-specific type of tuple table slot (see
|
2019-04-04 02:37:00 +02:00
|
|
|
<ulink url="https://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/include/executor/tuptable.h;hb=HEAD">
|
2019-04-05 18:45:59 +02:00
|
|
|
<filename>src/include/executor/tuptable.h</filename></ulink>), which allows
|
2019-04-04 02:37:00 +02:00
|
|
|
code outside the access method to hold references to tuples of the AM, and
|
|
|
|
to access the columns of the tuple.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2019-04-05 18:45:59 +02:00
|
|
|
Currently, the way an AM actually stores data is fairly unconstrained. For
|
|
|
|
example, it's possible, but not required, to use postgres' shared buffer
|
|
|
|
cache. In case it is used, it likely makes sense to use
|
|
|
|
<productname>PostgreSQL</productname>'s standard page layout as described in
|
|
|
|
<xref linkend="storage-page-layout"/>.
|
2019-04-04 02:37:00 +02:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
One fairly large constraint of the table access method API is that,
|
|
|
|
currently, if the AM wants to support modifications and/or indexes, it is
|
2019-04-05 18:45:59 +02:00
|
|
|
necessary for each tuple to have a tuple identifier (<acronym>TID</acronym>)
|
2019-04-04 02:37:00 +02:00
|
|
|
consisting of a block number and an item number (see also <xref
|
|
|
|
linkend="storage-page-layout"/>). It is not strictly necessary that the
|
2020-09-01 00:33:37 +02:00
|
|
|
sub-parts of <acronym>TIDs</acronym> have the same meaning they e.g., have
|
2019-04-04 02:37:00 +02:00
|
|
|
for <literal>heap</literal>, but if bitmap scan support is desired (it is
|
|
|
|
optional), the block number needs to provide locality.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2019-04-05 18:45:59 +02:00
|
|
|
For crash safety, an AM can use postgres' <link
|
|
|
|
linkend="wal"><acronym>WAL</acronym></link>, or a custom implementation.
|
|
|
|
If <acronym>WAL</acronym> is chosen, either <link
|
|
|
|
linkend="generic-wal">Generic WAL Records</link> can be used,
|
2022-08-25 19:26:31 +02:00
|
|
|
or a <link linkend="custom-rmgr">Custom WAL Resource Manager</link> can be
|
|
|
|
implemented.
|
2019-04-04 02:37:00 +02:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
To implement transactional support in a manner that allows different table
|
|
|
|
access methods be accessed within a single transaction, it likely is
|
|
|
|
necessary to closely integrate with the machinery in
|
|
|
|
<filename>src/backend/access/transam/xlog.c</filename>.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
Any developer of a new <literal>table access method</literal> can refer to
|
|
|
|
the existing <literal>heap</literal> implementation present in
|
2019-04-18 02:31:54 +02:00
|
|
|
<filename>src/backend/access/heap/heapam_handler.c</filename> for details of
|
2019-04-05 18:45:59 +02:00
|
|
|
its implementation.
|
2019-04-04 02:37:00 +02:00
|
|
|
</para>
|
|
|
|
|
|
|
|
</chapter>
|