DBLINK_GET_RESULT(3)

πŸ“¦ version 18.6, in opensuse-leap 16.0

okay bestie, gather round, because we're talking about dblink_get_result from the PostgreSQL 18.6 Documentation and honestly? she's the finisher of the async dblink duo. no cap.


✨ NAME ✨

dblink_get_result β€” gets an async query result πŸ¦‹


✨ SYNOPSIS ✨

dblink_get_result(text connname [, bool fail_on_error]) returns setof record

✨ DESCRIPTION ✨

so basically dblink_get_result is the one who collects the results of an async query that you already sent off with dblink_send_query. she's the "i'll wait for you" bestie πŸ’…

and if the query isn't done yet? she just waits. no drama, no error, she'll sit there until it's completed. patient queen. πŸ‘‘


✨ ARGUMENTS ✨

  • connname β€” the name of the connection you wanna use, periodt. πŸ”Œ
  • fail_on_error β€” if it's true (which is the default when you just omit it entirely, iykyk), then any error thrown on the remote side of the connection makes an error get thrown locally too. if it's false, the remote error gets reported locally as a NOTICE instead, and the function just returns no rows. she's like "noted, moving on" πŸ™ˆ

✨ RETURN VALUE ✨

okay so two vibes here, depending on what you sent:

async query β€” that's an SQL statement that returns rows. she gives you back the row(s) the query produced. to use this function you gotta specify the expected set of columns, exactly like we already talked about for dblink.

async command β€” that's an SQL statement that does not return rows. she gives you back a single row with a single text column containing the command's status string. and yes, you still have to specify that the result will have a single text column in the calling FROM clause. don't skip that part bestie, it's required. πŸŽ€


✨ NOTES ✨

🚨 real talk: this function must be called if dblink_send_query returned 1. and it must be called once for each query sent, plus one additional time to obtain an empty set result, before the connection can be used again. yes, the extra one is mandatory. she's thorough like that. πŸ’€

and here's the tea about memory: when you're using dblink_send_query and dblink_get_result together, dblink fetches the entire remote query result before returning any of it to the local query processor. so if the query returns a large number of rows, that can cause transient memory bloat in your local session. 😭

what's better? open that kind of query as a cursor with dblink_open and then fetch a manageable number of rows at a time. ✨ alternatively, just use plain dblink(), which avoids memory bloat by spooling large result sets to disk. pick your fighter, they're both valid. 🫢


✨ EXAMPLES ✨

here's the whole saga, verbatim, no edits, slay:

contrib_regression=# SELECT dblink_connect('dtest1', 'dbname=contrib_regression');
 dblink_connect
----------------
 OK
(1 row)

contrib_regression=# SELECT * FROM
contrib_regression-# dblink_send_query('dtest1', 'select * from foo where f1 < 3') AS t1;
 t1
----
  1
(1 row)

contrib_regression=# SELECT * FROM dblink_get_result('dtest1') AS t1(f1 int, f2 text, f3 text[]);
 f1 | f2 |     f3
----+----+------------
  0 | a  | {a0,b0,c0}
  1 | b  | {a1,b1,c1}
  2 | c  | {a2,b2,c2}
(3 rows)

contrib_regression=# SELECT * FROM dblink_get_result('dtest1') AS t1(f1 int, f2 text, f3 text[]);
 f1 | f2 | f3
----+----+----
(0 rows)

contrib_regression=# SELECT * FROM
contrib_regression-# dblink_send_query('dtest1', 'select * from foo where f1 < 3; select * from foo where f1 > 6') AS t1;
 t1
----
  1
(1 row)

contrib_regression=# SELECT * FROM dblink_get_result('dtest1') AS t1(f1 int, f2 text, f3 text[]);
 f1 | f2 |     f3
----+----+------------
  0 | a  | {a0,b0,c0}
  1 | b  | {a1,b1,c1}
  2 | c  | {a2,b2,c2}
(3 rows)

contrib_regression=# SELECT * FROM dblink_get_result('dtest1') AS t1(f1 int, f2 text, f3 text[]);
 f1 | f2 |      f3
----+----+---------------
  7 | h  | {a7,b7,c7}
  8 | i  | {a8,b8,c8}
  9 | j  | {a9,b9,c9}
 10 | k  | {a10,b10,c10}
(4 rows)

contrib_regression=# SELECT * FROM dblink_get_result('dtest1') AS t1(f1 int, f2 text, f3 text[]);
 f1 | f2 | f3
----+----+----
(0 rows)

see how she keeps calling until she hits that empty set? that's the "one additional time" thing. she's nothing if not consistent. πŸ’–


okay that's the whole page bestie!! 🌸 this came from the PostgreSQL 18.6 docs, dated 2026, and the manual page is DBLINK_GET_RESULT(3). go forth and async responsibly. πŸ§šβœ¨πŸ’œ

πŸ“¦ where this page comes from