DBLINK_FETCH(3)
π dblink_fetch β like, the manual page, bestie
okay so this is dblink_fetch(3), straight out of the PostgreSQL 17.11 docs, and I'm gonna walk you through it like we're texting at 2am π«Ά
πΈ NAME πΈ
dblink_fetch β returns rows from an open cursor in a remote database. that's it, that's the whole vibe β¨
π SYNOPSIS π
dblink_fetch(text cursorname, int howmany [, bool fail_on_error]) returns setof record
dblink_fetch(text connname, text cursorname, int howmany [, bool fail_on_error]) returns setof record
two whole signatures, pick your fighter π
π¦ DESCRIPTION π¦
so dblink_fetch fetches rows from a cursor that you already established earlier with dblink_open. like, cursor first, fetch second β that's the order, no skipping steps babe π
β¨ ARGUMENTS β¨
connnameβ the name of the connection you wanna use. omit this parameter and it'll use the unnamed connection instead, no big deal πcursornameβ the name of the cursor you're fetching from. literally the whole point lmaohowmanyβ the maximum number of rows to retrieve. the nexthowmanyrows get fetched, starting at the current cursor position, and moving forward. and once the cursor has reached its end, no more rows are produced β it's giving "we're done here" πfail_on_errorβ iftrue(which is the default when you omit it π«Ά) then an error thrown on the remote side of the connection causes an error to also be thrown locally. iffalse, the remote error is instead locally reported as aNOTICE, and the function returns no rows. so like, softer failure, y'know?
π RETURN VALUE π
the function returns the row(s) fetched from the cursor. and to use this function you will need to specify the expected set of columns β exactly like we previously discussed for dblink. don't skip that part, it's not optional, i don't make the rules π
β NOTES β
okay so listen, important one π₯ β if there's a mismatch between the number of return columns you specified in the FROM clause and the actual number of columns returned by the remote cursor, an error will be thrown. and here's the spicy part: in that event, the remote cursor is still advanced by as many rows as it would have been if the error had not occurred π the same is true for any other error occurring in the local query after the remote FETCH has been done. like, the damage is done, the cursor moved, periodt.
π EXAMPLES π
first, connect:
SELECT dblink_connect('dbname=postgres options=-csearch_path=');
dblink_connect
----------------
OK
(1 row)
then open a cursor named foo:
SELECT dblink_open('foo', 'select proname, prosrc from pg_proc where proname like ''bytea%''');
dblink_open
-------------
OK
(1 row)
and now we fetch 5 rows at a time, slay:
SELECT * FROM dblink_fetch('foo', 5) AS (funcname name, source text);
funcname | source
----------+----------
byteacat | byteacat
byteacmp | byteacmp
byteaeq | byteaeq
byteage | byteage
byteagt | byteagt
(5 rows)
again β¨:
SELECT * FROM dblink_fetch('foo', 5) AS (funcname name, source text);
funcname | source
-----------+-----------
byteain | byteain
byteale | byteale
bytealike | bytealike
bytealt | bytealt
byteane | byteane
(5 rows)
again π β and look, only 2 this time:
SELECT * FROM dblink_fetch('foo', 5) AS (funcname name, source text);
funcname | source
------------+------------
byteanlike | byteanlike
byteaout | byteaout
(2 rows)
and one more time β empty, bestie, the cursor is done π§:
SELECT * FROM dblink_fetch('foo', 5) AS (funcname name, source text);
funcname | source
----------+--------
(0 rows)
ngl the "0 rows" moment is lowkey iconic. cursor said "i have nothing left to give" π
and that's the whole page! this lil gem was brought to you by PostgreSQL 17.11, dated 2026, and it lives at the address DBLINK_FETCH(3) β go off, king ππ
π¦ where this page comes from
- opensuse-leap 16.0: postgresql17-docs 17.11-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