DBLINK_B...QL_UPDATE(3)

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

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 example foo or myschema.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 example 1 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 resulting UPDATE command. 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 in primary_key_attnums are interpreted as logical column numbers, corresponding to the column's position in SELECT * 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