2

我尝试为 postgresql 8.3 创建一个 plpgsql 触发器,它在按 id 列插入之前自动对表进行分区

如果目标表不存在,它将被创建,并且插入到那里

所以我用这样的新表名创建了插入语句

exec_insert := 'INSERT INTO '||TG_TABLE_SCHEMA||'.'||TG_RELNAME||'_'||destinationid||' VALUES('||NEW.*||')';
EXECUTE exec_insert;

导致错误:

ERROR:  NEW used in query that is not in a rule

我有两个问题:

  1. 甚至可以在 EXECUTE 中使用 NEW 还是语句中有错误?
  2. 如果不可能,任何人都知道如何从 NEW 中获取值,以便我可以在语句中使用它们?我唯一想到的是使用 information_schema 来检索主表的列名,然后尝试动态访问 NEW 的值——我也不知道如何:(

谢谢

4

2 回答 2

5

你可以使用

...
_sql varchar(100)= 'insert into some_table values ($1.*)';
...
execute _sql using new;
...

我发现它隐藏在一些偏远地区:D。我正在使用 9.1

于 2013-07-23T07:30:18.043 回答
0

此设置在这里运行良好(版本 8.4 和 9.0):

CREATE TABLE customer
(
  id bigserial NOT NULL,
  name text,
  datecreated timestamp with time zone DEFAULT now(),
  CONSTRAINT customer_pkey PRIMARY KEY (id) USING INDEX TABLESPACE pg_default
);

CREATE OR REPLACE FUNCTION test_trigger()
  RETURNS trigger AS
$BODY$
declare
    query   text;
begin
    query := 'INSERT INTO customer_' || substring(NEW.name,1,1) || ' VALUES(' || NEW.id || ','''|| NEW.name||''')';
    EXECUTE query;

    RETURN NULL;

    EXCEPTION
        WHEN undefined_table THEN
            query := 'CREATE TABLE "customer_' || substring(NEW.name,1,1) || 
            '"( CONSTRAINT "customer_' || substring(NEW.name,1,1) ||'_name_check" CHECK (lower(substring(name, 1, 1)) = ''' || substring(NEW.name,1,1) || '''::text)
            ) INHERITS (customer); CREATE INDEX "i_customer_' || substring(NEW.name,1,1) ||'_name" ON customer_' || substring(NEW.name,1,1) ||' USING btree(name);';
            EXECUTE query;

            query := 'INSERT INTO customer_' || substring(NEW.name,1,1) || ' VALUES(' || NEW.id || ','''|| NEW.name||''')';
            EXECUTE query;

            query := 'ANALYZE "customer_' || substring(NEW.name,1,1) || '"';
            EXECUTE query;

            RETURN NULL;

end;
$BODY$
  LANGUAGE plpgsql VOLATILE STRICT
  COST 100;

CREATE TRIGGER t_customer
  BEFORE INSERT OR UPDATE OR DELETE
  ON customer
  FOR EACH ROW
  EXECUTE PROCEDURE test_trigger();

CREATE TABLE customer
(
  id bigserial NOT NULL,
  name text,
  datecreated timestamp with time zone DEFAULT now(),
  CONSTRAINT customer_pkey PRIMARY KEY (id) USING INDEX TABLESPACE pg_default
);

CREATE OR REPLACE FUNCTION test_trigger()
  RETURNS trigger AS
$BODY$
declare
    query   text;
begin
    query := 'INSERT INTO customer_' || substring(NEW.name,1,1) || ' VALUES(' || NEW.id || ','''|| NEW.name||''')';
    EXECUTE query;

    RETURN NULL;

    EXCEPTION
        WHEN undefined_table THEN
            query := 'CREATE TABLE "customer_' || substring(NEW.name,1,1) || 
            '"( CONSTRAINT "customer_' || substring(NEW.name,1,1) ||'_name_check" CHECK (lower(substring(name, 1, 1)) = ''' || substring(NEW.name,1,1) || '''::text)
            ) INHERITS (customer); CREATE INDEX "i_customer_' || substring(NEW.name,1,1) ||'_name" ON customer_' || substring(NEW.name,1,1) ||' USING btree(name);';
            EXECUTE query;

            query := 'INSERT INTO customer_' || substring(NEW.name,1,1) || ' VALUES(' || NEW.id || ','''|| NEW.name||''')';
            EXECUTE query;

            query := 'ANALYZE "customer_' || substring(NEW.name,1,1) || '"';
            EXECUTE query;

            RETURN NULL;

end;
$BODY$
  LANGUAGE plpgsql VOLATILE STRICT
  COST 100;

CREATE TRIGGER t_customer
  BEFORE INSERT OR UPDATE OR DELETE
  ON customer
  FOR EACH ROW
  EXECUTE PROCEDURE test_trigger();

INSERT INTO customer(name) VALUES ('john'), ('peter');
于 2010-07-16T09:38:28.930 回答