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 1slCyi-0060dC-Oi for pgsql-general@arkaria.postgresql.org; Mon, 02 Sep 2024 19:44:45 +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 1slCyh-001Ro5-UU for pgsql-general@arkaria.postgresql.org; Mon, 02 Sep 2024 19:44:44 +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 1slCyh-001RlP-G6 for pgsql-general@lists.postgresql.org; Mon, 02 Sep 2024 19:44:43 +0000 Received: from mail-ej1-x636.google.com ([2a00:1450:4864:20::636]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1slCye-000KoP-KU for pgsql-general@lists.postgresql.org; Mon, 02 Sep 2024 19:44:42 +0000 Received: by mail-ej1-x636.google.com with SMTP id a640c23a62f3a-a86e9db75b9so492402066b.1 for ; Mon, 02 Sep 2024 12:44:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20230601.gappssmtp.com; s=20230601; t=1725306279; x=1725911079; darn=lists.postgresql.org; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:cc:to:from:subject:message-id:from:to:cc:subject :date:message-id:reply-to; bh=TQlq0lrnzXCUjBAQDI1GbesclzHvGOMSrjsLMVlTbVU=; b=gezecipUUcbmXW5Nq9rCrG29rODb0u5nyciGMGt+WoXcx4FpRo1LJZaqhG3c9uKqO4 qDO1SxxLsAMZwvkcMxhswVZBkAzkfeCV/EckvwdbxaV64HeOIw3i86XgPSI4jwMtlspD WeapRyWhzn+UgvpeMaLf8SFbN2XgLRS+4/hu7ae4aA+o6E+825RwrsKagWJSlMptvJiQ 83wC5yOsKMG/7ezuQAPn/e4Mn1aZlZ3muTgihnebCoTFlS89RhcbNWltELTkYFXyhwjU 9QhC0/GMzEvGvvWmNvoSbPhwp0r1DbkSKXvOxZGSnN2Xdh9KARZiNMcBb5DE0bpe4aXr 6ysw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725306279; x=1725911079; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:cc:to:from:subject:message-id:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=TQlq0lrnzXCUjBAQDI1GbesclzHvGOMSrjsLMVlTbVU=; b=BFqn2pQeScoUHhfKIS8hJfv05ePlUitkF9Sdx7fvdJjGZJ4w1epXejvkUTV9umic8r OuczhHKdHacj5KGuWfpjongV/HH0ruD/3C1fAab+t6Suo14VLahtttgvDz+7bOu/SiSV rY+x+H24f/Lna1WX9Lc/gjA8lZsC7Pw7nmBfIzgWsszeTIpC1cqSqOe58PbIOlb4lCLk cwS5ZPbxG7KVXKrGcN/EnffYEKGufCDFtSysisfVNq51CuTVALaLcl1Z02iNpaW40GKG QsKrFQ18T45IFjhsh0NdoIp2V2VByowd6KNY2z3vLt7pWaWTbalG6Yp5itoWtxRbMPHy YvHA== X-Gm-Message-State: AOJu0YzTqND1urb0myXWlUmqZsOarLcf2GVjk7IuifRBQAonuCLLMcH4 Tqhe09QUPJmGJEWe1ZgLGnVMpAzsv+NDzLY9986OhJ0Wq67t6vKBf1pMoR0KAswF1vjQNLhJFw1 k X-Google-Smtp-Source: AGHT+IFPYBgq+gN/3Lt0rGmxC8r+8glEzaeW2sZuw6UuwTknjouSmhYXPIWyNOo54S0hsHKUiOpFMw== X-Received: by 2002:a17:907:3f18:b0:a7a:bae8:f2a1 with SMTP id a640c23a62f3a-a8a1d4c3348mr94342566b.42.1725306278526; Mon, 02 Sep 2024 12:44:38 -0700 (PDT) Received: from dynamic-pd01.res.v6.highway.a1.net ([2001:871:260:d2e4:ac93:a861:74df:eb8a]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a89891d6d36sm603720166b.149.2024.09.02.12.44.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Sep 2024 12:44:38 -0700 (PDT) Message-ID: <7beda414a58970f917acf7959810cd9eeb94af89.camel@cybertec.at> Subject: Re: Partitioning and unique key From: Laurenz Albe To: veem v Cc: pgsql-general Date: Mon, 02 Sep 2024 21:44:37 +0200 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.52.4 (3.52.4-1.fc40) MIME-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Mon, 2024-09-02 at 21:39 +0530, veem v wrote: > On Mon, 2 Sept 2024 at 19:13, Laurenz Albe wro= te: > > On Sun, 2024-09-01 at 01:32 +0530, veem v wrote: > > > due to postgres limitations we are unable to have this unique constra= int or primary key > > > only on the transaction_id column, we have to include transaction_tim= estamp with it as > > > a composite key. So I want to understand from experts if there is any= possible way to > > > satisfy both partitioning on transaction_timestamp column and unique = key or pk just on > > > transaction_id only?=C2=A0 > >=20 > > No, you cannot have both. > >=20 > > Usually the solution is to *not* create a primary key on the partitione= d table > > and instead create a primary key on each partition. > >=20 > > That won't guarantee global uniqueness (and there is no way to do that)= , but it > > goes a long way by ensuring that the column is unique within each parti= tion. >=20 > So it means in our case the existing PK on table level on column (txn_id = and txn_timestamp), > we should drop that and create a unique index on each partition level and= also the same way > the foreign key also maps=C2=A0to the parent table partitions. And in tha= t case , can we follow > this as best practices to not have the=C2=A0primary=C2=A0keys defined at = the tabe level at all, but > on the partition level only, or there exist any=C2=A0down side to it too?= Please suggest. 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", and these tw= o 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. 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. Such foreign keys = won't be a problem. > Also then what I see is, it will make the data load query fail which uses= "insert on conflict" > to insert data into the table and that requires the primary key on both t= he columns to have > on table level. Yes, that is true. A disadvantage of not having a unique constraint on the= partitioned table. > Also the partition maintenance job which uses partman extension uses the = template table which > in turn uses table level properties for creating new partitions and they = will not have these > unique indexes created for the new partitions as because the unique index= property is not on > the table level but partition level. Can you share your=C2=A0thoughts=C2= =A0on these? Don't use partman. Or if you do, create the primary key yourself, after pa= rtman has created the partition. I wouldn't let the limitations of a tool govern my design choices. Yours, Laurenz Albe