Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lwdMX-000602-Jv for pgsql-performance@arkaria.postgresql.org; Fri, 25 Jun 2021 04:22:41 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lwdMU-0000g5-OI for pgsql-performance@arkaria.postgresql.org; Fri, 25 Jun 2021 04:22:38 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lwdMU-0000fx-BA for pgsql-performance@lists.postgresql.org; Fri, 25 Jun 2021 04:22:38 +0000 Received: from mail-io1-xd32.google.com ([2607:f8b0:4864:20::d32]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1lwdMN-0005iR-Lf for pgsql-performance@postgresql.org; Fri, 25 Jun 2021 04:22:37 +0000 Received: by mail-io1-xd32.google.com with SMTP id h2so11058475iob.11 for ; Thu, 24 Jun 2021 21:22:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=telsasoft-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:content-transfer-encoding:in-reply-to :user-agent; bh=TpMKRggtOY9zE1JTrik43cMqjM+R1roEuOb5Qv0sFgs=; b=CLFo3bPtaLLc6psMoFEKkb2UksOuvRB/+Pwkj2agxdl0v4C2wI5/5/q/mgvavh3yY5 Ujc1ZVGYE5VgkJCkbqUve4qTtotOvrUmyK7kvqlS/Bxgps2+2SLymOXbMvUTVG72SJ9I P5byQPBq3QcqyXGTA0SCO4Bj63xp74bG3jDwsJhUc+6GRd8Zz3Ze0DBL8yAJGVBdNlj5 ltOL7uh629/l6jVktiNWmgE4pE94WTz7fDi/dcd3fZJlADZAIN1qPFZpdrpMFXktJosJ yFwk4slMwEW2EdGHnYY5tyiceh3seadfTUGDqUUvCHpSK/qeD3arftQGEYBHQL1iPuHZ rlZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:content-transfer-encoding :in-reply-to:user-agent; bh=TpMKRggtOY9zE1JTrik43cMqjM+R1roEuOb5Qv0sFgs=; b=jg6Jy70WB+zeWI55IiVeLMUALEdEPb/GURqCB8qqzpIVU6GB/HP09HB8qOKNBEBzHp hxKQSZZafAH+0v2lKsbuOuq2ikaMZUlOXxsvWdhWSWkyiBJgKg0SG9R9kZDyYFmY3rc0 /nICuh2HhY6cT/Bzv5EmYM+LIHqbdtF8/jn00wgzrVFpKQT5RwqPvu01N43VARL+q6rs 01Gnpla48zKHm8Hl3KdgNYdySGc+O3Bkatyq7a3UdVKnqTSlwttfynVQNrYIoo9B0nSl h3V5hRGAQfuozIpshSeuxX+TFbG1BCSWgv4lHQDwK8L/w96iTCDfQOXck2zzo5LqCNvX 4UWA== X-Gm-Message-State: AOAM531WbU1c46OkLTxloc+p16ihuhEO/R+cv+1znAS2GkpGj4eFsKWD 7suf4LSUzPD40nfD3pCG4rHk4+fH0c2gTQ== X-Google-Smtp-Source: ABdhPJwF+OUGyenh3GDbrDL5dli6mc69/gRvqMNAX77eb9YFoCLMDWeY2OuNFm4HerlMuKsTUelaNA== X-Received: by 2002:a05:6638:13c3:: with SMTP id i3mr7734859jaj.140.1624594949459; Thu, 24 Jun 2021 21:22:29 -0700 (PDT) Received: from pryzbyj.telsasoft (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id c22sm2564017ioz.24.2021.06.24.21.22.28 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 24 Jun 2021 21:22:29 -0700 (PDT) Received: by pryzbyj.telsasoft (Postfix, from userid 1000) id 4360380076C; Thu, 24 Jun 2021 23:22:28 -0500 (CDT) Date: Thu, 24 Jun 2021 23:22:28 -0500 From: Justin Pryzby To: Nagaraj Raj Cc: pgsql-performance@postgresql.org Subject: Re: Partition column should be part of PK Message-ID: <20210625042228.GJ29179@telsasoft.com> References: <836265572.4593017.1624590607279.ref@mail.yahoo.com> <836265572.4593017.1624590607279@mail.yahoo.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <836265572.4593017.1624590607279@mail.yahoo.com> User-Agent: Mutt/1.9.4 (2018-02-28) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Declarative partitioning was new in v10 In v11, it was allowed to create an index on a partitioned table, including unique indexes. However it's not a "global" index - instead, it's an "inherited" index. For a unique index, uniqueness is enforced within each individual index. And so global uniqueness is only guaranteed if the partition key is included in the index. On Fri, Jun 25, 2021 at 03:10:07AM +0000, Nagaraj Raj wrote: > CREATE TABLE c_account_p ( >     billing_account_guid character varying(40)  NOT NULL, >     ingestion_process_id bigint NOT NULL DEFAULT '-1'::integer, >     load_dttm timestamp(6) without time zone NOT NULL, >     ban integer NOT NULL, >     CONSTRAINT billing_account_pkey PRIMARY KEY (billing_account_guid, ban) > ) PARTITION by RANGE(load_dttm); > When I try the create table, it's throwing below error: > ERROR:  insufficient columns in the PRIMARY KEY constraint definition > DETAIL:  PRIMARY KEY constraint on table "l_billing_account_p" lacks column "load_dttm" which is part of the partition key. > > If the partition column should be supposed to be a PK, it's challenging to create a partition by range with the date column, cause the load_dttm column chances to have duplicate if data loaded COPY or INSERT.INSERT INTO c_account_p SELECT * from c_account_p_bkp ON CONFLICT (billing_account_guid,ban,load_dttm) DO UPDATE SET 'some stuff..' > > If I receive billing_account_guid, ban combination with different load_dttm then it will end up with duplicate keys. It sounds like you want a unique index on (billing_account_guid, ban) to support INSERT ON CONFLICT. If DO UPDATE SET will never move tuples to a new partittion, then you could do the INSERT ON CONFLICT on the partition rather than its parent. But it cannot be a unique, "partitioned" index, without including load_dttm. -- Justin