pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Paul Jungwirth <pj@illuminatedcomputing.com>
To: pgsql-general@lists.postgresql.org
Subject: Re: Trigger (or something similar) on table rename?
Date: Thu, 15 Feb 2018 11:08:43 -0800
Message-ID: <261abb38-4c77-becf-fc1e-8bd7eee1445b@illuminatedcomputing.com> (raw)
In-Reply-To: <CAD3a31X1VJgH1zE5GuVL0Ws03y7UEHe5mQxar5gv0n3gCJ=Wng@mail.gmail.com>
References: <CAD3a31X1VJgH1zE5GuVL0Ws03y7UEHe5mQxar5gv0n3gCJ=Wng@mail.gmail.com>

On 02/15/2018 10:52 AM, Ken Tanzer wrote:
> Hi.  I'm wondering about possibilities for taking action when a table is 
> renamed.

I've looked into this a bit. Here is what I understand:

Since 9.3 Postgres has had "event triggers" which can run code on DDL 
events 
(https://www.postgresql.org/docs/current/static/event-triggers.html). 
There are events like `ddl_command_start` and tags like `ALTER TABLE`. 
So you could write a trigger that fires `ON ddl_command_start WHEN TAG 
IN ('ALTER TABLE')`.

Unfortunately I don't think you can get the old/new table name from 
inside the trigger function. If you need that, you might take a look at 
this extension which adds some custom event triggers with ways of 
getting that information:

https://github.com/CartoDB/pg_schema_triggers

If you are really adventurous you could even look at using the 
ProcessUtility hook directly to do what you need.

I'm looking forward to seeing what others say here because I'd like to 
know more myself!

Good luck!

-- 
Paul              ~{:-)
pj@illuminatedcomputing.com




view thread (6+ messages)  latest in thread

Message-ID: <261abb38-4c77-becf-fc1e-8bd7eee1445b@illuminatedcomputing.com>
Permalink:  ../261abb38-4c77-becf-fc1e-8bd7eee1445b@illuminatedcomputing.com/
Also on:    postgresql.org/message-id/261abb38-4c77-becf-fc1e-8bd7eee1445b@illuminatedcomputing.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: pj@illuminatedcomputing.com
  Subject: Re: Trigger (or something similar) on table rename?
  In-Reply-To: <261abb38-4c77-becf-fc1e-8bd7eee1445b@illuminatedcomputing.com>
  References: <CAD3a31X1VJgH1zE5GuVL0Ws03y7UEHe5mQxar5gv0n3gCJ=Wng@mail.gmail.com> <261abb38-4c77-becf-fc1e-8bd7eee1445b@illuminatedcomputing.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox