DBLINK_GET_PKEY(3)

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

okay bestie, gather round, because this one is literally the little function that tells you what a table's primary key is made of. it's dblink_get_pkey, it lives in the PostgreSQL 18.6 docs, and it's giving ✨schema detective✨ energy. πŸ•΅οΈβ™€οΈ


🌸 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 info about the primary key of a relation in the local database. that's the whole vibe. 🫢

and like, why do you care? because it's sometimes useful when you're generating queries to be sent to remote databases. you build the query on your side, you need to know which columns are the key columns, and boom β€” this function already knows. no cap. πŸ”₯


πŸŽ€ ARGUMENTS πŸŽ€

  • relname β€” the name of a local relation, for example foo or myschema.mytab. if the name is mixed-case or has special characters in it, you gotta include double quotes, like "FooBar". and if you don't quote it? the string gets folded to lower case, so "FooBar" and FooBar are not the same thing bestie, watch out 😭

πŸ‘‘ RETURN VALUE πŸ‘‘

you get one row for each primary key field, or no rows at all if the relation has no primary key. that's it. no drama, just rows. 🌈

the result row type is defined like this:

CREATE TYPE dblink_pkey_results AS (position int, colname text);

and about that position column β€” it simply runs from 1 to N. it is the number of the field within the primary key, not the number within the table's columns. important distinction, don't get it twisted fr fr πŸ’€


⭐ EXAMPLES ⭐

here's the whole thing in action, iconic behavior:

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 output. three rows, three key fields, all lined up and adorable. we love to see it. πŸ“πŸ§š


and that's the whole page! main character energy from start to finish. now go forth and inspect some primary keys, bestie πŸ’–

PostgreSQL 18.6 Β· 2026 Β· DBLINK_GET_PKEY(3) β€” mwah, ty for reading, slay πŸŽ€πŸ’…βœ¨

πŸ“¦ where this page comes from