Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Postgresql 触发器没有';t使用“复制自”命令激发_Postgresql_Triggers_Plpgsql - Fatal编程技术网

Postgresql 触发器没有';t使用“复制自”命令激发

Postgresql 触发器没有';t使用“复制自”命令激发,postgresql,triggers,plpgsql,Postgresql,Triggers,Plpgsql,我使用copyfrom命令填充了一个表,该命令将在汇总表中创建记录。当copy命令成功运行后,我在摘要表中看不到任何记录。谁能给我点线索?请查看下表以及存储程序:- CREATE TABLE apache_log ( log_name character varying(255), line integer, client_address character varying(255), rfc1413 character varyi

我使用copyfrom命令填充了一个表,该命令将在汇总表中创建记录。当copy命令成功运行后,我在摘要表中看不到任何记录。谁能给我点线索?请查看下表以及存储程序:-

CREATE TABLE apache_log (
        log_name character varying(255),
        line integer,
        client_address character varying(255),
        rfc1413 character varying(32),
        user_name character varying(32),
        local_time timestamp with time zone,
        log_date date,
        log_hour smallint,
        tenminute_bucket smallint,
        fiveminute_bucket smallint,
        method character varying(10),
        url character varying(8192),
        protocol character varying(10),
        status_code smallint,
        bytes_sent integer,
        referer character varying(8192),
        agent character varying(8192),
        canon_name character varying(512)
);

CREATE INDEX apache_log_local_time ON apache_log USING btree (local_time);
CREATE INDEX apache_log_client_address ON apache_log USING btree (client_address);
CREATE INDEX apache_log_user_name ON apache_log USING btree (user_name);
CREATE INDEX apache_log_canon_name ON apache_log USING btree (canon_name);
CREATE INDEX apache_log_url ON apache_log USING btree (url);
CREATE INDEX apache_log_method ON apache_log USING btree (method);
CREATE INDEX apache_log_status_code ON apache_log USING btree (status_code);
CREATE UNIQUE INDEX apache_log_name_line ON apache_log (log_name, line);

CREATE TABLE tenminute_summary (
        log_date date,
        log_hour smallint,
        bucket smallint,
        hit integer,
        bytes_sent bigint,
        status_code smallint
);

CREATE INDEX tenminute_summary_log_date_log_hour_bucket ON tenminute_summary (log_date, log_hour, bucket);
CREATE UNIQUE INDEX tenminute_summary_log_date_log_hour_bucket_status_code ON tenminute_summary (log_date, log_hour, bucket, status_code);

CREATE TABLE fiveminute_summary (
        log_date date,
        log_hour smallint,
        bucket smallint,
        hit integer,
        bytes_sent bigint,
        status_code smallint
);

CREATE INDEX fiveminute_summary_log_date_log_hour_bucket ON fiveminute_summary (log_date, log_hour, bucket);
CREATE UNIQUE INDEX fiveminute_summary_log_date_log_hour_bucket_status_code ON fiveminute_summary (log_date, log_hour, bucket, status_code);

