doing %-expansion in plpgsql RAISE USING

Lists: pgsql-hackers
From: Alvaro Herrera <alvherre(at)commandprompt(dot)com>
To: Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: doing %-expansion in plpgsql RAISE USING
Date: 2009-08-04 13:27:08
Message-ID: 20090804132708.GA6494@alvh.no-ip.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Hi,

It seems there's no way to do %-expansion in plpgsql when one is using
RAISE USING:

alvherre=# create or replace function f () returns void language plpgsql as $$
begin
raise using message = 'hello %' || 'world';
return;
end;
$$;
CREATE FUNCTION
alvherre=# select f();
ERROR: hello %world

I would like the % to be expanded to some argument, but obviously
there's no way to pass the arguments that it should expand to. We could
do something like

RAISE USING message = 'hello %st %', args = 1, 'world'

but this is obviously going to be difficult, if not impossible, to
implement in the grammar. Perhaps
RAISE USING message = 'brave %st %', args = (1, 'world')

Thoughts?

--
Alvaro Herrera http://www.CommandPrompt.com/
The PostgreSQL Company - Command Prompt, Inc.


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Alvaro Herrera <alvherre(at)commandprompt(dot)com>
Cc: Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: doing %-expansion in plpgsql RAISE USING
Date: 2009-08-04 13:51:57
Message-ID: 21970.1249393917@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Alvaro Herrera <alvherre(at)commandprompt(dot)com> writes:
> It seems there's no way to do %-expansion in plpgsql when one is using
> RAISE USING:

That's intentional. Just use string concatenation if you need a
run-time-variable message.

regards, tom lane


From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: Alvaro Herrera <alvherre(at)commandprompt(dot)com>
Cc: Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: doing %-expansion in plpgsql RAISE USING
Date: 2009-08-04 13:56:22
Message-ID: 162867790908040656x4ea28efdyfa312dd233ff8510@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Hello

2009/8/4 Alvaro Herrera <alvherre(at)commandprompt(dot)com>:
> Hi,
>
> It seems there's no way to do %-expansion in plpgsql when one is using
> RAISE USING:
>
> alvherre=# create or replace function f () returns void language plpgsql as $$
> begin
>  raise using message = 'hello %' || 'world';
>  return;
> end;
> $$;
> CREATE FUNCTION
> alvherre=# select f();
> ERROR:  hello %world
>
>

parameter is expression, so we could to define operator % like

text % any

% isn't defined for text so this is possible. This operator should be
generally used, not only in RAISE attribs.

> I would like the % to be expanded to some argument, but obviously
> there's no way to pass the arguments that it should expand to.  We could
> do something like
>
> RAISE USING message = 'hello %st %', args = 1, 'world'
>

RAISE USING message = 'hello %st %' % (1, 'world')

??

or simple use custom variadic function

RAISE USING message= subst('hello %st %', 1, 'world')

good example for parser hook :)

Pavel

> but this is obviously going to be difficult, if not impossible, to
> implement in the grammar.  Perhaps
> RAISE USING message = 'brave %st %', args = (1, 'world')
>
> Thoughts?
>
> --
> Alvaro Herrera                                http://www.CommandPrompt.com/
> The PostgreSQL Company - Command Prompt, Inc.
>
> --
> Sent via pgsql-hackers mailing list (pgsql-hackers(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-hackers
>


From: Alvaro Herrera <alvherre(at)commandprompt(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: doing %-expansion in plpgsql RAISE USING
Date: 2009-08-04 14:16:45
Message-ID: 20090804141645.GB6494@alvh.no-ip.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Tom Lane wrote:
> Alvaro Herrera <alvherre(at)commandprompt(dot)com> writes:
> > It seems there's no way to do %-expansion in plpgsql when one is using
> > RAISE USING:
>
> That's intentional. Just use string concatenation if you need a
> run-time-variable message.

Yes, I can do that, but it's really ugly and gets unmaintainable
quickly.

--
Alvaro Herrera http://www.CommandPrompt.com/
PostgreSQL Replication, Consulting, Custom Development, 24x7 support