DBLINK_B...QL_UPDATE(3)
β¨ DBLINK_BUILD_SQL_UPDATE π
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 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. πprimary_key_attnumsβ attribute numbers (1-based) of the primary key fields, for example1 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 resultingUPDATEcommand. 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
- opensuse-leap 16.0: postgresql14-docs 14.24-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