Skip to content

Enforce NOT NULL constraint on notification_status#4908

Merged
DilwoarH merged 1 commit into
mainfrom
add-ft-service-stats-db-updates-2
Jul 21, 2026
Merged

Enforce NOT NULL constraint on notification_status#4908
DilwoarH merged 1 commit into
mainfrom
add-ft-service-stats-db-updates-2

Conversation

@DilwoarH

@DilwoarH DilwoarH commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

What

Enforce NOT NULL constraint on notification_status

Why

Without NOT NULL - we cannot create an index for notification_status column

Checked DB:

SELECT count(*)
 FROM notifications where notification_status is NULL;

Result was 0

Context

This is needed as we need to implement the following replica identity:

ALTER TABLE notifications REPLICA IDENTITY USING INDEX ix_notifications_id_status;

Without adding this index we would have to put the replica on the whole table which will keep track of old values of every column which is not needed.

@DilwoarH
DilwoarH force-pushed the add-ft-service-stats-db-updates-2 branch from 8fce946 to e9e818f Compare July 2, 2026 15:08
@DilwoarH
DilwoarH force-pushed the add-ft-service-stats-db-updates-1 branch 2 times, most recently from 81f9491 to 661c1ca Compare July 7, 2026 09:20
)
op.execute("ALTER TABLE notifications ALTER COLUMN notification_status SET NOT NULL;")

with op.get_context().autocommit_block():

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Notifications table only handles INSERT and UPDATE operations, PostgreSQL’s default replica identity already includes all required columns (template_id, notification_status, created_at) in the wal2json stream using our existing id primary key.
The DELETE operations are for archiving notifications to notification_history where notification_status counts must remain unchanged, so our aggregator can simply ignore all delete logs.
I think in this case we can skip this migration and it also saves write overhead on notifications table.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😕 the default replica identity is simply id, is it not? And as such we don't get the old values for the status field in the wal2json stream, which we need for bookkeeping so we know which field in the status table to decrement as a notification moves to another status.

I agree it's a shame this increases our write load slightly, but I don't think there's another way.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah unfortunately, only returns ID so without this index there is no other way to get the old values.

@DilwoarH
DilwoarH force-pushed the add-ft-service-stats-db-updates-1 branch 8 times, most recently from 278215e to fb582fd Compare July 15, 2026 14:09
@DilwoarH
DilwoarH force-pushed the add-ft-service-stats-db-updates-2 branch from e9e818f to e3ccbdf Compare July 18, 2026 09:24
@DilwoarH
DilwoarH changed the base branch from add-ft-service-stats-db-updates-1 to main July 18, 2026 09:25
@DilwoarH

Copy link
Copy Markdown
Contributor Author

The base branch for this has now been changed to main - this can be merge in independently

@DilwoarH
DilwoarH force-pushed the add-ft-service-stats-db-updates-2 branch from e3ccbdf to 0aab4a5 Compare July 18, 2026 09:40

@whpearson whpearson left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change makes sense. Shouldn't lock the notifications table.Tested locally

@risicle

risicle commented Jul 20, 2026

Copy link
Copy Markdown
Member

I don't understand the name of the migration

@DilwoarH
DilwoarH force-pushed the add-ft-service-stats-db-updates-2 branch from 0aab4a5 to 53ecfa1 Compare July 20, 2026 11:39
Comment thread migrations/versions/0554_add_notification_status_idx.py Outdated
@DilwoarH
DilwoarH force-pushed the add-ft-service-stats-db-updates-2 branch from 53ecfa1 to 3bf4a5a Compare July 20, 2026 11:57
Comment thread migrations/versions/0554_add_notification_status_idx.py Outdated
@DilwoarH
DilwoarH force-pushed the add-ft-service-stats-db-updates-2 branch 3 times, most recently from fc0c862 to 6f49b5a Compare July 20, 2026 12:48
@DilwoarH

Copy link
Copy Markdown
Contributor Author

@risicle - migrations have been split up

@DilwoarH
DilwoarH force-pushed the add-ft-service-stats-db-updates-2 branch from 6f49b5a to 215a5af Compare July 20, 2026 13:20
@DilwoarH
DilwoarH force-pushed the add-ft-service-stats-db-updates-2 branch from 215a5af to 772484d Compare July 20, 2026 13:22
@DilwoarH
DilwoarH merged commit cbe4c21 into main Jul 21, 2026
10 checks passed
@DilwoarH
DilwoarH deleted the add-ft-service-stats-db-updates-2 branch July 21, 2026 08:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants