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 1su26P-003Zxh-AA for pgsql-general@arkaria.postgresql.org; Fri, 27 Sep 2024 03:57:10 +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 1su26O-0027w2-Lg for pgsql-general@arkaria.postgresql.org; Fri, 27 Sep 2024 03:57:08 +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 1su26O-0027vu-55 for pgsql-general@lists.postgresql.org; Fri, 27 Sep 2024 03:57:08 +0000 Received: from mail-pj1-x102d.google.com ([2607:f8b0:4864:20::102d]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1su26J-001IQ2-G1 for pgsql-general@postgresql.org; Fri, 27 Sep 2024 03:57:07 +0000 Received: by mail-pj1-x102d.google.com with SMTP id 98e67ed59e1d1-2e0af6e5da9so762689a91.2 for ; Thu, 26 Sep 2024 20:57:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bitnine-net.20230601.gappssmtp.com; s=20230601; t=1727409422; x=1728014222; darn=postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=nypSkVweVsgfsZSCHDTaYD1cbe4zJTHI+YB/cYNt8as=; b=qOZC4G3WWTZH1iE/eb6+uFeNqaloLEJ1flgQnKYN9d1nAfOW0NGF6iWStpxRiWiUt1 L7HgHjzaBq58GV/wNi6LRnIRf9FNRlaDJAgt+r9vdPUYZ6OkRuwIVjbpe5scVefX/kCk HJ2H6O2BH6s0I/RLZ/KMbZGQ6NUm8PiGdD2pvEbadjQwE5ndpo+xg/SEecF/gss5p/Tm Fqo4PZ804ipKQpOPXpi+pjj3a8ALMlO1a2Hi3zwcdCCtIfzNl9C7CP7fRfjwybJoxd/r xHi5EbSflzs7aSoR9WjT0QVb4pmsBEvP4Z4vb0l+soWnH9pTEUvvQCZZjY6+7v5qL9zn /qcw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727409422; x=1728014222; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=nypSkVweVsgfsZSCHDTaYD1cbe4zJTHI+YB/cYNt8as=; b=LzDgVuUKIl1PmtKbABEw3bknBFaX9A45Zy6ZiBQQzSuv5hA3MtfWYvDATUkcGHfERP x/5DIW5Oz+oCoZz3vulXFaDJSPuKRKCFif2U70QKAHKXJM1Pq0G0j1cOrq+wZSsldjjC jAE/X6lcF5ui27535XyImwiF/GduO7N9Rpxy9H04A/CKsBnNJbEm4VOIErg9KZU6hR3O GNqPe+mBupr4XevLwj9WnZNhTTQL0h45dgrY9uWHkJPfZ09gMQDQlVUOIsEiVr4Z6VAl 3uFvHZQCWUaj6Qr5q6s0SniGyR8UyuA9jQnfI4P+Z9mB8MLIc+GuBpvSArO/ecQfExPj MZkQ== X-Gm-Message-State: AOJu0YxXfIXAmugCNfW69CG75m4xZ2Wx1qzJ1bVF3QqSuTvgvMT4x9V6 yiPlGhvUlgUm4Ga5vFNcPqIjSUPGYhOxkKjZj/my10eJMdlYzxQtkAOMGZv+Np5/UsDhynJ0V1Y 5MpGqVEMH93PSEl5HqcWzvdCmnHZ7OMr598okYQ== X-Google-Smtp-Source: AGHT+IEzBg0Cm9jc5z98EnfABfnPzf+1w3/nxUyxWaqHwfrhVIUnSbKEmojIi18QS8dyby/8vIIXvZGgH26WJZqzf6A= X-Received: by 2002:a17:90a:ee8c:b0:2e0:6c5f:4eb9 with SMTP id 98e67ed59e1d1-2e0b89de123mr2072282a91.12.1727409422129; Thu, 26 Sep 2024 20:57:02 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Muhammad Usman Khan Date: Fri, 27 Sep 2024 08:56:49 +0500 Message-ID: Subject: Re: Request for Insights on ID Column Migration Approach To: Aditya Singh Cc: pgsql-general@postgresql.org Content-Type: multipart/alternative; boundary="000000000000fd0438062311d816" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000fd0438062311d816 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hi, Your approach to migrating the ID column from int4 to int8 with minimal downtime is generally sound but in my option, consider the following also: - Consider using PostgreSQL's CONCURRENTLY option when creating the unique index to avoid locking the entire table - Make sure to first alter the new column to be non-nullable if it=E2=80= =99s not already On Fri, 27 Sept 2024 at 06:57, Aditya Singh wrote: > I am just contacting you to talk about a current issue with our database. > We have run out of a positive sequence in one of our tables and are now > operating with negative sequences. To address this, we plan to migrate fr= om > the int4 ID column to an int8 ID column. > > The plan involves renaming the int8 column to the id column and setting > it as the primary key. However, this process will require downtime, which > may be substantial in a production environment. Fortunately, we have note= d > that other tables do not use the id column as a foreign key, which may > help mitigate some concerns. > Our Approach: > > 1. > > *Create a Unique Index*: We will first create a unique index on the > new ID column before renaming it and altering it to be non-nullable. T= his > step will necessitate scanning the entire table to verify uniqueness. > 2. > > *Add Primary Key*: After ensuring the uniqueness, we will add the ID > column as the primary key. By doing this, we hope to bypass the additi= onal > scanning for uniqueness and nullability, as the column will already be= set > as not nullable and will have the uniqueness constraint from the uniqu= e > index. > > We want to confirm if this approach will work as expected. If we should b= e > aware of any potential pitfalls or considerations, could you please provi= de > insights or point us toward relevant documentation? > > Thank you so much for your help, and I look forward to your guidance. > > Best regards, > > Aditya Narayan Singh > Loyalty Juggernaut Inc. > > ------------------------------ > *Confidentiality Warning:* > This message and any attachments are intended only for the use of the > intended recipient(s), are confidential, and may be privileged. If you ar= e > not the intended recipient, you are hereby notified that any disclosure, > copying, distribution, or other use of this message and any attachments i= s > strictly prohibited. If received in error, please notify the sender > immediately and permanently delete it. > --000000000000fd0438062311d816 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hi,

Your appro= ach to migrating the ID column from int4 to int8 with minimal downtime is g= enerally sound but in my option, consider the following also:

    Consider using PostgreSQL's CONCURRENTLY option when creating the uni= que index to avoid locking the entire table
  • Make sure to first alte= r the new column to be non-nullable if it=E2=80=99s not already

On Fri, 27 Sept 2024 at 06:57, Aditya Singh <aditya.singh@lji.io= > wrote:

I am just contacting you to talk about a current issue with= our database. We have run out of a positive sequence in one of our tables = and are now operating with negative sequences. To address this, we plan to = migrate from the=C2=A0int4=C2=A0ID column to an=C2=A0int= 8=C2=A0ID column.

The plan involves renaming the=C2=A0in= t8=C2=A0column to the=C2=A0id=C2=A0column and setting i= t as the primary key. However, this process will require downtime, which ma= y be substantial in a production environment. Fortunately, we have noted th= at other tables do not use the=C2=A0id=C2=A0column as a foreig= n key, which may help mitigate some concerns.

Our Approach:

    =
  1. Create a Unique Index: W= e will first create a unique index on the new ID column before renaming it = and altering it to be non-nullable. This step will necessitate scanning the= entire table to verify uniqueness.

  2. =

    Add Primary Key: After ensuring the uniqueness, we will= add the ID column as the primary key. By doing this, we hope to bypass the= additional scanning for uniqueness and nullability, as the column will alr= eady be set as not nullable and will have the uniqueness constraint from th= e unique index.

We want to confirm if this approach will wo= rk as expected. If we should be aware of any potential pitfalls or consider= ations, could you please provide insights or point us toward relevant docum= entation?

Thank you so much for your help, and I look forward to your= guidance.

Best regards,

Aditya Narayan Singh
Loyalty Jugger= naut Inc.



C= onfidentiality Warning:
Th= is message and any attachments are intended only for the use of the intende= d recipient(s), are confidential, and may be privileged. If you are not the= intended recipient, you are hereby notified that any disclosure, copying, = distribution, or other use of this message and any attachments is strictly = prohibited. If received in error, please notify the sender immediately and = permanently delete it.

--000000000000fd0438062311d816--