HeadlinesBriefing favicon HeadlinesBriefing.com

Speed Up PostgreSQL in Containers with Key Config Tweaks

DEV Community •
×

Speeding up PostgreSQL in containers can dramatically reduce CI test run times. On an older CI machine, tests were completing in over an hour, with PostgreSQL identified as the bottleneck. The issue stemmed from slow disk I/O, causing PostgreSQL to spend excessive time on data sync operations, particularly with `TRUNCATE` commands. These operations, unnecessary in an ephemeral CI environment, were consuming up to 81.7% CPU and taking over 15 seconds each.

The solution involved three key configuration tweaks. First, using the `--nosync` flag during database initialization eliminated expensive disk sync operations. Second, increasing `POSTGRES_SHARED_BUFFERS` to 256MB allowed more memory for caching frequently accessed data. The most impactful change was mounting PostgreSQL's data directory on `tmpfs`, an in-memory filesystem. This move eliminated disk I/O entirely, making all database operations occur in RAM. The result? A 6x speed improvement, reducing test run times from 60 minutes to 10 minutes.

These optimizations are particularly effective in CI environments where data durability is less critical than speed. By leveraging `tmpfs`, teams can significantly cut down on the time spent waiting for disk operations, especially on machines with slow disks. This approach not only speeds up test suites but also improves developer productivity by providing faster feedback loops. The key takeaway is that small, targeted configuration changes can yield substantial performance gains in CI pipelines.