DBLINK_B...QL_UPDATE(3)
π DBLINK_B...QL_UPDATE(3) π
okay bestie, so this is the dblink_build_sql_update page from the PostgreSQL 17.11 Documentation, and honestly? it's lowkey iconic. it's a (3) manpage, it's giving "library function that nobody talks about but everyone needs," and we're gonna get into it. π
β¨ NAME β¨
dblink_build_sql_update β builds an UPDATE statement using a local tuple, replacing the primary key field values with alternative supplied values π¦
πΈ SYNOPSIS πΈ
dblink_build_sql_update(text relname,
int2vector primary_key_attnums,
integer num_primary_key_atts,
text[] src_pk_att_vals_array,
text[] tgt_pk_att_vals_array) returns text
like yes, that's five arguments, no we're not scared of it. π
π₯ DESCRIPTION π₯
so dblink_build_sql_update can be useful in doing selective replication of a local table to a remote database. selective replication, say it with me β because that's the whole vibe.
here's the tea β: it selects a row from the local table based on primary key, and then builds an SQL UPDATE command that will duplicate that row, but with the primary key values replaced by the values in the last argument.
(and if you want to make an exact copy of the row? just specify the same values for the last two arguments. that's it, that's the trick. π«Ά)
the UPDATE command always assigns all fields of the row β no skipping, no cherry-picking, every field gets a value, periodt. and the main difference between this and dblink_build_sql_insert is that it's assumed that the target row already exists in the remote table. so like, this is the "update your existing bestie" function, not the "make a new friend" function. iykyk. π
π§ ARGUMENTS π§
relnameβ name of a local relation, for examplefooormyschema.mytab. include double quotes if the name is mixed-case or contains special characters, for example"FooBar"; without quotes, the string will be folded to lower case. so like, unquoted names get the lowercase treatment, don't @ me. πprimary_key_attnumsβ attribute numbers (1-based) of the primary key fields, for example1 2. yeah, 1-based, not 0-based, do not come for me on this. πnum_primary_key_attsβ the number of primary key fields. simple. counts them. that's the whole job. β¨src_pk_att_vals_arrayβ values of the primary key fields to be used to look up the local tuple. each field is represented in text form. an error is thrown if there is no local row with these primary key values. π¨ so like, if it's not there, it's not there, and it's throwing hands (an error). πtgt_pk_att_vals_arrayβ values of the primary key fields to be placed in the resultingUPDATEcommand. each field is represented in text form. these are the glow-up values, the new identity of the row. π
π RETURN VALUE π
returns the requested SQL statement as text. that's the whole return value. it's a string. it slays. π
β NOTES β
as of PostgreSQL 9.0, the attribute numbers inprimary_key_attnumsare interpreted as logical column numbers, corresponding to the column's position inSELECT * FROM relname. previous versions interpreted the numbers as physical column positions. π¨
and like, there is a difference if any column(s) to the left of the indicated column have been dropped during the lifetime of the table. so if you're on a table with a messy past (no judgement, we've all been there π), be aware that logical β physical and this is exactly the kind of thing that bites people. not me though, i read the notes. π
π EXAMPLES π
okay so here's what it looks like when it eats:
SELECT dblink_build_sql_update('foo', '1 2', 2, '{"1", "a"}', '{"1", "b"}');
dblink_build_sql_update
-------------------------------------------------------------
UPDATE foo SET f1='1',f2='b',f3='1' WHERE f1='1' AND f2='b'
(1 row)
look at that. f2 went from 'a' to 'b', every field got assigned, and the whole thing came back as one tidy text row. main character energy, fr fr. π¦β¨
ngl if you got this far you actually understand it now, and that's on you being a scholar. π
okay that's the page, bestie β and for the record, this whole thing came from the PostgreSQL 17.11 docs, dated 2026, filed under DBLINK_B...QL_UPDATE(3). go forth and replicate selectively. πΈπ«Άπ
π¦ 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