From: | Erik Wienhold <ewie(at)ewie(dot)name> |
---|---|
To: | Sasmit Utkarsh <utkarshsasmit(at)gmail(dot)com> |
Cc: | pgsql-general <pgsql-general(at)postgresql(dot)org>, pgsql-general(at)lists(dot)postgresql(dot)org |
Subject: | Re: Best Practices for Managing Schema Changes Dynamically with libpq |
Date: | 2024-12-06 02:06:03 |
Message-ID: | 3a73e644-1707-4640-a271-c5c8300f61ec@ewie.name |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
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
From | Date | Subject | |
---|---|---|---|
Next Message | Ron Johnson | 2024-12-06 02:44:07 | Re: Errors when restoring backup created by pg_dumpall |
Previous Message | David G. Johnston | 2024-12-05 23:23:49 | Re: Errors when restoring backup created by pg_dumpall |