Subject: Re: Regarding use of single column as primary key on partitioned table

From: Ron Johnson <[email-masked]>
Date: Sat, 28 Sep 2024 00:49:35 -0400

On Sat, Sep 28, 2024 at 12:39 AM David G. Johnston <
[email-masked]> wrote:

> On Friday, September 27, 2024, Durgamahesh Manne <
> [email-masked]> wrote:
>
>>
>> ERROR:  unique constraint on partitioned table must include all
>> partitioning columns
>> DETAIL:  PRIMARY KEY constraint on table "bet" lacks column "placedon"
>> which is part of the partition key.
>> test=>
>>
>> Can't we use primary key on singal column(betid) on partitioned table
>> rather than using composite key (placedon,betid)?
>>
>
> No.  It would be misleading to allow such a thing because a unique index
> can only span a single partition.
>

That is IMO a serious (and probably unfixable, given how PG stores tables)
flaw in PG's partitioning design.

-- 
Death to <Redacted>, and butter sauce.
Don't boil me, I'm still alive.
<Redacted> crustacean!

---

From: Tom Lane <[email-masked]>
Date: Sat, 28 Sep 2024 00:55:06 -0400

> flaw in PG's partitioning design.

You can call it a flaw if you want, but it's an intentional design
limitation.  The only way to relax it would be to invent global
indexes (that is, single indexes covering the entire partitioning
tree), which would basically throw away every advantage of making
a partitioned structure in the first place.  If that's what you
want, don't partition your table.

			regards, tom lane

---

From: Ron Johnson <[email-masked]>
Date: Sat, 28 Sep 2024 01:15:49 -0400

On Sat, Sep 28, 2024 at 12:55 AM Tom Lane <[email-masked]> wrote:

> a partitioned structure in the first place.

We've had this discussion before.  As a DBA, I don't think that requiring
that global indices be dropped before, and recreated after, ATTACH or
DETACH is onerous, but you do.

(I did that every six months for 15 years on a legacy system which supports
global indices.  One other feature that it had was the ability to create
multiple indices on the same table, at the same time, in different
transactions, by setting the isolation level to SHARED DATA DEFINITION,
which was specifically and only for that situation.)

If that's what you want, don't partition your table.

Or accept partitioning by the synthetic PK (which is often -- though not
always -- a reasonable approximation of timestamp).