liveBook · Manning

Data integrity

As explained in PostgreSQL Mistakes and How to Avoid Them

Data integrity is the accuracy, consistency, and trustworthiness of stored data throughout its lifecycle.

Data integrity depends on preserving accuracy, consistency, and recoverability as data moves through storage, migration, and operations. A careless encoding conversion can accept characters that cannot be decoded back, causing irreversible loss. section 10.3

Protect integrity during a PostgreSQL migration by following a controlled path:

  • Identify the source encoding and export using that encoding. section 10.3
  • Convert the export to UTF-8, remove characters PostgreSQL cannot accept, and configure the target database with UTF8 and an appropriate collation. section 10.3
  • Import only after testing with real or realistic data; tools such as iconv, pgloader, and ora2pg can assist. section 10.3

An encoding failure during migration

A staging restore can fail with `ERROR: invalid byte sequence for encoding "UTF8": 0xe9` or a null-byte error. Convert the dump with `iconv -f latin1 -t utf8`, remove null bytes with `tr` or `sed`, then import the cleaned file after validating representative data. section 10.3

Integrity threats also appear during routine administration. Ignored PostgreSQL logs may expose filesystem or hardware faults before they become major data-loss incidents; block-read errors and invalid pages warrant hardware checks and diagnostic tools such as pg_checksums. section 7.3.4

2024-09-21 14:45:11.172 BST [3281235] ERROR:  could not read block 8185 in
file "base/16384/2613": read only 0 of 8192 bytes
2024-09-21 14:45:11.172 BST [3281235] CONTEXT:  while reading block 8185 of
relation base/16384/2613
2024-09-21 14:45:11.172 BST [3281235] STATEMENT:  SELECT * FROM
audit.audit_log WHERE tstamp > '2024-09-21';
2024-09-21 14:45:11.173 BST [3281235] LOG:  invalid page in block 8185 of
relation base/16384/2613
2024-09-21 14:45:11.173 BST [3281235] HINT:  This could be a corruption
issue caused by hardware problems or a software bug. Check for hardware
issues and consider running pg_checksums or other diagnostic tools.

Inspect the diagnostic log showing a failed block read, invalid page, affected relation, and the recommendation to check hardware and run corruption diagnostics. section 7.3.4

How PostgreSQL Mistakes and How to Avoid Them covers it

How deep it goes
Provides a migration checklist and diagnostic response, but stops short of detailed corruption-repair procedures.
Examples
Uses invalid UTF-8 bytes, null bytes, conversion commands, and a PostgreSQL invalid-page log as concrete failure evidence.
What it assumes
Assumes database administration experience while teaching encoding identification, conversion, cleaning, target configuration, and integrity checks.
What you can do after
Diagnose integrity risks with PostgreSQL logs and pg_checksums, and operate migrations using encoding conversion, cleaning, configuration, and realistic-data testing.
How it connects
Connects data integrity with character encoding, data migration, ACID, replication, logging, hardware failure, and data corruption.

Also explained in

Compare how 18 books explain data integrity →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of PostgreSQL Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.