Get cursor name for invalid_cursor_name error - Mailing list pgsql-general

From PetSerAl
Subject Get cursor name for invalid_cursor_name error
Date
Msg-id CAKygsHT0dE+UNGR74_4dJrix9YTidPLGMu2GYj6ZBXMvzCc0sQ@mail.gmail.com
Whole thread Raw
Responses Re: Get cursor name for invalid_cursor_name error
List pgsql-general
How to reliable get cursor name which cause invalid_cursor_name error?

postgres=# CREATE FUNCTION value_from_cursor_unsafe(cursor_name text)
RETURNS integer
postgres-# STRICT LANGUAGE plpgsql
postgres-# AS $$
postgres$#   DECLARE
postgres$#     cursor CONSTANT refcursor NOT NULL := cursor_name;
postgres$#     result integer;
postgres$#   BEGIN
postgres$#     FETCH FIRST FROM cursor INTO result;
postgres$#     return result;
postgres$#   END
postgres$# $$;
CREATE FUNCTION
postgres=# CREATE FUNCTION value_from_cursor_safe(cursor_name text)
RETURNS integer
postgres-# STRICT LANGUAGE plpgsql
postgres-# AS $$
postgres$#   DECLARE
postgres$#     result integer;
postgres$#   BEGIN
postgres$#     BEGIN
postgres$#       result := value_from_cursor_unsafe(cursor_name);
postgres$#     EXCEPTION
postgres$#       WHEN invalid_cursor_name THEN
postgres$#         RAISE INFO '%', SQLERRM;
postgres$#     END;
postgres$#     return result;
postgres$#   END
postgres$# $$;
CREATE FUNCTION
postgres=# SELECT value_from_cursor_safe('asd'); -- case 1
INFO:  cursor "asd" does not exist
 value_from_cursor_safe
------------------------

(1 row)


postgres=# BEGIN;
BEGIN
postgres=*# DECLARE "fgh" SCROLL CURSOR FOR VALUES
(value_from_cursor_unsafe('jkl'));
DECLARE CURSOR
postgres=*# SELECT value_from_cursor_safe('fgh'); -- case 2
INFO:  cursor "jkl" does not exist
 value_from_cursor_safe
------------------------

(1 row)


postgres=*# COMMIT;
COMMIT

For example, in given example in "case 2" I want to rethrow error,
because it is not about 'fgh' cursor, which I directly query. But it
seems cursor name only available as part of localizable error message,
but not as separate field through GET STACKED DIAGNOSTICS.



pgsql-general by date:

Previous
From: Greg Sabino Mullane
Date:
Subject: Re: Repeatable Read Isolation Level "transaction start time"
Next
From: Adrian Klaver
Date:
Subject: Re: Get cursor name for invalid_cursor_name error