1 <!-- doc/src/sgml/tableam.sgml -->
4 <title>Table Access Method Interface Definition
</title>
7 <primary>Table Access Method
</primary>
10 <primary>tableam
</primary>
11 <secondary>Table Access Method
</secondary>
15 This chapter explains the interface between the core
16 <productname>PostgreSQL
</productname> system and
<firstterm>table access
17 methods
</firstterm>, which manage the storage for tables. The core system
18 knows little about these access methods beyond what is specified here, so
19 it is possible to develop entirely new access method types by writing
24 Each table access method is described by a row in the
<link
25 linkend=
"catalog-pg-am"><structname>pg_am
</structname></link> system
26 catalog. The
<structname>pg_am
</structname> entry specifies a name and a
27 <firstterm>handler function
</firstterm> for the table access method. These
28 entries can be created and deleted using the
<xref
29 linkend=
"sql-create-access-method"/> and
<xref
30 linkend=
"sql-drop-access-method"/> SQL commands.
34 A table access method handler function must be declared to accept a single
35 argument of type
<type>internal
</type> and to return the pseudo-type
36 <type>table_am_handler
</type>. The argument is a dummy value that simply
37 serves to prevent handler functions from being called directly from SQL commands.
39 The result of the function must be a pointer to a struct of type
40 <structname>TableAmRoutine
</structname>, which contains everything that the
41 core code needs to know to make use of the table access method. The return
42 value needs to be of server lifetime, which is typically achieved by
43 defining it as a
<literal>static const
</literal> variable in global
44 scope. The
<structname>TableAmRoutine
</structname> struct, also called the
45 access method's
<firstterm>API struct
</firstterm>, defines the behavior of
46 the access method using callbacks. These callbacks are pointers to plain C
47 functions and are not visible or callable at the SQL level. All the
48 callbacks and their behavior is defined in the
49 <structname>TableAmRoutine
</structname> structure (with comments inside the
50 struct defining the requirements for callbacks). Most callbacks have
51 wrapper functions, which are documented from the point of view of a user
52 (rather than an implementor) of the table access method. For details,
53 please refer to the
<ulink url=
"https://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/include/access/tableam.h;hb=HEAD">
54 <filename>src/include/access/tableam.h
</filename></ulink> file.
58 To implement an access method, an implementor will typically need to
59 implement an
<acronym>AM
</acronym>-specific type of tuple table slot (see
60 <ulink url=
"https://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/include/executor/tuptable.h;hb=HEAD">
61 <filename>src/include/executor/tuptable.h
</filename></ulink>), which allows
62 code outside the access method to hold references to tuples of the AM, and
63 to access the columns of the tuple.
67 Currently, the way an AM actually stores data is fairly unconstrained. For
68 example, it's possible, but not required, to use postgres' shared buffer
69 cache. In case it is used, it likely makes sense to use
70 <productname>PostgreSQL
</productname>'s standard page layout as described in
71 <xref linkend=
"storage-page-layout"/>.
75 One fairly large constraint of the table access method API is that,
76 currently, if the AM wants to support modifications and/or indexes, it is
77 necessary for each tuple to have a tuple identifier (
<acronym>TID
</acronym>)
78 consisting of a block number and an item number (see also
<xref
79 linkend=
"storage-page-layout"/>). It is not strictly necessary that the
80 sub-parts of
<acronym>TIDs
</acronym> have the same meaning they e.g., have
81 for
<literal>heap
</literal>, but if bitmap scan support is desired (it is
82 optional), the block number needs to provide locality.
86 For crash safety, an AM can use postgres'
<link
87 linkend=
"wal"><acronym>WAL
</acronym></link>, or a custom implementation.
88 If
<acronym>WAL
</acronym> is chosen, either
<link
89 linkend=
"generic-wal">Generic WAL Records
</link> can be used,
90 or a
<link linkend=
"custom-rmgr">Custom WAL Resource Manager
</link> can be
95 To implement transactional support in a manner that allows different table
96 access methods be accessed within a single transaction, it likely is
97 necessary to closely integrate with the machinery in
98 <filename>src/backend/access/transam/xlog.c
</filename>.
102 Any developer of a new
<literal>table access method
</literal> can refer to
103 the existing
<literal>heap
</literal> implementation present in
104 <filename>src/backend/access/heap/heapam_handler.c
</filename> for details of