Report the origin of syntax errors from pgbench.

Currently only the column number (for expressions) and command are essentially reported:

  sh> ./pgbench -f bad.sql
  syntax error at column 14
  set: parse error

The patch helps locate the origin of errors with the file name, line number and the actual text triggering the issue (either the line or an extract for expressions):

  sh> ./pgbench -f bad.sql
  syntax error at column 14
  error while processing "bad.sql" line 3: (1021 * :id) %
  set: parse error

Whether using a macro is the right tool is debatable. The contents could be expanded, but that would mean replicating the same message over and over again, so it seems cleaner to me this way. An function seems overkill.

--
Fabien.
diff --git a/contrib/pgbench/pgbench.c b/contrib/pgbench/pgbench.c
index 706fdf5..f45d28b 100644
--- a/contrib/pgbench/pgbench.c
+++ b/contrib/pgbench/pgbench.c
@@ -2200,6 +2200,11 @@ process_commands(char *buf, const char *source, const int lineno)
 	char	   *p,
 			   *tok;
 
+/* error message generation */
+#define PRINT_ERROR_AT(current_line)								\
+	fprintf(stderr, "error while processing \"%s\" line %d: %s\n",	\
+			source, lineno, current_line)
+
 	/* Make the string buf end at the next newline */
 	if ((p = strchr(buf, '\n')) != NULL)
 		*p = '\0';
@@ -2250,6 +2255,7 @@ process_commands(char *buf, const char *source, const int lineno)
 
 			if (my_commands->argc < 4)
 			{
+				PRINT_ERROR_AT(my_commands->line);
 				fprintf(stderr, "%s: missing argument\n", my_commands->argv[0]);
 				exit(1);
 			}
@@ -2267,11 +2273,13 @@ process_commands(char *buf, const char *source, const int lineno)
 			{
 				if (my_commands->argc < 6)
 				{
+					PRINT_ERROR_AT(my_commands->line);
 					fprintf(stderr, "%s(%s): missing threshold argument\n", my_commands->argv[0], my_commands->argv[4]);
 					exit(1);
 				}
 				else if (my_commands->argc > 6)
 				{
+					PRINT_ERROR_AT(my_commands->line);
 					fprintf(stderr, "%s(%s): too many arguments (extra:",
 							my_commands->argv[0], my_commands->argv[4]);
 					for (j = 6; j < my_commands->argc; j++)
@@ -2282,6 +2290,7 @@ process_commands(char *buf, const char *source, const int lineno)
 			}
 			else /* cannot parse, unexpected arguments */
 			{
+				PRINT_ERROR_AT(my_commands->line);
 				fprintf(stderr, "%s: unexpected arguments (bad:", my_commands->argv[0]);
 				for (j = 4; j < my_commands->argc; j++)
 					fprintf(stderr, " %s", my_commands->argv[j]);
@@ -2293,6 +2302,7 @@ process_commands(char *buf, const char *source, const int lineno)
 		{
 			if (my_commands->argc < 3)
 			{
+				PRINT_ERROR_AT(my_commands->line);
 				fprintf(stderr, "%s: missing argument\n", my_commands->argv[0]);
 				exit(1);
 			}
@@ -2301,6 +2311,7 @@ process_commands(char *buf, const char *source, const int lineno)
 
 			if (expr_yyparse() != 0)
 			{
+				PRINT_ERROR_AT(my_commands->argv[2]);
 				fprintf(stderr, "%s: parse error\n", my_commands->argv[0]);
 				exit(1);
 			}
@@ -2313,6 +2324,7 @@ process_commands(char *buf, const char *source, const int lineno)
 		{
 			if (my_commands->argc < 2)
 			{
+				PRINT_ERROR_AT(my_commands->line);
 				fprintf(stderr, "%s: missing argument\n", my_commands->argv[0]);
 				exit(1);
 			}
@@ -2343,6 +2355,7 @@ process_commands(char *buf, const char *source, const int lineno)
 					pg_strcasecmp(my_commands->argv[2], "ms") != 0 &&
 					pg_strcasecmp(my_commands->argv[2], "s") != 0)
 				{
+					PRINT_ERROR_AT(my_commands->line);
 					fprintf(stderr, "%s: unknown time unit '%s' - must be us, ms or s\n",
 							my_commands->argv[0], my_commands->argv[2]);
 					exit(1);
@@ -2357,6 +2370,7 @@ process_commands(char *buf, const char *source, const int lineno)
 		{
 			if (my_commands->argc < 3)
 			{
+				PRINT_ERROR_AT(my_commands->line);
 				fprintf(stderr, "%s: missing argument\n", my_commands->argv[0]);
 				exit(1);
 			}
@@ -2365,12 +2379,14 @@ process_commands(char *buf, const char *source, const int lineno)
 		{
 			if (my_commands->argc < 1)
 			{
+				PRINT_ERROR_AT(my_commands->line);
 				fprintf(stderr, "%s: missing command\n", my_commands->argv[0]);
 				exit(1);
 			}
 		}
 		else
 		{
+			PRINT_ERROR_AT(my_commands->line);
 			fprintf(stderr, "Invalid command %s\n", my_commands->argv[0]);
 			exit(1);
 		}
@@ -2395,6 +2411,8 @@ process_commands(char *buf, const char *source, const int lineno)
 		}
 	}
 
+#undef PRINT_ERROR_AT
+
 	return my_commands;
 }
 
-- 
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