Peter Downs开发的pgtestdb是一款基于Go语言的测试包,利用PostgreSQL的模板数据库功能来加速测试执行[1]。该工具通过Postgres的CREATE DATABASE命令克隆预先配置的模板数据库实现隔离的测试环境,每次测试设置耗时约100毫秒[1]。
模板克隆在Postgres内部通过枚举和复制8 kB页块大小的堆、索引和目录文件来完成[1]。这一性能水平与基于schema隔离的测试方法相当[1]。通过schema复用技术的进一步优化,测试设置时间可被降低至10-20毫秒[1]。Peter Downs建议用户在端到端测试中采用pgtestdb方法[1]。
Peter Downs has developed pgtestdb, a Go testing package that leverages PostgreSQL's template database functionality to accelerate database testing [1]. The tool uses PostgreSQL's CREATE DATABASE command with a TEMPLATE parameter to rapidly clone pre-configured database instances for test isolation [1].
The template cloning mechanism works by enumerating and copying 8 kB page blocks across heap, index, and catalog files within PostgreSQL [1]. This approach achieves test setup times of approximately 100 milliseconds, matching the performance of schema-based isolation methods [1]. The River test suite, which employs schema isolation, demonstrates overall test execution that is 3.5 times faster than the pgtestdb approach [1]. However, Downs notes that further optimization through schema reuse can reduce setup times to 10–20 milliseconds [1]. The developer recommends deploying pgtestdb in end-to-end testing scenarios where database isolation is critical, positioning it as more efficient than traditional Docker-based database testing approaches [1].