Re: SQL Property Graph Queries (SQL/PGQ)

From: Hannu Krosing <hannuk(at)google(dot)com>
To: assam258(at)gmail(dot)com
Cc: Junwang Zhao <zhjwpku(at)gmail(dot)com>, Ashutosh Bapat <ashutosh(dot)bapat(dot)oss(at)gmail(dot)com>, Alexander Lakhin <exclusion(at)gmail(dot)com>, Peter Eisentraut <peter(at)eisentraut(dot)org>, Amit Langote <amitlangote09(at)gmail(dot)com>, Vik Fearing <vik(at)postgresfriends(dot)org>, Ajay Pal <ajay(dot)pal(dot)k(at)gmail(dot)com>, Imran Zaheer <imran(dot)zhir(at)gmail(dot)com>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: SQL Property Graph Queries (SQL/PGQ)
Date: 2026-03-24 16:33:15
Message-ID: CAMT0RQQ243upKpB_xtTE-PBaJmCxtTvORyqCjiiv2+3Ct5TAFQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Thanks!

The SQL standard (ISO/IEC 9075-2: Foundation) defines these terms as follows:
- *Candidate Key*: Any combination of columns that can uniquely
identify a row in a table. A table can have multiple candidate keys
(for example, a Users table might have both an EmployeeID and a
SocialSecurityNumber, both of which are unique).
- *Preferred Candidate Key*: Because a table can have multiple
candidate keys, the SQL standard designates one as the "preferred" key
to represent the table when an arbitrary unique identifier is
required.
- If the table has a defined PRIMARY KEY, that primary key is
automatically the preferred candidate key.
- If the table has no primary key but has UNIQUE constraints, the
standard uses a specific set of rules (typically the "left-most"
unique constraint in the table definition) to elect the preferred
candidate key.

I guess the reason the example works in DuckDB is that they have an
implicit RowID which can be used as a fall-back "Preferred candidate
key" to make rows in table "person_knows_person" UNIQUE.

On Tue, Mar 24, 2026 at 4:07 PM Henson Choi <assam258(at)gmail(dot)com> wrote:
>>
>> > ERROR: 42P17: no key specified and no suitable primary key exists for
>> > definition of element "person_knows_person"
>> > LINE 6: Person_knows_person
>> > ^
>> > LOCATION: propgraph_element_get_key, propgraphcmds.c:334
>> > Time: 0.459 ms
>> > ------8<------------8<------------8<------------8<------------8<------------8<------------8<------
>> >
>> > It worked when I changed person_knows_person.person1id into PRIMARY KEY.
>>
>> Yes, each element is expected to have a key, either defined explicitly
>> when creating the graph or implicitly via its primary key.
>
>
> Right. Per SQL/PGQ Subclause 9.12 "Creation of an element table
> descriptor", Syntax Rule 4:
>
> a) If <element table key clause> is not specified, then
> i) If the table descriptor of ET includes a unique constraint
> descriptor UCD that specifies PRIMARY KEY, then let ETK be
> the list of the names of the unique columns included in UCD
> in order of their ordinal position in ET.
> ii) Otherwise, the table descriptor of ET shall include a
> preferred candidate key. Let ETK be the list of columns in
> that preferred candidate key in order of their ordinal
> position in ET.
>
> b) Otherwise, let ETK be the <column name list> simply contained
> in <element table key clause>.
>
> Note: our current implementation only checks for PRIMARY KEY when
> no KEY clause is specified. The standard also allows falling back
> to a "preferred candidate key", which we don't support yet.
>
>>
>> I'm not sure whether we should infer the key from the source and
>> destination keys, though it's doable.
>
>
> "Preferred candidate key" is likely defined in SQL Foundation
> (Part 2), which I don't have access to -- I suspect it refers
> to a UNIQUE NOT NULL constraint. But if the source +
> destination key columns happen to qualify as one, they would
> already be covered by rule 4.a.ii above. So rather than
> special-casing source + destination inference, implementing
> preferred candidate key support would cover that case naturally.
>
> Regards,
> Henson

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Mark Dilger 2026-03-24 16:44:27 Re: libpq: Process buffered SSL read bytes to support records >8kB on async API
Previous Message Yugo Nagata 2026-03-24 16:28:47 Re: Track skipped tables during autovacuum and autoanalyze