Skip to content

Commit 0d7dc4a

Browse files
committed
Postgresql-setup script updates
docker.nix: Bump postgresql to 17 (as restore snapshot fails if snapshots are made using postgres 17), postgres 14 is ancient anyways and there are various improvements (most prominent one being non-locking index maintenance) docker-compose*.yml: Bump postgresql version to 17, node version to 10.1.4 and dbsync version to 13.6.0.4 postgresql-setup.sh: - Parse psql output using csv instead of manipulating white chars or table seperators - Use strict checks for ${PGDATABASE} as there can be another database with a prefix/suffix attached - When creating/restoring a snapshot, existence of ledger_file should be optional, as there are dbsync config options that don't require lstate files, accordingly - make presence of ledger-state-file optional - When restoring a snapshot (eg: across postgres versions), there will be cases where an informational error may be expected. The presence of exit-on-error prevents use of these snapshots, especially when run from docker created by nix-ops (where it isnt as straightforward to override scripts)
1 parent 070a307 commit 0d7dc4a

File tree

5 files changed

+35
-36
lines changed

5 files changed

+35
-36
lines changed

docker-compose.example.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ name: cardano-db-sync
44

55
services:
66
postgres:
7-
image: postgres:14.10-alpine
7+
image: postgres:17.2-alpine
88
environment:
99
- POSTGRES_LOGGING=true
1010
- POSTGRES_DB=cexplorer
@@ -36,7 +36,7 @@ services:
3636
max-file: "10"
3737

3838
cardano-node:
39-
image: ghcr.io/intersectmbo/cardano-node:9.0.0
39+
image: ghcr.io/intersectmbo/cardano-node:10.1.4
4040
environment:
4141
- NETWORK=${NETWORK:-mainnet}
4242
networks:
@@ -59,7 +59,7 @@ services:
5959
max-file: "10"
6060

6161
cardano-db-sync:
62-
image: ghcr.io/intersectmbo/cardano-db-sync:13.3.0.0
62+
image: ghcr.io/intersectmbo/cardano-db-sync:13.6.0.4
6363
environment:
6464
- NETWORK=${NETWORK:-mainnet}
6565
- POSTGRES_HOST=postgres

docker-compose.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ version: "3.9"
33

44
services:
55
postgres:
6-
image: postgres:14.10-alpine
6+
image: postgres:17.2-alpine
77
environment:
88
- POSTGRES_LOGGING=true
99
- POSTGRES_DB_FILE=/run/secrets/postgres_db
@@ -33,7 +33,7 @@ services:
3333
max-file: "10"
3434

3535
cardano-node:
36-
image: ghcr.io/intersectmbo/cardano-node:10.1.3
36+
image: ghcr.io/intersectmbo/cardano-node:10.1.4
3737
environment:
3838
- NETWORK=${NETWORK:-mainnet}
3939
volumes:

docker-test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ version: "3.5"
22

33
services:
44
postgres:
5-
image: postgres:14.10-alpine
5+
image: postgres:17.2-alpine
66
environment:
77
- POSTGRES_LOGGING=true
88
- POSTGRES_DB_FILE=/run/secrets/postgres_db

nix/docker.nix

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
, bashInteractive, cacert, cardano-cli, cardano-db-sync, cardano-db-tool
1111
, cardano-smash-server, coreutils, curl, findutils, getconf, glibcLocales
1212
, gnused, gnutar, gzip, jq, iana-etc, iproute, iputils, lib, libidn, libpqxx
13-
, postgresql_14, socat, utillinux
13+
, postgresql_17, socat, utillinux
1414
}:
1515

1616
let
@@ -46,7 +46,7 @@ let
4646
iputils # Useful utilities for Linux networking
4747
libidn # Library for internationalized domain names
4848
libpqxx # A C++ library to access PostgreSQL databases
49-
postgresql_14 # A powerful, open source object-relational database system
49+
postgresql_17 # A powerful, open source object-relational database system
5050
socat # Utility for bidirectional data transfer
5151
utillinux # System utilities for Linux
5252
cardano-cli # tool for interacting with cardano-node

