DBLINK_OPEN(3)

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

okay bestie, welcome to DBLINK_OPEN(3) β€” straight from the PostgreSQL 13.23 Documentation πŸ’– this one is all about opening a cursor in a remote database, and yes it’s giving main-character database energy ✨

🌸 NAME 🌸

dblink_open β€” opens a cursor in a remote database πŸŽ€

✨ SYNOPSIS ✨

dblink_open(text cursorname, text sql [, bool fail_on_error]) returns text
dblink_open(text connname, text cursorname, text sql [, bool fail_on_error]) returns text

πŸ¦‹ DESCRIPTION πŸ¦‹

dblink_open() opens a cursor in a remote database. Once it’s open, you can lowkey manipulate that cursor with dblink_fetch() and dblink_close() β€” iconic teamwork, no cap. πŸ’…

πŸŽ€ ARGUMENTS πŸŽ€

  • connname β€” the name of the connection to use. Omit this parameter to use the unnamed connection. 🫢
  • cursorname β€” the name to assign to this cursor.
  • sql β€” the SELECT statement you want to execute in the remote database, for example select * from pg_class.
  • fail_on_error β€” if true (the default when omitted), then an error thrown on the remote side of the connection causes an error to also be thrown locally. If false, the remote error is locally reported as a NOTICE, and the function’s return value is set to ERROR.

πŸ”₯ RETURN VALUE πŸ”₯

Returns status, either OK or ERROR. simple as that, bestie ✨

πŸ‘‘ NOTES πŸ‘‘

okay so here’s the tea: a cursor can only persist within a transaction, so dblink_open starts an explicit transaction block (BEGIN) on the remote side, if the remote side was not already within a transaction. This transaction will be closed again when the matching dblink_close is executed. 🧚

big warning, bestie: if you use dblink_exec to change data between dblink_open and dblink_close, and then an error occurs or you use dblink_disconnect before dblink_close, your change will be lost because the transaction will be aborted. 😭 not dramatic, just true.

🌈 EXAMPLES 🌈

here’s the whole vibe in action:

           SELECT dblink_connect('dbname=postgres options=-csearch_path=');
            dblink_connect
           ----------------
            OK
           (1 row)

           SELECT dblink_open('foo', 'select proname, prosrc from pg_proc');
            dblink_open
            -------------
            OK
           (1 row)

that’s the DBLINK_OPEN(3) moment β€” PostgreSQL 13.23, dated 2025. byeee bestie, go open those remote cursors responsibly πŸ’œπŸ«Ά

πŸ“¦ where this page comes from