PostgreSQL High Performance Insert
Strategies to maximize insertion performance:
- Use
COPY:COPY FROM STDINis significantly faster thanINSERT. - Unlogged Tables:
CREATE UNLOGGED TABLEavoids WAL writes (data lost on crash, useful for staging). - Disable Autovacuum: TEMPORARILY set
WITH (autovacuum_enabled=false)for bulk loads. - Minimal Indexes: Drop indexes before bulk load, recreate after. Keep only essential ones (e.g., PK if needed, though dropping PK validates consistency later).
- Synchronous Commit:
SET synchronous_commit = off(risk of data loss on recent commits, but faster). - Partitioning: Use inheritance/partitioning for easy bulk deletion (
DROP TABLE).