Aurora PostgreSQL Migration Testing: Rehearse a NOT NULL Change on a Branch
Use Aurora PostgreSQL migration testing to rehearse a NOT NULL change on a writable branch, backfill one shipment, reject a NULL insert, and check the parent.
A migration that makes shipment_status mandatory can pass against an empty database and fail when it encounters one old shipment with a missing status. In this Aurora PostgreSQL migration testing tutorial, you’ll reproduce that failure on a writable branch, backfill the missing value, and verify that PostgreSQL rejects a new row with an explicit NULL. You’ll check the parent from its own connection and finally roll the branch back to its starting state.
The worked case, task:shipment-status-01, is for an application engineer reviewing a shipping service migration. Shipment 101 is queued, shipment 102 has a SQL NULL, and shipment 103 is dispatched. The example assigns pending_review to 102 because its actual shipping state is unknown. The known statuses belong to different shipments and must survive the migration. All outputs shown are illustrative expectations.
Prepare the parent and branch connections
- Obtain an approved network path and login for a nonproduction Aurora PostgreSQL parent containing only synthetic data. Gather the host, port, database name, username, and connection security settings for the parent and branch.
- Use an already provisioned writable branch that includes the committed fixture below. Ask the branch owner for its read/write endpoint and expiration. For a new lab, seed the parent in the next section before the owner provisions the branch.
- Keep a separate interactive
psqlsession for each target. Verify the connection information in each session before executing SQL. Keep the fixture free of other writers during the exercise so the comparisons have a known starting point.
AWS announced the database branching preview on December 2, 2026, for Aurora PostgreSQL 17 and higher in US East (Ohio), Europe (Frankfurt), and Asia Pacific (Mumbai). Aurora branches use copy-on-write storage and have their own read/write endpoints. AWS announcement · Aurora User Guide on database branches

