慢触发器使用postgresql检查主键约束

时间:2015-12-10 08:43:19

标签: postgresql inheritance triggers plpython

我正在尝试使用plpythonu触发器(check_pk)检查postgresql中继承树的主键约束。我正在使用(PostgreSQL)9.4.5。

我的问题是,当执行触发器本身最多需要5 ms时,为什么插入或更新需要至少50 ms?难道我做错了什么?有没有办法改善这个?

代码

git@github.com:collorg/oopg.git

表格:

我正在使用三个表parentchildbchildc进行测试(请参阅下面的触发器):

create table parent(
   a text primary key
);

create trigger check_pk
    before insert or update on parent
    for each row execute procedure check_pk();

create table childb(
   b text,
   primary key(a, b)
) inherits(parent);

create trigger check_pk
    before insert or update on childb
    for each row execute procedure check_pk();

create table childc(
   c text,
   primary key(a, c)
) inherits(parent);

create trigger check_pk
    before insert or update on childc
    for each row execute procedure check_pk();

测试:

insert into parent (a) values ('a')         -- OK
insert into childb (a, b) values ('a', 'a') -- FAILS
insert into childb (a, b) values ('b', 'a') -- OK
insert into parent (a) values ('b')         -- FAILS
insert into parent (a) values ('b')         -- FAILS
insert into childc (a, c) values ('b', 'a') -- FAILS
insert into childc (a, c) values ('c', 'a') -- OK
select * from parent                        -- a, b, c
update parent set a = 'b'                   -- FAILS
update childb set a = 'c'                   -- FAILS
update childb set a = 'd'                   -- OK

以下是postgresql日志的摘录(我在postgresql.conf中将log_min_duration_statement设置为10毫秒):

======== get_pk_fields(59959)
check_pk_oid: SELECT a FROM public.parent WHERE a = 'c' limit 1
CLEF DUPLIQUEE
check_pk_oid duration: 0:00:00.003948
check_pk duration: 0:00:00.004504
2015-12-10 08:53:16 CET LOG:  durée : 71.940 ms, instruction : update parent set a = 'c'

当执行check_pk触发器需要4.5 ms时,更新需要71.940 ms。

触发器:

create language plpythonu;

--
--
--

CREATE FUNCTION check_pk()
    RETURNS trigger
AS $$
from datetime import datetime
from sys import stderr

begin = datetime.now()
oid = TD['relid']
GD['td'] = TD
ok = plpy.execute(
    "SELECT check_pk_oid({})".format(oid))[0]['check_pk_oid']
stderr.write("check_pk duration: {}\n".format(datetime.now() - begin))
if not ok:
    return 'SKIP'
$$ LANGUAGE plpythonu;

--
--
--

CREATE FUNCTION check_pk_oid(integer)
  returns boolean
AS $$
"""Return False if the key is found in any of the parents."""
from datetime import datetime
from sys import stderr
from psycopg2.extensions import adapt

begin = datetime.now()
oid = args[0]
stderr.write("{} check_pk_oid({})\n".format(8*'=', oid))
TD = GD['td']
stderr.write("GD['td'] = {}\n".format(TD))
parent_oid = plpy.execute(
    "SELECT get_inhparent('{}')".format(oid))[0]['get_inhparent']
stderr.write("oid du parent {}\n".format(parent_oid))
if parent_oid:
   # recurse on parent_oid
   query = ("SELECT check_pk_oid({})".format(parent_oid))
   stderr.write("check uid request: {}\n".format(query))
   return plpy.execute(query)[0]['check_pk_oid']
# Get the FQTN and the field names of the primary key
pk_infos = plpy.execute(
   "SELECT get_pk_fields({})".format(oid))[0]['get_pk_fields']
fqtn, pk_fieldnames = pk_infos[0], pk_infos[1:]
if not pk_fieldnames:
   stderr.write(
       "check_pk_oid duration ok 1: {}\n".format(datetime.now() - begin))
   return True

# Clause for the SELECT request
fields = []
clause = []
for field in pk_fieldnames:
   fields.append(field)
   if TD['new'][field] == 0:
     valeur = 0
   else:
     valeur = TD['new'][field] or ""
     valeur = adapt(valeur)
   clause.append("{} = {}".format(field, str(valeur)))

# construction de la requête d''extraction
req = "SELECT {} FROM {} WHERE {} limit 1".format(
    ', '.join(fields), fqtn, ' and '.join(clause))
stderr.write("check_pk_oid: {}\n".format(req))
if len(plpy.execute(req)) == 1:
    stderr.write("CLEF DUPLIQUEE\n")
    stderr.write("check_pk_oid duration: {}\n".format(datetime.now() - begin))
    return False

stderr.write("check_pk_oid duration ok 2: {}\n".format(datetime.now() - begin))
return True
$$ LANGUAGE plpythonu;

--
--
--

CREATE FUNCTION get_inhparent(integer)
    RETURNS integer
AS $$
from sys import stderr
relid = args[0]
stderr.write("{} get_inhparent({})\n".format(8*'=', relid))
query = (
    "SELECT inhparent FROM pg_catalog.pg_inherits WHERE inhrelid = {}".format(
    relid))
stderr.write('get_inhparent: {}\n'.format(query))
rec = plpy.execute(query)
try:
   return rec[0]['inhparent']
except:
   return 0
$$ LANGUAGE plpythonu;

--
--
--

CREATE FUNCTION get_pk_fields(oid)
    RETURNS varchar[]
AS $$
"""
Return the field names in the primary key
"""
from sys import stderr
oid = args[0]
stderr.write("{} get_pk_fields({})\n".format(8*'=', oid))
# rec_st : record contenant schemaname et relname
rec_st = plpy.execute(
   """SELECT schemaname, relname 
      FROM pg_catalog.pg_stat_all_tables
      WHERE relid = {}""".format(oid))
schemaname = rec_st[0]['schemaname']
relname = rec_st[0]['relname']
l_fieldnames = plpy.execute(
   """
SELECT
    a.attrelid AS tableid,
    c.relkind AS tablekind,
    n.nspname::varchar AS schemaname,
    c.relname::varchar AS relationname,
    array_agg(distinct i.inhparent) as parent,
    array_agg(a.attname::varchar) AS fieldnames,
    array_agg(a.attnum) as attnums,
    array_agg(a.attislocal) AS local,
    cn_pk.contype AS pkey
FROM
    pg_class c -- table
    LEFT JOIN pg_namespace n ON
    c.relname = '{}' and
    n.oid = c.relnamespace and
    n.nspname = '{}'
    LEFT JOIN pg_inherits i ON
    i.inhrelid = c.oid
    LEFT JOIN pg_attribute a ON
    a.attrelid = c.oid
    JOIN pg_type pt ON
    a.atttypid = pt.oid
--    LEFT JOIN pg_constraint cn_uniq ON
--    cn_uniq.contype = 'u' AND
--    cn_uniq.conrelid = a.attrelid AND
--    a.attnum = ANY( cn_uniq.conkey )
    JOIN pg_constraint cn_pk ON
    cn_pk.contype = 'p' AND
    cn_pk.conrelid = a.attrelid AND
    a.attnum = ANY( cn_pk.conkey )
WHERE
    n.nspname <> 'pg_catalog'::name AND
    n.nspname <> 'information_schema'::name AND
    ( c.relkind = 'r'::"char" )
GROUP BY
    a.attrelid,
    c.relkind,
    n.nspname,
    c.relname,
    cn_pk.contype""".format(relname, schemaname))[0]['fieldnames']
fqtn = "{}.{}".format(schemaname, relname)
return [fqtn] + l_fieldnames
fieldnames = ','.join(l_fieldnames)
resultat = fqtn + ":" + fieldnames
stderr.write("{}\n".format(resultat))
return resultat
$$ LANGUAGE plpythonu;

1 个答案:

答案 0 :(得分:0)

我得到了答案。 plpython非常适合原型设计,但需要付出代价。如果我使用这个基本上什么都不做的触发器,插入时间大约是30毫秒......所以我想如果我想要更好的演奏,我必须在C中编码。

create language plpythonu;

CREATE OR REPLACE FUNCTION slow()
    RETURNS trigger
AS $$
pass
$$ LANGUAGE plpythonu;

注意:此处发布的代码已过时且有错误(没有多重继承,......)但它会继续在https://github.com/collorg/oopg上发展。