I've been fooling with adding a report of the executing query to the
CONTEXT stack when an error happens within a query submitted by a
plpgsql function. Since plpgsql submits all its queries through SPI,
the most convenient place to do this is in spi.c, and so the behavior
will also apply to queries submitted via SPI by user-written C
functions.
What I've currently got labels the failing query as a SPI query,
for example
regression=# create or replace function foo(text) returns text as $$
regression$# begin
regression$# execute 'select * from ' || $1;
regression$# return 'good';
regression$# end
regression$# $$ language plpgsql;
CREATE FUNCTION
regression=# select foo('int4_tbl');
foo
--
good
(1 row)
regression=# select foo('nosuch_tbl');
ERROR: relation nosuch_tbl does not exist
CONTEXT: SPI query select * from nosuch_tbl
PL/pgSQL function foo line 2 at execute statement
regression=# select foo('fee fie fo fum');
ERROR: syntax error at or near fo at character 23
CONTEXT: SPI query select * from fee fie fo fum
PL/pgSQL function foo line 2 at execute statement
Although this is quite reasonable for queries submitted by user-written
C functions, I'm worried that plpgsql programmers will be confused
because they've never heard of SPI. I toyed with saying SQL query
instead, but that seems pretty nearly content-free ... it doesn't
distinguish these queries from ones submitted directly by the client.
Can anyone think of a better wording? Does this bother people enough
to justify hacking the SPI interface to allow a label to be passed in?
regards, tom lane
---(end of broadcast)---
TIP 6: Have you searched our list archives?
http://archives.postgresql.org