DBLINK_EXEC(3)
π DBLINK_EXEC(3) β bestie's guide to running commands remotely
okay so real talk: this is the dblink_exec page from the PostgreSQL 13.23 Documentation, and it's lowkey one of the most useful things ever. buckle up π
β¨ NAME β¨
dblink_exec β executes a command in a remote database. that's it, that's the whole vibe. π
π¦ 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 whole forms, and yes bestie, you do need to know which one you're using π
πΈ DESCRIPTION πΈ
so dblink_exec executes a command β and by "command" we mean any SQL statement that doesn't return rows β in a remote database. it's giving "i don't want receipts, i just want it done" energy. π₯
and here's the cute part: when you pass two text arguments, the first one gets looked up as a persistent connection's name first. if it's found, the command runs on that connection, slay. if it's not found, then the first argument gets treated as a connection info string β exactly like the one for dblink_connect β and the indicated connection is made just for the duration of this command. so it's like a little pop-up connection that vanishes when it's done, very main character. β¨
π«Ά ARGUMENTS π«Ά
connnameβ the name of the connection to use. omit this parameter and you get the unnamed connection instead, no biggie.connstrβ a connection info string, as previously described fordblink_connect. it's the whole "where am i going and how do i get in" of it all.sqlβ the SQL command that you wish to execute in the remote database. for example,insert into foo values(0, 'a', '{"a0","b0","c0"}'). look at that nested-quote situation, iykyk πfail_on_errorβ iftrue(which is the default when omitted, so don't even worry about 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 locally reported as aNOTICE, and the function's return value is set toERROR. so basically: drama by default, chill mode on request. π
π RETURN VALUE π
returns status, either the command's status string or ERROR. that's the whole deal. π
π EXAMPLES π
okay these are iconic, watch closely:
SELECT dblink_connect('dbname=dblink_test_standby');
dblink_connect
----------------
OK
(1 row)
connected, we love to see it β¨ then:
SELECT dblink_exec('insert into foo values(21, ''z'', ''{"a0","b0","c0"}'');');
dblink_exec
-----------------
INSERT 943366 1
(1 row)
ate. then we do the named-connection era:
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)
and now the chaotic one β fail_on_error set to false, so instead of everything exploding, you get a NOTICE and an ERROR return value: π
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 how the vibe stayed intact? no crash, just a lil note. πβ¨
word of warning bestie: thatfalsepath means the remote error does not blow up locally β it's reported as aNOTICEand the return value becomesERROR. so if you're checking for success, check the return value, fr fr. π«Ά
π sign-off π
that's the whole page, no cap β straight from the PostgreSQL 13.23 Documentation, dated 2025, and yeah the manual page source is DBLINK_EXEC(3). go forth and execute remotely, bestie, you're gonna slay π§πΈ
π¦ where this page comes from
- opensuse-leap 16.0: postgresql13-docs 13.23-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