Skip site navigation (1) Skip section navigation (2)

Peripheral Links

Header And Logo

PostgreSQL
| The world's most advanced open source database.

Site Navigation

Search for
  Advanced Search

plpgsql replication stored procedure


  • From: aaron(dot)clauson(at)gmail(dot)com
  • To: pgsql-general(at)postgresql(dot)org
  • Subject: plpgsql replication stored procedure
  • Date: 17 Apr 2006 06:58:58 -0700
  • Message-id: <1145282338(dot)790460(dot)109290(at)e56g2000cwe(dot)googlegroups(dot)com>

Hi,

I'm trying to write a stored procedure that can capture all the changes
to a table and record the changes based on the table's primary key.

I can almost get there but the sticking point is being able to access
the primary key field of the NEW/OLD record in the trigger stored
procedure without knowing it's name.

The stored procedure is below and what I'm trying to do is find a way
to get the column from the NEW record that has the name
constraintColName. If I could do that I would replace NEW.oid with the
equivalent of NEW[constraintColName] and remove the need for oid's on
the table.

create or replace function replicate() returns trigger as
$$

 declare
  constraintName varchar;
  constraintColName varchar;
  keyId varchar;
  slaves record;

 begin
    select into constraintName constraint_name from
information_schema.table_constraints where table_name = TG_RELNAME and
constraint_type = 'PRIMARY KEY';
    select into constraintColName column_name from
information_schema.key_column_usage where constraint_name =
constraintName;
    -- execute 'select ' || constraintColName || ' from ' || NEW into
keyId;

    for slaves in
     select slaveid from replicationslaves
    loop
     insert into replicationentries values(default, slaves.slaveid,
TG_OP, TG_RELNAME , NEW.oid, default);
    end loop;
    return NULL;
end;$$
language 'plpgsql';

Aaron




Home | Main Index | Thread Index

Privacy Policy | PostgreSQL Archives hosted by Command Prompt, Inc. | Designed by tinysofa
Copyright © 1996 – 2008 PostgreSQL Global Development Group