如何将PL/CTL语言安装到PostgreSQL数据库8.1.22中



嗨,我正在使用postgresql 8.1.22,我正在尝试使用以下函数设置postgresql审核。

CREATE OR REPLACE FUNCTION audit.if_modified_func() RETURNS TRIGGER AS $body$
DECLARE
    v_old_data TEXT;
    v_new_data TEXT;
BEGIN
    /*  If this actually for real auditing (where you need to log EVERY action),
        then you would need to use something like dblink or plperl that could log outside the transaction,
        regardless of whether the transaction committed or rolled back.
    */
    /* This dance with casting the NEW and OLD values to a ROW is not necessary in pg 9.0+ */
    IF (TG_OP = 'UPDATE') THEN
        v_old_data := ROW(OLD.*);
        v_new_data := ROW(NEW.*);
        INSERT INTO audit.logged_actions (schema_name,table_name,user_name,action,original_data,new_data,query) 
        VALUES (TG_TABLE_SCHEMA::TEXT,TG_TABLE_NAME::TEXT,session_user::TEXT,substring(TG_OP,1,1),v_old_data,v_new_data, current_query());
        RETURN NEW;
    ELSIF (TG_OP = 'DELETE') THEN
        v_old_data := ROW(OLD.*);
        INSERT INTO audit.logged_actions (schema_name,table_name,user_name,action,original_data,query)
        VALUES (TG_TABLE_SCHEMA::TEXT,TG_TABLE_NAME::TEXT,session_user::TEXT,substring(TG_OP,1,1),v_old_data, current_query());
        RETURN OLD;
    ELSIF (TG_OP = 'INSERT') THEN
        v_new_data := ROW(NEW.*);
        INSERT INTO audit.logged_actions (schema_name,table_name,user_name,action,new_data,query)
        VALUES (TG_TABLE_SCHEMA::TEXT,TG_TABLE_NAME::TEXT,session_user::TEXT,substring(TG_OP,1,1),v_new_data, current_query());
        RETURN NEW;
    ELSE
        RAISE WARNING '[AUDIT.IF_MODIFIED_FUNC] - Other action occurred: %, at %',TG_OP,now();
        RETURN NULL;
    END IF;
EXCEPTION
    WHEN data_exception THEN
        RAISE WARNING '[AUDIT.IF_MODIFIED_FUNC] - UDF ERROR [DATA EXCEPTION] - SQLSTATE: %, SQLERRM: %',SQLSTATE,SQLERRM;
        RETURN NULL;
    WHEN unique_violation THEN
        RAISE WARNING '[AUDIT.IF_MODIFIED_FUNC] - UDF ERROR [UNIQUE] - SQLSTATE: %, SQLERRM: %',SQLSTATE,SQLERRM;
        RETURN NULL;
    WHEN OTHERS THEN
        RAISE WARNING '[AUDIT.IF_MODIFIED_FUNC] - UDF ERROR [OTHER] - SQLSTATE: %, SQLERRM: %',SQLSTATE,SQLERRM;
        RETURN NULL;
END;
$body$
LANGUAGE plpgsql
SECURITY DEFINER

但是,如果您在上面的函数中观察到current_query()并没有附带上述语言plpgsql。它抛出了一些错误。当我在谷歌上搜索时,我发现为了使用current_query()函数,必须安装PL/CTL语言。我尝试安装如下所述。它抛出了一个错误。因此,请帮助我如何将PL/CTL语言安装到我的数据库中,以便current_query()函数能够在中工作

-bash-3.2$ createlang -d dbname pltcl
createlang: language installation failed: ERROR:  could not access file "$libdir/pltcl": No such file or directory

好吧,正如你所建议的,我创建了current_query()函数,但这次我得到了这样的东西,我所做的是,

CREATE TABLE phonebook(phone VARCHAR(32), firstname VARCHAR(32), lastname VARCHAR(32), address VARCHAR(64));

CREATE TRIGGER phonebook_auditt AFTER INSERT OR UPDATE OR DELETE ON phonebook 
    FOR EACH ROW EXECUTE PROCEDURE audit.if_modified_func();

INSERT INTO phonebook(phone, firstname, lastname, address) VALUES('9966888200', 'John', 'Doe', 'North America'); 

为了测试该函数,我创建了一个名为phonebook的表,并创建了触发器,以便在插入、更新或删除后执行上面提到的函数audit.if_modified_func(

WARNING:  [AUDIT.IF_MODIFIED_FUNC] - UDF ERROR [OTHER] - SQLSTATE: 42703, SQLERRM: column "*" not found in data type phonebook
Query returned successfully: 1 rows affected, 10 ms execution time.

请告诉我我能做些什么来消除上面的错误。

不确定在哪里找到了关于current_query和pltcl的信息。这些都是不相关的。找不到pltcl的原因很简单,因为你使用的PostgreSQL太旧了。current_query()已在8.4版中添加到Pg中。

你使用这么旧的版本有什么特别的原因吗?它不再受支持,并且缺少近8年的附加功能!

如果必须使用8.1,您可能需要定义:

create function current_query() returns text as '
select current_query from pg_stat_activity where procpid = pg_backend_pid();
' language sql;

但仅仅升级是个好主意。

至于编辑和添加的第二个问题,第8.1页很可能不能使用"row.*"结构。找到谁写了"舞蹈评论"的原始代码,并询问它。也许它是为了在新的PG中工作。

相关内容

  • 没有找到相关文章

最新更新