Skip to content

Events

Hook into DuckDB's internal events — query begin/end, transactions, connections — and ship them as JSON to any external program via stdin.

27,934
extension loads · last 90 days
On this page

Technical Overview

DuckDB's internal lifecycle, as a JSON stream

What it is

How it works

  • Configure with PRAGMAs: events_destination is a fully-qualified executable path (invoked directly, not via a shell — python3 handler.py won't work; point at a shebang script or the interpreter binary). events_types picks which events to capture, events_session_name tags every event, and events_async switches delivery mode.
  • Seven event types: connection_opened, connection_closed, query_begin, query_end, transaction_begin, transaction_commit, and transaction_rollback. The default capture is ['query_begin', 'query_end'] — the smallest set that still gives query-level audit visibility, two events per statement.
  • JSON lines on stdin: Every event is one line of JSON. A common envelope (event, timestamp in ISO 8601, database_path, session_name, connection_id, process_id) carries per-type fields on top — query_id (pairs query_begin/query_end), transaction_id, has_error, error_message, error_type, and attached_databases[]. The handler can be a shell script, a Python service, a Go binary — anything that reads stdin a line at a time.
  • Sync or fire-and-forget delivery: Synchronous (the default) writes the event and waits for the handler to consume it before the originating query returns — reliable, but adds latency to every query. SET events_async = TRUE is fire-and-forget: no blocking, but events are dropped silently if the handler stalls, dies, or can't keep up. There's no in-between mode; pick one per session.

Production caveats

  • Process-scoped, not durable: Configuration lives in the running DuckDB process — restart and it's gone. Set the PRAGMAs at session start or wrap them in a startup script. The destination can be durable (file, S3, Kafka, syslog); the configuration is not. Same caveat as the sibling cronjob extension — both are operate-your-DuckDB-process tools, observable only while the process is up.
  • One handler per session: Only one destination at a time. For fan-out — file plus Kafka plus alerting — point events_destination at a small dispatcher that reads stdin and forks the stream itself (tee, a Python script, vector, etc.). There's also no per-query filter at the extension layer; every event of an enabled type is emitted, so filter downstream in the handler.
  • Sync mode blocks queries: In the default synchronous mode the handler is on the critical path — a 50 ms-per-event handler adds 50 ms to every query, and a hung handler holds up everything. Add timeouts and a fast path, or accept the trade-off and switch to events_async = TRUE.
  • Handler runs with DuckDB's privileges: The destination program is launched by the DuckDB process and inherits its privileges and environment. Treat the events_destination path as security-sensitive — point it at a fully-qualified file that's write-protected from the database user.

Deep Dive

Technical Details

Install

INSTALL events FROM community;
LOAD events;

Quick Start

Configure where events go and what to capture

SET events_destination = '/usr/local/bin/event-handler.py';
SET events_types = ['query_begin', 'query_end'];

Optional: don't block queries on the handler

SET events_async = TRUE;

Optional: tag every event with a session name

SET events_session_name = 'analytics-replica';

Reference

Extension Contents

Quick reference to all available functions and settings organized by category.

Name Description
Configuration
events_async When TRUE, events are delivered fire-and-forget — DuckDB doesn't wait for the handler before returning from the originating query.
events_destination Path to an external program that receives event JSON via stdin.
events_session_name Optional string included in every event.
events_types List of event types to capture.

Configuration

Settings

Configure the events extension behavior using these settings.

events_async

Default Value: false

events_destination

Default Value:

events_session_name

Default Value:

events_types

Default Value: [query_begin, query_end]

Practical Examples

Cookbook

Real-world recipes and patterns for common use cases.

Platform Support

Compatibility

Extension availability may vary by platform and DuckDB version. Check below to ensure this extension supports your environment before installation.

Quick Facts

Release status Stable
Software License MIT
Pricing Free
Written In C++
Source Available Yes
View on GitHub
Usage
27,934
loads · last 90 days

Platforms

  • Linux x86_64 aarch64
  • Linux (musl) Not available
  • macOS Intel Apple Silicon
  • Windows x86_64
  • WASM Not available
Compiled binary sizes
Platform Architecture Size
Linux x86_64 3.22 MB
Linux aarch64 2.85 MB
macOS Intel 2.60 MB
macOS Apple Silicon 2.26 MB
Windows x86_64 7.39 MB

Compressed download size from the Haybarn extension repository.

DuckDB & Haybarn

Release calendar