pg.ddx.io pgsql-hackers@postgresql.org mailing list archive
help / color / mirror / Atom feedFix bug in handling of dropped columns in pltcl triggers
6+ messages / 3 participants
[nested] [flat]
* Fix bug in handling of dropped columns in pltcl triggers
@ 2016-10-31 19:17 Jim Nasby <Jim.Nasby@BlueTreble.com>
0 siblings, 1 reply; 6+ messages in thread
From: Jim Nasby @ 2016-10-31 19:17 UTC (permalink / raw)
To: pgsql-hackers; +Cc: Karl Lehenbauer <karl@flightaware.com>
While reviewing code coverage in pltcl, I uncovered a bug in trigger
function return handling. If you returned the munged name of a dropped
column, that would silently be ignored. It would be unusual to hit this,
since dropped columns end up with names like
".......pg.dropped.2.......", but since that's still a legitimate name
for a column silently ignoring it seems rather bogus.
Work sponsored by FlightAware.
https://github.com/postgres/postgres/compare/master...decibel:tcl_dropped
--
Jim Nasby, Data Architect, Blue Treble Consulting, Austin TX
Experts in Analytics, Data Architecture and PostgreSQL
Data in Trouble? Get it in Treble! http://BlueTreble.com
855-TREBLE2 (855-873-2532) mobile: 512-569-9461
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers
Attachments:
pltcl_trigger_return.patch (text/plain, 5.6K) download
commit 8f88fda52751f88aca4786f45d3d7f16a5343fc0
Author: Jim Nasby <Jim.Nasby@BlueTreble.com>
Date: Mon Oct 31 14:11:43 2016 -0500
Fix trigger dropped column handling
Previously, if a trigger returned a column that had been dropped, using the munged column name,
no error would be thrown. Since dropping a column does forcibly overwrite the column name, it
would be very unusual for this to happen in practice, but silently ignoring what would otherwise
be a legitimate column name seemed rather bogus.
diff --git a/src/pl/tcl/expected/pltcl_queries.out b/src/pl/tcl/expected/pltcl_queries.out
index 6cb1fdb..c3f33d9 100644
--- a/src/pl/tcl/expected/pltcl_queries.out
+++ b/src/pl/tcl/expected/pltcl_queries.out
@@ -184,6 +184,21 @@ select * from T_pkey2 order by key1 using @<, key2 collate "C";
(4 rows)
-- show dump of trigger data
+insert into test_return values(-1,'1 element array');
+ERROR: trigger's return list must have even number of elements
+insert into test_return values(-2,'return dropped column');
+ERROR: unrecognized attribute "........pg.dropped.2........"
+insert into test_return values(-3,'return ctid column');
+ERROR: cannot set system attribute "ctid"
+insert into test_return values(-10,'do nothing');
+insert into test_return values(1,'good');
+select * from test_return;
+ i | t
+---+------
+ |
+ 1 | good
+(2 rows)
+
insert into trigger_test values(1,'insert');
NOTICE: NEW: {i: 1, v: insert}
NOTICE: OLD: {}
diff --git a/src/pl/tcl/expected/pltcl_setup.out b/src/pl/tcl/expected/pltcl_setup.out
index e65e9e3..707d77b 100644
--- a/src/pl/tcl/expected/pltcl_setup.out
+++ b/src/pl/tcl/expected/pltcl_setup.out
@@ -89,6 +89,33 @@ FOR EACH ROW EXECUTE PROCEDURE trigger_data(23,'skidoo');
CREATE TRIGGER show_trigger_data_view_trig
INSTEAD OF INSERT OR UPDATE OR DELETE ON trigger_test_view
FOR EACH ROW EXECUTE PROCEDURE trigger_data(24,'skidoo view');
+-- test handling of return
+CREATE TABLE test_return(i int, dropme int, t text);
+ALTER TABLE test_return DROP dropme;
+CREATE FUNCTION trigger_return() RETURNS trigger LANGUAGE pltcl AS $body$
+ switch $NEW(i) {
+ -1 {
+ return {"single element"}
+ }
+ -2 {
+ spi_exec "SELECT attname FROM pg_attribute WHERE attrelid=$TG_relid AND attisdropped"
+ set a($attname) 1
+ return [array get a]
+ }
+ -3 {
+ return {ctid 1}
+ }
+ -10 {
+ # Will return nothing
+ }
+ default {
+ return OK
+ }
+ }
+$body$;
+CREATE TRIGGER test_trigger_return
+BEFORE INSERT ON test_return
+FOR EACH ROW EXECUTE PROCEDURE trigger_return();
--
-- Trigger function on every change to T_pkey1
--
diff --git a/src/pl/tcl/pltcl.c b/src/pl/tcl/pltcl.c
index d236890..5d9a857 100644
--- a/src/pl/tcl/pltcl.c
+++ b/src/pl/tcl/pltcl.c
@@ -1128,7 +1128,9 @@ pltcl_trigger_handler(PG_FUNCTION_ARGS, bool pltrusted)
* Get the attribute number
************************************************************/
attnum = SPI_fnumber(tupdesc, ret_name);
- if (attnum == SPI_ERROR_NOATTRIBUTE)
+ /* Assume system attributes can't be marked as dropped */
+ if (attnum == SPI_ERROR_NOATTRIBUTE ||
+ (attnum > 0 && tupdesc->attrs[attnum - 1]->attisdropped))
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_COLUMN),
errmsg("unrecognized attribute \"%s\"",
@@ -1140,12 +1142,6 @@ pltcl_trigger_handler(PG_FUNCTION_ARGS, bool pltrusted)
ret_name)));
/************************************************************
- * Ignore dropped columns
- ************************************************************/
- if (tupdesc->attrs[attnum - 1]->attisdropped)
- continue;
-
- /************************************************************
* Lookup the attribute type in the syscache
* for the input function
************************************************************/
diff --git a/src/pl/tcl/sql/pltcl_queries.sql b/src/pl/tcl/sql/pltcl_queries.sql
index a0a9619..abc6eb0 100644
--- a/src/pl/tcl/sql/pltcl_queries.sql
+++ b/src/pl/tcl/sql/pltcl_queries.sql
@@ -74,6 +74,13 @@ select * from T_pkey1 order by key1 using @<, key2 collate "C";
select * from T_pkey2 order by key1 using @<, key2 collate "C";
-- show dump of trigger data
+insert into test_return values(-1,'1 element array');
+insert into test_return values(-2,'return dropped column');
+insert into test_return values(-3,'return ctid column');
+insert into test_return values(-10,'do nothing');
+insert into test_return values(1,'good');
+select * from test_return;
+
insert into trigger_test values(1,'insert');
insert into trigger_test_view values(2,'insert');
diff --git a/src/pl/tcl/sql/pltcl_setup.sql b/src/pl/tcl/sql/pltcl_setup.sql
index 8df65a5..42aef60 100644
--- a/src/pl/tcl/sql/pltcl_setup.sql
+++ b/src/pl/tcl/sql/pltcl_setup.sql
@@ -102,6 +102,34 @@ CREATE TRIGGER show_trigger_data_view_trig
INSTEAD OF INSERT OR UPDATE OR DELETE ON trigger_test_view
FOR EACH ROW EXECUTE PROCEDURE trigger_data(24,'skidoo view');
+-- test handling of return
+CREATE TABLE test_return(i int, dropme int, t text);
+ALTER TABLE test_return DROP dropme;
+CREATE FUNCTION trigger_return() RETURNS trigger LANGUAGE pltcl AS $body$
+ switch $NEW(i) {
+ -1 {
+ return {"single element"}
+ }
+ -2 {
+ spi_exec "SELECT attname FROM pg_attribute WHERE attrelid=$TG_relid AND attisdropped"
+ set a($attname) 1
+ return [array get a]
+ }
+ -3 {
+ return {ctid 1}
+ }
+ -10 {
+ # Will return nothing
+ }
+ default {
+ return OK
+ }
+ }
+$body$;
+CREATE TRIGGER test_trigger_return
+BEFORE INSERT ON test_return
+FOR EACH ROW EXECUTE PROCEDURE trigger_return();
+
--
-- Trigger function on every change to T_pkey1
--
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: Fix bug in handling of dropped columns in pltcl triggers
@ 2016-11-01 13:29 Michael Paquier <michael.paquier@gmail.com>
parent: Jim Nasby <Jim.Nasby@BlueTreble.com>
0 siblings, 1 reply; 6+ messages in thread
From: Michael Paquier @ 2016-11-01 13:29 UTC (permalink / raw)
To: Jim Nasby <Jim.Nasby@bluetreble.com>; +Cc: pgsql-hackers; Karl Lehenbauer <karl@flightaware.com>
On Tue, Nov 1, 2016 at 4:17 AM, Jim Nasby <Jim.Nasby@bluetreble.com> wrote:
> While reviewing code coverage in pltcl, I uncovered a bug in trigger
> function return handling. If you returned the munged name of a dropped
> column, that would silently be ignored. It would be unusual to hit this,
> since dropped columns end up with names like ".......pg.dropped.2.......",
> but since that's still a legitimate name for a column silently ignoring it
> seems rather bogus.
It seems to me that this patch breaks $TG_relatts and what existing
applications would except from it:
<varlistentry>
<term><varname>$TG_relatts</varname></term>
<listitem>
<para>
A Tcl list of the table column names, prefixed with an empty list
element. So looking up a column name in the list with
<application>Tcl</>'s
<function>lsearch</> command returns the element's number starting
with 1 for the first column, the same way the columns are customarily
numbered in <productname>PostgreSQL</productname>. (Empty list
elements also appear in the positions of columns that have been
dropped, so that the attribute numbering is correct for columns
to their right.)
</para>
</listitem>
</varlistentry>
As this is a behavior present since 2004, it does not sound wise to change it.
--
Michael
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: Fix bug in handling of dropped columns in pltcl triggers
@ 2016-11-04 21:28 Tom Lane <tgl@sss.pgh.pa.us>
parent: Michael Paquier <michael.paquier@gmail.com>
0 siblings, 1 reply; 6+ messages in thread
From: Tom Lane @ 2016-11-04 21:28 UTC (permalink / raw)
To: Michael Paquier <michael.paquier@gmail.com>; +Cc: Jim Nasby <Jim.Nasby@bluetreble.com>; pgsql-hackers; Karl Lehenbauer <karl@flightaware.com>
Michael Paquier <michael.paquier@gmail.com> writes:
> On Tue, Nov 1, 2016 at 4:17 AM, Jim Nasby <Jim.Nasby@bluetreble.com> wrote:
>> While reviewing code coverage in pltcl, I uncovered a bug in trigger
>> function return handling. If you returned the munged name of a dropped
>> column, that would silently be ignored. It would be unusual to hit this,
>> since dropped columns end up with names like ".......pg.dropped.2.......",
>> but since that's still a legitimate name for a column silently ignoring it
>> seems rather bogus.
> It seems to me that this patch breaks $TG_relatts and what existing
> applications would except from it:
I'm not sure how it would do that. What's being changed here is the
code for processing the returned tuple; it doesn't change what is put
into $TG_relatts. Also, per the manual,
> (Empty list
> elements also appear in the positions of columns that have been
> dropped, so that the attribute numbering is correct for columns
> to their right.)
If the trigger function were blindly using that information without any
exception for dropped columns, then what it would provide here as the
column name is an empty string, not ".......pg.dropped.2.......", so that
it would get a failure anyway. This change wouldn't affect that.
So I'm inclined to agree that we should change this, but I think Jim's
patch doesn't go far enough: what we really ought to do is change
SPI_fnumber itself to reject matches to dropped columns. I just did
a survey of callers, and only a small minority of them are explicitly
testing for a match to a dropped column, but it doesn't look to me
like any of the rest are really prepared to do something reasonable
with one. I find it impossible to think of a situation where a caller
would want to treat a match to a dropped column as valid.
My proposal therefore is for SPI_fnumber to ignore (not match to)
dropped columns, and to remove any caller-side attisdropped tests that
thereby become redundant.
It's possible that it'd make sense for pltcl_trigger_handler to ignore
empty-string column names in the returned list, so that the behavior
with stupid trigger functions would be a bit more forgiving; but that
is more or less independent of this patch.
regards, tom lane
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: Fix bug in handling of dropped columns in pltcl triggers
@ 2016-11-07 22:25 Jim Nasby <Jim.Nasby@BlueTreble.com>
parent: Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 2 replies; 6+ messages in thread
From: Jim Nasby @ 2016-11-07 22:25 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; Michael Paquier <michael.paquier@gmail.com>; +Cc: pgsql-hackers; Karl Lehenbauer <karl@flightaware.com>
On 11/4/16 4:28 PM, Tom Lane wrote:
> My proposal therefore is for SPI_fnumber to ignore (not match to)
> dropped columns, and to remove any caller-side attisdropped tests that
> thereby become redundant.
Yeah, SPI users certainly shouldn't need to worry about attisdropped,
and exposing attnum doesn't seem like a good idea either.
> It's possible that it'd make sense for pltcl_trigger_handler to ignore
> empty-string column names in the returned list, so that the behavior
> with stupid trigger functions would be a bit more forgiving; but that
> is more or less independent of this patch.
I'm a bit reluctant to do that since it'd be nice to be consistent with
regular pltcl functions returning composites. The same kind of issue
exists with the holes in $TG_relatts; we shouldn't be exposing the
details of attnum that way. Any code that's expecting those holes is
going to blow up after a dump/restore anyway.
--
Jim Nasby, Data Architect, Blue Treble Consulting, Austin TX
Experts in Analytics, Data Architecture and PostgreSQL
Data in Trouble? Get it in Treble! http://BlueTreble.com
855-TREBLE2 (855-873-2532) mobile: 512-569-9461
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: Fix bug in handling of dropped columns in pltcl triggers
@ 2016-11-08 14:39 Tom Lane <tgl@sss.pgh.pa.us>
parent: Jim Nasby <Jim.Nasby@BlueTreble.com>
1 sibling, 0 replies; 6+ messages in thread
From: Tom Lane @ 2016-11-08 14:39 UTC (permalink / raw)
To: Jim Nasby <Jim.Nasby@bluetreble.com>; +Cc: Michael Paquier <michael.paquier@gmail.com>; pgsql-hackers; Karl Lehenbauer <karl@flightaware.com>
Jim Nasby <Jim.Nasby@bluetreble.com> writes:
> On 11/4/16 4:28 PM, Tom Lane wrote:
>> It's possible that it'd make sense for pltcl_trigger_handler to ignore
>> empty-string column names in the returned list, so that the behavior
>> with stupid trigger functions would be a bit more forgiving; but that
>> is more or less independent of this patch.
> I'm a bit reluctant to do that since it'd be nice to be consistent with
> regular pltcl functions returning composites. The same kind of issue
> exists with the holes in $TG_relatts; we shouldn't be exposing the
> details of attnum that way. Any code that's expecting those holes is
> going to blow up after a dump/restore anyway.
Hm. Offhand it seems like the functions that pltcl itself exposes don't
really do anything that would depend on $TG_relatts indexes matching
physical column numbers. The only way you could write a pltcl function
that would depend on that would be to have it do some catalog queries that
expect the indexes to match pg_attribute.attnum. That's possible I guess
but it seems neither likely nor good practice.
I think I'd be in favor of trying to remove the business about having
empty-string entries in $TG_relatts. Do you want to draft a patch
for that?
regards, tom lane
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: Fix bug in handling of dropped columns in pltcl triggers
@ 2016-11-08 18:13 Tom Lane <tgl@sss.pgh.pa.us>
parent: Jim Nasby <Jim.Nasby@BlueTreble.com>
1 sibling, 0 replies; 6+ messages in thread
From: Tom Lane @ 2016-11-08 18:13 UTC (permalink / raw)
To: Jim Nasby <Jim.Nasby@BlueTreble.com>; +Cc: Michael Paquier <michael.paquier@gmail.com>; pgsql-hackers; Karl Lehenbauer <karl@flightaware.com>
Jim Nasby <Jim.Nasby@BlueTreble.com> writes:
> On 11/4/16 4:28 PM, Tom Lane wrote:
>> My proposal therefore is for SPI_fnumber to ignore (not match to)
>> dropped columns, and to remove any caller-side attisdropped tests that
>> thereby become redundant.
> Yeah, SPI users certainly shouldn't need to worry about attisdropped,
> and exposing attnum doesn't seem like a good idea either.
OK, I pushed a patch to centralize this in SPI_fnumber.
regards, tom lane
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers
^ permalink raw reply [nested|flat] 6+ messages in thread
end of thread, other threads:[~2016-11-08 18:13 UTC | newest]
Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2016-10-31 19:17 Fix bug in handling of dropped columns in pltcl triggers Jim Nasby <Jim.Nasby@BlueTreble.com>
2016-11-01 13:29 ` Michael Paquier <michael.paquier@gmail.com>
2016-11-04 21:28 ` Tom Lane <tgl@sss.pgh.pa.us>
2016-11-07 22:25 ` Jim Nasby <Jim.Nasby@BlueTreble.com>
2016-11-08 14:39 ` Tom Lane <tgl@sss.pgh.pa.us>
2016-11-08 18:13 ` Tom Lane <tgl@sss.pgh.pa.us>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox