本節(jié)介紹了如何使用C語言實現(xiàn)性能更高的輕量級觸發(fā)器.
主要內(nèi)容翻譯自
A Faster, Lightweight Trigger Function in C for PostgreSQL
創(chuàng)新互聯(lián)是一家專業(yè)提供阿爾山企業(yè)網(wǎng)站建設(shè),專注與成都做網(wǎng)站、網(wǎng)站設(shè)計、外貿(mào)營銷網(wǎng)站建設(shè)、H5建站、小程序制作等業(yè)務(wù)。10年已為阿爾山眾多企業(yè)、政府機構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)絡(luò)公司優(yōu)惠進行中。
已在CentOs7.x上使用源碼安裝了PG,當前用戶為PG實例的owner(pg12),已配置好環(huán)境變量,可以運行pg_config命令
[pg12@localhost demo_plus]$ whoami
pg12
[pg12@localhost ~]$ cat .bashrc
# .bashrc
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
# Uncomment the following line if you don't like systemctl's auto-paging feature:
# export SYSTEMD_PAGER=
# User specific aliases and functions
export PATH=/appdb/xdb/pg12beta1/bin:$PATH
export PGDATA=/data/pgsql/pg12db1
[pg12@localhost ~]$ pg_config --pgxs
/appdb/xdb/pg12beta1/lib/postgresql/pgxs/src/makefiles/pgxs.mk
本例的場景是希望在update/insert的時候記錄更新時間/插入時間,通常我們會使用plpgsql實現(xiàn)此需求,但這次改用C語言實現(xiàn).
數(shù)據(jù)表腳本如下:
CREATE TABLE t_demo_trig(
id int,
insert_ts timestamp,
update_ts timestamp
);
下面是C實現(xiàn)源文件
#include
#include
#include "postgres.h"
#include "utils/rel.h"
#include "executor/spi.h"
#include "commands/trigger.h"
#include "utils/fmgrprotos.h"
#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif
extern Datum demo_trig(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(demo_trig);
Datum
demo_trig(PG_FUNCTION_ARGS)
{
//從函數(shù)調(diào)用上下文中獲取觸發(fā)器(TriggerData結(jié)構(gòu)體)
TriggerData *trigdata = (TriggerData *) fcinfo->context;
//TupleDesc tupdesc;
HeapTuple tuple;//更新前的元組
HeapTuple rettuple;//更新后的元組
int attnum = 0;//屬性編號
Datum datumVal;//數(shù)據(jù)值(Datum其實是一個普通指針)
//Get the structure of the tuple in the table.
//tupdesc = trigdata->tg_relation->rd_att;
//Make sure that the function is called from a trigger
if (!CALLED_AS_TRIGGER(fcinfo))
elog(ERROR, "are you sure you are calling from trigger manager?");
//If the trigger is part of an UPDATE event
if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))//UPDATE操作
{
//attnum = SPI_fnumber(tupdesc,"update_ts");
attnum = 3;
tuple = trigdata->tg_newtuple;
}
//If the trigger is part of INSERT event
else//插入操作
{
//attnum = SPI_fnumber(tupdesc,"insert_ts");
attnum = 2;
tuple = trigdata->tg_trigtuple;
}
//Get the current timestamp using "now"
//調(diào)用函數(shù)now(),獲取當前時間
datumVal = DirectFunctionCall3(timestamp_in, CStringGetDatum("now"), ObjectIdGetDatum(InvalidOid), Int32GetDatum(-1));
//Connect to Server and modify the tuple
//使用SPI連接到數(shù)據(jù)庫,并執(zhí)行更新
SPI_connect();
rettuple = SPI_modifytuple(trigdata->tg_relation, tuple, 1, &attnum, &datumVal, NULL);
if (rettuple == NULL)
{
if (SPI_result == SPI_ERROR_ARGUMENT || SPI_result == SPI_ERROR_NOATTRIBUTE)
elog(ERROR, "SPI_result failed! SPI_ERROR_ARGUMENT or SPI_ERROR_NOATTRIBUTE");
elog(ERROR, "SPI_modifytuple failed!");
}
//收尾工作
SPI_finish(); /* don't forget say Bye to SPI mgr */
//返回更新后的元組
return PointerGetDatum(rettuple);
}
Makefile文件
[pg12@localhost demo_trgr]$ cat Makefile
MODULES = trgr
EXTENSION = trgr
DATA = trgr--0.0.1.sql
#PG_CONFIG = pg_config
#PGXS := $(shell $(PG_CONFIG) --pgxs)
#include $(PGXS)
#prefix=/appdb/xdb/pg12beta1/
ifdef USE_PGXS
PG_CONFIG = pg_config
PGXS := $(shell $(PG_CONFIG) --pgxs)
include $(PGXS)
else
subdir = contrib/demo_trgr
top_builddir = ../..
include $(top_builddir)/src/Makefile.global
include $(top_srcdir)/contrib/contrib-global.mk
endif
control文件
[pg12@localhost demo_trgr]$ cat trgr.control
comment = 'Simple number add function'
default_version = '0.0.1'
relocatable = true
module_pathname = '/appdb/pg12/pg12beta3/lib/postgresql/trgr.so'
sql安裝文件
[pg12@localhost demo_trgr]$ cat trgr--0.0.1.sql
CREATE OR REPLACE FUNCTION demop_trig() RETURNS trigger
AS 'MODULE_PATHNAME','demo_trig'
LANGUAGE C STRICT;
編譯&安裝
[pg12@localhost demo_trgr]$ make
make -C ../../src/backend generated-headers
make[1]: Entering directory `/home/pg12/source/postgresql-12beta3/src/backend'
make -C catalog distprep generated-header-symlinks
make[2]: Entering directory `/home/pg12/source/postgresql-12beta3/src/backend/catalog'
make[2]: Nothing to be done for `distprep'.
make[2]: Nothing to be done for `generated-header-symlinks'.
make[2]: Leaving directory `/home/pg12/source/postgresql-12beta3/src/backend/catalog'
make -C utils distprep generated-header-symlinks
make[2]: Entering directory `/home/pg12/source/postgresql-12beta3/src/backend/utils'
make[2]: Nothing to be done for `distprep'.
make[2]: Nothing to be done for `generated-header-symlinks'.
make[2]: Leaving directory `/home/pg12/source/postgresql-12beta3/src/backend/utils'
make[1]: Leaving directory `/home/pg12/source/postgresql-12beta3/src/backend'
gcc -std=gnu99 -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -g -O0 -DOPTIMIZER_DEBUG -g3 -gdwarf-2 -fPIC -I. -I. -I../../src/include -D_GNU_SOURCE -I/usr/include/libxml2 -c -o trgr.o trgr.c -MMD -MP -MF .deps/trgr.Po
gcc -std=gnu99 -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -g -O0 -DOPTIMIZER_DEBUG -g3 -gdwarf-2 -fPIC trgr.o -L../../src/port -L../../src/common -Wl,--as-needed -Wl,-rpath,'/appdb/pg12/pg12beta3/lib',--enable-new-dtags -shared -o trgr.so
[pg12@localhost demo_trgr]$ make install
make -C ../../src/backend generated-headers
make[1]: Entering directory `/home/pg12/source/postgresql-12beta3/src/backend'
make -C catalog distprep generated-header-symlinks
make[2]: Entering directory `/home/pg12/source/postgresql-12beta3/src/backend/catalog'
make[2]: Nothing to be done for `distprep'.
make[2]: Nothing to be done for `generated-header-symlinks'.
make[2]: Leaving directory `/home/pg12/source/postgresql-12beta3/src/backend/catalog'
make -C utils distprep generated-header-symlinks
make[2]: Entering directory `/home/pg12/source/postgresql-12beta3/src/backend/utils'
make[2]: Nothing to be done for `distprep'.
make[2]: Nothing to be done for `generated-header-symlinks'.
make[2]: Leaving directory `/home/pg12/source/postgresql-12beta3/src/backend/utils'
make[1]: Leaving directory `/home/pg12/source/postgresql-12beta3/src/backend'
/bin/mkdir -p '/appdb/pg12/pg12beta3/share/postgresql/extension'
/bin/mkdir -p '/appdb/pg12/pg12beta3/share/postgresql/extension'
/bin/mkdir -p '/appdb/pg12/pg12beta3/lib/postgresql'
/bin/install -c -m 644 ./trgr.control '/appdb/pg12/pg12beta3/share/postgresql/extension/'
/bin/install -c -m 644 ./trgr--0.0.1.sql '/appdb/pg12/pg12beta3/share/postgresql/extension/'
/bin/install -c -m 755 trgr.so '/appdb/pg12/pg12beta3/lib/postgresql/'
[pg12@localhost demo_trgr]$
創(chuàng)建觸發(fā)器
[local]:5432 pg12@testdb=# CREATE TABLE t_demo_trig(
pg12@testdb(# id int,
pg12@testdb(# insert_ts timestamp,
pg12@testdb(# update_ts timestamp
pg12@testdb(# );
CREATE TABLE
Time: 93.441 ms
[local]:5432 pg12@testdb=# create extension trgr;
CREATE EXTENSION
Time: 1.403 ms
[local]:5432 pg12@testdb=# CREATE FUNCTION demo_trig() RETURNS trigger
pg12@testdb-# AS '/appdb/pg12/pg12beta3/lib/postgresql/trgr.so'
pg12@testdb-# LANGUAGE C;
CREATE FUNCTION
Time: 2.937 ms
[local]:5432 pg12@testdb=# CREATE TRIGGER trigger_demotrgr
pg12@testdb-# BEFORE INSERT OR UPDATE ON public.t_demo_trig
pg12@testdb-# FOR EACH ROW EXECUTE PROCEDURE public.demo_trig();
CREATE TRIGGER
Time: 31.037 ms
性能對比,C實現(xiàn) VS plpgsql實現(xiàn)
[local]:5432 pg12@testdb=# drop trigger trigger_demotrgr on t_demo_trig;
DROP TRIGGER
Time: 58.935 ms
[local]:5432 pg12@testdb=# insert into t_demo_trig(id) select x from generate_series(1,1000000) as x;
INSERT 0 1000000
Time: 5063.936 ms (00:05.064)
[local]:5432 pg12@testdb=# drop trigger trigger_demotrgr on t_demo_trig;
DROP TRIGGER
Time: 58.935 ms
[local]:5432 pg12@testdb=# CREATE OR REPLACE FUNCTION demo_trig_plpgsql()
pg12@testdb-# RETURNS TRIGGER AS $$
pg12@testdb$# BEGIN
pg12@testdb$# if (TG_OP = 'UPDATE') then
pg12@testdb$# NEW.update_ts = now();
pg12@testdb$# else
pg12@testdb$# NEW.insert_ts = now();
pg12@testdb$# end if;
pg12@testdb$# RETURN NEW;
pg12@testdb$# END;
pg12@testdb$# $$ language 'plpgsql';
CREATE FUNCTION
Time: 60.053 ms
[local]:5432 pg12@testdb=#
[local]:5432 pg12@testdb=# CREATE TRIGGER trigger_demotrgr
pg12@testdb-# BEFORE INSERT OR UPDATE ON public.t_demo_trig
pg12@testdb-# FOR EACH ROW EXECUTE PROCEDURE public.demo_trig_plpgsql();
CREATE TRIGGER
Time: 0.938 ms
[local]:5432 pg12@testdb=# insert into t_demo_trig(id) select x from generate_series(1,1000000) as x;
INSERT 0 1000000
Time: 8716.367 ms (00:08.716)
5063ms(C函數(shù)) vs 8716ms(plpgsql)
A Faster, Lightweight Trigger Function in C for PostgreSQL