DBLINK_B...QL_UPDATE(3)

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

okay bestie, so this is a page straight out of the PostgreSQL 14.24 Documentation and it's the glow-up guide for dblink_build_sql_update β€” the little function that builds you an UPDATE statement out of a local tuple, swapping the primary key values for some alternate ones you hand it. iconic behavior honestly πŸŽ€


🌸 NAME 🌸

dblink_build_sql_update β€” builds an UPDATE statement using a local tuple, replacing the primary key field values with alternative supplied values. that's the whole assignment and she ate πŸ’…


πŸ¦‹ 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

πŸ’– DESCRIPTION πŸ’–

so dblink_build_sql_update is lowkey the main character when you're doing selective replication of a local table over to a remote database. here's the vibe: it selects a row from the local table based on primary key, and then it builds an SQL UPDATE command that will duplicate that row β€” except the primary key values get replaced by the values in the last argument. 🫢

and like, if you want an exact copy of the row? just specify the same values for the last two arguments. easy. ✨

the UPDATE command always assigns all fields of the row β€” no picking and choosing, she's doing the whole row or nothing. the main difference between this and dblink_build_sql_insert is that here it's assumed that the target row already exists in the remote table. no cap. 😌


🧚 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. πŸ’œ
  • primary_key_attnums β€” attribute numbers (1-based) of the primary key fields, for example 1 2. πŸ”’
  • num_primary_key_atts β€” the number of primary key fields.
  • 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. 😭 (yes, she will absolutely let you know.)
  • 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. 🌈

πŸ‘‘ RETURN VALUE πŸ‘‘

returns the requested SQL statement as text. that's it, that's the gift. 🎁


πŸ’€ NOTES πŸ’€

okay so important lore: 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 those numbers as physical column positions. 🚨

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 like, mind the gap bestie. πŸ™ˆ


⭐ EXAMPLES ⭐

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 her go. πŸ”₯


reminder that all those literal values β€” '1 2', the arrays, the field assignments β€” need to stay exactly as written. copy-paste is your bestie here, not ✨vibes✨.

and that's the whole page, bestie 🫢 this was written for PostgreSQL 14.24, dated 2026 β€” go forth and replicate selectively, you gorgeous nerd. πŸ’–βœ¨πŸŽ€

πŸ“¦ where this page comes from