Jim Nasby <jim.na...@bluetreble.com> writes:
> On 1/12/16 5:00 PM, Tom Lane wrote:
>> Also, there's some technology in CREATE FUNCTION that deals with the fact
>> that we may be calling the parser on a string different from the original
>> user command, which might be worth borrowing here --- at least part of
>> the confusion is that it's evidently reporting a cursor position relative
>> to the extension script as though it applied to the CREATE EXTENSION.

> Are you talking about plpgsql_compile_error_callback()? It looks like it 
> does it's magic by relying on the plpgsql parser to keep track of where 
> it's at.

Not really.  It's about transposing the error to an "internal query"
instead of reporting it against the user's text.

I was imagining something roughly like the attached.  Experimenting with
this, the good news is that you get a decent error position:

regression=# create extension pg_trgm ;
ERROR:  syntax error at or near "USINGgist"
LINE 129: ALTER OPERATOR FAMILY gist_trgm_ops USINGgist ADD
                                              ^
QUERY:  /* contrib/pg_trgm/pg_trgm--1.2.sql */

-- complain if script is sourced in psql, rather than via CREATE EXTENSION
...
ALTER OPERATOR FAMILY gin_trgm_ops USING gin ADD
        FUNCTION        6      (text,text) gin_trgm_triconsistent (internal, 
int2, text, int4, internal, internal, internal);

CONTEXT:  extension script file 
"/home/postgres/testversion/share/extension/pg_trgm--1.2.sql"
regression=# 

The bad news is that psql regurgitates the whole script in the QUERY
field, because that's what we said was the internal query.

There are various ways you could imagine printing just part of the script;
for instance, starting from the cursor position, scan forward and back for
semicolons at line ends, and trim the query string to report just that
much.  Or maybe it's worth teaching the grammar to report the first token
location of each parsetree, and then the loop in execute_sql_string could
stash that away for use by the error reporter.

I don't really have time to fool with this, but if someone else wants
to run with it ...

                        regards, tom lane

diff --git a/src/backend/commands/extension.c b/src/backend/commands/extension.c
index 9d84b79..b4b9286 100644
*** a/src/backend/commands/extension.c
--- b/src/backend/commands/extension.c
*************** typedef struct ExtensionVersionInfo
*** 96,102 ****
--- 96,110 ----
  	struct ExtensionVersionInfo *previous;		/* current best predecessor */
  } ExtensionVersionInfo;
  
+ /* State needed by script_error_callback */
+ typedef struct
+ {
+ 	const char *script;
+ 	const char *filename;
+ }	script_error_callback_arg;
+ 
  /* Local functions */
+ static void script_error_callback(void *arg);
  static List *find_update_path(List *evi_list,
  				 ExtensionVersionInfo *evi_start,
  				 ExtensionVersionInfo *evi_target,
*************** read_extension_script_file(const Extensi
*** 682,692 ****
--- 690,713 ----
  static void
  execute_sql_string(const char *sql, const char *filename)
  {
+ 	script_error_callback_arg callback_arg;
+ 	ErrorContextCallback sqlerrcontext;
  	List	   *raw_parsetree_list;
  	DestReceiver *dest;
  	ListCell   *lc1;
  
  	/*
+ 	 * Setup error traceback support for ereport().
+ 	 */
+ 	callback_arg.script = sql;
+ 	callback_arg.filename = filename;
+ 
+ 	sqlerrcontext.callback = script_error_callback;
+ 	sqlerrcontext.arg = (void *) &callback_arg;
+ 	sqlerrcontext.previous = error_context_stack;
+ 	error_context_stack = &sqlerrcontext;
+ 
+ 	/*
  	 * Parse the SQL string into a list of raw parse trees.
  	 */
  	raw_parsetree_list = pg_parse_query(sql);
*************** execute_sql_string(const char *sql, cons
*** 755,765 ****
--- 776,809 ----
  		}
  	}
  
+ 	error_context_stack = sqlerrcontext.previous;
+ 
  	/* Be sure to advance the command counter after the last script command */
  	CommandCounterIncrement();
  }
  
  /*
+  * error context callback for errors within an extension script
+  */
+ static void
+ script_error_callback(void *arg)
+ {
+ 	script_error_callback_arg *callback_arg = (script_error_callback_arg *) arg;
+ 	int			syntaxerrposition;
+ 
+ 	/* If it's a syntax error, convert to internal syntax error report */
+ 	syntaxerrposition = geterrposition();
+ 	if (syntaxerrposition > 0)
+ 	{
+ 		errposition(0);
+ 		internalerrposition(syntaxerrposition);
+ 		internalerrquery(callback_arg->script);
+ 	}
+ 
+ 	errcontext("extension script file \"%s\"", callback_arg->filename);
+ }
+ 
+ /*
   * Execute the appropriate script file for installing or updating the extension
   *
   * If from_version isn't NULL, it's an update
-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Reply via email to