Adapting to Evolving Data Models Without Disrupting Replication
As business requirements evolve, the way applications uniquely identify records can change. A table that initially uses a combination of two columns as its primary key may eventually move to a single-column primary key.
For data replication pipelines, this is an important schema change. The primary key determines how records are uniquely identified, so changes to its structure need to be reflected correctly in the destination.
Hevo seamlessly handles primary-key structure changes, adapting to the updated key configuration while preserving existing destination data and continuing to replicate new records.
Let's look at how this works in a PostgreSQL-to-Snowflake pipeline.
The Scenario: Moving from a Composite Key to a Single-Column Key
Consider a procurement system that maintains information about parts and their suppliers.
Initially, a record is uniquely identified using a combination of ps_partkey and ps_suppkey. Together, these columns form a composite primary key.
As the business model evolves, each part is now associated with a single supplier in this dataset. This means ps_partkey alone is sufficient to uniquely identify each record.
The primary key therefore needs to change from:
- Composite primary key:
ps_partkey+ps_suppkey - New primary key:
ps_partkey
Here's how Hevo handles this change.
Step 1: Inspect the Existing Source Table
Start by examining the partsupp_pk_demo table in PostgreSQL.
| 1 | SELECT * |
| 2 | FROM tpch1g.partsupp_pk_demo |
| 3 | ORDER BY ps_partkey; |
Next, inspect its primary-key definition:
| 1 | SELECT |
| 2 | kcu.column_name, |
| 3 | kcu.ordinal_position |
| 4 | FROM information_schema.table_constraints tc |
| 5 | JOIN information_schema.key_column_usage kcu |
| 6 | ON tc.constraint_name = kcu.constraint_name |
| 7 | AND tc.table_schema = kcu.table_schema |
| 8 | WHERE tc.table_schema = 'tpch1g' |
| 9 | AND tc.table_name = 'partsupp_pk_demo' |
| 10 | AND tc.constraint_type = 'PRIMARY KEY' |
| 11 | ORDER BY kcu.ordinal_position; |
Initially, both ps_partkey and ps_suppkey participate in identifying a unique record.
Step 2: Verify the Existing Snowflake Data
Before changing the source, inspect the corresponding destination table.
| 1 | DESC TABLE YOUR_DATABASE.YOUR_SCHEMA.PARTSUPP_PK_DEMO; |
Check the existing records:
| 1 | SELECT * |
| 2 | FROM YOUR_DATABASE.YOUR_SCHEMA.PARTSUPP_PK_DEMO |
| 3 | ORDER BY PS_PARTKEY; |
This establishes the baseline for validating that the existing destination data remains intact after the primary-key change.
Step 3: Change the Primary Key in PostgreSQL
The business model now requires ps_partkey to serve as the sole primary key.
First, remove the existing composite primary-key constraint:
| 1 | ALTER TABLE tpch1g.partsupp_pk_demo |
| 2 | DROP CONSTRAINT partsupp_pk_demo_pkey; |
Next, define ps_partkey as the new primary key:
| 1 | ALTER TABLE tpch1g.partsupp_pk_demo |
| 2 | ADD CONSTRAINT partsupp_pk_demo_pkey |
| 3 | PRIMARY KEY (ps_partkey); |
The source table now uses a single-column primary key instead of the original composite key.
Verify the updated definition:
| 1 | SELECT |
| 2 | kcu.column_name, |
| 3 | kcu.ordinal_position |
| 4 | FROM information_schema.table_constraints tc |
| 5 | JOIN information_schema.key_column_usage kcu |
| 6 | ON tc.constraint_name = kcu.constraint_name |
| 7 | AND tc.table_schema = kcu.table_schema |
| 8 | WHERE tc.table_schema = 'tpch1g' |
| 9 | AND tc.table_name = 'partsupp_pk_demo' |
| 10 | AND tc.constraint_type = 'PRIMARY KEY' |
| 11 | ORDER BY kcu.ordinal_position; |
The result should now show only ps_partkey.
Step 4: Let Hevo Handle the Primary-Key Change
Once the source primary key is updated, Hevo processes the changed key configuration through the existing pipeline in the next incremental run.
Hevo adapts to the new primary key, moving from the combination of ps_partkey and ps_suppkey to ps_partkey alone.
The existing destination table and its historical records are retained. There is no need to recreate the pipeline or manually rebuild the destination table.
Step 5: Insert a New Record Using the Updated Key
To validate replication after the change, insert a new record into PostgreSQL.
| 1 | INSERT INTO tpch1g.partsupp_pk_demo |
| 2 | (ps_partkey, ps_suppkey, ps_availqty, ps_supplycost, ps_comment) |
| 3 | VALUES |
| 4 | (999999, 9999, 150, 35.50, 'New record after primary key change'); |
Verify the new source record:
| 1 | SELECT * |
| 2 | FROM tpch1g.partsupp_pk_demo |
| 3 | WHERE ps_partkey = 999999; |
This confirms that the source table accepts new records using the updated primary-key definition.
Step 6: Verify the Updated Schema and Data in Snowflake
After Hevo processes the pipeline run, verify that the updated primary-key configuration and the newly inserted record are reflected in the Snowflake destination.
First, inspect the destination table's primary-key configuration and schema:
| 1 | DESC TABLE YOUR_DATABASE.YOUR_SCHEMA.PARTSUPP_PK_DEMO; |
Verify that PS_PARTKEY is now the primary key, replacing the earlier composite key of PS_PARTKEY and PS_SUPPKEY.
Next, verify that the newly inserted record is available:
| 1 | SELECT * |
| 2 | FROM YOUR_DATABASE.YOUR_SCHEMA.PARTSUPP_PK_DEMO |
| 3 | WHERE PS_PARTKEY = 999999; |
The new record should be present in the same destination table.
Finally, verify that the existing records have been retained:
| 1 | SELECT COUNT(*) AS total_records |
| 2 | FROM YOUR_DATABASE.YOUR_SCHEMA.PARTSUPP_PK_DEMO; |
The destination should contain all the original records along with the newly replicated record.
Together, these checks confirm that Hevo has reflected the updated primary-key configuration in Snowflake, retained the existing data, and successfully replicated new records into the same destination table.
Step 7: Confirm Successful Pipeline Execution
Review the pipeline run details and Job History in Hevo.
The pipeline completes successfully, and the new record is replicated using the updated primary-key configuration without disrupting ongoing data replication.
The Takeaway
Changes to primary-key definitions can affect how a data pipeline identifies and processes records. Moving from a composite key to a single-column key is one such change.
Hevo adapts to the updated primary-key structure, preserves existing destination data, and continues replicating new records into the same Snowflake table.
With Hevo, evolving source data models don't have to mean rebuilding existing pipelines or losing continuity in the warehouse.