DBLINK_GET_PKEY(3)

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

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 example foo or myschema.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