Re: BUG #1329: Bug in IF-ELSEIF-ELSE construct

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Neil Conway <neilc(at)samurai(dot)com>
Cc: Rico Wind <rw(at)rico-wind(dot)dk>, pgsql-bugs(at)postgresql(dot)org
Subject: Re: BUG #1329: Bug in IF-ELSEIF-ELSE construct
Date: 2004-11-29 16:59:28
Message-ID: 19420.1101747568@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs pgsql-patches

Neil Conway <neilc(at)samurai(dot)com> writes:
> (2) The syntax error message is wrong (we print a character offset and
> query context that is relative to the CREATE FUNCTION statement, not the
> individual SQL statement we're executing). I fooled around a bit with
> defining a custom ereport() callback to print the right line number and
> query context, but I couldn't get it right. Do you have any guidance on
> the proper way to do this.

Hmm ... I was about to say the SQL function validator already does this
(see sql_function_parse_error_callback in pg_proc.c), but it has the
advantage that there's a one-to-one correspondence between the string it
sends to the main parser and a substring of the original function text.
In plpgsql that's not true because of (a) substitution of parameter
symbols for names and (b) the liberties that the plpgsql lexer takes
with whitespace and eliding comments.

You might be best off just to strive for output like this:

ERROR: syntax error at or near...
QUERY: select ...
CONTEXT: compile of plpgsql function "frob" at SQL statement line 12

which ought to be relatively easy to get.

BTW, don't forget to check SQL expressions (eg, the condition of an IF)
as well as SQL statements. In the case of EXECUTE, you can check
the expression that gives rise to the text string.

>> The error would be more useful if reported immediately
>> after the putative SQL statement is parsed. Not sure if that's
>> hard or not. (The same remark applies to dead code checking, now
>> that I think about it.)

> In the case of dead code checking, I don't think it matters.

My thought was that a dead-code error could well indicate a problem
along the lines of a missing begin or end, and that flagging the
dead-code error would probably provide a much closer pointer to the
true problem than barfing at the end of the input when we realize
we have unmatched begin/end structure. (Especially since the barf
will likely take the form of a nonspecific "syntax error" message.
Anytime you can do better than that, you're ahead.) Similarly, checking
expressions immediately will help report mismatched-parenthesis problems
in a more useful way than we do now.

regards, tom lane

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Luciano - UNILOG SA 2004-11-29 17:08:06 PLTCL
Previous Message Tom Lane 2004-11-29 16:27:14 Re: BUG #1331: after update: vacuum blocks parallel to select

Browse pgsql-patches by date

  From Date Subject
Next Message Tom Lane 2004-11-30 00:14:01 Preliminary patch for on-the-fly relpages/reltuples estimation
Previous Message Tom Lane 2004-11-29 15:42:29 Re: multiline CSV fields