Postgresql – Trigger function to update data from one table to another table with different servers in PostgreSQL

postgresqltrigger

There are two databases with different servers in PostgreSQL in PGAdmin.
One database db1 of server s1 and another database db2 of server s2.
The condition is: if the field id.table1 of db1 is same as id.table2 of db2 (of the other server) then one of the field 'status' of table2 of db2 which has its value as submit should changes to open in PostgreSQL in PGAdmin.
I need to write trigger function for this.

Best Answer

There are couple of ways to deal with this using triggers

  1. FDW wrapper as stated by a_horse_with_no_name and use PLpgSQL
  2. use an un-trusted PL language such as PLpython.

FDW: depending on the version of PG can be fast or extremely slow.

PLpython requires python language with psycopg library to be installed.

I like to use plpython as its easier to add logic if the connection between the two servers becomes broken, When the connection is broken use another table to hold onto the pending changes, then use PgAgent task to retry updating the data.