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 1s6yJA-002mrB-Ql for pgsql-general@arkaria.postgresql.org; Tue, 14 May 2024 19:59:34 +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 1s6yJA-002DCx-1O for pgsql-general@arkaria.postgresql.org; Tue, 14 May 2024 19:59:32 +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 1s6yJ9-002DCo-Ml for pgsql-general@lists.postgresql.org; Tue, 14 May 2024 19:59:31 +0000 Received: from mail-vs1-xe2b.google.com ([2607:f8b0:4864:20::e2b]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1s6yJ7-000DLy-5N for pgsql-general@lists.postgresql.org; Tue, 14 May 2024 19:59:30 +0000 Received: by mail-vs1-xe2b.google.com with SMTP id ada2fe7eead31-481e9c27378so739753137.3 for ; Tue, 14 May 2024 12:59:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1715716768; x=1716321568; darn=lists.postgresql.org; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=5c59haHTz44Abs8oOOiJKnwZdp4KbKyc1O39olYCCfg=; b=DirSVPsmdJ0gXyuU4r+/zwALmqkbXWlUiARM4ol0MMPS/jFa1JpHR5hjXiy8G85pZt j1TWdlpuglL4j91dwQ/Juy2NyHEZLlefLjx/3jiGICQxdq29c7qrLiAXp6DzZaZ9LZwS euzX203wVupg4ZrnxK94wfbbbRx3L+tBuw37wfj/8DZZ9dHeY4oq8CObngPs726yZOaJ eNehekRzx0gTdSjIKUn49HyJUduNYRP1kjgJoR5BX2DOoXKelgO4VeZ2VlFRWvwMLnE4 775WqFp2zyuuj6pr9DiYDMScsU6WJADitusmL2PbFM33+EGEsnzePWYG/dV3cTYlVCZV W6PQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715716768; x=1716321568; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=5c59haHTz44Abs8oOOiJKnwZdp4KbKyc1O39olYCCfg=; b=g06gnAHGmAiJ3vZ9hF3dIYvdF/1bltNuXP2yAk0heRHWjNUb29FXPQEk0/dop8x4GJ RMPGZ1Vx69B3g6c1bagyVOqXxzQX0dpfOREkprvGu6FvsUYwtymYQNwf7SqjzTmYc/6c R3MueKEs/c+kae/PX6rZa/sOx63d3Qz4XnVYXyYrBr1iGNNEdOUSgi+JpXt5Hm2ET1He HfQSN430gf7MW5bfoznhfO8qeai0YrD2msELnVODwhFcBBF8FQgoG5Q8gQPXKv1difYF z/IJbCtHVyZRXVsXbCt6e00Zg6RoJmrE4kWd7QE/yngKlBSIkHfXYjlO+Rudizgz0+XK T6Aw== X-Gm-Message-State: AOJu0YwYemhJB/c7rRqba9ZIAz1wu6iZau6cfT8VjLEDI84llRqygVn+ 3KDrmdIlYk1v+kbPANpDZs1+ACNt9uvCOCYa3Ib7uE4OD/fiEBFd1eQA9pWGUe4cc28RnsI9Xrj w/cwhZyv0C6iPa/EUGDUtBUr1vYq7kLM+ X-Google-Smtp-Source: AGHT+IF0AH9qxU0lN2O5ybtZ5HvSpYxpxivqdyGYZ9jxp00FFL4bEq6dlnp63k+uOyeshs5JMuDL9xnJnrXr6vNagjM= X-Received: by 2002:a05:6102:38ce:b0:47e:f14d:ac49 with SMTP id ada2fe7eead31-48077eacb97mr14119432137.29.1715716768227; Tue, 14 May 2024 12:59:28 -0700 (PDT) MIME-Version: 1.0 From: sud Date: Wed, 15 May 2024 01:29:16 +0530 Message-ID: Subject: Adding constraints faster To: pgsql-general Content-Type: multipart/alternative; boundary="000000000000816c0f06186f70a7" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000816c0f06186f70a7 Content-Type: text/plain; charset="UTF-8" Hi, It's postgres version 15.4. We want to create foreign keys on three different partitioned tables which already have data in them in production. They all are referring to the same parent table which is also partitioned. All the tables(both parent and child) are having ~2TB+ in size each and having ~100 million rows in each of the partitions. These are range partitioned on truncated date columns and the total number of partitions in the tables is around 30 in each of them. It's easy to create the foreign key on the new partitions of the child table which are blank and going to be filled with data in future, however adding the foreign key on the existing partition with data is going to take time. We tried with one sample partition with existing data in it and it took ~20minutes. So this way , it's going to take a long time and we may not have the application down for such a long time. I have the following questions. To make this activity faster we were thinking of using the "NOT VALID" option. I.e create the foreign key constraints on the existing partitions with "NOT VALID" option and create the foreign key on the blank future partitions with the VALID option. Is this okay? As because we also see in some documents stating that , if the foreign key is in the "NOT VALID" state ,optimizer won't be using it for estimating the row counts during making join cardinality estimation, so want to understand from experts if its fine or we have to make that foreign key constraints "VALID" anyway, even if that runs longer? We also tried to set the max_parallel_workers_per_gather to 8 and then run the "validate constraint" step but that is still running in a single thread only. So wondering if we have any other options available to make this foreign key addition faster with existing data in it? ****** ALTER TABLE ADD FOREIGN KEY ... NOT VALID. ALTER TABLE ... VALIDATE CONSTRAINT; Regards Sud --000000000000816c0f06186f70a7 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hi,
It's=C2=A0postgres version 15.4. We want to cre= ate foreign keys on three different partitioned tables which already have d= ata in them in production. They all are referring to the same parent table = which is also partitioned. All the tables(both parent and child) are having= ~2TB+ in size each and having ~100 million rows in each of the partitions.= These are range partitioned on truncated date columns and the total number= of partitions in the tables is around 30 in each of them.

It's = easy to create the foreign key on the new partitions of the child table whi= ch are blank and going to be filled with data in future, however adding the= foreign key on the existing partition with data is going to take time. We = tried with one sample partition with existing data in it and it took ~20min= utes. So this way , it's going to take a long time and we may not have = the application down for such a long time.

I have the following ques= tions.
To make this activity faster we were thinking of using the "= NOT VALID" option. I.e create the foreign key constraints on the exist= ing partitions with "NOT VALID" option and create the foreign key= on the blank future partitions with the VALID option. Is this okay? As bec= ause we also see in some documents stating that , if the foreign key is in = the "NOT VALID" state ,optimizer won't be using it for estima= ting the row counts during making join cardinality estimation, so want to u= nderstand from experts if its fine or we have to make that foreign key cons= traints "VALID" anyway, even if that runs longer?

We also = tried to set the max_parallel_workers_per_gather to 8 and then run the &quo= t;validate constraint" step but that is still running in a single thre= ad only. So wondering if we have any other options available to make this f= oreign key addition faster with existing data in it?

******
ALTER TABLE ADD FOREIGN KEY ... NOT VALID.
ALTER T= ABLE ... VALIDATE CONSTRAINT;

Regards
Sud
--000000000000816c0f06186f70a7--