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 1tJNjd-002deJ-I4 for pgsql-general@arkaria.postgresql.org; Fri, 06 Dec 2024 02:06:26 +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 1tJNja-009Tsg-Tk for pgsql-general@arkaria.postgresql.org; Fri, 06 Dec 2024 02:06:24 +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 1tJNja-009TsX-7T for pgsql-general@lists.postgresql.org; Fri, 06 Dec 2024 02:06:23 +0000 Received: from mout-u-107.mailbox.org ([80.241.59.207]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tJNjY-001Hb5-1I for pgsql-general@lists.postgresql.org; Fri, 06 Dec 2024 02:06:22 +0000 Received: from smtp202.mailbox.org (smtp202.mailbox.org [10.196.197.202]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-u-107.mailbox.org (Postfix) with ESMTPS id 4Y4F3q2tC1z9tW3; Fri, 6 Dec 2024 03:06:15 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ewie.name; s=MBO0001; t=1733450775; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=y7Tev4pgvBsnlWP8gkZKPMM1yqIhgmdOIzzmX9WzjYo=; b=tCmwePkLLgp6OeQ1zgJkJqoU0gmnHOL7peBrryPRfMSXOHmPfv5nYjA9/L5UvRQSv9seMQ TSYKBtF+/Hoq6UBvuoPL5d11zlk/s3qA5t27L/1nlvNZ8z2y0DODPgzqD2sibtMSP7naST NNeInLRwhDtd/NF5TH5pXn+HhCr0d6DuZQcnAkkPCyuBYo9FPgTn9K3zW6BnlLsszHW4zr fiWF9XiYhi5uogTqACdCXZF33G38oaCttA23qq95cNbyej7MpRMXrCKxOiSIMVa7FF42tx R/UjNbMvJFiEBfpV9gI7wBtLiurDdH03hIaUE+S+lm4/PnsMGVJM3n4sjj/vww== Date: Fri, 6 Dec 2024 03:06:03 +0100 From: Erik Wienhold To: Sasmit Utkarsh Cc: pgsql-general , pgsql-general@lists.postgresql.org Subject: Re: Best Practices for Managing Schema Changes Dynamically with libpq Message-ID: <3a73e644-1707-4640-a271-c5c8300f61ec@ewie.name> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 2024-12-03 18:43 +0100, Sasmit Utkarsh wrote: > I am working on a project that uses libpq along with C language to interact > with PostgreSQL, and we face challenges with managing schema changes > dynamically in production while avoiding downtime. Specifically, we need > guidance on handling table structure changes/additions without tightly > coupling these changes to application updates. > > *Current Approach:* > Schema changes are deployed first, followed by application updates to align > with the new structure. > > *Challenges:* > Ensuring application stability during the transitional phase when the > schema and code are not fully in sync. > Handling table structure changes (e.g., adding new columns) dynamically > without requiring immediate code changes. What you're looking for is the "Expand and Contract" pattern[1][2]. The transitional phase between expand and contract has to support both old and new code until the old code is migrated as well. How you keep the schema compatible with the old code for some time depends on the kind of schema changes. Some use cases from the top of my head: 1) expand: add unconstrained columns transition: adapt code to use new columns contract: add constraints 2) expand: rename tables/columns transition: add (updatable) views that expose the old names until the code is adapted to the new names contract: drop views 3) expand: add columns with constraints transition: backfill new columns with triggers contract: drop triggers [1] https://www.tim-wellhausen.de/papers/ExpandAndContract/ExpandAndContract.html [2] https://martinfowler.com/articles/evodb.html#everything_refactoring -- Erik