CREATE OR REPLACE FUNCTION update_history(history_log_date date, history_log_hour smallint, history_status_code smallint, history_fiveminute_bucket smallint, history_tenminute_bucket smallint, history_fiveminute_bytes_sent bigint, history_fiveminute_hit integer, history_fiveminute_bytes_sent bigint, history_fiveminute_hit integer) RETURNS INTEGER AS
$update_history$
        BEGIN
                IF ( history_fiveminute_bucket IS NOT NULL) THEN
                        <<fiveminute_update>>
                        LOOP
                                UPDATE fiveminute_summary
                                   SET  bytes_sent = bytes_sent + history_fiveminute_bytes_sent,
                                                hit = hit + history_fiveminute_hit
                                        WHERE log_date = history_log_date AND
                                                        log_hour = history_log_hour AND
                                                        bucket = history_fiveminute_bucket AND
                                                        status_code = history_status_code;
                                EXIT fiveminute_update WHEN found;
                                BEGIN
                                        INSERT INTO fiveminute_summary (
                                                log_date,
                                                log_hour,
                                                bucket,
                                                status_code,
                                                bytes_sent,
                                                hit)
                                        VALUES (
                                                history_log_date,
                                                history_log_hour,
                                                history_fiveminute_bucket,
                                                history_status_code,
                                                history_fiveminute_bytes_sent,
                                                history_fiveminute_hit);
                                        EXIT fiveminute_update;
                                EXCEPTION
                                        WHEN UNIQUE_VIOLATION THEN
                                                -- do nothing
                                END;
                        END LOOP fiveminute_update;
                END IF;
                IF ( history_tenminute_bucket IS NOT NULL) THEN
                        <<tenminute_update>>
                        LOOP
                                UPDATE tenminute_summary
                                   SET  bytes_sent = bytes_sent + history_tenminute_bytes_sent,
                                                hit = hit + history_tenminute_hit
                                        WHERE log_date = history_log_date AND
                                                        log_hour = history_log_hour AND
                                                        bucket = history_tenminute_bucket AND
                                                        status_code = history_status_code;
                                EXIT tenminute_update WHEN found;
                                BEGIN
                                        INSERT INTO tenminute_summary (
                                                log_date,
                                                log_hour,
                                                bucket,
                                                status_code,
                                                bytes_sent,
                                                hit)
                                        VALUES (
                                                history_log_date,
                                                history_log_hour,
                                                history_tenminute_bucket,
                                                history_status_code,
                                                history_tenminute_bytes_sent,
                                                history_tenminute_hit);
                                        EXIT tenminute_update;
                                EXCEPTION
                                        WHEN UNIQUE_VIOLATION THEN
                                                -- do nothing
                                END;
                        END LOOP tenminute_update;
                END IF;
                RETURN 0;
        END;
