Skip to content

Recipe DuckDB

Avinash Gupta edited this page Oct 18, 2026 · 1 revision

Recipe: SQL over RDS logs with DuckDB

The fastest way to answer "what happened on this database?" when there's no log platform. DuckDB is a single binary. It reads rdstail's gzipped NDJSON directly, from S3 or from a local file, and nothing has to stay running afterwards.

All queries are in examples/sql/duckdb.sql.

1. Get the logs

No archive yet? Dump the incident window (read-only IAM, no config):

rdstail dump -i orders-db --region us-east-1 --since 6h -o incident.ndjson.gz
rdstail dump -i orders-db -i orders-db-replica --since 24h --grep 'deadlock|timeout' -o locks.ndjson.gz

dump is bounded by the RDS API's per-file read speed: expect roughly 1 minute per 20–50 MB of raw log. A 6-hour dump of a busy slow-query log (117 MB) took about 6 minutes. Use --grep to keep only matching lines. The filter runs after the download, so it makes the output file smaller but the dump no faster.

Already shipping to S3? Point DuckDB at the sink's prefix instead.

2. Load the views

duckdb rds.duckdb

Tell the script where the logs are, then read it:

SET VARIABLE logs_path = 'incident.ndjson.gz';
-- or: 's3://my-log-bucket/rds/orders-db/**/2026/10/1*/*.ndjson.gz'
.read examples/sql/duckdb.sql

This creates three views:

View What
rds_logs_raw Every record, with the source object in filename
rds_logs Deduplicated: a batch re-delivered after a crash is counted once
pg_logs PostgreSQL rows with db_user / db_name lifted from RDS's fixed line prefix

S3 credentials come from the normal AWS chain (env, profile, SSO, instance role) via DuckDB's aws extension. Narrow the glob to the databases and days you need: DuckDB lists and downloads every object it matches. The S3 sink's key layout is <prefix>/<instance>/<engine>/<log file>/YYYY/MM/DD/<ms>-<batch_id>.ndjson.gz (the date is the write date).

3. Ask questions

The file runs these in order. Each is a starting point to edit.

Query Answers
Warnings/errors per database (24h) Which DB is unhappy?
Top error messages Numbers and quoted values are folded so duplicate key value "42" and "43" group together
Free-text search in a window regexp_matches(message, '(?i)deadlock|lock wait timeout')
Slowest query shapes pt-query-digest-style: fingerprints MySQL slow-log and PostgreSQL duration: statements, ranks by total time
Destructive DDL + privilege changes DROP, TRUNCATE, GRANT, REVOKE, user/role changes
pgAudit non-read statements Uses rdstail's structured audit.class / audit.command
Failed logins by user and client PostgreSQL auth failures and MySQL/MariaDB audit FAILED_CONNECT
DELETE/UPDATE without WHERE Whole-table rewrites

A few one-liners for the shell:

# errors in a dump, newest last
duckdb -c "SELECT timestamp, severity, message FROM read_json('incident.ndjson.gz')
           WHERE severity IN ('ERROR','FATAL') ORDER BY timestamp"

# lines per hour per log file: where did the volume come from?
duckdb -c "SELECT date_trunc('hour', timestamp) h, log_file, count(*)
           FROM read_json('incident.ndjson.gz') GROUP BY ALL ORDER BY h"

Notes

  • Timestamps are the engine's own event time when the line has one, inherited by continuation lines, and fetch time otherwise. The setup sets TimeZone = 'UTC' so results match the raw log lines.
  • Multi-line entries (slow-query blocks, wrapped SQL, stack traces) are one record with embedded newlines. Use (?s) in a regex to match across them.
  • Huge statements: bulk INSERTs in slow logs can be hundreds of KB per record. The slow-query fingerprint collapses VALUES (…),(…) lists to (?+) and truncates to 300 characters.
  • For a recurring, shared view of the same data, move to Athena (serverless) or the Loki + Grafana stack (dashboards and alerts).

Clone this wiki locally