DBLINK_GET_PKEY(3)
π dblink_get_pkey(3) β the PostgreSQL 14.24 manual, but make it make sense
okay so bestie, this is a page from the PostgreSQL 14.24 Documentation, and it's about a little function called dblink_get_pkey. it's short, it's sweet, it slays. let's get into it β¨
πΈ NAME πΈ
dblink_get_pkey β returns the positions and field names of a relation's primary key fields π
like, it tells you which columns make up the primary key and in what order. that's it. that's the whole vibe.
β¨ SYNOPSIS β¨
dblink_get_pkey(text relname) returns setof dblink_pkey_results
one function, one argument, one set of rows back. iconic behaviour honestly π
π¦ DESCRIPTION π¦
so dblink_get_pkey gives you info about the primary key of a relation in the local database (emphasis on local, bestie β this is the db you're standing in, not the remote one).
and why would you care? because sometimes it's genuinely useful when you're generating queries to be sent to remote databases. yes, that's the whole use case, and honestly? it ate. π₯
π ARGUMENTS π
relnameβ the name of a local relation. for examplefooormyschema.mytab. and listen π: if the name is mixed-case or contains special characters, you gotta include double quotes, like"FooBar". without quotes, the string gets folded to lower case. no cap, that's just how it works.
π RETURN VALUE π
you get one row for every primary key field β or no rows at all if the relation has no primary key (sad but valid, we don't judge π).
the result row type is defined as:
CREATE TYPE dblink_pkey_results AS (position int, colname text);
and about that position column: it just runs from 1 to N. it's the number of the field within the primary key, not the number within the table's columns. huge difference, don't get it twisted π
π EXAMPLES π
here's the whole thing in action, bestie:
CREATE TABLE foobar (
f1 int,
f2 int,
f3 int,
PRIMARY KEY (f1, f2, f3)
);
CREATE TABLE
SELECT * FROM dblink_get_pkey('foobar');
position | colname
----------+---------
1 | f1
2 | f2
3 | f3
(3 rows)
look at that. 1, 2, 3. clean, ordered, gorgeous. the primary key said "I'll have f1, f2 and f3" and the function said "noted π
". periodt.
quick reminder bestie: that position column is the field's place in the primary key, not its place in the table's column list. iykyk, but now you know fr fr π«Ά
and that's the page! written for PostgreSQL 14.24 (the docs, dated 2026), and that's the whole DBLINK_GET_PKEY(3) situation. go forth and inspect those primary keys, you absolute main character π§π
π¦ 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