Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc1233c8

Browse files
committed
>>This patch adds another plpgsql trigger example to the chapter. It uses
>>the emp table again, but shows how to audit changes into another table>>(emp_audit).Mark Kirkwood
1 parentcf52f83 commitc1233c8

File tree

1 file changed

+65
-1
lines changed

1 file changed

+65
-1
lines changed

‎doc/src/sgml/plpgsql.sgml

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/plpgsql.sgml,v 1.49 2004/11/15 06:32:14 neilc Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/plpgsql.sgml,v 1.50 2004/12/03 17:12:09 momjian Exp $
33
-->
44

55
<chapter id="plpgsql">
@@ -2556,6 +2556,70 @@ $emp_stamp$ LANGUAGE plpgsql;
25562556
CREATE TRIGGER emp_stamp BEFORE INSERT OR UPDATE ON emp
25572557
FOR EACH ROW EXECUTE PROCEDURE emp_stamp();
25582558
</programlisting>
2559+
2560+
2561+
</example>
2562+
2563+
<para>
2564+
Another way to log changes to a table involves creating a new table that
2565+
holds a row for each insert, update, delete that occurs. This approach can
2566+
be thought of as auditing changes to a table.
2567+
</para>
2568+
2569+
<para>
2570+
<xref linkend="plpgsql-trigger-audit-example"> shows an example of an
2571+
audit trigger procedure in <application>PL/pgSQL</application>.
2572+
</para>
2573+
2574+
<example id="plpgsql-trigger-audit-example">
2575+
<title>A <application>PL/pgSQL</application> Trigger Procedure For Auditing</title>
2576+
2577+
<para>
2578+
This example trigger ensures that any insert, update or delete of a row
2579+
in the emp table is recorded (i.e. audited) in the emp_audit table.
2580+
The current time and user name are stamped into the row, together with
2581+
the type of operation performed on it.
2582+
</para>
2583+
2584+
<programlisting>
2585+
CREATE TABLE emp (
2586+
empname text NOT NULL,
2587+
salary integer
2588+
);
2589+
2590+
CREATE TABLE emp_audit(
2591+
operation char(1) NOT NULL,
2592+
stamp timestamp NOT NULL,
2593+
userid text NOT NULL,
2594+
empname text NOT NULL,
2595+
salary integer
2596+
);
2597+
2598+
CREATE OR REPLACE FUNCTION process_emp_audit() RETURNS TRIGGER AS $emp_audit$
2599+
BEGIN
2600+
--
2601+
-- Create a row in emp_audit to reflect the operation performed on emp,
2602+
-- make use of the special variable TG_OP to work out the operation.
2603+
--
2604+
IF (TG_OP = 'DELETE') THEN
2605+
INSERT INTO emp_audit SELECT 'D', now(), user, OLD.*;
2606+
RETURN OLD;
2607+
ELSIF (TG_OP = 'UPDATE') THEN
2608+
INSERT INTO emp_audit SELECT 'U', now(), user, NEW.*;
2609+
RETURN NEW;
2610+
ELSIF (TG_OP = 'INSERT') THEN
2611+
INSERT INTO emp_audit SELECT 'I', now(), user, NEW.*;
2612+
RETURN NEW;
2613+
END IF;
2614+
END;
2615+
$emp_audit$ language plpgsql;
2616+
2617+
2618+
CREATE TRIGGER emp_audit
2619+
AFTER INSERT OR UPDATE OR DELETE ON emp
2620+
FOR EACH ROW EXECUTE PROCEDURE process_emp_audit()
2621+
;
2622+
</programlisting>
25592623
</example>
25602624
</sect1>
25612625

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp