Re: Why no INSTEAD OF triggers on tables?

Lists: pgsql-hackers
From: Josh Berkus <josh(at)agliodbs(dot)com>
To: PostgreSQL-development <pgsql-hackers(at)postgreSQL(dot)org>
Subject: Why no INSTEAD OF triggers on tables?
Date: 2013-12-17 00:16:01
Message-ID: 52AF97C1.8060303@agliodbs.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Hackers,

I've looked in the archives, but I can't find a reason why INSTEAD OF
triggers were never enabled for tables. I'm interested in them in order
to return a rowcount to JDBC for INSERTs into partitioned tables.

Was there a technical obstacle, or is this just a TUIT issue?

--
Josh Berkus
PostgreSQL Experts Inc.
http://pgexperts.com


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Josh Berkus <josh(at)agliodbs(dot)com>
Cc: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Why no INSTEAD OF triggers on tables?
Date: 2013-12-17 00:22:35
Message-ID: 19020.1387239755@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Josh Berkus <josh(at)agliodbs(dot)com> writes:
> I've looked in the archives, but I can't find a reason why INSTEAD OF
> triggers were never enabled for tables.

What would that mean exactly? And how would you do the actual update
when it came time to?

> Was there a technical obstacle, or is this just a TUIT issue?

It sounds more like a requestor-hasnt-thought-it-through issue.

regards, tom lane


From: Josh Berkus <josh(at)agliodbs(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Why no INSTEAD OF triggers on tables?
Date: 2013-12-17 02:16:24
Message-ID: 52AFB3F8.5080307@agliodbs.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On 12/16/2013 04:22 PM, Tom Lane wrote:
> Josh Berkus <josh(at)agliodbs(dot)com> writes:
>> I've looked in the archives, but I can't find a reason why INSTEAD OF
>> triggers were never enabled for tables.
>
> What would that mean exactly? And how would you do the actual update
> when it came time to?

Well, I'm specifically thinking of master partition tables. In that
case, we really want an INSTEAD OF trigger.

It seems a little silly that I need to create a separate view, and then
an INSTEAD OF trigger on the view, in order to get a rows-updated count
back from an INSERT which hits a partitioned table.

--
Josh Berkus
PostgreSQL Experts Inc.
http://pgexperts.com


From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Josh Berkus <josh(at)agliodbs(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Why no INSTEAD OF triggers on tables?
Date: 2013-12-17 03:53:08
Message-ID: CA+TgmoZPkiq8H0Jn2f8BrTzSwu0nCpzVWaiq-=zd7X8tVW8hRg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Mon, Dec 16, 2013 at 9:16 PM, Josh Berkus <josh(at)agliodbs(dot)com> wrote:
> On 12/16/2013 04:22 PM, Tom Lane wrote:
>> Josh Berkus <josh(at)agliodbs(dot)com> writes:
>>> I've looked in the archives, but I can't find a reason why INSTEAD OF
>>> triggers were never enabled for tables.
>>
>> What would that mean exactly? And how would you do the actual update
>> when it came time to?
>
> Well, I'm specifically thinking of master partition tables. In that
> case, we really want an INSTEAD OF trigger.

/me scratches head.

So, put a BEFORE trigger, and make it return NULL. Same effect,
different notation.

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company


From: Marko Tiikkaja <marko(at)joh(dot)to>
To: Robert Haas <robertmhaas(at)gmail(dot)com>, Josh Berkus <josh(at)agliodbs(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Why no INSTEAD OF triggers on tables?
Date: 2013-12-17 08:27:37
Message-ID: 52B00AF9.6040700@joh.to
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On 12/17/13, 4:53 AM, Robert Haas wrote:
>> Well, I'm specifically thinking of master partition tables. In that
>> case, we really want an INSTEAD OF trigger.
>
> /me scratches head.
>
> So, put a BEFORE trigger, and make it return NULL. Same effect,
> different notation.

But it's not the same effect at all, that's the point:

=# create view foov as select 1 as a;
CREATE VIEW

=# create function insteadof() returns trigger as $$
$# begin
$# -- INSERT here
$# return new;
$# end
$# $$ language plpgsql;
CREATE FUNCTION

=# create function before() returns trigger as $$
$# begin
$# -- INSERT here
$# return null;
$# end
$# $$ language plpgsql;
CREATE FUNCTION

=# create trigger t1 instead of insert on foov for each row execute
procedure insteadof();
CREATE TRIGGER

=# create trigger t2 before insert on bart for each row execute
procedure before();
CREATE TRIGGER

=# insert into foov values (1) returning *;
a
---
1
(1 row)

INSERT 0 1

local:marko=#* insert into bart values (1) returning *;
a
---
(0 rows)

INSERT 0 0

Regards,
Marko Tiikkaja


From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Marko Tiikkaja <marko(at)joh(dot)to>
Cc: Josh Berkus <josh(at)agliodbs(dot)com>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Why no INSTEAD OF triggers on tables?
Date: 2013-12-17 14:33:36
Message-ID: CA+TgmoaHOBpJz=KyirUg4LAwoK_11estxW3i3x=veQFHE1T6xQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Tue, Dec 17, 2013 at 3:27 AM, Marko Tiikkaja <marko(at)joh(dot)to> wrote:
> On 12/17/13, 4:53 AM, Robert Haas wrote:
>>>
>>> Well, I'm specifically thinking of master partition tables. In that
>>> case, we really want an INSTEAD OF trigger.
>>
>> /me scratches head.
>>
>> So, put a BEFORE trigger, and make it return NULL. Same effect,
>> different notation.
>
>
> But it's not the same effect at all, that's the point:
>
> =# create view foov as select 1 as a;
> CREATE VIEW
>
> =# create function insteadof() returns trigger as $$
> $# begin
> $# -- INSERT here
> $# return new;
> $# end
> $# $$ language plpgsql;
> CREATE FUNCTION
>
> =# create function before() returns trigger as $$
> $# begin
> $# -- INSERT here
> $# return null;
> $# end
> $# $$ language plpgsql;
> CREATE FUNCTION
>
> =# create trigger t1 instead of insert on foov for each row execute
> procedure insteadof();
> CREATE TRIGGER
>
> =# create trigger t2 before insert on bart for each row execute procedure
> before();
> CREATE TRIGGER
>
> =# insert into foov values (1) returning *;
> a
> ---
> 1
> (1 row)
>
> INSERT 0 1
>
> local:marko=#* insert into bart values (1) returning *;
> a
> ---
> (0 rows)
>
> INSERT 0 0

Ah, interesting point. I didn't realize it worked like that. That
does seem like a mighty useful thing to be able to do.

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company


From: Josh Berkus <josh(at)agliodbs(dot)com>
To: Robert Haas <robertmhaas(at)gmail(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Why no INSTEAD OF triggers on tables?
Date: 2013-12-17 17:42:44
Message-ID: 52B08D14.8080100@agliodbs.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On 12/16/2013 07:53 PM, Robert Haas wrote:
> So, put a BEFORE trigger, and make it return NULL. Same effect,
> different notation.

NOT the same:

Master partition table with BEFORE trigger:

josh=# insert into a ( id, val ) values ( 23, 'test' ), ( 24, 'test'),
(25,'test');

INSERT 0 0
^^^

View with INSTEAD OF trigger:

josh=# insert into a_v ( id, val ) values ( 23, 'test' ), ( 24, 'test'),
(25,'test');

INSERT 0 3
^^^

The difference here is that the INSTEAD OF trigger returns a
rows-affected count, and the BEFORE trigger does not (it returns 0).
Some drivers and ORMs, most notably Hibernate, check this rows-returned
count, and error if they don't match the rows sent.

--
Josh Berkus
PostgreSQL Experts Inc.
http://pgexperts.com