Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1skbUj-001RU7-RC for pgsql-general@arkaria.postgresql.org; Sun, 01 Sep 2024 03:43:18 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1skbUi-008GXw-5O for pgsql-general@arkaria.postgresql.org; Sun, 01 Sep 2024 03:43:16 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1skbUh-008GXn-Or for pgsql-general@lists.postgresql.org; Sun, 01 Sep 2024 03:43:16 +0000 Received: from mail-oa1-x35.google.com ([2001:4860:4864:20::35]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1skbUb-00054q-NJ for pgsql-general@lists.postgresql.org; Sun, 01 Sep 2024 03:43:15 +0000 Received: by mail-oa1-x35.google.com with SMTP id 586e51a60fabf-273cf2dbf7dso1725958fac.1 for ; Sat, 31 Aug 2024 20:43:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725162187; x=1725766987; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:references:in-reply-to :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=gw92Z2tipaINCnkv4zb/+UlimFn6HVIggIeVUqZ0YGc=; b=bcA63G02lvNs++/2iF6kseN58JuqMaUZqxdtVcTVhJptqrFxN93a3WlJOG6WP54y57 sQGzVPsvh+fdqfJVvUycU7n3AJmqQV+HScDg0rgZw2wEGEFmRFZgPoBtzhNSowAuZSLd AL5j4MaD1cw93yKenronf333acDFmSBvODNKJajU+3pm4szerB33gWQ8bXG7ZybB83ZF xFUe6PwMSXRbipqa/7N1anzCw5BZgygFoUzlr3OW35ThEPtRNfLXMnOSxWx8+gEg4jvI EtqwL88Nh07rZVNedPtQqtOUZ4RkNRPaZvkm5rKO/vcp7EEFNzQ0xePBcNHl9B8ec7Nv Ruqg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725162187; x=1725766987; h=cc:to:subject:message-id:date:from:references:in-reply-to :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=gw92Z2tipaINCnkv4zb/+UlimFn6HVIggIeVUqZ0YGc=; b=HUHuSBxh2xtU7LML0YbcGtW1XY9VAdPXU6ICOZFtnK5yXoVKS9N6iXp+7ZDjKilDPT 0JMpzYGGZY4k4YC9fiLqnGEznvenW4XqDThmbpkpC9/u4Ij8cCM+LmTxtr/vwEEpUoTv xeM6YgOCyrPkju9kFZrD5gq+I0FGG7/q/zMdS2xl1uEbkE1+uDpm1Ln/I6AOctO+aad2 +WofLnzTW6rNzYVcHKQvSSgFzO0g/mB+ymIwTYuNrojWHv9VU6vrflvawF0AdeaHWiSK qfzUXO98nWRA29RDqMVN9jPWyZAH2fqXVJhDQdTuXf2zdG+Ag/qAt/Cn/SSlyfdQNFOG qOQw== X-Forwarded-Encrypted: i=1; AJvYcCVABjAUnK6Ze9Y5hPtDUesj5dxZxjgQGcSL2ny+ViflvCke+pu7qZBx964c8wXNMhZPMy1rGeru5LqRS+6F@lists.postgresql.org X-Gm-Message-State: AOJu0Yx4M/gQH5GoQdFoytespmjdqF/TttKXtsxnxqu7shhqXnC5ao9V TVsNFGaUlBfx5KpFl4m4tV15zpuA3ZCPP16nrrmCjZF4FGe+7Cj0Wf2rmpFw5iKYD07S2XnIfmJ S4e1uMsV8Rowl2ntP1eRREGtivJ0= X-Google-Smtp-Source: AGHT+IHjqgfQQq4mKHuax19Ql8Ig/9v27snGvJY2ceN+EEBSPpmhHtcNdaVqjPl5BhokhmmV8TQ69+NLAnjrAd7RdHA= X-Received: by 2002:a05:6871:c082:b0:277:cc6e:15de with SMTP id 586e51a60fabf-277cc6f6555mr2717768fac.4.1725162187329; Sat, 31 Aug 2024 20:43:07 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:ac9:6694:0:b0:557:c384:fb61 with HTTP; Sat, 31 Aug 2024 20:43:06 -0700 (PDT) In-Reply-To: References: <7fa81130-0b33-4e53-bb32-39d84f06c680@aklaver.com> From: "David G. Johnston" Date: Sat, 31 Aug 2024 20:43:06 -0700 Message-ID: Subject: Re: Partitioning and unique key To: veem v Cc: Adrian Klaver , pgsql-general Content-Type: multipart/alternative; boundary="0000000000005b1adf0621069f72" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --0000000000005b1adf0621069f72 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Saturday, August 31, 2024, veem v wrote: > > 1) if it's technically possible to have a unique key on only the > transaction_id column having the partition key on the > transaction_timestamp, because the table is going to be queried/purged > based on the transaction_timestamp? > There is presently no such thing as a cross-partition unique constraint. If you define the constraint on the [partitioned] table the documentation is perfectly clear, as are I believe the error messages, that it will require all partitioning columns to be included - since that is what happens in reality. If you target the partitions directly with the unique index or constraint no such limitation should exist. > > 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 mo= re > 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. > iv) Repeat this step for all child partition tables and then for the > parent partition tables. > I=E2=80=99d suggest trying to just build a new partitioned table that is co= rrectly defined. Then populate it. Add a trigger to the existing one to keep the new one in sync. Then change your application code to point to the new partitioned table. At which point the old partitioned table can be dropped= . David J. --0000000000005b1adf0621069f72 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Saturday, August 31, 2024, veem v <veema0000@gmail.com> wrote:
<= div dir=3D"ltr">

1) if it's technically possible to hav= e a unique key on only the transaction_id column having the partition key o= n the transaction_timestamp, because the table is going to be queried/purge= d based on the transaction_timestamp?

There is presently no such thing as a cross-partition uniq= ue constraint.=C2=A0 If you define the constraint on the [partitioned] tabl= e the documentation is perfectly clear, as are I believe the error messages= , that it will require all partitioning columns to be included - since that= is what happens in reality.=C2=A0 If you target the partitions directly wi= th the unique index or constraint no such limitation should exist.
=C2=A0

iii)And then alter the datatype of the pa= rtition key transaction_date to DATE in one shot at the table level(which s= hould be fast as its having more granularity as compare to existing timesta= mptype, so should be catalog or dictionary change only), and that will rema= in the part of composite PK (transaction_id,transaction_date).

While this might seem logica= l, 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 rewr= ite.=C2=A0 Best you could do is leave the timestamptz in place and just tru= ncate to day so the time is always midnight UTC.
=C2=A0
iv) Repeat this step for all child partition tables and then for the paren= t partition tables.

I=E2= =80=99d suggest trying to just build a new partitioned table that is correc= tly defined. Then populate it.=C2=A0 Add a trigger to the existing one to k= eep the new one in sync.=C2=A0 Then change your application code to point t= o the new partitioned table.=C2=A0 At which point the old partitioned table= can be dropped.

David J.
--0000000000005b1adf0621069f72--