1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-30 11:03:19 +03:00

Allow on-the-fly capture of DDL event details

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
This commit is contained in:
Alvaro Herrera
2015-05-11 19:14:31 -03:00
parent fa2642438f
commit b488c580ae
72 changed files with 2667 additions and 155 deletions

View File

@ -29,7 +29,8 @@
occurs in the database in which it is defined. Currently, the only
supported events are
<literal>ddl_command_start</>,
<literal>ddl_command_end</>
<literal>ddl_command_end</>,
<literal>table_rewrite</>
and <literal>sql_drop</>.
Support for additional events may be added in future releases.
</para>
@ -52,7 +53,13 @@
<para>
The <literal>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 <acronym>DDL</>
operations that took place, use the set-returning function
<literal>pg_event_trigger_ddl_commands()</> from the
<literal>ddl_command_end</> event trigger code (see
<xref linkend="functions-event-triggers">). 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.
</para>
<para>

View File

@ -18066,8 +18066,99 @@ FOR EACH ROW EXECUTE PROCEDURE suppress_redundant_updates_trigger();
see <xref linkend="event-triggers">.
</para>
<sect2 id="pg-event-trigger-ddl-command-end-functions">
<title>Capturing Changes at Command End</title>
<indexterm>
<primary>pg_event_trigger_ddl_commands</primary>
</indexterm>
<para>
<function>pg_event_trigger_ddl_commands</> returns a list of
<acronym>DDL</acronym> commands executed by each user action,
when invoked in a function attached to a
<literal>ddl_command_end</> event trigger. If called in any other
context, an error is raised.
<function>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:
<informaltable>
<tgroup cols="3">
<thead>
<row>
<entry>Name</entry>
<entry>Type</entry>
<entry>Description</entry>
</row>
</thead>
<tbody>
<row>
<entry><literal>classid</literal></entry>
<entry><type>Oid</type></entry>
<entry>OID of catalog the object belongs in</entry>
</row>
<row>
<entry><literal>objid</literal></entry>
<entry><type>Oid</type></entry>
<entry>OID of the object in the catalog</entry>
</row>
<row>
<entry><literal>objsubid</literal></entry>
<entry><type>integer</type></entry>
<entry>Object sub-id (e.g. attribute number for columns)</entry>
</row>
<row>
<entry><literal>command_tag</literal></entry>
<entry><type>text</type></entry>
<entry>command tag</entry>
</row>
<row>
<entry><literal>object_type</literal></entry>
<entry><type>text</type></entry>
<entry>Type of the object</entry>
</row>
<row>
<entry><literal>schema_name</literal></entry>
<entry><type>text</type></entry>
<entry>
Name of the schema the object belongs in, if any; otherwise <literal>NULL</>.
No quoting is applied.
</entry>
</row>
<row>
<entry><literal>object_identity</literal></entry>
<entry><type>text</type></entry>
<entry>
Text rendering of the object identity, schema-qualified. Each and every
identifier present in the identity is quoted if necessary.
</entry>
</row>
<row>
<entry><literal>in_extension</literal></entry>
<entry><type>bool</type></entry>
<entry>whether the command is part of an extension script</entry>
</row>
<row>
<entry><literal>command</literal></entry>
<entry><type>pg_ddl_command</type></entry>
<entry>
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.
</entry>
</row>
</tbody>
</tgroup>
</informaltable>
</para>
</sect2>
<sect2 id="pg-event-trigger-sql-drop-functions">
<title>Processing objects dropped by a DDL command.</title>
<title>Processing Objects Dropped by a DDL Command</title>
<indexterm>
<primary>pg_event_trigger_dropped_objects</primary>