Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1k5aSZ-00017k-KW for pgsql-hackers@arkaria.postgresql.org; Tue, 11 Aug 2020 20:01:24 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1k5aSW-0006Us-9r for pgsql-hackers@arkaria.postgresql.org; Tue, 11 Aug 2020 20:01:20 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1k5aSV-0006SN-2M for pgsql-hackers@lists.postgresql.org; Tue, 11 Aug 2020 20:01:19 +0000 Received: from mail-oi1-x230.google.com ([2607:f8b0:4864:20::230]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1k5aSR-0001g3-7Y for pgsql-hackers@lists.postgresql.org; Tue, 11 Aug 2020 20:01:17 +0000 Received: by mail-oi1-x230.google.com with SMTP id u63so13398092oie.5 for ; Tue, 11 Aug 2020 13:01:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=XbzmcYN1J/YmCZ2coivzbTDocfSQ+ZWp6fhk5WU2kik=; b=OdUCm0DY4D4jAcPV8I7uT6nUJbJFtB9XmBrhitKFIgbmCh+B6QMNIjMFuCd0f5TFLY lXJ1woauBI0WzsYVwT+fgk1I7hZMrMdaWsmgI0T7benDO9S2DlX9LPluGPuLQjyGGF0C 70NZvrrofMtfQrHsKp07do7LjAb6FcrBHwEpaZLxxO81Ar0tJUYywKFk3zKp+FvC8NiO i6h8j361mSraknFqQV88sR5uNvzOvRPNzbY8Ne2VOGL9xw24gDCXtIsoSovMlyqSrxkE 6RJmzyNII2S27dENMI90v+AjqIvGczmdfEGvxBSQMHt3z5yFegB6LwhIKoCTYkkHukAV S/FQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=XbzmcYN1J/YmCZ2coivzbTDocfSQ+ZWp6fhk5WU2kik=; b=nGc8lQJ58elDe9nneVRC3zfiym6V/YGSwoqZQHKQxf2NmwHOqsbODiEU1b4nBqIIgE UOZjOf/bS6ud9acHzhoMjCcgW3HSQHXO1Z1j8pDhrCTUrhQPc74yjx3NZbvxnNjlg+pK 25/4MXW+S/JpvYR21DHYNjX5cjLOhkz/VRsgI7CE3sBpGiC/6I8bCqqRWcSBLDrVEePW pNpBDx7w89J0sj6GoREbivF5gvW2PYgWHuZW4dgj8nGjkHBhWhHfxIKmisk5SDEUDRgZ CtNfq4WHDyU6rFuBhw7CuWAKaznTjURNiuGyjgKKE9drbBit45jXMqEWY9yGnVAIWAd8 KPlg== X-Gm-Message-State: AOAM530gzYDvymF1zNrVe6s8U8TdhwvvkveormuunzqgK8H1KRM8Ffn+ Kr2MmKmr6c1XMV9WVf1fmUcJyyat9bM= X-Google-Smtp-Source: ABdhPJzm93WeoOpf2HMA6h1NOiowC9WIzCGa2B93N43KzX/o+iISMT5/cweOU/L25PclIGS7KGq9Tg== X-Received: by 2002:aca:6708:: with SMTP id z8mr4909776oix.96.1597176074057; Tue, 11 Aug 2020 13:01:14 -0700 (PDT) Received: from 2ndQuadrant.com (c-67-169-212-172.hsd1.or.comcast.net. [67.169.212.172]) by smtp.gmail.com with ESMTPSA id i204sm4529647oif.16.2020.08.11.13.01.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Aug 2020 13:01:13 -0700 (PDT) Date: Tue, 11 Aug 2020 13:01:10 -0700 From: Mark Wong To: Michael Paquier Cc: Tom Lane , pgsql-hackers@lists.postgresql.org Subject: Re: doc examples for pghandler Message-ID: <20200811200109.GA26294@2ndQuadrant.com> References: <20200612172648.GA3327@2ndQuadrant.com> <776992.1591989020@sss.pgh.pa.us> <20200612223525.GB3327@2ndQuadrant.com> <796709.1592014421@sss.pgh.pa.us> <20200613041917.GK3362@paquier.xyz> <20200615034517.GA20670@2ndQuadrant.com> <20200615074701.GJ52676@paquier.xyz> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="+HP7ph2BbKc20aGI" Content-Disposition: inline In-Reply-To: <20200615074701.GJ52676@paquier.xyz> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --+HP7ph2BbKc20aGI Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Mon, Jun 15, 2020 at 04:47:01PM +0900, Michael Paquier wrote: > On Sun, Jun 14, 2020 at 08:45:17PM -0700, Mark Wong wrote: > > Sounds good to me. Something more like the attached patch? > > That's the idea. I have not gone in details into what you have here, > but perhaps it would make sense to do a bit more and show how things > are done in the context of a PL function called in a trigger? Your > patch removes from the docs a code block that outlined that. Ah, right. For the moment I've added some empty conditionals for trigger and event trigger handling. I've created a new entry in the commitfest app. [1] I'll keep at it. :) Regards, Mark [1] https://commitfest.postgresql.org/29/2678/ -- Mark Wong 2ndQuadrant - PostgreSQL Solutions for the Enterprise https://www.2ndQuadrant.com/ --+HP7ph2BbKc20aGI Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="plsample-v2.patch" diff --git a/doc/src/sgml/plhandler.sgml b/doc/src/sgml/plhandler.sgml index e1b0af7a60..7b2c5624c0 100644 --- a/doc/src/sgml/plhandler.sgml +++ b/doc/src/sgml/plhandler.sgml @@ -96,62 +96,12 @@ - This is a template for a procedural-language handler written in C: - -#include "postgres.h" -#include "executor/spi.h" -#include "commands/trigger.h" -#include "fmgr.h" -#include "access/heapam.h" -#include "utils/syscache.h" -#include "catalog/pg_proc.h" -#include "catalog/pg_type.h" - -PG_MODULE_MAGIC; - -PG_FUNCTION_INFO_V1(plsample_call_handler); - -Datum -plsample_call_handler(PG_FUNCTION_ARGS) -{ - Datum retval; - - if (CALLED_AS_TRIGGER(fcinfo)) - { - /* - * Called as a trigger function - */ - TriggerData *trigdata = (TriggerData *) fcinfo->context; - - retval = ... - } - else - { - /* - * Called as a function - */ - - retval = ... - } - - return retval; -} - - Only a few thousand lines of code have to be added instead of the - dots to complete the call handler. - - - - After having compiled the handler function into a loadable module - (see ), the following commands then - register the sample procedural language: - -CREATE FUNCTION plsample_call_handler() RETURNS language_handler - AS 'filename' - LANGUAGE C; -CREATE LANGUAGE plsample - HANDLER plsample_call_handler; - + A template for a procedural-language handler written as a C extension is + provided in src/test/modules/plsample. This is a + working sample demonstrating one way to create a procedural-language + handler, process parameters, and return a value. A few thousand lines of + additional code may have to be added to complete a fully functional + handler. diff --git a/src/test/modules/Makefile b/src/test/modules/Makefile index 29de73c060..95144d8d7c 100644 --- a/src/test/modules/Makefile +++ b/src/test/modules/Makefile @@ -9,6 +9,7 @@ SUBDIRS = \ commit_ts \ dummy_index_am \ dummy_seclabel \ + plsample \ snapshot_too_old \ test_bloomfilter \ test_ddl_deparse \ diff --git a/src/test/modules/plsample/Makefile b/src/test/modules/plsample/Makefile new file mode 100644 index 0000000000..757b47c785 --- /dev/null +++ b/src/test/modules/plsample/Makefile @@ -0,0 +1,20 @@ +# src/test/modules/plsample/Makefile + +PGFILEDESC = "PL/Sample - procedural language" + +REGRESS = create_pl create_func select_func + +EXTENSION = plsample +EXTVERSION = 0.1 + +MODULE_big = plsample + +OBJS = plsample.o + +DATA = plsample.control plsample--0.1.sql + +plsample.o: plsample.c + +PG_CONFIG = pg_config +PGXS := $(shell $(PG_CONFIG) --pgxs) +include $(PGXS) diff --git a/src/test/modules/plsample/README b/src/test/modules/plsample/README new file mode 100644 index 0000000000..7ee213700b --- /dev/null +++ b/src/test/modules/plsample/README @@ -0,0 +1,3 @@ +plsample is an example procedural-language handler. It is a simple functional +template that demonstrates some of the things that need to be done in order to +build a fully functional procedural-language handler. diff --git a/src/test/modules/plsample/expected/create_func.out b/src/test/modules/plsample/expected/create_func.out new file mode 100644 index 0000000000..df2b915a97 --- /dev/null +++ b/src/test/modules/plsample/expected/create_func.out @@ -0,0 +1,5 @@ +CREATE FUNCTION plsample_func(a1 NUMERIC, a2 TEXT, a3 INTEGER[]) +RETURNS TEXT +AS $$ + This is function's source text. +$$ LANGUAGE plsample; diff --git a/src/test/modules/plsample/expected/create_pl.out b/src/test/modules/plsample/expected/create_pl.out new file mode 100644 index 0000000000..5365391284 --- /dev/null +++ b/src/test/modules/plsample/expected/create_pl.out @@ -0,0 +1,8 @@ +CREATE FUNCTION plsample_call_handler() +RETURNS language_handler +AS '$libdir/plsample' +LANGUAGE C; +CREATE LANGUAGE plsample +HANDLER plsample_call_handler; +COMMENT ON LANGUAGE plsample +IS 'PL/Sample procedural language'; diff --git a/src/test/modules/plsample/expected/select_func.out b/src/test/modules/plsample/expected/select_func.out new file mode 100644 index 0000000000..dc396cbc04 --- /dev/null +++ b/src/test/modules/plsample/expected/select_func.out @@ -0,0 +1,8 @@ +SELECT plsample_func(1.23, 'abc', '{4, 5, 6}'); + plsample_func +----------------------------------- + + + This is function's source text.+ + +(1 row) + diff --git a/src/test/modules/plsample/plsample--0.1.sql b/src/test/modules/plsample/plsample--0.1.sql new file mode 100644 index 0000000000..b429b83ceb --- /dev/null +++ b/src/test/modules/plsample/plsample--0.1.sql @@ -0,0 +1,9 @@ +CREATE FUNCTION plsample_call_handler() +RETURNS language_handler +AS 'MODULE_PATHNAME' +LANGUAGE C; + +CREATE LANGUAGE plsample +HANDLER plsample_call_handler; + +COMMENT ON LANGUAGE plsample IS 'PL/Sample procedural language'; diff --git a/src/test/modules/plsample/plsample.c b/src/test/modules/plsample/plsample.c new file mode 100644 index 0000000000..69f76e6067 --- /dev/null +++ b/src/test/modules/plsample/plsample.c @@ -0,0 +1,156 @@ +/*------------------------------------------------------------------------- + * + * plsample.c + * Handler for the PL/Sample procedural language + * + * Copyright (c) 2020, PostgreSQL Global Development Group + * + * IDENTIFICATION + * src/test/modules/plsample.c + * + *------------------------------------------------------------------------- + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +MemoryContext TopMemoryContext = NULL; + +PG_MODULE_MAGIC; + +PG_FUNCTION_INFO_V1(plsample_call_handler); + +static Datum plsample_func_handler(PG_FUNCTION_ARGS); + +/* + * Handle function, procedure, and trigger calls. + */ +Datum +plsample_call_handler(PG_FUNCTION_ARGS) +{ + Datum retval = (Datum) 0; + + PG_TRY(); + { + /* + * Determine if called as function or trigger and call appropriate + * subhandler. + */ + if (CALLED_AS_TRIGGER(fcinfo)) + { + /* TODO: Invoke the trigger handler. */ + } + else if (CALLED_AS_EVENT_TRIGGER(fcinfo)) + { + /* TODO: Invoke the event trigger handler. */ + } + else + { + /* Invoke the regular function handler. */ + retval = plsample_func_handler(fcinfo); + } + } + PG_FINALLY(); + { + } + PG_END_TRY(); + return retval; +} + +/* Handler for regular function and stored procedure calls. */ +static Datum +plsample_func_handler(PG_FUNCTION_ARGS) +{ + HeapTuple pl_tuple; + Datum ret; + char *source; + bool isnull; + + int i; + FmgrInfo *arg_out_func; + Form_pg_type type_struct; + HeapTuple type_tuple; + Form_pg_proc pl_struct; + volatile MemoryContext proc_cxt = NULL; + Oid *argtypes; + char **argnames; + char *argmodes; + char *value; + + Form_pg_type pg_type_entry; + Oid result_typioparam; + FmgrInfo result_in_func; + + /* Fetch the source of the function. */ + + pl_tuple = SearchSysCache(PROCOID, + ObjectIdGetDatum(fcinfo->flinfo->fn_oid), 0, 0, 0); + if (!HeapTupleIsValid(pl_tuple)) + elog(ERROR, "cache lookup failed for function %u", + fcinfo->flinfo->fn_oid); + pl_struct = (Form_pg_proc) GETSTRUCT(pl_tuple); + + ret = SysCacheGetAttr(PROCOID, pl_tuple, Anum_pg_proc_prosrc, &isnull); + if (isnull) + elog(ERROR, "null prosrc"); + ReleaseSysCache(pl_tuple); + + source = DatumGetCString(DirectFunctionCall1(textout, ret)); + elog(LOG, "source text:\n%s", source); + + arg_out_func = (FmgrInfo *) palloc0(fcinfo->nargs * sizeof(FmgrInfo)); + proc_cxt = AllocSetContextCreate(TopMemoryContext, + "PL/Sample function", 0, (1 * 1024), (8 * 1024)); + get_func_arg_info(pl_tuple, &argtypes, &argnames, &argmodes); + + /* Iterate through all of the function arguments. */ + elog(LOG, "number of arguments : %d", fcinfo->nargs); + for (i = 0; i < fcinfo->nargs; i++) + { + Oid argtype = pl_struct->proargtypes.values[i]; + type_tuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(argtype)); + if (!HeapTupleIsValid(type_tuple)) + elog(ERROR, "cache lookup failed for type %u", argtype); + + type_struct = (Form_pg_type) GETSTRUCT(type_tuple); + fmgr_info_cxt(type_struct->typoutput, &(arg_out_func[i]), proc_cxt); + ReleaseSysCache(type_tuple); + + value = OutputFunctionCall(&arg_out_func[i], fcinfo->args[i].value); + + elog(LOG, "argument position: %d; name: %s; value: %s", i, argnames[i], + value); + } + + /* Fetch the return type of the function. */ + + type_tuple = SearchSysCache1(TYPEOID, + ObjectIdGetDatum(pl_struct->prorettype)); + if (!HeapTupleIsValid(type_tuple)) + elog(ERROR, "cache lookup failed for type %u", pl_struct->prorettype); + + pg_type_entry = (Form_pg_type) GETSTRUCT(type_tuple); + + proc_cxt = AllocSetContextCreate(TopMemoryContext, "PL/Sample function", + ALLOCSET_SMALL_SIZES); + + result_typioparam = getTypeIOParam(type_tuple); + + fmgr_info_cxt(pg_type_entry->typinput, &result_in_func, proc_cxt); + ReleaseSysCache(type_tuple); + + /* Simply return the function source text. */ + ret = InputFunctionCall(&result_in_func, source, result_typioparam, -1); + PG_RETURN_DATUM(ret); +} diff --git a/src/test/modules/plsample/plsample.control b/src/test/modules/plsample/plsample.control new file mode 100644 index 0000000000..dba58e5abf --- /dev/null +++ b/src/test/modules/plsample/plsample.control @@ -0,0 +1,7 @@ +# plsample extension +comment = 'PL/Sample' +default_version = '0.1' +module_pathname = '$libdir/plsample' +relocatable = false +schema = pg_catalog +superuser = false diff --git a/src/test/modules/plsample/sql/create_func.sql b/src/test/modules/plsample/sql/create_func.sql new file mode 100644 index 0000000000..df2b915a97 --- /dev/null +++ b/src/test/modules/plsample/sql/create_func.sql @@ -0,0 +1,5 @@ +CREATE FUNCTION plsample_func(a1 NUMERIC, a2 TEXT, a3 INTEGER[]) +RETURNS TEXT +AS $$ + This is function's source text. +$$ LANGUAGE plsample; diff --git a/src/test/modules/plsample/sql/create_pl.sql b/src/test/modules/plsample/sql/create_pl.sql new file mode 100644 index 0000000000..c3ace0f1aa --- /dev/null +++ b/src/test/modules/plsample/sql/create_pl.sql @@ -0,0 +1,10 @@ +CREATE FUNCTION plsample_call_handler() +RETURNS language_handler +AS '$libdir/plsample' +LANGUAGE C; + +CREATE LANGUAGE plsample +HANDLER plsample_call_handler; + +COMMENT ON LANGUAGE plsample +IS 'PL/Sample procedural language'; diff --git a/src/test/modules/plsample/sql/select_func.sql b/src/test/modules/plsample/sql/select_func.sql new file mode 100644 index 0000000000..5ded186984 --- /dev/null +++ b/src/test/modules/plsample/sql/select_func.sql @@ -0,0 +1 @@ +SELECT plsample_func(1.23, 'abc', '{4, 5, 6}'); --+HP7ph2BbKc20aGI--