I want to be able to track the history of related records in a table. On an Oracle system, I accomplished this by creating an insert trigger that would populate a field with the original record's PK, if known, or with the new PK if the original PK wasn't specified.
In other words, given the following code, the INS_Nomination trigger would populate a new Nomination.parent_nomination_id field with the incoming parent_nomination_id, or with the value generated for the table's PK (nomination_id) if the parent_nomination_id is NULL:
/*create nomination tables*/
CREATE TABLE Nomination (
nomination_id NUMBER(12,0) NOT NULL ENABLE,
parent_nomination_id NUMBER(12,0) NOT NULL,
whatever VARCHAR2(400 BYTE),
created_by NUMBER(12,0) NOT NULL ENABLE,
created_date DATE DEFAULT SYSDATE NOT NULL ENABLE,
active_ind NUMBER(1,0) DEFAULT 1 NOT NULL ENABLE,
CONSTRAINT Nomination_PK PRIMARY KEY (nomination_id) ENABLE
);
CREATE INDEX Nomination_INDEX1 ON Nomination (parent_nomination_id, active_ind DESC) ;
CREATE UNIQUE INDEX Nomination_PK ON Nomination (nomination_id);
CREATE SEQUENCE Nomination_SEQ;
CREATE OR REPLACE TRIGGER INS_Nomination BEFORE INSERT ON Nomination FOR EACH ROW
BEGIN
SELECT Nomination_SEQ.nextval
INTO :new.nomination_id
FROM Dual;
SELECT NVL(:NEW.parent_nomination_id, :NEW.nomination_id)
INTO :NEW.parent_nomination_id
FROM Dual;
END;
/
ALTER TRIGGER INS_Nomination ENABLE;
How can I accomplish this same functionality with Postgres?
serialcolumn? And what have you tried so far? Did you see the examples in the Postgres manual? The pretty much show you how this can be done: postgresql.org/docs/current/static/plpgsql-trigger.htmlserialdeclaration on the PK. I didn't provide a snippet of my PostgreSQL because it was different table/field names than the Oracle example I had handy from a prior project. And it was 3am when I asked, so my Googling superpower was apparently diminished. All things considered, I'm fairly proud of how coherent the question comes across.