Re: Surprisingly forgiving behavior when a case expression is terminated with "end case"

From: Bryn Llewellyn <bryn(at)yugabyte(dot)com>
To: Tom Lane PostgreSQL <tgl(at)sss(dot)pgh(dot)pa(dot)us>, adrian(dot)klaver(at)aklaver(dot)com
Cc: pgsql-general list <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: Surprisingly forgiving behavior when a case expression is terminated with "end case"
Date: 2022-08-10 23:12:47
Message-ID: 9F2C5B40-033B-4120-836B-3B42FAA5679B@yugabyte.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

tgl(at)sss(dot)pgh(dot)pa(dot)us wrote:

> I think we've spent a great deal of blood, sweat, and tears making that so, or as nearly so as we could. We will in fact take any keyword after "AS", and in recent versions many non-reserved keywords will work that way without "AS".
>
> (Mind you, I think the SQL spec made a serious design error in allowing "AS" to be optional. But we have to live with that as best we can.)

Also, adrian(dot)klaver(at)aklaver(dot)com wrote:

> Even reserved key words are not completely reserved in PostgreSQL, but can be used as column labels (for example, SELECT 55 AS CHECK, even though CHECK is a reserved key word):
> https://www.postgresql.org/docs/current/sql-keywords-appendix.html

Thank you both. I never would have guessed that a word with "reserved" status could be used as a column alias (with or without preceding it with AS). "not completely reserved in PostgreSQL" makes this sound like a PG special.

So I was caught out yet again. And I fear that I'll continue to be caught out with other things—and maybe this one too, at some later date, when I've forgotten the present exchanges…

Anyway, I believe that I have the answer to my question. And my new mental model allowed me to predict that, as presented, this would work:

create function f()
returns text
language plpgsql
as $body$
declare
a constant int := 3;
b constant int := 5;
c constant int := 7;
begin
return a + b case /* + c */;
end;
$body$;

select f();

It does! It predicted, too, that when "+ c" is uncommented, "create function" would fail with a syntax error. And that prediction also held out.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Bryn Llewellyn 2022-08-11 01:53:25 Surprising results from current_role in a "security invoker" trigger function in a "cascade delete via FK" scenario
Previous Message Adrian Klaver 2022-08-10 23:11:11 Re: Why is DEFAULT much faster than UPDATE?