DBLINK_EXEC(3)

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

okay so this is dblink_exec from the PostgreSQL 16.15 Documentation, and honestly? it's a little icon. it does one thing and it does it well: it runs a command on a remote database, no cap. let's get into it 🫢


✨ NAME ✨

dblink_exec β€” executes a command in a remote database 🌈


πŸŽ€ SYNOPSIS πŸŽ€

dblink_exec(text connname, text sql [, bool fail_on_error]) returns text
dblink_exec(text connstr, text sql [, bool fail_on_error]) returns text
dblink_exec(text sql [, bool fail_on_error]) returns text

three flavors, pick your fighter πŸ’…


πŸ¦‹ DESCRIPTION πŸ¦‹

so dblink_exec executes a command in a remote database. and when i say "command" i mean any SQL statement that doesn't return rows β€” like, don't come here asking for rows back, that's not her thing, that's giving main character energy of a different function entirely.

now here's the tea on the arguments β˜• when you give it two text arguments, the first one gets looked up first as a persistent connection's name. if it's found β€” slay, the command runs on that connection. if it's not found, that first argument gets treated as a connection info string (same deal as dblink_connect), and the connection it points to is made just for the duration of this command. pop in, do the thing, dip. iconic. ✨


🌸 ARGUMENTS 🌸

  • connname β€” name of the connection to use. omit this parameter and you get the unnamed connection. lowkey convenient πŸ’œ
  • connstr β€” a connection info string, as previously described for dblink_connect. iykyk 🧚
  • sql β€” the SQL command that you wish to execute in the remote database. for example insert into foo values(0, 'a', '{"a0","b0","c0"}') πŸ“
  • fail_on_error β€” if true (and that's the default when you omit it, bestie), then an error thrown on the remote side of the connection causes an error to also be thrown locally. fr fr, it propagates. if false, the remote error gets locally reported as a NOTICE instead, and the function's return value is set to ERROR.

⭐ RETURN VALUE ⭐

returns status β€” either the command's status string or ERROR. that's it, that's the whole vibe check πŸŽ€


πŸ”₯ EXAMPLES πŸ”₯

okay these are the good part, look at her go πŸ‘‘

SELECT dblink_connect('dbname=dblink_test_standby');
 dblink_connect
----------------
 OK
(1 row)

SELECT dblink_exec('insert into foo values(21, ''z'', ''{"a0","b0","c0"}'');');
   dblink_exec
-----------------
 INSERT 943366 1
(1 row)

SELECT dblink_connect('myconn', 'dbname=regression');
 dblink_connect
----------------
 OK
(1 row)

SELECT dblink_exec('myconn', 'insert into foo values(21, ''z'', ''{"a0","b0","c0"}'');');
   dblink_exec
------------------
 INSERT 6432584 1
(1 row)

SELECT dblink_exec('myconn', 'insert into pg_class values (''foo'')',false);
NOTICE:  sql error
DETAIL:  ERROR:  null value in column "relnamespace" violates not-null constraint

 dblink_exec
-------------
 ERROR
(1 row)

see that last one? that's fail_on_error set to false doing its whole thing β€” the remote error shows up as a NOTICE locally and the return value is literally ERROR. no dramatic meltdown, just a lil note and an ERROR string. πŸ’€


quick vibe reminder: when you're passing SQL with string literals inside, those single quotes get doubled up (''z'', ''{"a0","b0","c0"}'') β€” that's just how you escape them in a SQL string. keep it exact, bestie, don't get creative πŸ™ˆ

aaaaand that's the page!! mwah πŸ’– this was DBLINK_EXEC(3) from the PostgreSQL 16.15 Documentation, dated 2026. go execute something cute on a remote database for me, ok? periodt. πŸŽ€βœ¨πŸ‘‘

πŸ“¦ where this page comes from