$update_history$
LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION update_apache_log() RETURNS TRIGGER AS $update_apache_log$
        DECLARE
                history_log_date date := null;
                history_log_hour smallint := null;
                history_status_code smallint := null;
                history_fiveminute_bucket smallint := null;
                history_tenminute_bucket smallint := null;
                history_fiveminute_bytes_sent bigint := null;
                history_fiveminute_hit integer := null;
                history_tenminute_bytes_sent bigint := null;
                history_tenminute_hit integer := null;
                future_log_date date := null;
                future_log_hour smallint := null;
                future_status_code smallint := null;
                future_fiveminute_bucket smallint := null;
                future_tenminute_bucket smallint := null;
                future_fiveminute_bytes_sent bigint := null;
                future_fiveminute_hit integer := null;
                future_tenminute_bytes_sent bigint := null;
                future_tenminute_hit integer := null;
                dummy integer := 0;
        BEGIN
                IF (TG_OP = 'DELETE') THEN
                        history_log_date                                := OLD.log_date;
                        history_log_hour                                := OLD.log_hour;
                        history_fiveminute_bucket               := OLD.fiveminute_bucket;
                        history_tenminute_bucket                := OLD.tenminute_bucket;
                        history_status_code                             := OLD.status_code;
                        history_fiveminute_bytes_sent   := 0 - OLD.bytes_sent;
                        history_fiveminute_hit                  := -1;
                        history_tenminute_bytes_sent    := 0 - OLD.bytes_sent;
                        history_tenminute_hit                   := -1;
                        dummy:=update_history(history_log_date, history_log_hour, history_status_code, history_fiveminute_bucket, history_tenminute_bucket, history_fiveminute_bytes_sent, history_fiveminute_hit, history_fiveminute_bytes_sent, history_fiveminute_hit);
                        RETURN OLD;
                ELSIF (TG_OP = 'INSERT') THEN
                        NEW.log_date                                    := extract(date from NEW.log_date AT TIME ZONE 'GMT+8');
                        NEW.log_hour                                    := extract(hour from NEW.log_date AT TIME ZONE 'GMT+8');
                        NEW.fiveminute_bucket                   := floor(extract(minute from NEW.log_date AT TIME ZONE 'GMT+8') / 5);
                        NEW.tenminute_bucket                    := floor(extract(minute from NEW.log_date AT TIME ZONE 'GMT+8') / 10);
                        future_log_date                                 := NEW.log_date;
                        future_log_hour                                 := NEW.log_hour;
                        future_status_code                              := NEW.status_code;
                        future_fiveminute_bucket                := NEW.fiveminute_bucket;
                        future_tenminute_bucket                 := NEW.tenminute_bucket;
                        future_fiveminute_bytes_sent    := NEW.bytes_sent;
                        future_fiveminute_hit                   := 1;
                        future_tenminute_bytes_sent             := NEW.bytes_sent;
                        future_tenminute_hit                    := 1;
                        dummy:=update_history(future_log_date, future_log_hour, future_status_code, future_fiveminute_bucket, future_tenminute_bucket, future_fiveminute_bytes_sent, future_fiveminute_hit, future_fiveminute_bytes_sent, future_fiveminute_hit);
                        RETURN NEW;
                ELSIF (TG_OP = 'UPDATE') THEN
                        IF (NEW.log_date <> OLD.log_date) THEN
                                NEW.date                                        := extract(date from NEW.log_date AT TIME ZONE 'GMT+8');
                                NEW.hour                                        := extract(hour from NEW.log_date AT TIME ZONE 'GMT+8');
                                NEW.fiveminute_bucket           := floor(extract(minute from NEW.log_date AT TIME ZONE 'GMT+8') / 5);
                                NEW.tenminute_bucket            := floor(extract(minute from NEW.log_date AT TIME ZONE 'GMT+8') / 10);
                                history_log_date                        := OLD.log_date;
                                history_log_hour                        := OLD.log_hour;
                                history_fiveminute_bucket       := OLD.fiveminute_bucket;
                                history_tenminute_bucket        := OLD.tenminute_bucket;
                                history_status_code                     := OLD.status_code;
                                IF (OLD.status_code = NEW.status_code) THEN
                                        history_fiveminute_bytes_sent   := 0 - OLD.bytes_sent;
                                        history_fiveminute_hit                  := -1;
                                        history_tenminute_bytes_sent    := 0 - OLD.bytes_sent;
                                        history_tenminute_hit                   := -1;
                                        future_log_date                              := NEW.log_date;
                                        future_log_hour                              := NEW.log_hour;
                                        future_status_code                           := NEW.status_code;
                                        future_fiveminute_bucket                := NEW.fiveminute_bucket;
                                        future_tenminute_bucket                 := NEW.tenminute_bucket;
                                        future_fiveminute_bytes_sent    := NEW.bytes_sent;
                                        future_fiveminute_hit                   := 1;
                                        future_tenminute_bytes_sent             := NEW.bytes_sent;
                                        future_tenminute_hit                    := 1;
                                        dummy:=update_history(future_log_date, future_log_hour, future_status_code, future_fiveminute_bucket, future_tenminute_bucket, future_fiveminute_bytes_sent, future_fiveminute_hit, future_fiveminute_bytes_sent, future_fiveminute_hit);
                                ELSE
                                        IF (OLD.fiveminute_bucket = NEW.fiveminute_bucket AND OLD.log_date = NEW.log_date AND OLD.log_hour = OLD.log_hour) THEN
                                                history_fiveminute_bytes_sent   := NEW.bytes_sent - OLD.bytes_sent;
                                                history_tenminute_bytes_sent    := NEW.bytes_sent - OLD.bytes_sent;
                                                history_tenminute_hit                := 0;
                                        ELSE
                                                history_fiveminute_bytes_sent   := 0 - OLD.bytes_sent;
                                                history_fiveminute_hit               := -1;
                                                future_log_date                              := NEW.log_date;
                                                future_log_hour                              := NEW.log_hour;
                                                future_status_code                           := NEW.status_code;
                                                future_fiveminute_bucket             := NEW.fiveminute_bucket;
                                                future_fiveminute_bytes_sent    := NEW.bytes_sent;
                                                future_fiveminute_hit                := 1;
                                                IF (OLD.tenminute_bucket = NEW.tenminute_bucket) THEN
                                                        history_tenminute_bytes_sent := NEW.bytes_sent - OLD.bytes_sent;
                                                        history_tenminute_hit                := 0;
                                                ELSE
                                                        history_tenminute_bytes_sent := 0 - OLD.bytes_sent;
                                                        history_tenminute_hit                := -1;
                                                        future_tenminute_bucket              := NEW.tenminute_bucket;
                                                        future_tenminute_bytes_sent          := NEW.bytes_sent;
                                                        future_tenminute_hit                 := 1;
                                                END IF;
                                                dummy:=update_history(future_log_date, future_log_hour, future_status_code, future_fiveminute_bucket, future_tenminute_bucket, future_fiveminute_bytes_sent, future_fiveminute_hit, future_fiveminute_bytes_sent, future_fiveminute_hit);
                                        END IF;
                                END IF;
                        ELSE
                                history_log_date                                := OLD.log_date;
                                history_log_hour                                := OLD.log_hour;
                                history_status_code                             := OLD.status_code;
                                history_fiveminute_bucket               := OLD.fiveminute_bucket;
                                history_tenminute_bucket                := OLD.tenminute_bucket;
                                IF (OLD.status_code <> NEW.status_code) THEN
                                        history_fiveminute_bytes_sent   := 0 - OLD.bytes_sent;
                                        history_fiveminute_hit                  := -1;
                                        history_tenminute_bytes_sent    := 0 - OLD.bytes_sent;
                                        history_tenminute_hit                   := -1;
                                        future_log_date                              := NEW.log_date;
                                        future_log_hour                              := NEW.log_hour;
                                        future_status_code                           := NEW.status_code;
                                        future_fiveminute_bucket                := NEW.fiveminute_bucket;
                                        future_tenminute_bucket                 := NEW.tenminute_bucket;
                                        future_fiveminute_bytes_sent            := NEW.bytes_sent;
                                        future_fiveminute_hit                   := 1;
                                        future_tenminute_bytes_sent             := NEW.bytes_sent;
                                        future_tenminute_hit                    := 1;
                                        dummy:=update_history(future_log_date, future_log_hour, future_status_code, future_fiveminute_bucket, future_tenminute_bucket, future_fiveminute_bytes_sent, future_fiveminute_hit, future_fiveminute_bytes_sent, future_fiveminute_hit);
                                ELSIF (OLD.bytes_sent <> NEW.bytes_sent) THEN
                                        history_fiveminute_bytes_sent   := NEW.bytes_sent - OLD.bytes_sent;
                                        history_tenminute_bytes_sent    := NEW.bytes_sent - OLD.bytes_sent;
                                END IF;
                        END IF;
                        dummy:=update_history(history_log_date, history_log_hour, history_status_code, history_fiveminute_bucket, history_tenminute_bucket, history_fiveminute_bytes_sent, history_fiveminute_hit, history_fiveminute_bytes_sent, history_fiveminute_hit);
                        RETURN NEW;
                END IF;
                RETURN NULL;
        END;
$update_apache_log$ LANGUAGE plpgsql;

CREATE TRIGGER update_apache_log
        BEFORE INSERT OR UPDATE OR DELETE ON apache_log
        FOR EACH ROW EXECUTE PROCEDURE update_apache_log();
创建表apache\u日志(
日志名称字符变化(255),
行整数,
客户端地址字符变化(255),
rfc1413字符变化(32),
用户名字符变化(32),
带有时区的本地时间戳,
日志日期,
对数小时小整数,
十分钟,
五分钟,
方法特征变化(10),
url字符变化(8192),
协议字符变化(10),
状态代码smallint,
字节\u发送整数,
参考字符变化(8192),
代理字符变化(8192),
佳能名称字符变化(512)
);
使用btree(local_time)在apache_日志上创建索引apache_log_local_time;
使用btree(客户机地址)在apache_日志上创建索引apache_日志_客户机地址;
使用btree(user\u name)在apache\u日志上创建索引apache\u log\u user\u name;
使用btree(canon_name)在apache_日志上创建索引apache_log_canon_name;
使用btree(url)在apache_日志上创建索引apache_log_url;
使用btree(方法)在apache_日志上创建索引apache_log_方法;
使用btree(状态代码)在apache_日志上创建索引apache_日志_状态代码;
在apache_日志(log_name,line)上创建唯一索引apache_log_name_行;
创建十分钟表格摘要(
日志日期,
对数小时小整数,
小桶,
命中整数,
字节_发送bigint,
状态代码smallint
);
在十分钟摘要(日志日期、日志小时、存储桶)上创建索引十分钟摘要、日志日期、日志小时、存储桶;
在十分钟摘要上创建唯一索引十分钟摘要日志日期日志小时桶状态代码(日志日期、日志小时、桶、状态代码);
创建表五分钟摘要(
日志日期,
对数小时小整数,
小桶,
命中整数,
字节_发送bigint,
状态代码smallint
);
在五分钟摘要(日志日期、日志小时、桶)上创建索引五分钟摘要、日志日期、日志小时、桶;
在五分钟摘要上创建唯一索引五分钟摘要日志日期日志小时桶状态代码(日志日期、日志小时、桶状态代码);
创建或替换函数更新\u历史记录(历史记录\u日期日期、历史记录\u日志\u小时smallint、历史记录\u状态\u代码smallint、历史记录\u五分钟\u bucket smallint、历史记录\u十分钟\u bucket smallint、历史记录\u五分钟\u字节发送bigint、历史记录\u五分钟\u命中整数、历史记录\u五分钟\u字节发送bigint、历史记录\u五分钟\u命中整数)将整数返回为
$update_历史记录$
开始
如果(history_fiveminute_bucket不为空),则
环
更新五分钟摘要
设置字节发送=字节发送+历史记录五分钟字节发送,
命中=命中+历史记录\u五分钟\u命中
其中log_date=历史记录,log_date和
日志小时=历史日志小时和
bucket=历史\五分钟\ bucket和
状态\代码=历史\状态\代码;
找到时退出五分钟更新;
开始
插入五分钟摘要(
日志日期,
对数小时,
水桶
状态代码,
发送的字节数,
击中)
价值观(
历史日志日期,
历史日志小时,
历史五分钟,
历史\状态\代码,
发送的历史记录(五分钟字节),
历史(五分钟),;
退出五分钟更新;
例外情况
那什么时候
--无所事事
结束;
结束循环五分钟更新;
如果结束;
如果(历史\u十分钟\u存储桶不为空),则
环
更新十分钟摘要
设置字节发送=字节发送+历史记录十分钟字节发送,
命中=命中+历史记录\u十分钟\u命中
其中log_date=历史记录,log_date和
日志小时=历史日志小时和
bucket=历史\十分钟\ bucket和
状态\代码=历史\状态\代码;
找到时退出十分钟_更新;
开始
插入十分钟摘要(
日志日期,
对数小时,
水桶
ERROR: parameter name "history_fiveminute_bytes_sent" used more than once
SQL status:42P13
CREATE OR REPLACE FUNCTION update_history(
    history_log_date date, 
    history_log_hour smallint, 
    history_status_code smallint, 
    history_fiveminute_bucket smallint, 
    history_tenminute_bucket smallint, 
    history_fiveminute_bytes_sent bigint, <=== See errormessage
    history_fiveminute_hit integer,            <=== And this one as well
    history_fiveminute_bytes_sent bigint, <===
    history_fiveminute_hit integer             <===
) RETURNS INTEGER AS
RAISE NOTICE 'function X is doing something';