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 1slMTw-0075ag-KY for pgsql-general@arkaria.postgresql.org; Tue, 03 Sep 2024 05:53:37 +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 1slMTv-007KyB-KD for pgsql-general@arkaria.postgresql.org; Tue, 03 Sep 2024 05:53:35 +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 1slMTv-007Ky3-4u for pgsql-general@lists.postgresql.org; Tue, 03 Sep 2024 05:53:35 +0000 Received: from mail-ej1-x632.google.com ([2a00:1450:4864:20::632]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1slMTs-000PME-Sw for pgsql-general@lists.postgresql.org; Tue, 03 Sep 2024 05:53:34 +0000 Received: by mail-ej1-x632.google.com with SMTP id a640c23a62f3a-a83562f9be9so448743566b.0 for ; Mon, 02 Sep 2024 22:53:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20230601.gappssmtp.com; s=20230601; t=1725342811; x=1725947611; 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=mRBVPYv1z8QnL6UW14z48NCImuOY7tsMP4kgmurLYSU=; b=2EuRJqxRc9D1Uv7EwSfGC2K1V96V75wCjV3fmmCQEhgf2H+XxivmdFZ/eLu30VdW+A 5+Cap2DSFWmOONCXPlkwC7zhbYzXl0YxAIP0muO9oThotVgx3O6UOOJiFBslbZPAfO4S bQN3sZh6QQT0970jEPxiQ0+utPZETlWfhLB9AQz0jG4m6pyyFy5JnczG1Tz0tDaQCo9L CAypqh/kNhyOwkzMx3SFOm14CKMxJLideSVbgjhin5v8Oxt17xaxVphw7cnB//DWu4yA FxClMxEqH/svIo23+ivf+r3P62mBXPByyItktfImulwIgAmSCS0rL+1nytOByLNwZzcm Vqfw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725342811; x=1725947611; 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=mRBVPYv1z8QnL6UW14z48NCImuOY7tsMP4kgmurLYSU=; b=BXBPSoQr+BB9ucjMMlwr9M2WzR6zpBkFLjQetYIo57bjKRyIxaQX/wTZ2N0viCAP9F /Ja8JX0RR0cdXyuYnSsgsD+HzB/spZyp3mQ8QDfxKY/3zqtHMXZcJrPWeufslDozEeaW yDiMHVLF0XbTQTkxHCkvjZYim4XhXw/cohdNnntmd1VPGDnNox0aQUTia1Og3nEvDqKT MngoCa63ClWpaZUPOjwP+XlEbw8lkD8LGiHVhbzWlc5IpFZiNr3yapC2DhlpIjtEKH7s Q0yRhqKIjlEDJY+rjK92ZvY/aDt1xXP/+Q3fq91yLEeqGv+PM1QEaWutZmaT/6xAhzOu pP0g== X-Gm-Message-State: AOJu0YyD6dJVoY6+EFbixcVgFlGLc0mXcxJGlxtUCQ4qQ0k57UmE+3la Xh8CJq4tLE7Gmdsaa/7GYy1HRa9POShSm+XbWXZJjGwpMjh1QyXVzO+oMxl6kvc= X-Google-Smtp-Source: AGHT+IFt2t0u7c/F+nCP5BEW2GjGfIHT14bxk3GAOivXYfRMAp9zNmAy8Dv4Y6RIJGV0Tb6rDCsJzw== X-Received: by 2002:a17:907:3f95:b0:a80:f6a9:c311 with SMTP id a640c23a62f3a-a89b906997dmr748131666b.0.1725342810227; Mon, 02 Sep 2024 22:53:30 -0700 (PDT) Received: from dynamic-pd01.res.v6.highway.a1.net ([2001:871:5e:b3f6:f9da:3dd8:46f1:b34b]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a89891968a3sm648523066b.142.2024.09.02.22.53.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Sep 2024 22:53:30 -0700 (PDT) Message-ID: <1b018e6f3df43c4f6c8f3c2b2280ba411c38581a.camel@cybertec.at> Subject: Re: Partitioning and unique key From: Laurenz Albe To: veem v Cc: pgsql-general Date: Tue, 03 Sep 2024 07:53:29 +0200 In-Reply-To: References: <7beda414a58970f917acf7959810cd9eeb94af89.camel@cybertec.at> 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 Tue, 2024-09-03 at 10:39 +0530, veem v wrote: > As you rightly said "they will make it more difficult to detach a partiti= on." , > 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 ke= y > defined table level or it's because we have FK defined in table level > (for multiple child tables which are also partitioned)? I'd say it is because of the foreign key. If you have a foreign key that points to a partitioned table, and you detac= h a partition, PostgreSQL has to verify that that won't violate the constrain= t, so it has to scan the tables, which will take time if the partitions are la= rge. > We were thinking it's because we have FK defined on tablelevel=C2=A0, so = we were > planning to make the FK on partition level. Good move. > But as you just pointed now , even keeping the PK on table level will als= o > make the detach partition slow? I understand, for detaching partitions , > it may be scanning while child because of the FK defined on the table lev= el. > but i am unable to understand how the table level PK impacts the detach > partition from parent here. No, a primary key on the partitioned table won't be a problem for performan= ce. My concern was that if what you really would like is "id" to be unique, how= does a primary key on (id, some_timestamp) benefit you? > My understanding is PK can only be created on table level but not on the > partition level. On the partition level we=C2=A0only can have a "unique i= ndex" > defined. Correct me if my understanding is wrong. No, you can define a primary key on the partition. That is, if you have no primary key on the partitioned table. A primary key on the partitioned tab= le is a primary key on each partition, and a table can only have a single prim= ary key, so adding another primary key on the partition would cause an error. Yours, Laurenz Albe