mirror of
https://github.com/postgres/postgres.git
synced 2025-08-30 06:01:21 +03:00
Event Trigger for table_rewrite
Generate a table_rewrite event when ALTER TABLE attempts to rewrite a table. Provide helper functions to identify table and reason. Intended use case is to help assess or to react to schema changes that might hold exclusive locks for long periods. Dimitri Fontaine, triggering an edit by Simon Riggs Reviewed in detail by Michael Paquier
This commit is contained in:
@@ -17607,15 +17607,23 @@ FOR EACH ROW EXECUTE PROCEDURE suppress_redundant_updates_trigger();
|
||||
<sect1 id="functions-event-triggers">
|
||||
<title>Event Trigger Functions</title>
|
||||
|
||||
<para>
|
||||
<productname>PostgreSQL</> provides these helper functions
|
||||
to retrieve information from event triggers.
|
||||
</para>
|
||||
|
||||
<para>
|
||||
For more information about event triggers,
|
||||
see <xref linkend="event-triggers">.
|
||||
</para>
|
||||
|
||||
<sect2 id="pg-event-trigger-sql-drop-functions">
|
||||
<title>Processing objects dropped by a DDL command.</title>
|
||||
|
||||
<indexterm>
|
||||
<primary>pg_event_trigger_dropped_objects</primary>
|
||||
</indexterm>
|
||||
|
||||
<para>
|
||||
Currently <productname>PostgreSQL</> provides one built-in event trigger
|
||||
helper function, <function>pg_event_trigger_dropped_objects</>.
|
||||
</para>
|
||||
|
||||
<para>
|
||||
<function>pg_event_trigger_dropped_objects</> returns a list of all objects
|
||||
dropped by the command in whose <literal>sql_drop</> event it is called.
|
||||
@@ -17709,11 +17717,72 @@ CREATE EVENT TRIGGER test_event_trigger_for_drops
|
||||
EXECUTE PROCEDURE test_event_trigger_for_drops();
|
||||
</programlisting>
|
||||
</para>
|
||||
</sect2>
|
||||
|
||||
<para>
|
||||
For more information about event triggers,
|
||||
see <xref linkend="event-triggers">.
|
||||
<sect2 id="pg-event-trigger-table-rewrite-functions">
|
||||
<title>Handling a Table Rewrite Event</title>
|
||||
|
||||
<para>
|
||||
The functions shown in
|
||||
<xref linkend="functions-event-trigger-table-rewrite">
|
||||
provide information about a table for which a
|
||||
<literal>table_rewrite</> event has just been called.
|
||||
If called in any other context, an error is raised.
|
||||
</para>
|
||||
|
||||
<table id="functions-event-trigger-table-rewrite">
|
||||
<title>Table Rewrite information</title>
|
||||
<tgroup cols="3">
|
||||
<thead>
|
||||
<row><entry>Name</entry> <entry>Return Type</entry> <entry>Description</entry></row>
|
||||
</thead>
|
||||
|
||||
<tbody>
|
||||
<row>
|
||||
<entry>
|
||||
<indexterm><primary>pg_event_trigger_table_rewrite_oid</primary></indexterm>
|
||||
<literal><function>pg_event_trigger_table_rewrite_oid()</function></literal>
|
||||
</entry>
|
||||
<entry><type>Oid</type></entry>
|
||||
<entry>The Oid of the table about to be rewritten.</entry>
|
||||
</row>
|
||||
|
||||
<row>
|
||||
<entry>
|
||||
<indexterm><primary>pg_event_trigger_table_rewrite_reason</primary></indexterm>
|
||||
<literal><function>pg_event_trigger_table_rewrite_reason()</function></literal>
|
||||
</entry>
|
||||
<entry><type>int</type></entry>
|
||||
<entry>
|
||||
The reason code(s) explaining the reason for rewriting. The exact
|
||||
meaning of the codes is release dependent.
|
||||
</entry>
|
||||
</row>
|
||||
</tbody>
|
||||
</tgroup>
|
||||
</table>
|
||||
|
||||
<para>
|
||||
The <function>pg_event_trigger_table_rewrite_oid</> function can be used
|
||||
in an event trigger like this:
|
||||
<programlisting>
|
||||
CREATE FUNCTION test_event_trigger_table_rewrite_oid()
|
||||
RETURNS event_trigger
|
||||
LANGUAGE plpgsql AS
|
||||
$$
|
||||
BEGIN
|
||||
RAISE NOTICE 'rewriting table % for reason %',
|
||||
pg_event_trigger_table_rewrite_oid()::regclass,
|
||||
pg_event_trigger_table_rewrite_reason();
|
||||
END;
|
||||
$$;
|
||||
|
||||
CREATE EVENT TRIGGER test_table_rewrite_oid
|
||||
ON table_rewrite
|
||||
EXECUTE PROCEDURE test_event_trigger_table_rewrite_oid();
|
||||
</programlisting>
|
||||
</para>
|
||||
</sect2>
|
||||
</sect1>
|
||||
|
||||
</chapter>
|
||||
|
Reference in New Issue
Block a user