Re: Why does the PL/pgSQL compiler do this?

From: Michael Moore <michaeljmoore(at)gmail(dot)com>
To: Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>
Cc: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>, postgres list <pgsql-sql(at)postgresql(dot)org>
Subject: Re: Why does the PL/pgSQL compiler do this?
Date: 2016-11-01 01:09:39
Message-ID: CACpWLjNjuiEp-PnHH=NqXQ=BxX9H4GOnxoQj-9pWuCGP-7YbXQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Thanks Adrian, but is ROLLBACK *ever* possible in PL/pgSQL? My
understanding is, "No".

On Mon, Oct 31, 2016 at 4:38 PM, Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>
wrote:

> On 10/31/2016 04:32 PM, Michael Moore wrote:
>
>> I'm still a bit confused. If I replace the ROLLBACK; command with
>> ELEPHANT; the result is a syntax error. Why doesn't ROLLBACK; produce
>> the same error since it is not valid in the LANGUAGE plpgsql. I
>> understand that "ROLLBACK TO SAVEPOINT" IS valid. But it's not the same
>> thing.
>>
>
> I am guessing this:
>
> https://www.postgresql.org/docs/9.5/static/plpgsql-implementation.html
> " A disadvantage is that errors in a specific expression or command cannot
> be detected until that part of the function is reached in execution.
> (Trivial syntax errors will be detected during the initial parsing pass,
> but anything deeper will not be detected until execution.)"
>
> ROLLBACK might actually be valid at some point, ELEPHANT will not so it
> caught in the trivial error stage.
>
>
>> On Mon, Oct 31, 2016 at 3:55 PM, Michael Moore <michaeljmoore(at)gmail(dot)com
>> <mailto:michaeljmoore(at)gmail(dot)com>> wrote:
>>
>> Cool, thanks David, I'll give it a read.
>>
>>
>> On Mon, Oct 31, 2016 at 3:24 PM, David G. Johnston
>> <david(dot)g(dot)johnston(at)gmail(dot)com <mailto:david(dot)g(dot)johnston(at)gmail(dot)com>>
>> wrote:
>>
>> On Mon, Oct 31, 2016 at 3:13 PM, Michael Moore
>> <michaeljmoore(at)gmail(dot)com <mailto:michaeljmoore(at)gmail(dot)com>>wrote:
>>
>> Here is the complete function, but all you need to look at
>> is the exception block. (I didn't write this code) :-) I
>> will ask the question after the code.
>> ​[...]​
>>
>> RETURN TRUE;
>>
>> EXCEPTION WHEN OTHERS THEN
>>
>> RAISE EXCEPTION '% %', SQLERRM, SQLSTATE;
>>
>> ROLLBACK;
>>
>> RETURN FALSE;
>>
>> END;
>>
>> $BODY$
>>
>> LANGUAGE plpgsql VOLATILE
>>
>> COST 100;
>>
>>
>> So, here is the question. Why does the compiler not catch:
>>
>> 1) ROLLBACK; is not a valid PL/pgSQL command
>>
>>
>> R
>> ​eading section ​41.10.2 at the linked page should answer this
>> part.
>>
>> https://www.postgresql.org/docs/current/static/plpgsql-imple
>> mentation.html
>> <https://www.postgresql.org/docs/current/static/plpgsql-impl
>> ementation.html>
>>
>>
>> 2) ROLLBACK; and RETURN FALSE; can never be reached
>>
>>
>>
>> Similar to the above - though "static analysis" is yet a step
>> beyond even what the syntax checking skipping covered above
>> would reveal.
>>
>> ​David J.​
>>
>>
>>
>>
>
> --
> Adrian Klaver
> adrian(dot)klaver(at)aklaver(dot)com
>

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Adrian Klaver 2016-11-01 01:20:45 Re: Why does the PL/pgSQL compiler do this?
Previous Message Adrian Klaver 2016-10-31 23:38:54 Re: Why does the PL/pgSQL compiler do this?