Re: Partitioning and unique key

From: veem v <veema0000(at)gmail(dot)com>
To: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
Cc: Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>, pgsql-general <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: Partitioning and unique key
Date: 2024-09-01 04:33:21
Message-ID: CAB+=1TXH0F4vSx25iiz+c_PDrx=REc7toV0KaKiQySABQvzisQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Sun, 1 Sept 2024 at 09:13, David G. Johnston <david(dot)g(dot)johnston(at)gmail(dot)com>
wrote:

> On Saturday, August 31, 2024, veem v <veema0000(at)gmail(dot)com> wrote:
>
>>
>>
>> iii)And then alter the datatype of the partition key transaction_date to
>> DATE in one shot at the table level(which should be fast as its having more
>> granularity as compare to existing timestamptype, so should be catalog or
>> dictionary change only), and that will remain the part of composite PK
>> (transaction_id,transaction_date).
>>
>
> While this might seem logical, in reality date and timestamptz are
> different fixed-width data types and thus any attempt to change from one to
> the other will involve a table rewrite. Best you could do is leave the
> timestamptz in place and just truncate to day so the time is always
> midnight UTC.
>
>
>>
>>
Here , if we keep the PK column as is i.e. the transaction_timestamp as
timestamptz but truncate the time component , in that case again in future
if someone tries to insert(using insert on conflict) data into the table
with time component , it will get consumed and will not be restricted by
the PK constraint. So I was trying to make the data type also as DATE for
the transaction_timestap column.

As in this case anyway we have to create another column to populate the
date+timestamp values as we cant throw those values away per business need,
so we will be kind of rewriting the table.So is it okay if if we will

1) Detach all the partitions.
2)Do the alter using "only" key word in table level. (For adding new column
transaction_timestamp_new to hold date+timestamp value and also altering
the existing transaction_timestamp column to DATE from type timestamptz).
3)Then do the data fix(delete the duplicates) and alter the column, one
partition at a time for all of the partitions and once done , attach those
partitions one by one.
5)Rename the columns at table level.Hope this won't need any table rewrite.

Is there any downside if we go by the above approach?

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message veem v 2024-09-01 06:08:15 Re: Partitioning and unique key
Previous Message David G. Johnston 2024-09-01 03:43:06 Re: Partitioning and unique key