Catch table name with Trigger/Function in Postgres

Asked

Viewed 341 times

0

I am implementing a simple replication form of two tables or more. I have the table replication which has the table name to be replicated and id.

To feed the table replication I have a Rigger in each table that will be replicated by taking their information and inserting it into the table replication.

I know that in my Function I can use the OLD and NEW to take the fields from the table that fired Rigger, but I wanted, within Function (which will be run by Rigger) to take the name of the table that fired the event.

That’s possible?

  • Because it does not pass as parameter by Function? function_rep('tabela_origem');

  • @Everson could be, except at Trigger how would I pass that parameter? Remembering that who will call the function is the Trigger which in turn is triggered by a table to which I want the name.

  • @Everson I wanted something more generic like: ;CREATE TRIGGER replica AFTER INSERT
ON teste1 FOR EACH ROW 
EXECUTE PROCEDURE inserir_em_replicacao(old.nome_tabela); Ai I could run this Trigger in the database only by changing the table name in the header.

1 answer

0

Searching the documentation of postgres found how to pick up, just use the: TG_TABLE_NAME

Documentación Postgres

Browser other questions tagged

You are not signed in. Login or sign up in order to post.