From b488c580aef4e05f39be5daaab6464da5b22a494 Mon Sep 17 00:00:00 2001 From: Alvaro Herrera Date: Mon, 11 May 2015 19:14:31 -0300 Subject: Allow on-the-fly capture of DDL event details MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This feature lets user code inspect and take action on DDL events. Whenever a ddl_command_end event trigger is installed, DDL actions executed are saved to a list which can be inspected during execution of a function attached to ddl_command_end. The set-returning function pg_event_trigger_ddl_commands can be used to list actions so captured; it returns data about the type of command executed, as well as the affected object. This is sufficient for many uses of this feature. For the cases where it is not, we also provide a "command" column of a new pseudo-type pg_ddl_command, which is a pointer to a C structure that can be accessed by C code. The struct contains all the info necessary to completely inspect and even reconstruct the executed command. There is no actual deparse code here; that's expected to come later. What we have is enough infrastructure that the deparsing can be done in an external extension. The intention is that we will add some deparsing code in a later release, as an in-core extension. A new test module is included. It's probably insufficient as is, but it should be sufficient as a starting point for a more complete and future-proof approach. Authors: Álvaro Herrera, with some help from Andres Freund, Ian Barwick, Abhijit Menon-Sen. Reviews by Andres Freund, Robert Haas, Amit Kapila, Michael Paquier, Craig Ringer, David Steele. Additional input from Chris Browne, Dimitri Fontaine, Stephen Frost, Petr Jelínek, Tom Lane, Jim Nasby, Steven Singer, Pavel Stěhule. Based on original work by Dimitri Fontaine, though I didn't use his code. Discussion: https://www.postgresql.org/message-id/m2txrsdzxa.fsf@2ndQuadrant.fr https://www.postgresql.org/message-id/20131108153322.GU5809@eldon.alvh.no-ip.org https://www.postgresql.org/message-id/20150215044814.GL3391@alvh.no-ip.org --- doc/src/sgml/event-trigger.sgml | 11 ++++- doc/src/sgml/func.sgml | 93 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 101 insertions(+), 3 deletions(-) (limited to 'doc/src') diff --git a/doc/src/sgml/event-trigger.sgml b/doc/src/sgml/event-trigger.sgml index f151eb73754..0cb31a478fa 100644 --- a/doc/src/sgml/event-trigger.sgml +++ b/doc/src/sgml/event-trigger.sgml @@ -29,7 +29,8 @@ occurs in the database in which it is defined. Currently, the only supported events are ddl_command_start, - ddl_command_end + ddl_command_end, + table_rewrite and sql_drop. Support for additional events may be added in future releases. @@ -52,7 +53,13 @@ The ddl_command_end event occurs just after the execution of - this same set of commands. + this same set of commands. To obtain more details on the DDL + operations that took place, use the set-returning function + pg_event_trigger_ddl_commands() from the + ddl_command_end event trigger code (see + ). Note that the trigger fires + after the actions have taken place (but before the transaction commits), + and thus the system catalogs can be read as already changed. diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml index fb397316048..1ee4f634d3a 100644 --- a/doc/src/sgml/func.sgml +++ b/doc/src/sgml/func.sgml @@ -18066,8 +18066,99 @@ FOR EACH ROW EXECUTE PROCEDURE suppress_redundant_updates_trigger(); see . + + Capturing Changes at Command End + + + pg_event_trigger_ddl_commands + + + + pg_event_trigger_ddl_commands returns a list of + DDL commands executed by each user action, + when invoked in a function attached to a + ddl_command_end event trigger. If called in any other + context, an error is raised. + pg_event_trigger_ddl_commands returns one row for each + base command executed; some commands that are a single SQL sentence + may return more than one row. This function returns the following + columns: + + + + + + Name + Type + Description + + + + + + classid + Oid + OID of catalog the object belongs in + + + objid + Oid + OID of the object in the catalog + + + objsubid + integer + Object sub-id (e.g. attribute number for columns) + + + command_tag + text + command tag + + + object_type + text + Type of the object + + + schema_name + text + + Name of the schema the object belongs in, if any; otherwise NULL. + No quoting is applied. + + + + object_identity + text + + Text rendering of the object identity, schema-qualified. Each and every + identifier present in the identity is quoted if necessary. + + + + in_extension + bool + whether the command is part of an extension script + + + command + pg_ddl_command + + A complete representation of the command, in internal format. + This cannot be output directly, but it can be passed to other + functions to obtain different pieces of information about the + command. + + + + + + + + - Processing objects dropped by a DDL command. + Processing Objects Dropped by a DDL Command pg_event_trigger_dropped_objects -- cgit v1.2.3