Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit a8a3b54

Browse files
committed
Arrange to emit a CONTEXT: SQL function "foo" entry in an error
message that is reporting a prechecking error in a SQL function. This is to cue client-side code that the syntax error position, if any, is with respect to the function body and not the outer command.
1 parent afaf252 commit a8a3b54

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

src/backend/catalog/pg_proc.c

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/catalog/pg_proc.c,v 1.111 2004/01/06 23:55:18 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/catalog/pg_proc.c,v 1.112 2004/03/14 01:58:41 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -44,6 +44,7 @@ Datum fmgr_sql_validator(PG_FUNCTION_ARGS);
4444

4545
static Datum create_parameternames_array(int parameterCount,
4646
const char *parameterNames[]);
47+
static void sql_function_parse_error_callback(void *arg);
4748

4849

4950
/* ----------------------------------------------------------------
@@ -708,6 +709,7 @@ fmgr_sql_validator(PG_FUNCTION_ARGS)
708709
bool isnull;
709710
Datum tmp;
710711
char *prosrc;
712+
ErrorContextCallback sqlerrcontext;
711713
char functyptype;
712714
bool haspolyarg;
713715
int i;
@@ -760,6 +762,16 @@ fmgr_sql_validator(PG_FUNCTION_ARGS)
760762

761763
prosrc = DatumGetCString(DirectFunctionCall1(textout, tmp));
762764

765+
/*
766+
* Setup error traceback support for ereport(). This is mostly
767+
* so we can add context info that shows that a syntax-error
768+
* location is inside the function body, not out in CREATE FUNCTION.
769+
*/
770+
sqlerrcontext.callback = sql_function_parse_error_callback;
771+
sqlerrcontext.arg = proc;
772+
sqlerrcontext.previous = error_context_stack;
773+
error_context_stack = &sqlerrcontext;
774+
763775
/*
764776
* We can't do full prechecking of the function definition if there
765777
* are any polymorphic input types, because actual datatypes of
@@ -778,9 +790,32 @@ fmgr_sql_validator(PG_FUNCTION_ARGS)
778790
}
779791
else
780792
querytree_list = pg_parse_query(prosrc);
793+
794+
error_context_stack = sqlerrcontext.previous;
781795
}
782796

783797
ReleaseSysCache(tuple);
784798

785799
PG_RETURN_VOID();
786800
}
801+
802+
/*
803+
* error context callback to let us supply a context marker
804+
*/
805+
static void
806+
sql_function_parse_error_callback(void *arg)
807+
{
808+
Form_pg_proc proc = (Form_pg_proc) arg;
809+
810+
/*
811+
* XXX it'd be really nice to adjust the syntax error position to
812+
* account for the offset from the start of the statement to the
813+
* function body string, not to mention any quoting characters in
814+
* the string, but I can't see any decent way to do that...
815+
*
816+
* In the meantime, put in a CONTEXT entry that can cue clients
817+
* not to trust the syntax error position completely.
818+
*/
819+
errcontext("SQL function \"%s\"",
820+
NameStr(proc->proname));
821+
}

src/test/regress/output/create_function_1.source

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,20 @@ CREATE FUNCTION test1 (int) RETURNS int LANGUAGE sql
5252
AS 'SELECT ''not an integer'';';
5353
ERROR: return type mismatch in function declared to return integer
5454
DETAIL: Actual return type is "unknown".
55+
CONTEXT: SQL function "test1"
5556
CREATE FUNCTION test1 (int) RETURNS int LANGUAGE sql
5657
AS 'not even SQL';
5758
ERROR: syntax error at or near "not" at character 1
59+
CONTEXT: SQL function "test1"
5860
CREATE FUNCTION test1 (int) RETURNS int LANGUAGE sql
5961
AS 'SELECT 1, 2, 3;';
6062
ERROR: return type mismatch in function declared to return integer
6163
DETAIL: Final SELECT must return exactly one column.
64+
CONTEXT: SQL function "test1"
6265
CREATE FUNCTION test1 (int) RETURNS int LANGUAGE sql
6366
AS 'SELECT $2;';
6467
ERROR: there is no parameter $2
68+
CONTEXT: SQL function "test1"
6569
CREATE FUNCTION test1 (int) RETURNS int LANGUAGE sql
6670
AS 'a', 'b';
6771
ERROR: only one AS item needed for language "sql"

0 commit comments

Comments
 (0)