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 1suJau-005r5t-PQ for pgsql-general@arkaria.postgresql.org; Fri, 27 Sep 2024 22:37:49 +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 1suJat-00GNut-Oe for pgsql-general@arkaria.postgresql.org; Fri, 27 Sep 2024 22:37:47 +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 1suJat-00GNuQ-EH for pgsql-general@lists.postgresql.org; Fri, 27 Sep 2024 22:37:47 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1suJap-001QOy-Hd for pgsql-general@lists.postgresql.org; Fri, 27 Sep 2024 22:37:46 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 48RMbZBF729479; Fri, 27 Sep 2024 18:37:36 -0400 From: Tom Lane To: "Peter J. Holzer" cc: pgsql-general@lists.postgresql.org Subject: Re: Request for Insights on ID Column Migration Approach In-reply-to: <20240927222742.xmun3i57hjwiakuz@hjp.at> References: <20240927222742.xmun3i57hjwiakuz@hjp.at> Comments: In-reply-to "Peter J. Holzer" message dated "Sat, 28 Sep 2024 00:27:42 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <729477.1727476655.1@sss.pgh.pa.us> Date: Fri, 27 Sep 2024 18:37:35 -0400 Message-ID: <729478.1727476655@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk "Peter J. Holzer" writes: > As you can see, adding the primary key takes just as much time as > creating the unique index. So it doesn't look like PostgreSQL is able to > take advantage of the existing index (which makes sense since it still > has to create a new index). No, but you can attach an existing unique index as a primary key: regression=# create table t1 (f1 int not null); CREATE TABLE regression=# create unique index t1_f1 on t1(f1); CREATE INDEX regression=# alter table t1 add primary key using index t1_f1; ALTER TABLE If you build the unique index concurrently, this needn't involve much downtime. regards, tom lane