DBLINK_B...QL_UPDATE(3)
okay bestie, welcome to the DBLINK_B...QL_UPDATE(3) page from the PostgreSQL 16.15 docs π this one is a little function with main-character energy: it builds an UPDATE for selective replication, and it wants your primary key tea. no cap.
πΈ NAME πΈ
dblink_build_sql_update β builds an UPDATE statement using a local tuple, replacing the primary key field values with alternative supplied values. slay. π
β¨ 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 π
okay so dblink_build_sql_update can be useful in doing selective replication of a local table to a remote database. 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. (to make an exact copy of the row, just specify the same values for the last two arguments β iconic shortcut, honestly.) The UPDATE command always assigns all fields of the row β 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. periodt.
π 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"FooBar"stays cute and cased, butFooBarbecomes lowercase. iykyk.primary_key_attnumsβ attribute numbers (1-based) of the primary key fields, for example1 2. bestie, that's one-based, not zero-based.num_primary_key_attsβ the number of primary key fields. just the count, no drama.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 if the lookup misses, it's not giving "meh", it's error time.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 new-key values, and they get to be main character in the final statement. π₯
π RETURN VALUE π
Returns the requested SQL statement as text. that's it, that's the tea. π
β NOTES β
okay history lesson, because this matters: 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. 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 yes, dropped columns can change the vibe check here β do not ignore that.
π EXAMPLES π
here's the example, bestie β copy it exactly, no freestyling:
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)
mwah π this was PostgreSQL 16.15, dated 2026, signing off from the DBLINK_B...QL_UPDATE(3) page. stay iconic, bestie. πβ¨
π¦ where this page comes from
- opensuse-leap 16.0: postgresql16-docs 16.15-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