Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1ZG7d0-0005q4-MF for pgsql-sql@arkaria.postgresql.org; Fri, 17 Jul 2015 15:32:46 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1ZG7cz-0001rn-VN for pgsql-sql@arkaria.postgresql.org; Fri, 17 Jul 2015 15:32:46 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1ZG7c0-0000mB-W6 for pgsql-sql@postgresql.org; Fri, 17 Jul 2015 15:31:45 +0000 Received: from newmail.postgrespro.ru ([93.174.131.138] helo=mail.postgrespro.ru) by magus.postgresql.org with esmtp (Exim 4.84) (envelope-from ) id 1ZG7bu-0003dl-Be for pgsql-sql@postgresql.org; Fri, 17 Jul 2015 15:31:44 +0000 Received: from [127.0.0.1] (unknown [192.168.27.1]) by mail.postgrespro.ru (Postfix) with ESMTPSA id A8F7221C2198 for ; Fri, 17 Jul 2015 18:31:37 +0300 (MSK) Message-ID: <55A91FDB.1010706@postgrespro.ru> Date: Fri, 17 Jul 2015 18:31:39 +0300 From: Alex Ignatov User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.7.0 MIME-Version: 1.0 To: "pgsql-sql@postgresql.org" Subject: Re: User defined exceptions References: <55A669CA.3070302@postgrespro.ru> <55A8AFF3.7040305@ocslab.com> In-Reply-To: <55A8AFF3.7040305@ocslab.com> Content-Type: multipart/alternative; boundary="------------080103000301010107000508" X-Antivirus: avast! (VPS 150717-0, 17.07.2015), Outbound message X-Antivirus-Status: Clean X-Pg-Spam-Score: -3.1 (---) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org This is a multi-part message in MIME format. --------------080103000301010107000508 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit On 17.07.2015 10:34, Alexey Bashtanov wrote: > On 15.07.2015 17:10, Alex Ignatov wrote: >> Hello all! >> Trying to emulate "named" user defined exception with: >> CREATE OR REPLACE FUNCTION exception_aaa () RETURNS text AS $body$ >> BEGIN >> return 31234; >> END; >> $body$ >> LANGUAGE PLPGSQL >> SECURITY DEFINER >> ; >> >> do $$ >> begin >> raise exception using errcode=exception_aaa(); >> exception >> when sqlstate exception_aaa() >> then >> raise notice 'got exception %',sqlstate; >> end; >> $$ >> >> Got: >> >> ERROR: syntax error at or near "exception_aaa" >> LINE 20: sqlstate exception_aaa() >> >> I looks like "when sqlstate exception_aaa()" doesn't work. >> >> How can I catch exception in this case? > > Hello Alex, > > The following workaround could be used: > > do $$ > begin > raise exception using errcode = exception_aaa(); > exception > when others then > if sqlstate = exception_aaa() then > raise notice 'got exception %',sqlstate; > else > raise; --reraise > end if; > end; > $$ > > Not sure if its performance is the same as in simple exception catch, > maybe it would degrade. > > Best Regards, > Alexey Bashtanov Yep already used this trick =) Anyway thank you! -- Alex Ignatov Postgres Professional: http://www.postgrespro.com The Russian Postgres Company --- This email has been checked for viruses by Avast antivirus software. https://www.avast.com/antivirus --------------080103000301010107000508 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit

On 17.07.2015 10:34, Alexey Bashtanov wrote:
On 15.07.2015 17:10, Alex Ignatov wrote:
Hello all!
Trying to emulate "named" user defined exception with:
CREATE OR REPLACE FUNCTION exception_aaa ()  RETURNS text AS $body$
BEGIN
   return 31234;     
END;
$body$
LANGUAGE PLPGSQL
SECURITY DEFINER
;

do $$
begin
   raise exception using errcode=exception_aaa();
exception
   when  sqlstate exception_aaa()
   then
      raise notice 'got exception %',sqlstate;
end;
$$
 
Got:

ERROR:  syntax error at or near "exception_aaa"
LINE 20: sqlstate exception_aaa()

I looks like "when  sqlstate exception_aaa()" doesn't work.

How can I catch exception in this case?

Hello Alex,

The following workaround could be used:

do $$
begin
   raise exception using errcode = exception_aaa();
exception
   when others then
      if sqlstate = exception_aaa() then
         raise notice 'got exception %',sqlstate;
      else
         raise; --reraise
      end if;
end;
$$

Not sure if its performance is the same as in simple exception catch, maybe it would degrade.

Best Regards,
   Alexey Bashtanov
Yep already used this trick =)
Anyway thank you!
-- 
Alex Ignatov
Postgres Professional: http://www.postgrespro.com
The Russian Postgres Company




Avast logo

This email has been checked for viruses by Avast antivirus software.
www.avast.com


--------------080103000301010107000508--