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 1slLnl-0070U9-Oe for pgsql-general@arkaria.postgresql.org; Tue, 03 Sep 2024 05:10:02 +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 1slLnk-006tbL-Vu for pgsql-general@arkaria.postgresql.org; Tue, 03 Sep 2024 05:10:01 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1slLnk-006tbC-LR for pgsql-general@lists.postgresql.org; Tue, 03 Sep 2024 05:10:00 +0000 Received: from mail-lf1-x12a.google.com ([2a00:1450:4864:20::12a]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1slLni-000OUe-5p for pgsql-general@lists.postgresql.org; Tue, 03 Sep 2024 05:09:59 +0000 Received: by mail-lf1-x12a.google.com with SMTP id 2adb3069b0e04-5334b0e1a8eso6370807e87.0 for ; Mon, 02 Sep 2024 22:09:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725340196; x=1725944996; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=a/wKPsVfDldtFmU9V6W143S8oO9hvAYvO5G+KfglDOA=; b=F5vxehb29nYkYZ+GAkNLoDfzH48TsVjWnBa6hJ6Plthjgfo8SDqGzk1VzlsWZtd+7k cjgf4hXyVj7IYy9pOEJ9a27+CPd+fApo2DDuOdUur2oaDYwJ1xTkf+sxnYGxMGaKJ7Pe g2FhD8nVwspJoOEf/leEqBKDxSrDApM1xc249SyV95sj1oSlS2S3bTPlO82ekn5V0t+k ms35GJd5b7ZaIxF2Mz2Quzuur1RWMPPWyBS8mxmcuPpyTWHGuRuubzLnVtR+bOYEkYik /yXgle33X7WuD8lP7msRJleeR9ZpfMVKTHYLF0h9VUw1hpPZfpRRZ4UTZPyh26JgDs91 OcMQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725340196; x=1725944996; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=a/wKPsVfDldtFmU9V6W143S8oO9hvAYvO5G+KfglDOA=; b=LJl4FYGcidTqy/3UNC3RgJ4j0YDcLgM7dw4mLO0XIi/nTX47Haz9lZBHpUWx1+sxJW rchakBOhE8yVja8Kmt/bRKTvIWBOBm2RYe1nJkIwCp0QvZf7pywBMoyAolDLAbVVdfu1 SbkqQNy9IZ7ylm3U0HGnUl/qycxXRCEdHYxHnw0qtCBOtX62V3hjH4q3UL2tYV+hPxvm rcX925kxFQydIMzVlJy4iV9Szm9CUS4dxuLD+JCmIjbhTtaVUhrZOOjw33O9iKAWQIA7 xgUgXOSkqBuPwfrWeTQq6gh87uCLy12YjUx7QmmzntKr7inCE5qRBO2FO4gtDrMUhPLQ 2cew== X-Gm-Message-State: AOJu0Yx6ibudUuIUT70w8AHXANiTYygAM50V8+DrdnbrUiY8u5c9/rUr ngYZBQDQk9KphYx++e8IuSCeY4g8SlYGbFVDPEHRwPjpRG/oSrjmdwefGf/L5k51365RwpFYOxa Dhqzg9glyz0pCZ2t2bb4cDBWEnhg= X-Google-Smtp-Source: AGHT+IFZcUUUIzOJzsyLwRA4StyAc8xazOpQkP48Tgp4X9+sGIJI7F/myFxMRc3Qf73eLETpd2yol0IPi7nZSNyfxjI= X-Received: by 2002:a05:6512:1154:b0:533:3268:b959 with SMTP id 2adb3069b0e04-53546ba8fe9mr7563174e87.53.1725340195335; Mon, 02 Sep 2024 22:09:55 -0700 (PDT) MIME-Version: 1.0 References: <7beda414a58970f917acf7959810cd9eeb94af89.camel@cybertec.at> In-Reply-To: <7beda414a58970f917acf7959810cd9eeb94af89.camel@cybertec.at> From: veem v Date: Tue, 3 Sep 2024 10:39:42 +0530 Message-ID: Subject: Re: Partitioning and unique key To: Laurenz Albe Cc: pgsql-general Content-Type: multipart/alternative; boundary="00000000000075b97b0621301186" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --00000000000075b97b0621301186 Content-Type: text/plain; charset="UTF-8" On Tue, 3 Sept 2024 at 01:14, Laurenz Albe wrote: > > You can keep the primary key defined on both columns if it is good enough > for you. > But it will give you lower guarantees of uniqueness: with that primary > key, there could > be two rows with a different timestamp, but the same "txn_id", and these > two rows could > be in the same partition... > > Also, if you need a foreign key pointing *to* the partitioned table, you > cannot do without > a primary key. But I recommend that you do *not* define such foreign > keys: they will make > it more difficult to detach a partition. > > If you partition two tables in the same way, you can use foreign keys > between the partitions > instead of foreign keys between the partitioned tables. Such foreign keys > won't be a problem. > > Thank You so much. As you rightly said *"they will make it more difficult to detach a partition." , *we are really seeing a longer time when detaching parent table partitions.It runs forever sometimes. So do you mean it's because we have primary key defined table level or it's because we have FK defined in table level (for multiple child tables which are also partitioned)? We were thinking it's because we have FK defined on tablelevel , so we were planning to make the FK on partition level. But as you just pointed now , even keeping the PK on table level will also make the detach partition slow? I understand, for detaching partitions , it may be scanning while child because of the FK defined on the table level. but i am unable to understand how the table level PK impacts the detach partition from parent here. My understanding is PK can only be created on table level but not on the partition level. On the partition level we only can have a "unique index" defined. Correct me if my understanding is wrong. --00000000000075b97b0621301186 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

On Tue, 3 Sept 2024 at 01:14, Laurenz Alb= e <laurenz.albe@cybertec.at<= /a>> wrote:
<= br> You can keep the primary key defined on both columns if it is good enough f= or you.
But it will give you lower guarantees of uniqueness: with that primary key,= there could
be two rows with a different timestamp, but the same "txn_id", an= d these two rows could
be in the same partition...

Also, if you need a foreign key pointing *to* the partitioned table, you ca= nnot do without
a primary key.=C2=A0 But I recommend that you do *not* define such foreign = keys: they will make
it more difficult to detach a partition.

If you partition two tables in the same way, you can use foreign keys betwe= en the partitions
instead of foreign keys between the partitioned tables.=C2=A0 Such foreign = keys won't be a problem.




--00000000000075b97b0621301186--