DBLINK_GET_RESULT(3)
π DBLINK_GET_RESULT π
okay bestie, welcome to the dblink_get_result page β it's a PostgreSQL 16.15 joint and honestly it's kind of the payoff function, the one you call after you've already yeeted a query into the void with dblink_send_query. let's get into it π«Ά
β¨ NAME β¨
dblink_get_result β literally the function that gets you an async query result. that's it, that's the whole vibe. π
πΈ SYNOPSIS πΈ
dblink_get_result(text connname [, bool fail_on_error]) returns setof record
π¦ DESCRIPTION π¦
so here's the tea: dblink_get_result collects the results of an asynchronous query that you previously sent off with dblink_send_query. it's the "come back and pick up your food" of SQL functions, ngl.
and if the query is not already completed? it doesn't just bail β it waits until it is. patient queen behavior. β
π ARGUMENTS π
connnameβ the name of the connection to use. like, which friend are we talking to rn. you know the one.fail_on_errorβ iftrue(which is the default when you just... don't pass it, we've all been there), then an error thrown on the remote side of the connection causes an error to also be thrown locally. iffalse, the remote error gets reported locally as aNOTICEinstead, and the function returns no rows. so like, quieter, but emptier. π
π RETURN VALUE π
okay this part actually has two whole moods, depending on what you sent:
- For an async query (that is, an SQL statement returning rows), the function returns the row(s) produced by the query. and β important β to use this function, you will need to specify the expected set of columns, exactly as previously discussed for
dblink. no guessing, bestie, you gotta declare. - For an async command (that is, an SQL statement not returning rows), the function returns a single row with a single text column containing the command's status string. and yes, it is still necessary to specify that the result will have a single text column in the calling
FROMclause. it's giving "one row, one column, one job." π
π₯ NOTES π₯
pay attention here, this is the part that will get you if you skip it π
This function must be called if dblink_send_query returned 1. it must be called once for each query sent, and one additional time to obtain an empty set result, before the connection can be used again. like, literally. the connection is not free until you do this. it's in its "do not disturb" era. π
and now the memory warning: when using dblink_send_query and dblink_get_result, 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, this can result in transient memory bloat in the local session. not cute. not slay. π
so what do you do instead? two options, both valid:
- It may be better to open such a query as a cursor with
dblink_openand then fetch a manageable number of rows at a time. little sips, not the whole drink. π - Alternatively, use plain
dblink(), which avoids memory bloat by spooling large result sets to disk. writing it down instead of holding it all in your head, we love a coping mechanism. π
π EXAMPLES π
here's a whole little story in four acts. enjoy π§
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 you keep calling it until you get that empty (0 rows)? that's the "we're so done here" signal. the connection is free. you may vibe again. β¨
and that's the page!! π this whole thing comes to you from the PostgreSQL 16.15 docs, dated 2026 β okurrr, go be asynchronous and responsible about it π π¦
π¦ where this page comes from
- opensuse-leap 16.0: postgresql16-docs 16.15-160000.1.1 Β· PostgreSQL
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms