Re: Trigger to create string to inverse SQL statement - Mailing list pgsql-general

From Francisco Olarte
Subject Re: Trigger to create string to inverse SQL statement
Date
Msg-id CA+bJJbwyx-WsSv7oeRVwB=fKmo77PNgiJdDqYTBH5qwSgC6WZw@mail.gmail.com
Whole thread Raw
In response to Re: Trigger to create string to inverse SQL statement  (Łukasz Jarych <jaryszek@gmail.com>)
List pgsql-general
Hello:

On Thu, Mar 8, 2018 at 1:51 PM, Łukasz Jarych <jaryszek@gmail.com> wrote:
> what do you mean use external program to build inverted queries. Do you have any examples?

Please, do not top quote, or the thread will get difficult to follow fast.

That being said. Capturing a DML in a trigger is relatively easy. On
recent postgres I've seen just using json to capture the whole new and
old rows, which looks promissing.

But then, building a 'rollback' DML is difficult to do in sql or
pl-pgsql, you would normally want a more normal programming language.
i.e., I would normally turn to perl for this, having used it since the
mid 90s.

Then, you have pl-perl, but this is difficult to debug/manage. So what
I would normally do is to just capture the changes in a trigger and
then have a program which queries the log table, builds the anti-query
and executes it ( no point in keeping it, since once you execute it
there is nothing to undo ). The beauty of this is you can take a
sample from your log table and easily test the program just
implementing a debug flag which prints the queries instead of
executing ( and does not touch the log table, which I assume a real
undoer will need to fro record-keepint ).

This is assuming the log is used as an "undo log", which is what I
would assume from the very scarce information I have. And this kind of
programs normally are seldom used, so postponing the query building to
a later time and logging minimal info fast is normally better ( is
like backups, you normally make backups faster, then optimize what you
can of restores, as ideally they would never be used, or transactions,
you normally optimize for commits first, then rollbacks ).

Francisco Olarte.


pgsql-general by date:

Previous
From: Adrian Klaver
Date:
Subject: Re: What is the meaning of pg_restore output?
Next
From: Francisco Olarte
Date:
Subject: Re: Trigger to create string to inverse SQL statement