Nada, intentos muy cortos...
Lo que intento es hacer un sistema de auditoria, que me permita guardar en una tabla el campo que se está modificando, la tabla, el tipo de operación. donde estos últimos los tomo con: TG_RELNAME y TG_OP respectivamente.
El script, proveniente de un dump completo, por sí alguien quiere probar... y ayudar:
Código:
--
-- PostgreSQL database dump
--
SET client_encoding = 'UTF8';
SET check_function_bodies = false;
SET client_min_messages = warning;
--
-- Name: SCHEMA public; Type: COMMENT; Schema: -; Owner: postgres
--
COMMENT ON SCHEMA public IS 'Standard public schema';
--
-- Name: plpgsql; Type: PROCEDURAL LANGUAGE; Schema: -; Owner:
--
CREATE PROCEDURAL LANGUAGE plpgsql;
SET search_path = public, pg_catalog;
--
-- Name: auditoria(); Type: FUNCTION; Schema: public; Owner: carbonara
--
CREATE FUNCTION auditoria() RETURNS "trigger"
AS $$
DECLARE
tabla integer;
BEGIN
tabla := column_name FROM information_schema.columns WHERE table_name = TG_RELNAME AND ordinal_position = 1;
IF (TG_OP = 'DELETE') THEN
INSERT INTO auditor (id, op, tbl) VALUES (OLD.tabla,TG_OP,TG_RELNAME);
RETURN OLD;
ELSIF (TG_OP = 'UPDATE') THEN
INSERT INTO auditor (id, op, tbl) VALUES (NEW.tabla,TG_OP,TG_RELNAME);
RETURN NEW;
ELSIF (TG_OP = 'INSERT') THEN
INSERT INTO auditor (id, op, tbl) VALUES (NEW.tabla,TG_OP,TG_RELNAME);
RETURN NEW;
END IF;
END;
$$
LANGUAGE plpgsql;
ALTER FUNCTION public.auditoria() OWNER TO carbonara;
SET default_tablespace = '';
SET default_with_oids = false;
--
-- Name: apellidos; Type: TABLE; Schema: public; Owner: carbonara; Tablespace:
--
CREATE TABLE apellidos (
apellidos_id serial NOT NULL,
apellido1 character varying(20),
apellido2 character varying(20)
);
ALTER TABLE public.apellidos OWNER TO carbonara;
--
-- Name: auditor; Type: TABLE; Schema: public; Owner: carbonara; Tablespace:
--
CREATE TABLE auditor (
id integer,
op text,
fecha timestamp without time zone DEFAULT now(),
tbl character varying
);
ALTER TABLE public.auditor OWNER TO carbonara;
--
-- Name: audit; Type: TRIGGER; Schema: public; Owner: carbonara
--
CREATE TRIGGER audit
AFTER INSERT OR DELETE OR UPDATE ON apellidos
FOR EACH ROW
EXECUTE PROCEDURE auditoria();
--
-- Name: public; Type: ACL; Schema: -; Owner: postgres
--
REVOKE ALL ON SCHEMA public FROM PUBLIC;
REVOKE ALL ON SCHEMA public FROM postgres;
GRANT ALL ON SCHEMA public TO postgres;
GRANT ALL ON SCHEMA public TO PUBLIC;
--
-- PostgreSQL database dump complete
--