DBLINK_GET_PKEY(3)

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

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