DBLINK_GET_PKEY(3)
π DBLINK_GET_PKEY π
okay so bestie, welcome to the dblink_get_pkey page from the PostgreSQL 16.15 docs β yes, that's the manual name, we're doing this, it's giving reference-section realness and honestly? iconic. let's go π«Ά
β¨ NAME β¨
dblink_get_pkey β returns the positions and field names of a relation's primary key fields πΈ
β¨ SYNOPSIS β¨
dblink_get_pkey(text relname) returns setof dblink_pkey_results
π¦ DESCRIPTION π¦
so dblink_get_pkey hands you the tea about the primary key of a relation in the local database β like, the one you're literally connected to right now, no cap π
and this is sometimes useful for generating queries to be sent to remote databases. iykyk β you build the query here, you ship it over there π₯
π ARGUMENTS π
relnameβ the name of a local relation. for examplefooormyschema.mytab. lowkey important: wrap it in double quotes if the name is mixed-case or contains special characters, like"FooBar"β because without quotes, the string gets folded to lower case. periodt π
π RETURN VALUE π
returns one row for each primary key field, or no rows if the relation has no primary key. that's it, that's the vibe check π
the result row type is defined as:
CREATE TYPE dblink_pkey_results AS (position int, colname text);
and about that position column β it simply runs from 1 to N; it's the number of the field within the primary key, not the number within the table's columns. please don't mix those up bestie i'm begging π
β EXAMPLES β
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 gorgeous output. three fields, numbered 1, 2, 3. slay β¨
fr fr though β if you remember nothing else, remember the position thing. it counts within the key, not within the table. that's the whole personality of this function ngl π
and that's the whole page, bestie!! mwah π written up in the PostgreSQL 16.15 documentation, dated 2026, filed under DBLINK_GET_PKEY(3) β go forth and get those primary keys, you absolute main character ππ
π¦ 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