|
35 | 35 | argument of type <type>internal</type> and to return the pseudo-type
|
36 | 36 | <type>table_am_handler</type>. The argument is a dummy value that simply
|
37 | 37 | 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; |
38 | 49 |
|
| 50 | +CREATE ACCESS METHOD myam TYPE TABLE HANDLER my_tableam_handler; |
| 51 | +</programlisting> |
| 52 | + |
| 53 | + <para> |
39 | 54 | The result of the function must be a pointer to a struct of type
|
40 | 55 | <structname>TableAmRoutine</structname>, which contains everything that the
|
41 | 56 | core code needs to know to make use of the table access method. The return
|
42 | 57 | 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 |
45 | 92 | access method's <firstterm>API struct</firstterm>, defines the behavior of
|
46 | 93 | the access method using callbacks. These callbacks are pointers to plain C
|
47 | 94 | functions and are not visible or callable at the SQL level. All the
|
|
0 commit comments