DBLINK_EXEC(3)
π dblink_exec β the "go do this thing over there" function
okay bestie, welcome to dblink_exec(3), straight out of the PostgreSQL 18.6 Documentation β¨ this is the page where we yeet SQL commands into a remote database and don't even care about getting rows back. no rows, no problem, we're just here to make stuff happen π«Ά
β¨ 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
β¨ DESCRIPTION β¨
so like. dblink_exec executes a command β and by "command" we mean any SQL statement that doesn't return rows β in a remote database. that's it, that's the vibe. it's giving "do the thing, don't text me back" π΅
when you pass it two text arguments, here's the lil algorithm, no cap:
- the first argument gets looked up as a persistent connection's name first π
- if it's found, boom β the command runs on that connection π―
- if it's not found, the first argument gets treated as a connection info string, exactly like for
dblink_connect, and the connection it describes gets made just for the duration of this command β a little pop-up connection, here for a good time not a long time π¦
β¨ ARGUMENTS β¨
connnameβ the name of the connection you wanna use π omit this parameter entirely and you get the unnamed connection instead (she's mysterious, she's unnamed, she's available).connstrβ a connection info string, same as the one previously described fordblink_connectπsqlβ the SQL command you wish to execute in the remote database π for exampleinsert into foo values(0, 'a', '{"a0","b0","c0"}').fail_on_errorβ if it'strue(which is the default when you omit it, obviously, she's the default for a reason π ) then an error thrown on the remote side of the connection causes an error to also be thrown locally. if it'sfalse, the remote error gets locally reported as aNOTICEinstead, and the function's return value is set toERROR. so like... quiet mode. graceful degradation. she's unbothered π
β¨ RETURN VALUE β¨
returns status β either the command's status string or ERROR. that's the whole tea π΅
β¨ EXAMPLES β¨
okay this is the part where we actually do it, watch π
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 how the last one used false and just slid into a NOTICE instead of fully exploding? that's the move. she said "i'll tell you about it but i won't ruin your whole afternoon" πΈ
π heads up bestie: that last example is a real error happening on purpose βnull value in column "relnamespace" violates not-null constraint. thefalseis what keeps it from being thrown locally. don't @ me, that's literally what the docs show.
mwah π that's the whole page! this was dblink_exec(3) from the PostgreSQL 18.6 Documentation, dated 2026. go execute something in a remote database, you absolute icon πβ¨ bye bestie! ππ¦π
π¦ where this page comes from
- alpine 3.23: postgresql18-doc 18.6-r0 Β· PostgreSQL
- alpine 3.24: postgresql18-doc 18.6-r0 Β· PostgreSQL
- arch rolling: postgresql 18.6-4 Β· PostgreSQL
- fedora 43: postgresql-contrib 18.6-1.fc43 Β· PostgreSQL
- fedora 44: postgresql-contrib 18.6-1.fc44 Β· PostgreSQL
- nixos 26.05: postgresql 18.6
- ubuntu resolute: postgresql-doc-18 18.6-0ubuntu0.26.04.1 Β· PostgreSQL
- ubuntu stonking: postgresql-doc-18 18.6-3build2 Β· PostgreSQL
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms