Skip to main content

PostgreSQL NOT NULL Check: Count Existing NULLs Before a Schema Change

A practical PostgreSQL NOT NULL preflight: count full-table NULLs with SQL, then use SqlOtter Column Statistics to inspect the current table view without confusing loaded rows for the whole table.

Updated 2 min readMarkdown
Part of seriesSQL Table Filtering & Schema Inspection

Before adding a PostgreSQL NOT NULL constraint, count every NULL in the target column with SQL. SqlOtter's Column Statistics panel can quickly inspect the selected rows or rows currently loaded in the table grid, but that view is not a whole-table preflight. Use both views for their proper jobs: inspect a sample in the UI, then run a full-table query before changing the schema.

1. Count NULL values across the full PostgreSQL table

For example, before requiring every customer to have an email address:

SELECT COUNT(*) AS null_count
FROM public.customer
WHERE email IS NULL;

If the result is greater than zero, identify a valid cleanup or backfill rule before adding the constraint. Do not replace missing values with a placeholder unless that value is valid for your application.

For a large production table, consider the scan cost, locking behavior, deployment sequence, and rollback plan for your PostgreSQL version. Apply the constraint only after the data and operational plan are ready.

2. Inspect the current table view in SqlOtter

  1. Connect to the intended PostgreSQL database and open the target table's Table Content view.
  2. Select the rows you want to inspect, or clear the selection to use the rows currently loaded in the grid.
  3. Right-click the target column header and choose Column Statistics.
  4. Check NULL Count and NULL percentage, and confirm which column and record scope the panel reports.

SqlOtter Column Statistics panel with count, distinct count, NULL count and numeric summary for three selected rows

The published SqlOtter example shows statistics for three selected rows. The values describe that selection, not the full table or all rows in a paged result.

3. Verify with SQL before the schema change

The UI statistics help inspect the current grid. For a definitive PostgreSQL preflight, run the COUNT query against the full table and review the returned count. After a valid cleanup, check again; only then plan the constraint migration.

SqlOtter's Table Content tools and Column Statistics are documented here: PostgreSQL table data and column statistics in SqlOtter. Explore the product at SqlOtter.

Disclosure: This article is published by the SqlOtter project team.

Tags: SqlOtter, PostgreSQL, SQL, Database

About the author

  • SqlOtter , The world's most powerful database management tool

No comments yet. Be the first to comment.