1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
|
<sect1 id="functions-event-triggers">
<title>Event Trigger Functions</title>
<para>
<productname>PostgreSQL</productname> 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-ddl-command-end-functions">
<title>Capturing Changes at Command End</title>
<indexterm>
<primary>pg_event_trigger_ddl_commands</primary>
</indexterm>
<synopsis>
<function>pg_event_trigger_ddl_commands</function> () <returnvalue>setof record</returnvalue>
</synopsis>
<para>
<function>pg_event_trigger_ddl_commands</function> 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</literal> event trigger. If called in any other
context, an error is raised.
<function>pg_event_trigger_ddl_commands</function> 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 itself</entry>
</row>
<row>
<entry><literal>objsubid</literal></entry>
<entry><type>integer</type></entry>
<entry>Sub-object ID (e.g., attribute number for a column)</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</literal>.
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
identifier included in the identity is quoted if necessary.
</entry>
</row>
<row>
<entry><literal>in_extension</literal></entry>
<entry><type>boolean</type></entry>
<entry>True if 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>
<indexterm>
<primary>pg_event_trigger_dropped_objects</primary>
</indexterm>
<synopsis>
<function>pg_event_trigger_dropped_objects</function> () <returnvalue>setof record</returnvalue>
</synopsis>
<para>
<function>pg_event_trigger_dropped_objects</function> returns a list of all objects
dropped by the command in whose <literal>sql_drop</literal> event it is called.
If called in any other context, an error is raised.
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 belonged in</entry>
</row>
<row>
<entry><literal>objid</literal></entry>
<entry><type>oid</type></entry>
<entry>OID of the object itself</entry>
</row>
<row>
<entry><literal>objsubid</literal></entry>
<entry><type>integer</type></entry>
<entry>Sub-object ID (e.g., attribute number for a column)</entry>
</row>
<row>
<entry><literal>original</literal></entry>
<entry><type>boolean</type></entry>
<entry>True if this was one of the root object(s) of the deletion</entry>
</row>
<row>
<entry><literal>normal</literal></entry>
<entry><type>boolean</type></entry>
<entry>
True if there was a normal dependency relationship
in the dependency graph leading to this object
</entry>
</row>
<row>
<entry><literal>is_temporary</literal></entry>
<entry><type>boolean</type></entry>
<entry>
True if this was a temporary object
</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 belonged in, if any; otherwise <literal>NULL</literal>.
No quoting is applied.
</entry>
</row>
<row>
<entry><literal>object_name</literal></entry>
<entry><type>text</type></entry>
<entry>
Name of the object, if the combination of schema and name can be
used as a unique identifier for the object; otherwise <literal>NULL</literal>.
No quoting is applied, and name is never schema-qualified.
</entry>
</row>
<row>
<entry><literal>object_identity</literal></entry>
<entry><type>text</type></entry>
<entry>
Text rendering of the object identity, schema-qualified. Each
identifier included in the identity is quoted if necessary.
</entry>
</row>
<row>
<entry><literal>address_names</literal></entry>
<entry><type>text[]</type></entry>
<entry>
An array that, together with <literal>object_type</literal> and
<literal>address_args</literal>, can be used by
the <function>pg_get_object_address</function> function to
recreate the object address in a remote server containing an
identically named object of the same kind.
</entry>
</row>
<row>
<entry><literal>address_args</literal></entry>
<entry><type>text[]</type></entry>
<entry>
Complement for <literal>address_names</literal>
</entry>
</row>
</tbody>
</tgroup>
</informaltable>
</para>
<para>
The <function>pg_event_trigger_dropped_objects</function> function can be used
in an event trigger like this:
<programlisting>
CREATE FUNCTION test_event_trigger_for_drops()
RETURNS event_trigger LANGUAGE plpgsql AS $$
DECLARE
obj record;
BEGIN
FOR obj IN SELECT * FROM pg_event_trigger_dropped_objects()
LOOP
RAISE NOTICE '% dropped object: % %.% %',
tg_tag,
obj.object_type,
obj.schema_name,
obj.object_name,
obj.object_identity;
END LOOP;
END;
$$;
CREATE EVENT TRIGGER test_event_trigger_for_drops
ON sql_drop
EXECUTE FUNCTION test_event_trigger_for_drops();
</programlisting>
</para>
</sect2>
<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</literal> 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 Functions</title>
<tgroup cols="1">
<thead>
<row>
<entry role="func_table_entry"><para role="func_signature">
Function
</para>
<para>
Description
</para></entry>
</row>
</thead>
<tbody>
<row>
<entry role="func_table_entry"><para role="func_signature">
<indexterm>
<primary>pg_event_trigger_table_rewrite_oid</primary>
</indexterm>
<function>pg_event_trigger_table_rewrite_oid</function> ()
<returnvalue>oid</returnvalue>
</para>
<para>
Returns the OID of the table about to be rewritten.
</para></entry>
</row>
<row>
<entry role="func_table_entry"><para role="func_signature">
<indexterm>
<primary>pg_event_trigger_table_rewrite_reason</primary>
</indexterm>
<function>pg_event_trigger_table_rewrite_reason</function> ()
<returnvalue>integer</returnvalue>
</para>
<para>
Returns a code explaining the reason(s) for rewriting. The value is
a bitmap built from the following values: <literal>1</literal>
(the table has changed its persistence), <literal>2</literal>
(default value of a column has changed), <literal>4</literal>
(a column has a new data type) and <literal>8</literal>
(the table access method has changed).
</para></entry>
</row>
</tbody>
</tgroup>
</table>
<para>
These functions 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 FUNCTION test_event_trigger_table_rewrite_oid();
</programlisting>
</para>
</sect2>
</sect1>
|