Skip to content

Commit 5de0e12

Browse files
committed
Simplify process-orioledb-coredumps.sh using jq and JSON output
1 parent f0382c3 commit 5de0e12

2 files changed

Lines changed: 34 additions & 48 deletions

File tree

‎ansible/files/coredump/process-orioledb-coredumps.sh‎

Lines changed: 30 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,6 @@ if [[ -z ${INVOCATION_ID:-} ]]; then
4141
exit 1
4242
fi
4343

44-
# /usr/lib/postgresql/bin/postgres is a Nix wrapper *script* (sets
45-
# NIX_PGLIBDIR, then execs the real ELF at .../bin/.postgres-wrapped) - not
46-
# an executable itself. A crashing backend's real, kernel-recorded
47-
# Executable: is that wrapped path (basename ".postgres-wrapped"), never the
48-
# wrapper. Normalize both forms to "postgres" for comparison, and always use
49-
# the per-crash Executable: path (not a hardcoded one) for readelf/gdb.
50-
normalize_exe_basename() {
51-
local base
52-
base=$(basename "$1")
53-
base="${base#.}"
54-
base="${base%-wrapped}"
55-
printf '%s' "$base"
56-
}
57-
5844
# orioledb.so has no fixed, predictable path either - there is no
5945
# /usr/lib/postgresql/lib mirror of it. It lives in the same nix store
6046
# derivation as the resolved postgres executable, just under lib/ instead
@@ -67,7 +53,7 @@ orioledb_lib_path() {
6753
}
6854

6955
# coredumpctl on this systemd version (255.4) has no "rm"/"delete" verb - the
70-
# only reliable way to remove a core is to delete its on-disk Storage: path
56+
# only reliable way to remove a core is to delete its on-disk Filename path
7157
# directly. Returns success only if the path is actually gone afterward.
7258
delete_core() {
7359
local path="$1"
@@ -117,17 +103,21 @@ process_one() {
117103
# coredumpctl matches reliably by PID; matching by the raw storage path
118104
# (which encodes an escaped comm, e.g. "core.\x2epostgres-wrapp....zst"
119105
# for the wrapped binary below) was found to fail in practice.
120-
local meta
121-
if ! meta=$(coredumpctl info "$pid" --no-pager 2>/dev/null); then
106+
local info_json
107+
if ! info_json=$(coredumpctl --no-pager --json=short info "$pid" 2>/dev/null); then
122108
log "pid ${pid}: coredumpctl info failed"
123109
return 1
124110
fi
125111

126-
local exe boot_id storage_path
127-
exe=$(awk -F': ' '/^ *Executable:/ {print $2; exit}' <<<"$meta")
128-
boot_id=$(awk -F': ' '/^ *Boot ID:/ {print $2; exit}' <<<"$meta")
129-
# "Storage: /path/to/core (present)" -> "/path/to/core"
130-
storage_path=$(sed -n 's/^ *Storage: \(.*\) (.*)$/\1/p' <<<"$meta" | head -1)
112+
local exe boot_id storage signal signal_name timestamp_usec filename
113+
IFS=$'\t' read -r exe boot_id storage signal signal_name timestamp_usec filename < <(
114+
jq -r '[.Executable, .BootID, .Storage, .Signal, .SignalName, .Timestamp, (.Filename // "")] | @tsv' <<<"$info_json"
115+
)
116+
# "Storage" is a status keyword ("present"/"missing"/"none"/...), not a
117+
# path - "Filename" is populated regardless of status, so it's only safe
118+
# to treat as a still-on-disk core when Storage says "present".
119+
local storage_path=""
120+
[[ $storage == "present" ]] && storage_path="$filename"
131121
# PID alone isn't a safe dedup key long-term (PIDs get reused across
132122
# boots), so pair it with boot ID - matches "state keyed by boot ID plus
133123
# dump identifier" from the original design.
@@ -143,7 +133,7 @@ process_one() {
143133
[ -f "${state_file}.attempts" ] && attempts=$(cat "${state_file}.attempts")
144134
if [ "$attempts" -ge "$MAX_ATTEMPTS" ]; then
145135
log "pid ${pid}: exceeded ${MAX_ATTEMPTS} attempts, discarding core (metadata kept in ${QUARANTINE_DIR})"
146-
echo "$meta" >"${QUARANTINE_DIR}/${key}.info"
136+
jq . <<<"$info_json" >"${QUARANTINE_DIR}/${key}.info"
147137
if delete_core "$storage_path"; then
148138
log "pid ${pid}: raw core deleted"
149139
else
@@ -155,9 +145,9 @@ process_one() {
155145

156146
# /usr/lib/postgresql/bin/postgres is a wrapper script that execs the
157147
# real ELF at .../bin/.postgres-wrapped - the kernel (and therefore
158-
# coredumpctl's Executable:) always records the latter. Normalize both
159-
# forms before comparing, so we don't silently ignore every real crash.
160-
if [[ "$(normalize_exe_basename "$exe")" != "postgres" ]]; then
148+
# coredumpctl's Executable:) always records the latter, so match on
149+
# that suffix directly rather than a hardcoded/normalized basename.
150+
if [[ $exe != */.postgres-wrapped ]]; then
161151
log "pid ${pid}: executable '${exe}' is not postgres, ignoring"
162152
touch "${state_file}.done"
163153
return 0
@@ -183,24 +173,25 @@ process_one() {
183173
return 1
184174
fi
185175

186-
# Already have this in $meta from the coredumpctl info call above -
187-
# just pulling out the two fields the bundle header needs.
188-
local signal timestamp
189-
signal=$(awk -F': ' '/^ *Signal:/ {print $2; exit}' <<<"$meta")
190-
timestamp=$(awk -F': ' '/^ *Timestamp:/ {print $2; exit}' <<<"$meta")
191-
192176
# Everything from here to the closing "}" is the diagnostic bundle
193177
# itself, one section at a time, redirected straight to $bundle -
194178
# there's no in-memory buffering of it, so a slow/hanging step just
195179
# shows up as a truncated file rather than blocking the whole write.
180+
# Timestamp from coredumpctl JSON is microseconds since the epoch, so it
181+
# converts straight to a UTC date/time with no string parsing - and that
182+
# same "YYYY-MM-DD HH:MM:SS" form is what postgres's own log line prefix
183+
# starts with, so it doubles as the grep key below.
184+
local crash_timestamp
185+
crash_timestamp=$(date -u -d "@$((timestamp_usec / 1000000))" '+%Y-%m-%d %H:%M:%S')
186+
196187
local bundle="${OUTPUT_DIR}/${key}.txt"
197188
{
198189
echo "== OrioleDB/PostgreSQL coredump diagnostic bundle =="
199190
echo "generated: $(date -u '+%Y-%m-%dT%H:%M:%SZ')"
200191
echo "pid: ${pid}"
201192
echo "boot_id: ${boot_id}"
202-
echo "signal: ${signal}"
203-
echo "crash_timestamp: ${timestamp}"
193+
echo "signal: ${signal} (${signal_name})"
194+
echo "crash_timestamp: ${crash_timestamp} UTC"
204195
echo "executable: ${exe}"
205196
echo
206197

@@ -224,15 +215,10 @@ process_one() {
224215
echo
225216

226217
echo "== postgresql.log excerpt around crash =="
227-
# $timestamp looks like "Thu 2026-09-17 11:25:49 UTC (1s ago)" - pull
228-
# out just the "YYYY-MM-DD HH:MM:SS" portion to match against
229-
# postgres's own log line prefix (a fixed offset previously grabbed
230-
# the leading weekday name instead and never matched anything).
231-
log_ts=$(grep -oE '[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}' <<<"$timestamp" | head -1)
232218
postgres_log=$(current_postgres_log)
233-
if [ -n "$log_ts" ] && [ -n "$postgres_log" ] && [ -f "$postgres_log" ]; then
234-
grep -F "$log_ts" -A 5 -B 20 "$postgres_log" 2>/dev/null | tail -200 ||
235-
echo "(no log lines found matching ${log_ts} in ${postgres_log})"
219+
if [ -n "$postgres_log" ] && [ -f "$postgres_log" ]; then
220+
grep -F "$crash_timestamp" -A 5 -B 20 "$postgres_log" 2>/dev/null | tail -200 ||
221+
echo "(no log lines found matching ${crash_timestamp} in ${postgres_log})"
236222
else
237223
echo "(no crash timestamp or log file available)"
238224
fi
@@ -259,9 +245,7 @@ main() {
259245
# Enumerate via coredumpctl (per INSTRUCTIONS.md's original design),
260246
# restricted to entries whose raw core is still on disk ("present") -
261247
# already-removed/historical journal entries are skipped without
262-
# needing a coredumpctl info round-trip. Field positions match the
263-
# observed `coredumpctl list` table layout (systemd 255):
264-
# TIME(4 tokens) PID UID GID SIG COREFILE EXE SIZE
248+
# needing a coredumpctl info round-trip.
265249
local rc=0
266250
local pid
267251
while read -r pid; do
@@ -270,7 +254,7 @@ main() {
270254
log "pid ${pid}: processing failed, will retry on next run"
271255
rc=1
272256
}
273-
done < <(coredumpctl --no-legend --no-pager list 2>/dev/null | awk '$9 == "present" {print $5}')
257+
done < <(coredumpctl --no-pager --json=short list 2>/dev/null | jq -r '.[] | select(.corefile == "present") | .pid')
274258

275259
return "$rc"
276260
}

‎ansible/tasks/setup-coredump-processing.yml‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@
2121
- Regenerate grub configuration
2222

2323
# gdb and binutils (readelf) are only used by the processor script below,
24-
# which is only installed for OrioleDB images due to is_psql_oriole
25-
- name: Ensure gdb and binutils are installed
24+
# which is only installed for OrioleDB images due to is_psql_oriole. jq
25+
# parses coredumpctl's --json output instead of scraping its text tables.
26+
- name: Ensure gdb, binutils, and jq are installed
2627
ansible.builtin.apt:
2728
name:
2829
- gdb
2930
- binutils
31+
- jq
3032

3133
- name: Create orioledb-coredumps state/output directories
3234
ansible.builtin.file:

0 commit comments

Comments
 (0)