IntersectMBO / cardano-db-sync

A component that follows the Cardano chain and stores blocks and transactions in PostgreSQL
Apache License 2.0
291 stars 160 forks source link

postgresql-setup.sh Enhancements #1666

Open rdlrt opened 7 months ago

rdlrt commented 7 months ago

Versions The db-sync version (eg cardano-db-sync --version): 13.2.0.1 PostgreSQL version: 16.2

Build/Install Method The method you use to build or install cardano-db-sync: Release binaries

Run method The method you used to run cardano-db-sync (eg Nix/Docker/systemd/none): systemd

Additional context The file scripts/postgresql-setup.sh is currently lagging in compatibility:

Scitz0 commented 5 months ago

In addition to the above mentioned items, separate to that but also connected to dump/restore. I just wanted to add that I have found that from a performance perspective, its better to compress outside of pg_dump (-Z0). Additionally, we can leverage our multi-core cpu (-j and pigz -p) to speed up the process. pigz being an external application might be a bit controversial as it needs to be added to nix flow but parallel gzip compression does help greatly when the horsepower exists to utilize it.

Some example code for how I have dumped and restored with great performance gains:

# dump
db="explorer";
tmp_dir="/tmp/dump_${db}";
threads=$(( nproc - 1 ));
pg_dump -d ${db} -Fd -j ${threads} -Z0 -f "${tmp_dir}";
tar -cf - "${tmp_dir}/" | pigz -p ${threads} > "${tmp_dir}.tar.gz";

# restore
db="explorer";
tmp_dir="/tmp/dump_${db}";
threads=$(( nproc - 1 ));
pigz -p ${threads} -dc /tmp/dump_cexplorer.tar.gz | tar -C "${tmp_dir}" --strip-components 1 -xf -;
pg_restore -j ${threads} -Fd -O -d ${db} "${tmp_dir}";

Take it for what it is... I wanted to put it out there if found useful to possibly integrate (or parts of it) in the flow for dbsync dump/restore.