Skip to content

Commit e09fff7

Browse files
committed
doc: Add minimal C and SQL example to add a custom table AM handler
The documentation was rather sparse on this matter and there is no extension in-core that shows how to do it. Adding a small example will hopefully help newcomers. An advantage of writing things this way is that the contents are not going to rot because of backend changes. Author: Phil Eaton Reviewed-by: Robert Haas, Fabrízio de Royes Mello Discussion: https://postgr.es/m/CAByiw+r+CS-ojBDP7Dm=9YeOLkZTXVnBmOe_ajK=en8C_zB3_g@mail.gmail.com
1 parent 2e7c4ab commit e09fff7

File tree

1 file changed

+49
-2
lines changed

1 file changed

+49
-2
lines changed

doc/src/sgml/tableam.sgml

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,60 @@
3535
argument of type <type>internal</type> and to return the pseudo-type
3636
<type>table_am_handler</type>. The argument is a dummy value that simply
3737
serves to prevent handler functions from being called directly from SQL commands.
38+
</para>
39+
40+
<para>
41+
Here is how an extension SQL script file might create a table access
42+
method handler:
43+
</para>
44+
45+
<programlisting>
46+
CREATE OR REPLACE FUNCTION my_tableam_handler(internal)
47+
RETURNS table_am_handler AS 'my_extension', 'my_tableam_handler'
48+
LANGUAGE C STRICT;
3849

50+
CREATE ACCESS METHOD myam TYPE TABLE HANDLER my_tableam_handler;
51+
</programlisting>
52+
53+
<para>
3954
The result of the function must be a pointer to a struct of type
4055
<structname>TableAmRoutine</structname>, which contains everything that the
4156
core code needs to know to make use of the table access method. The return
4257
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
58+
defining it as a <literal>static const</literal> variable in global scope.
59+
</para>
60+
61+
<para>
62+
Here is how a source file with the table access method handler might
63+
look like:
64+
</para>
65+
66+
<programlisting><![CDATA[
67+
#include "postgres.h"
68+
69+
#include "access/tableam.h"
70+
#include "fmgr.h"
71+
72+
PG_MODULE_MAGIC;
73+
74+
static const TableAmRoutine my_tableam_methods = {
75+
.type = T_TableAmRoutine,
76+
77+
/* Methods of TableAmRoutine omitted from example, add them here. */
78+
};
79+
80+
PG_FUNCTION_INFO_V1(my_tableam_handler);
81+
82+
Datum
83+
my_tableam_handler(PG_FUNCTION_ARGS)
84+
{
85+
PG_RETURN_POINTER(&my_tableam_methods);
86+
}
87+
]]>
88+
</programlisting>
89+
90+
<para>
91+
The <structname>TableAmRoutine</structname> struct, also called the
4592
access method's <firstterm>API struct</firstterm>, defines the behavior of
4693
the access method using callbacks. These callbacks are pointers to plain C
4794
functions and are not visible or callable at the SQL level. All the

0 commit comments

Comments
 (0)