Replace PARENT_HOST, BRANCH_HOST, and LAB_USER with the approved values. The example uses shipment_lab and port 5432. Substitute the actual database name, port, and login for each connection as needed. Use the authentication and connection security settings supplied for your lab. Use a psql version that matches the server’s major version when available. The -X option skips startup files so local psql settings in those files do not alter error handling or output during the exercise. PostgreSQL 17 psql reference
In the parent terminal, start this session.
psql -X --host=PARENT_HOST --port=5432 --dbname=shipment_lab --username=LAB_USER
In a separate terminal, start the branch session once its endpoint is available.
psql -X --host=BRANCH_HOST --port=5432 --dbname=shipment_lab --username=LAB_USER
Run these client commands in each session.
\conninfo
\pset null '<NULL>'
Compare the reported host, port, database, and login with the assigned details. The database name in the prompt can be identical in the two windows. If you find a mismatch before starting any SQL, exit that session with \q and start a fresh process using the assigned endpoint. A failed interactive \connect can leave the previous connection active. In this exercise, each session stays attached to one target. The \pset command makes SQL nulls visible as <NULL> in query output, where they could otherwise resemble an empty string. PostgreSQL 17 psql reference
Label the terminal windows Parent and Branch after checking their connection details. Record the corresponding host alongside each saved result. Once the fixture is ready, use the parent window only for baseline and comparison queries. If a coding agent prepared the SQL, check the endpoint in the terminal where the SQL will run. A correct working directory establishes which migration file you’re reviewing. The database session determines where the SQL takes effect. Verify Each Git Worktree’s Database Branch with Neon CLI Checkout · AgentOps
Capture the three-row baseline
- For a new lab, run the setup block once on the verified synthetic parent. For a seeded lab, start with the verification block.
- Save the ordered rows and column metadata from the parent. Once the branch is ready, run the same checks on the branch and compare both results with the expected fixture.
Use an approved role with permission to create the table in the lab’s public schema for setup. Run the branch migration as the fixture table’s owning role, with the ability to read, update, and insert fixture rows. PostgreSQL assigns ownership to the role that creates the table and requires table ownership for ALTER TABLE. The fixture is a permanent table so the committed setup is available across sessions. Its integer primary key gives each shipment a distinct identity. The text status starts nullable with no declared default. PostgreSQL 17 CREATE TABLE · PostgreSQL 17 ALTER TABLE
BEGIN;
CREATE TABLE public.shipments_rehearsal_01 (
shipment_id integer PRIMARY KEY,
shipment_status text
);
INSERT INTO public.shipments_rehearsal_01 (shipment_id, shipment_status)
VALUES (101, 'queued'), (102, NULL), (103, 'dispatched');
Look for CREATE TABLE and INSERT 0 3. If you get an error, issue ROLLBACK and inspect the cause. If the name already exists, inspect that relation using the verification block. The setup deliberately leaves out IF NOT EXISTS because that option can let a command continue without confirming that an existing relation has the intended definition. Commit only after the setup statements succeed. PostgreSQL 17 CREATE TABLE · PostgreSQL 17 INSERT · PostgreSQL 17 transactions
COMMIT;
Keep this verification block available in both terminals. The first query lists all fixture rows, in ID order. Reading the entire table lets you detect a missing known shipment and an unexpected fourth row. The second query identifies the table by schema and name and reports the column definitions in declaration order. The final psql command shows the relation’s definition, including its primary key and any triggers or additional constraints that would make a reused table differ from the example. PostgreSQL 17 information schema columns · PostgreSQL 17 psql reference
SELECT shipment_id, shipment_status
FROM public.shipments_rehearsal_01
ORDER BY shipment_id;
SELECT column_name, data_type, is_nullable, column_default
FROM information_schema.columns
WHERE table_schema = 'public'
AND table_name = 'shipments_rehearsal_01'
ORDER BY ordinal_position;
\d public.shipments_rehearsal_01
Expect the following on both targets before starting the migration. Spacing is simplified.
shipment_id shipment_status
101 queued
102 <NULL>
103 dispatched
column_name data_type is_nullable column_default
shipment_id integer NO <NULL>
shipment_status text YES <NULL>
The NO on shipment_id comes from its primary key. Check the shipment_status row specifically when comparing nullability. In this fixture, YES means the column can contain a null, and <NULL> under column_default shows that it has no declared default. The information schema view lists columns accessible to the current user. If metadata is absent, check both name filters and verify the role before proceeding. PostgreSQL 17 information schema columns · PostgreSQL 17 CREATE TABLE
A missing fixture on the intended branch calls for an endpoint and database check. If the connection details are correct and the parent has the committed rows, ask the branch owner for a branch with that starting state. If the branch already shows pending_review or has is_nullable = NO for the status, ask whether someone previously used it for the exercise. Restoring the original baseline matters here because an already migrated fixture cannot reproduce the first failure. Preserve the initial parent output for the comparison later.
Make PostgreSQL reject the premature change
- In the verified branch session, open a transaction and try to make the column mandatory with the original null still present.
- Read the server error, roll back in the same window, and rerun the verification block before starting the migration.
BEGIN;
ALTER TABLE public.shipments_rehearsal_01
ALTER COLUMN shipment_status SET NOT NULL;
Expect an error stating that shipment_status contains null values. PostgreSQL normally scans existing rows when applying SET NOT NULL. A valid check constraint that proves the absence of nulls can let it skip the scan, but this fixture has no such constraint. Shipment 102 prevents the alteration. A permission error points to the role or table ownership. A missing relation points to the target or fixture setup. A connection failure prevents the test from establishing anything about the constraint. If the alteration succeeds unexpectedly, roll it back and check the branch’s starting data and definition. PostgreSQL 17 ALTER TABLE
ROLLBACK;
Under the default psql error handling used here, the SQL error leaves the transaction aborted. The standard prompt can show ! in its transaction indicator. If you try a regular query before issuing the rollback, PostgreSQL can report that the current transaction is aborted. Use the earlier ALTER TABLE error to diagnose the cause. After rollback, the branch should again display 101 queued, 102 <NULL>, 103 dispatched, and YES for the status column’s nullability. This read supplies the starting point for the next transaction. PostgreSQL 17 psql reference · PostgreSQL 17 transactions
Backfill shipment 102 and enforce NOT NULL
- Start a fresh transaction on the branch and inspect the row returned by the guarded update.
- Apply the constraint in that same transaction. Run the verification block in the branch window and leave the transaction open for the insert probe.
BEGIN;
UPDATE public.shipments_rehearsal_01
SET shipment_status = 'pending_review'
WHERE shipment_id = 102
AND shipment_status IS NULL
RETURNING shipment_id, shipment_status;
Expect exactly one returned row with ID 102 and status pending_review. The ID selects the legacy shipment and IS NULL checks that the missing value still needs a backfill. If the same shipment already has a status, the predicate protects it from being overwritten. RETURNING shows the actual updated row and its new value. A result with zero rows is valid SQL and fails this rehearsal checkpoint. Roll back and inspect whether shipment 102 is missing or has acquired another status. If another shipment has a missing status, investigate its business meaning before assigning any value to it. The illustrative rule applies only to 102. PostgreSQL 17 UPDATE
After the expected row appears, run the alteration.
ALTER TABLE public.shipments_rehearsal_01
ALTER COLUMN shipment_status SET NOT NULL;
Expect the ALTER TABLE command tag. Then rerun the verification block in the same branch session. Look for 101 queued, 102 pending_review, and 103 dispatched. The status remains text, has no declared default, and now shows NO under is_nullable. The ID remains the primary key. A successful alteration alone would also permit the wrong backfill value or an overwritten status on shipment 103. The full row comparison catches those errors. The metadata confirms that the schema now enforces the requirement. PostgreSQL 17 ALTER TABLE · PostgreSQL 17 information schema columns
Keeping the backfill and alteration in one transaction gives you a single rollback boundary for the row change and the schema change. PostgreSQL takes an ACCESS EXCLUSIVE lock for this alteration and normally scans the table to validate the constraint. Complete the checks while you have both terminals ready and close the branch transaction before stepping away. The fixture tests statement order and behavior on three known rows. Before applying a similar change to a busy table, evaluate the scan and lock separately and review which application writers still send a missing status. PostgreSQL 17 ALTER TABLE · PostgreSQL 17 transactions
| Input | Action | Expected result |
|---|---|---|
task:shipment-status-01, branch rows 101 queued, 102 NULL, 103 dispatched |
Run ALTER TABLE public.shipments_rehearsal_01 ALTER COLUMN shipment_status SET NOT NULL in the first transaction, then run ROLLBACK |
PostgreSQL reports existing null values in shipment_status. The branch baseline returns with shipment 102 still missing its status. |
task:shipment-status-01, shipment 102 has SQL NULL |
Run the guarded UPDATE with RETURNING, then SET NOT NULL inside the same transaction |
The update returns only 102 pending_review. Rows 101 and 103 retain their statuses, and is_nullable for shipment_status is NO. |
task:shipment-status-01, ID 104 is unused and the branch status is mandatory |
Attempt INSERT INTO public.shipments_rehearsal_01 (shipment_id, shipment_status) VALUES (104, NULL) after creating the savepoint |
PostgreSQL rejects the explicit null. Rolling back to the savepoint leaves the backfill and constraint available for inspection. |
task:shipment-status-01, branch transaction shows 102 pending_review |
Run \conninfo and the verification block from the separately connected parent window |
Parent row 102 remains SQL NULL, the other two statuses match, and the parent status column still reads YES under is_nullable. |
Test a new NULL and check the parent
- In the same branch transaction, establish a savepoint before attempting to insert shipment
104with an explicit SQLNULL. - Recover to the savepoint and verify that the branch transaction still contains the migration. Check the parent independently while the branch transaction remains open.
SAVEPOINT before_null_insert;
INSERT INTO public.shipments_rehearsal_01 (shipment_id, shipment_status)
VALUES (104, NULL);
Expect a server error saying that the null value in shipment_status violates its not-null constraint. The earlier full table read established that 104 is unused. The explicit column list sets the attempted status directly, which keeps the probe meaningful even if a later schema introduces a default. PostgreSQL supplies a default for an omitted column when one exists. It checks the NOT NULL constraint immediately, so the result is visible at insert time. PostgreSQL 17 INSERT · PostgreSQL 17 CREATE TABLE
ROLLBACK TO SAVEPOINT before_null_insert;
The savepoint sits after the backfill and constraint, so recovery discards work after that boundary and keeps the earlier changes in the open transaction. Rerun the branch verification block. Expect three rows, with 102 pending_review, no 104, and is_nullable = NO for shipment_status. If the insert succeeds or reports a different failure, use the savepoint to discard the probe and then issue a full ROLLBACK. Check the earlier constraint result, the verified endpoint, and the starting fixture before trying again. If you never established the savepoint, use a full ROLLBACK to end the transaction. PostgreSQL 17 transactions
Run \conninfo and the verification block in the Parent window. Compare this parent output with the saved parent baseline, including both known statuses, the NULL on shipment 102, the YES for shipment_status nullability, and the primary key. Compare that result with the branch output you just recorded. One session should see the original row and schema, while the session with the open branch transaction should see the backfill and altered schema. If the parent output differs from its baseline, verify the endpoints first and then ask the lab owner to account for any writes to the parent fixture. Identical initial results in two windows did not by themselves establish that the connections were different. Aurora User Guide on database branches · PostgreSQL 17 transactions
Illustrative expected observations for shipment 102 and the nullability of shipment_status, based on the Aurora User Guide and PostgreSQL 17 documentation consulted December 2, 2026.
Save the parent comparison and the branch read with their respective endpoint labels. The branch read captures the row and schema inside the transaction that changed them. The parent read checks the fixture during this rehearsal. Demonstrating that a committed branch migration is visible from another connection to the branch would require a separate exercise. For related examples of checking exact database contents and keeping evidence from a run, see Copy and Verify a Live SQLite Snapshot and Fleet Replay.
Roll back the rehearsal and handle a lost branch
- In the Branch window, issue
ROLLBACKand rerun the baseline verification there. Compare the result with the first branch read. - Once you’ve recorded the outcome, exit both sessions with
\q. Use the configured time to live, or TTL, for infrastructure cleanup. If the expiry is uncertain, ask the branch owner to check the active branch listing.
ROLLBACK;
The final rollback discards both the uncommitted update and alteration. Expect shipment 102 to return to <NULL> and shipment_status to return to is_nullable = YES. Verify that 101 still reads queued, 103 still reads dispatched, and the committed fixture has the same columns and primary key. A later rollback cannot undo a migration that someone already committed. DROP NOT NULL would restore nullability, with the backfilled value requiring a separate correction. For this lab, use a fresh branch seeded with the original rows if someone committed the migration and you need to repeat both paths. PostgreSQL 17 transactions · PostgreSQL 17 ALTER TABLE
If you lose connectivity, check the cluster’s Branches tab with the branch owner. It lists active branches, their status and TTL. Aurora automatically deletes branches at expiration. If the branch is active, compare the assigned host, port, database and login, then confirm the approved network path. If it expired, obtain a replacement branch with the original committed fixture, verify its new endpoint, and start at the baseline. A timeout alone leaves the cause open. Aurora branch status and expiration · PostgreSQL 17 psql reference
If a session drops in the middle of the migration, open a fresh session to the verified branch and read the fixture before submitting more writes. The old savepoint belongs to the interrupted transaction. Once the old server session has ended, its uncommitted work is discarded. If the new read waits and the old session might still hold the table lock, ask the branch owner to inspect the old session before repeating the migration. If you discover that you’re on the wrong endpoint with an open transaction, roll it back in that session before reconnecting. If unintended writes were committed, record the endpoint and the changes you can identify for the database owner. For workflows in which an agent only needs to query PostgreSQL, see Make Read-Only Mean Read-Only With a Database Role. PostgreSQL 17 psql reference · PostgreSQL 17 transactions
Attach the actual outputs to your migration review. Record the initial failure from SET NOT NULL, the one-row backfill, and the explicit null rejection. Keep the parent comparison and the final branch reset under separate labels. They have the same expected values and answer different questions about the two targets. The example leaves the status without a default. Check any application insert path that still omits shipment_status, since PostgreSQL would supply a null for that omitted field and the new constraint would reject it. PostgreSQL 17 INSERT
Sources
- AWS announcement of the Aurora PostgreSQL database branching preview
- AWS Aurora User Guide, database branches
- AWS Aurora User Guide, getting started with database branches
- PostgreSQL 17 psql reference
- PostgreSQL 17 CREATE TABLE
- PostgreSQL 17 INSERT
- PostgreSQL 17 UPDATE
- PostgreSQL 17 ALTER TABLE
- PostgreSQL 17 information schema columns
- PostgreSQL 17 transactions

