From e09fff7c980a8b545a18e119be41bf67cd2e9182 Mon Sep 17 00:00:00 2001 From: Michael Paquier Date: Mon, 7 Oct 2024 15:47:40 +0900 Subject: [PATCH] doc: Add minimal C and SQL example to add a custom table AM handler MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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 --- doc/src/sgml/tableam.sgml | 51 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 2 deletions(-) diff --git a/doc/src/sgml/tableam.sgml b/doc/src/sgml/tableam.sgml index 4b37f2e5a60..9ccf5b739ed 100644 --- a/doc/src/sgml/tableam.sgml +++ b/doc/src/sgml/tableam.sgml @@ -35,13 +35,60 @@ argument of type internal and to return the pseudo-type table_am_handler. The argument is a dummy value that simply serves to prevent handler functions from being called directly from SQL commands. + + + + Here is how an extension SQL script file might create a table access + method handler: + + + +CREATE OR REPLACE FUNCTION my_tableam_handler(internal) + RETURNS table_am_handler AS 'my_extension', 'my_tableam_handler' + LANGUAGE C STRICT; +CREATE ACCESS METHOD myam TYPE TABLE HANDLER my_tableam_handler; + + + The result of the function must be a pointer to a struct of type TableAmRoutine, 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 static const variable in global - scope. The TableAmRoutine struct, also called the + defining it as a static const variable in global scope. + + + + Here is how a source file with the table access method handler might + look like: + + + + + + + The TableAmRoutine struct, also called the access method's API struct, 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 -- 2.30.2