scripts/postgresql-setup.sh

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ function check_connect_as_user {
9898

9999
function check_db_exists {
100100
set +e
101-
count=$(psql -l "${PGDATABASE}" | grep -c "${PGDATABASE} ")
101+
count=$(psql -t -l --csv "${PGDATABASE}" | grep -c "^${PGDATABASE},")
102102
if test "${count}" -lt 1 ; then
103103
echo
104104
echo "Error : No '${PGDATABASE}' database."
@@ -108,7 +108,7 @@ function check_db_exists {
108108
echo
109109
exit 1
110110
fi
111-
count=$(psql -l "${PGDATABASE}" | grep "${PGDATABASE} " | cut -d \| -f 3 | grep -c UTF8)
111+
count=$(psql -t -l --csv "${PGDATABASE}" | grep "^${PGDATABASE}," | grep -c UTF8)
112112
if test "${count}" -ne 1 ; then
113113
echo
114114
echo "Error : '${PGDATABASE}' database exists, but is not UTF8."
@@ -164,19 +164,20 @@ function create_snapshot {
164164
tmp_dir=$(mktemp "${directory}" -t db-sync-snapshot-XXXXXXXXXX)
165165
echo $"Working directory: ${tmp_dir}"
166166
pg_dump --no-owner --schema=public --jobs="${numcores}" "${PGDATABASE}" --format=directory --file="${tmp_dir}/db/"
167-
lstate_gz_file=$(basename "${ledger_file}").gz
168-
gzip --to-stdout "${ledger_file}" > "${tmp_dir}/$(basename "${ledger_file}").gz"
169-
tree "${tmp_dir}"
167+
if [ -n "${ledger_file}" ]; then
168+
lstate_gz_file=$(basename "${ledger_file}").gz
169+
gzip --to-stdout "${ledger_file}" > "${tmp_dir}/${lstate_gz_file}"
170+
fi
170171
# Use plain tar here because the database dump files and the ledger state file are already gzipped.
171-
tar cvf - --directory "${tmp_dir}" "db" "${lstate_gz_file}" | tee "${tgz_file}.tmp" \
172-
| sha256sum | head -c 64 | sed -e "s/$/ ${tgz_file}\n/" > "${tgz_file}.sha256sum"
172+
tar cvf - --directory "${tmp_dir}" "db" $( [ -f "/${tmp_dir}/${lstate_gz_file}" ] && echo "${lstate_gz_file}" ) | tee "${tgz_file}.tmp" | \
173+
sha256sum | head -c 64 | sed -e "s/$/ ${tgz_file}\n/" > "${tgz_file}.sha256sum"
173174
mv "${tgz_file}.tmp" "${tgz_file}"
174175
rm "${recursive}" "${force}" "${tmp_dir}"
175176
if test "$(tar "${test}" --file "${tgz_file}")" ; then
176177
echo "Tar reports the snapshot file as being corrupt."
177178
echo "It is not safe to drop the database and restore using this file."
178179
exit 1
179-
fi
180+
fi
180181
echo "Created ${tgz_file} + .sha256sum"
181182
}
182183

@@ -185,24 +186,26 @@ function restore_snapshot {
185186
if test "${file_count}" -gt 0 ; then
186187
echo "Ledger state directory ($2) is not empty. Please empty it and then retry."
187188
exit 1
188-
fi
189+
fi
189190
tmp_dir=$(mktemp "${directory}" -t db-sync-snapshot-XXXXXXXXXX)
190191
tar xvf "$1" --directory "$tmp_dir"
191192
if test -d "${tmp_dir}/db/" ; then
192193
# New pg_dump format
193194
lstate_gz_file=$(find "${tmp_dir}/" -iname "*.lstate.gz")
194-
lstate_file=$(basename "${lstate_gz_file}" | sed 's/.gz$//')
195-
gunzip --to-stdout "${lstate_gz_file}" > "$2/${lstate_file}"
195+
if [ -n "${lstate_gz_file}" ] ; then
196+
lstate_file=$(basename "${lstate_gz_file}" | sed 's/.gz$//')
197+
gunzip --to-stdout "${lstate_gz_file}" > "$2/${lstate_file}"
198+
fi
196199

197-
# Important: specify --schema=public below to skip over `create schema public`
198-
# statement generated by pg_dump
200+
# Important: specify --schema=public below to skip over `create schema public`
201+
# statement generated by pg_dump
199202
pg_restore \
200203
--schema=public \
201204
--jobs="${numcores}" \
202205
--format=directory \
203206
--dbname="${PGDATABASE}" \
204-
--no-owner \
205-
--exit-on-error \
207+
--no-owner \
208+
$( [ -z "${SKIP_RESTORE_ERROR}" ] && echo " --exit-on-error" ) \
206209
"${tmp_dir}/db/"
207210
else
208211
# Old snapshot format produced by this script
@@ -228,7 +231,7 @@ function usage_exit {
228231
echo " $progname --dump-schema - Dump the schema of the database."
229232
echo
230233
echo " - Create a db-sync state snapshot"
231-
echo " $progname --create-snapshot <snapshot-file> <ledger-state-file>"
234+
echo " $progname --create-snapshot <snapshot-file> [<ledger-state-file>]"
232235
echo
233236
echo " - Restore a db-sync state snapshot."
234237
echo " $progname --restore-snapshot <snapshot-file> <ledger-state-dir>"
@@ -300,22 +303,18 @@ case "${1:-""}" in
300303
--create-snapshot)
301304
check_pgpass_file
302305
check_db_exists
303-
if test $# -ne 3 ; then
304-
echo "Expecting exactly 2 more arguments, the snapshot file name template and the ledger state file."
306+
if test $# -lt 2 ; then
307+
echo "Expecting the snapshot file name (without extension) and optionally the ledger state file as arguments."
305308
exit 1
306-
fi
309+
fi
307310
if test -z "$2" ; then
308-
echo "Second argument should be the snapshot file name template."
309-
exit 1
310-
fi
311-
if test -z "$3" ; then
312-
echo "Third argument should be the ledger state file."
311+
echo "Second argument should be the snapshot file name (without extension)."
313312
exit 1
314-
fi
315-
if test -d "$3" ; then
316-
echo "Third argument is a directory and expecting a file."
313+
fi
314+
if test -n "$3" && test -d "$3" ; then
315+
echo "Third argument provided is a directory but expecting a file."
317316
exit 1
318-
fi
317+
fi
319318
create_snapshot "$2" "$3"
320319
;;
321320
--restore-snapshot)

0 commit comments

Comments
 (0)