Re: Add CREATE support to event triggers

From: Alvaro Herrera <alvherre(at)2ndquadrant(dot)com>
To: Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Add CREATE support to event triggers
Date: 2014-06-13 20:31:56
Message-ID: 20140613203156.GR18688@eldon.alvh.no-ip.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Here's a refreshed version of this patch. I have split it up in a
largish number of pieces, which hopefully makes it easier to understand
what is going on. First there are a number of trivial fixups which
mostly shouldn't require much of an explanation, or add functionality to
ruleutils that later patches need. I also split builtins.h to create a
new ruleutils.h header, in which there are several prototypes for
functions in ruleutils.c. (The reason for this is that a later patch
adds a bunch more prototypes which require other headers to be #included
by ruleutils.h, and it didn't seem a good idea to include those in
builtins.h. This doesn't affect much -- only 7 .c files had to #include
the new file.)

The first really interesting patch is 0014, which introduces the concept
of a "command queue" in event triggers, where DDL commands are stored as
parse nodes plus supporting data. At ddl_command_end, the user code can
call the new pg_event_trigger_get_creation_commands() function, which
causes the parse node to be deparsed and returned as a JSON blob. This
patch also introduces a function to decode the JSON blob back into a
text string, possibly after being tweaked by the user code. A bunch of
DDL commands are implemented here, such as CREATE SCHEMA, CREATE TABLE,
and the like -- the basics.

Then we have a bunch more patches which add support for more commands.
In these patches you can see how the deparsing code for a certain
command can be written. Most are pretty simple, but interesting
examples such as CREATE FUNCTION are also included. We also have some
commands that are not merely create, such as ALTER..RENAME.

Patch 23 is the next interesting one -- it adds support for ALTER TABLE.
This one is a lot more involved, because deparsing ALTER TABLE
subcommands requires adding a call to collect each subcommand in
tablecmds.c itself. In order to implement this, I modified most
subcommand functions so that they return the OID of the affected object,
or the attribute number of the affected column. For example if you add
a constraint, the OID of the constraint is returned. This OID is
collected and added to the list of subcommands. At deparse time, the
collected commands can be deparsed back into JSON blobs as usual. Since
certain commands execute AlterTable internally, I had to add
"bracketing" calls to them, which set up a pseudo-ALTER TABLE context
onto which the subcommands can be added. For instance, CREATE INDEX,
CREATE VIEW and CREATE TABLESPACE MOVE are known to do this.

... then a bunch more boring commands are implemented ...

Finally, patch 36 adds support for GRANT and REVOKE deparsing. This one
is interesting because it's completely unlike the others. Here we don't
have a single OID of an affected object. I chose to support this by
exposing the InternalGrantStmt struct (in a new aclchk.h file), which we
can deparse easily into the familiar JSON format. (Of course, in an
patch earlier in the series I also had to modify event triggers so that
grant/revoke fire them as well, which currently they do not.)

There are no docs yet. This has been tested in the bidirectional
replication suite -- it works by saving the deparsed (SQL, not JSON)
command in a replicated table; the remote side executes the command when
it receives the tuple. Commands that fail to deparse sanely cause very
visible errors.

--
Álvaro Herrera http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services

Attachment Content-Type Size
0001-gram.y-more-psprintf.patch text/x-diff 779 bytes
0002-core-use-PG_FUNCNAME_MACRO-to-avoid-stale-name.patch text/x-diff 945 bytes
0003-deparse-core-return-OID-in-CREATE-TABLE-AS.patch text/x-diff 3.0 KB
0004-deparse-core-return-OID-of-matview-in-REFRESH.patch text/x-diff 1.7 KB
0005-deparse-core-split-builtins.h-to-new-ruleutils.h.patch text/x-diff 6.7 KB
0006-deparse-core-return-the-base-type-OID-not-the-array-.patch text/x-diff 3.5 KB
0008-deparse-core-have-ALTER-TABLE-return-OIDs-and-col-of.patch text/x-diff 37.1 KB
0009-core-support-ALTER-TABLESPACE-MOVE-in-event-trigs.patch text/x-diff 2.1 KB
0010-deparse-core-event-triggers-support-GRANT-REVOKE.patch text/x-diff 1.8 KB
0011-deparse-core-add-format_type_detailed.patch text/x-diff 4.6 KB
0012-deparse-core-add-get_sequence_values.patch text/x-diff 2.1 KB
0013-deparse-core-PGDLLIMPORT-creating_extension.patch text/x-diff 751 bytes
0014-deparse-Initial-support-for-JSON-command-deparsing.patch.gz application/octet-stream 30.5 KB
0015-deparse-support-CREATE-TYPE-AS-RANGE.patch text/x-diff 4.3 KB
0016-deparse-Add-support-for-CREATE-EXTENSION.patch text/x-diff 4.3 KB
0017-deparse-add-support-for-CREATE-RULE.patch text/x-diff 6.9 KB
0018-deparse-support-ALTER-TYPE-ADD-VALUE-for-enums.patch text/x-diff 2.7 KB
0019-deparse-add-support-for-ALTER-THING-RENAME.patch text/x-diff 11.1 KB
0020-deparse-support-CREATE-DOMAIN.patch text/x-diff 2.6 KB
0021-deparse-core-enable-deparse-of-function-defaults-exp.patch text/x-diff 1.8 KB
0022-deparse-deparse-CREATE-FUNCTION.patch text/x-diff 10.6 KB
0023-deparse-initial-support-for-ALTER-TABLE.patch text/x-diff 24.0 KB
0024-deparse-Support-CREATE-OPERATOR-FAMILY.patch text/x-diff 2.5 KB
0025-deparse-Support-CREATE-CONVERSION.patch text/x-diff 2.8 KB
0026-deparse-Support-CREATE-OPERATOR-via-DefineStmt.patch text/x-diff 4.3 KB
0027-deparse-Support-CREATE-COLLATION-via-DefineStmt.patch text/x-diff 2.1 KB
0028-deparse-Support-CREATE-TEXT-SEARCH-TEMPLATE-via-Defi.patch text/x-diff 2.7 KB
0029-deparse-Support-CREATE-TEXT-SEARCH-PARSER-via-Define.patch text/x-diff 3.6 KB
0030-deparse-Support-CREATE-TEXT-SEARCH-DICTIONARY-via-De.patch text/x-diff 2.9 KB
0031-deparse-Support-CREATE-TYPE-via-DefineStmt.patch text/x-diff 6.8 KB
0032-deparse-Initial-support-for-CREATE-TEXT-SEARCH-CONFI.patch text/x-diff 4.0 KB
0033-deparse-Support-CREATE-AGGREGATE.patch text/x-diff 7.9 KB
0034-deparse-support-ALTER-THING-OWNER-TO.patch text/x-diff 2.3 KB
0035-deparse-Support-ALTER-EXTENSION-UPDATE-TO.patch text/x-diff 2.3 KB
0036-deparse-support-GRANT-REVOKE.patch text/x-diff 15.8 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2014-06-13 20:44:42 Re: ALTER TABLESPACE MOVE command tag tweak
Previous Message Alvaro Herrera 2014-06-13 19:50:50 Re: replicating DROP commands across servers