Re: Add CREATE support to event triggers - Mailing list pgsql-hackers

From Alvaro Herrera
Subject Re: Add CREATE support to event triggers
Date
Msg-id 20140206172005.GX10723@eldon.alvh.no-ip.org
Whole thread Raw
In response to Re: Add CREATE support to event triggers  (Tom Lane <tgl@sss.pgh.pa.us>)
Responses Re: Add CREATE support to event triggers  (Alvaro Herrera <alvherre@2ndquadrant.com>)
Re: Add CREATE support to event triggers  (Alvaro Herrera <alvherre@2ndquadrant.com>)
Re: Add CREATE support to event triggers  (Jim Nasby <jim@nasby.net>)
List pgsql-hackers
Tom Lane escribió:
> Alvaro Herrera <alvherre@2ndquadrant.com> writes:

> > One idea is to chop the typmod output string at the closing parens.
>
> +1.  The only reason timestamptypmodout works like that is that we're
> trying to match the SQL standard's spelling of the type names, and
> that committee apparently considers it an off day whenever they can't
> invent some randomly-incompatible-with-everything syntax.

Okay, I did it that way.

I also fixed the sequence OWNED BY problem simply by adding support for
ALTER SEQUENCE.  Of course, the intention is that all forms of CREATE
and ALTER are supported, but this one seems reasonable standalone
because CREATE TABLE uses it internally.

So in the attached patch I have fixed all the three problems I reported
for the previous version.

I also changed the EventTriggerStashCreatedObject() function in two
ways:

1. rename it to EventTriggerStashCommand().  This is because it will
   take ALTER commands as arguments, so the old name seemed a misnomer.
   (I guess I was initially thinking it'd only handle object creation,
   but obviously that will not work.)

2. Take ObjectType instead of ObjectClass.  I'm not really sure about
   this change; the old usage of OCLASS at the utility.c level seemed
   wrong (in particular, in the DefineStmt case it was having to
   translate the stmt->kind into ObjClass manually).  But on the other
   hand, the new usage of OBJECT_FOO requires a function to translate
   each enum value to the catalog that contains objects of that type
   (which is repetitive; the OCLASS_FOO code in dependency.c already has
   a table with that).  And also I had to add OBJECT_USER_MAPPING, which
   was missing.  I think I might end up reverting this bit, unless
   somebody sees a way to beautify the Objtype->catalog OID
   transformation.  (The bit about missing USER MAPPING stuff seems a
   bit troubling; EventTriggerSupportsObjectType wasn't aware of that
   object type.)


By way of illustration, here's the output a simple command using the
snitch() function previously posted.  You can see that the CREATE TABLE
command is expanded as three commands: CREATE SEQUENCE, CREATE TABLE,
ALTER SEQUENCE.

alvherre=# create unlogged table t1 (a serial);
NOTICE:  JSON blob: {
    "definition": [
        {
            "clause": "cache",
            "fmt": "CACHE %{value}s",
            "value": "1"
        },
        {
            "clause": "cycle",
            "fmt": "%{no}s CYCLE",
            "no": "NO"
        },
        {
            "clause": "increment_by",
            "fmt": "INCREMENT BY %{value}s",
            "value": "1"
        },
        {
            "clause": "minvalue",
            "fmt": "MINVALUE %{value}s",
            "value": "1"
        },
        {
            "clause": "maxvalue",
            "fmt": "MAXVALUE %{value}s",
            "value": "9223372036854775807"
        },
        {
            "clause": "start",
            "fmt": "START WITH %{value}s",
            "value": "1"
        },
        {
            "clause": "restart",
            "fmt": "RESTART %{value}s",
            "value": "1"
        }
    ],
    "fmt": "CREATE %{persistence}s SEQUENCE %{identity}D %{definition: }s",
    "identity": {
        "objname": "t1_a_seq",
        "schemaname": "public"
    },
    "persistence": ""
}
NOTICE:  expanded: CREATE  SEQUENCE public.t1_a_seq CACHE 1 NO CYCLE INCREMENT BY 1 MINVALUE 1 MAXVALUE
9223372036854775807START WITH 1 RESTART 1 
NOTICE:  JSON blob: {
    "fmt": "CREATE %{persistence}s TABLE %{identity}D %{if_not_exists}s (%{table_elements:, }s) %{inherits}s
%{on_commit}s%{tablespace}s",  
    "identity": {
        "objname": "t1",
        "schemaname": "public"
    },
    "if_not_exists": "",
    "inherits": {
        "fmt": "INHERITS (%{parents:, }D)",
        "parents": null,
        "present": false
    },
    "on_commit": {
        "fmt": "ON COMMIT %{on_commit_value}s",
        "on_commit_value": null,
        "present": false
    },
    "persistence": "UNLOGGED",
    "table_elements": [
        {
            "collation": {
                "fmt": "COLLATE %{name}D",
                "present": false
            },
            "coltype": {
                "is_array": false,
                "schemaname": "pg_catalog",
                "typename": "int4",
                "typmod": ""
            },
            "default": {
                "default": "nextval('t1_a_seq'::regclass)",
                "fmt": "DEFAULT %{default}s"
            },
            "fmt": "%{name}I %{coltype}T %{default}s %{not_null}s %{collation}s",
            "name": "a",
            "not_null": "NOT NULL",
            "type": "column"
        }
    ],
    "table_kind": "plain",
    "tablespace": {
        "fmt": "TABLESPACE %{tablespace}I",
        "present": false,
        "tablespace": null
    }
}
NOTICE:  expanded: CREATE UNLOGGED TABLE public.t1  (a pg_catalog.int4 DEFAULT nextval('t1_a_seq'::regclass) NOT NULL )
  
NOTICE:  JSON blob: {
    "definition": [
        {
            "clause": "owned",
            "fmt": "OWNED BY %{owner}D",
            "owner": {
                "attrname": "a",
                "objname": "t1",
                "schemaname": "public"
            }
        }
    ],
    "fmt": "ALTER SEQUENCE %{identity}D %{definition: }s",
    "identity": {
        "objname": "t1_a_seq",
        "schemaname": "public"
    }
}
NOTICE:  expanded: ALTER SEQUENCE public.t1_a_seq OWNED BY public.t1.a
CREATE TABLE

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

Attachment

pgsql-hackers by date:

Previous
From: Greg Stark
Date:
Subject: Re: extension_control_path
Next
From: Sawada Masahiko
Date:
Subject: 'dml' value for log_statement