DBLINK_GET_PKEY(3)

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

okay so bestie, we're looking at page DBLINK_GET_PKEY(3) from the PostgreSQL 17.11 Documentation and honestly? it's a little icon and you're gonna love it πŸŽ€


✨ 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 literally hands you the info about the primary key of a relation in the local database, no cap. It's the vibe check for your PK 🫢

And why do we care? Because this is sometimes useful in generating queries to be sent to remote databases. That's the whole thing. That's the assignment. ✨

πŸ¦‹ ARGUMENTS πŸ¦‹

  • relname β€” the name of a local relation, like foo or myschema.mytab. Simple enough, right? But listen πŸ‘€ if the name is mixed-case or contains special characters you gotta wrap it in 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, be careful πŸ’€

πŸ‘‘ RETURN VALUE πŸ‘‘

You get one row for each primary key field. That's it. And if the relation has no primary key? zero rows. nothing. an empty vibe 😭

The result row type is defined as:

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

Now about position β€” it simply runs from 1 to N, and here's the thing that trips people up: it is the number of the field within the primary key, not the number within the table's columns. iykyk 🧚 so don't go assuming position matches up with your column order, because it does not πŸ’…

🌈 EXAMPLES 🌈

okay here's the whole thing in action, gorgeous:

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)

ate and left no crumbs πŸ”₯ three fields, three rows, positions 1 through 3, exactly what we said.


πŸ“ quick reminder: quote your mixed-case names or the lower-case folding will get you, and remember position is the field's spot in the primary key β€” not in the table. mwah πŸ’‹

and that's the page!! slay πŸ’œ this was PostgreSQL 17.11, dated 2026, page DBLINK_GET_PKEY(3) β€” see you in the next man page bestie, muah πŸŽ€πŸ’–β­

πŸ“¦ where this page comes from