Sub topic 11
Verifying the Result and Round-Tripping Data
This chapter assumes basic SQL, PostgreSQL data types, CSV files, and the psql command-line client. You should already know how to create tables and run SELECT statements.
A successful COPY command proves only that PostgreSQL accepted the input or wrote the output. It does not prove that the values mapped to the intended columns or that the exported file can be imported again. Verification compares expected properties before and after the operation.
Check the import
Start with a row count:
SELECT count(*) AS imported_rows
FROM products;Compare this value with the number of data records in the source file. A CSV header is not a data row, so a simple line count is often one greater than the expected row count. However, CSV fields can contain quoted newline characters. For such files, use a CSV-aware tool rather than wc -l.
Check key and required-column constraints:
SELECT
count(*) AS rows,
count(id) AS non_null_ids,
count(DISTINCT id) AS distinct_ids,
count(*) FILTER (WHERE name IS NULL) AS missing_names
FROM products;For a table whose id is a primary key and whose name is NOT NULL, non_null_ids and distinct_ids should both equal rows, and missing_names should be zero.
Check values against known business rules. These checks are useful even when the table has no corresponding database constraint:
SELECT id, price, released
FROM products
WHERE price < 0
OR released > CURRENT_DATE;An empty result is evidence that no rows currently violate these rules. For repeatable verification, store expected counts or validation queries in a script rather than checking them manually.
Check an export
Export a deterministic result when possible. An ORDER BY makes the file easier to compare between runs:
psql "$DATABASE_URL" -c "\copy (SELECT id, name, price, released, notes FROM products ORDER BY id) TO 'products_export.csv' WITH (FORMAT csv, HEADER true)"Inspect the header and a few records:
head -n 5 products_export.csvThe header should contain the expected column names in the expected order. Look especially at values containing commas, quotes, newlines, empty strings, and NULLs. CSV quoting should preserve these values without changing their meaning.
The following checks provide useful evidence:
| Check | What it detects |
|---|---|
| Data-row count | Missing or extra records |
| Header and column order | Incorrect export shape |
| Key uniqueness | Duplicate or repeated records |
| NULL and empty-value checks | Incorrect missing-value handling |
| Round-trip comparison | Changes during export and import |
Round-trip the file
A round trip exports data, imports the file into a separate table, and compares the original with the imported copy. Never use the production table as the test destination.
CREATE TEMP TABLE products_roundtrip (LIKE products);Run the import in psql:
psql "$DATABASE_URL" -c "\copy products_roundtrip FROM 'products_export.csv' WITH (FORMAT csv, HEADER true)"Then compare both directions:
SELECT *
FROM products
EXCEPT ALL
SELECT *
FROM products_roundtrip;
SELECT *
FROM products_roundtrip
EXCEPT ALL
SELECT *
FROM products;Both queries should return zero rows. EXCEPT ALL preserves duplicate counts, so it detects an extra or missing copy of a duplicated row. PostgreSQL set operations also compare NULL values consistently for this purpose.
The comparison is valid only when both tables have the same columns and compatible types. If the export intentionally selects only some columns, compare the matching projection instead:
SELECT id, name, price
FROM products
EXCEPT ALL
SELECT id, name, price
FROM products_roundtrip;Exercise
Why should a round-trip test use EXCEPT ALL in both directions instead of only comparing row counts?
Show answer
Row counts cannot show which rows differ, and one-direction comparison can miss extra rows. EXCEPT ALL in both directions detects missing rows, extra rows, and changed values while preserving duplicate counts.