DBLINK_GET_PKEY(3)
π DBLINK_GET_PKEY π
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, likefooormyschema.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"andFooBarare 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
- opensuse-leap 16.0: postgresql17-docs 17.11-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