1

I want to use dblink in PL/pgSQL stored procedure in such way:

PERFORM dblink_exec('myconn', 'BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE');
PERFORM dblink_exec('myconn', 'SELECT another_stored_procedure()');
PERFORM dblink_exec('myconn', 'COMMIT');

but I got an error in runtime:

ERROR:  statement returning results not allowed
CONTEXT:  SQL statement "SELECT dblink_exec('myconn', 'select another_stored_procedure()')"

so execution fails, although I tried to get the desired result in different ways.

UPDATE 1:

I know that stored procedures in postgresql are transactional. I'm using dblink for the autonomous transactions functionality to use it on the same server.

The matter is that the default level of transactions on my server is "read commited" but sometimes I need to start transactions with another level, e.g. "serializable".

So I need to execute stored procedure in autonomous transaction with explicit transaction level specifying.

And as far as I know dblink allows that, but I failed to find any useful info about dblink or dblink_exec functions which are suitable for my situation.

2
  • Have you connected with another PostgreSQL server at the other end? Commented Feb 3, 2015 at 13:14
  • @pozs See the update 1 pls Commented Feb 3, 2015 at 14:30

1 Answer 1

8

I assume, you have connected with another PostgreSQL server at the other end.

You need to call the dblink() function to execute statements, which has result(s), and not dblink_exec(). (Even if your function on the other end has returns void -- in that case, you could get a single NULL from calling that function in a SELECT.)

Also, you might not need transaction management:

In short, you need to execute:

-- PERFORM dblink_exec('myconn', 'BEGIN ...');
--    if you need explicit transaction management
PERFORM * FROM dblink('myconn', 'SELECT another_stored_procedure()') alias(col text);
-- PERFORM dblink_exec('myconn', 'COMMIT');
Sign up to request clarification or add additional context in comments.

3 Comments

@FrozenHeart that doesn't matter (that's why I wrote you might not need transaction management). As I stated, you should use dblink() for the SELECT statement. Have you tried yet?
Thanks for aiming. Your variant failed with: ERROR: statement returning results not allowed but this works for me (my another_stored_procedure() returns void): PERFORM * FROM dblink('myconn', 'SELECT another_stored_procedure()') as t1(test text);
@FrozenHeart yeah, I forgot returns setof record (like dblink() returns) always need an alias, where you specify column alias/types.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.