Skip to content

DmitryNFomin/pg_trace

Repository files navigation

pg_trace - Oracle 10046-Style Tracing for PostgreSQL

License PostgreSQL

Complete Oracle 10046-style tracing solution for PostgreSQL with per-block I/O analysis, OS cache detection, and comprehensive per-node statistics.

🎯 Features

  • SQL text, bind variables, execution plans - Full query visibility
  • Per-block I/O timing - See exactly where I/O happens
  • Three-tier cache analysis - PG buffers → OS cache → Physical disk
  • OS cache vs disk distinction - Timing-based detection (no eBPF needed!)
  • Comprehensive per-node statistics - Timing, buffers, I/O, CPU estimation
  • CPU time from /proc - User and system time per query
  • File paths and relation names - Full visibility into data access
  • No root or eBPF required - Pure PostgreSQL extension
  • Low overhead - Only 2-4% performance impact

🚀 Quick Start

Installation

# Clone the repository
git clone https://github.com/DmitryNFomin/pg_trace.git
cd pg_trace

# Build
make
sudo make install

# Configure PostgreSQL (add to postgresql.conf)
echo "shared_preload_libraries = 'pg_trace_ultimate'" >> $PGDATA/postgresql.conf
echo "track_io_timing = on" >> $PGDATA/postgresql.conf

# Restart PostgreSQL
sudo pg_ctl restart -D $PGDATA

Usage

-- Create extension
CREATE EXTENSION pg_trace_ultimate;

-- Start tracing
SELECT pg_trace_start_trace();

-- Run your queries
SELECT * FROM your_table WHERE ...;

-- Stop tracing and get trace file
SELECT pg_trace_stop_trace();
-- Returns: /tmp/pg_trace/pg_trace_12345_1699186215.trc

-- View the trace
\! cat /tmp/pg_trace/pg_trace_*.trc

📊 Example Output

=====================================================================
PARSE #1
SQL: SELECT * FROM employees WHERE salary > 50000
PARSE TIME: 0.001523 sec
---------------------------------------------------------------------
EXEC #1
EXEC TIME: ela=2.456 sec rows=1234
CPU: user=1.850 sec system=0.606 sec
---------------------------------------------------------------------
Three-Tier Cache Analysis:

  Tier 1 - PostgreSQL Shared Buffers:
    Hits: 9500 blocks (95.0%) - instant access

  Tier 2 - OS Page Cache:
    Hits: 450 blocks (4.5%) - fast (18 us avg)

  Tier 3 - Physical Disk:
    Reads: 50 blocks (0.5%) - slow (1567 us avg) ← BOTTLENECK!

EXECUTION PLAN #1:
-> SeqScan on employees (actual rows=1234 loops=1)
   Timing: startup=0.123 ms, total=2456.78 ms
   Buffers: shared hit=9500 read=500 (95.0% cache hit)
   I/O Detail: total=78.36 ms, avg=156.7 us/block, ~450 from OS cache, ~50 from disk
   Time breakdown: CPU ~2378.4 ms (96.8%), I/O ~78.4 ms (3.2%)
=====================================================================

📚 Documentation

🏗️ Three Versions

This repository includes three implementations:

pg_trace_ultimate(RECOMMENDED)

  • Per-block I/O + OS cache detection + per-node stats
  • Build: make && sudo make install

🆚 Comparison with Oracle 10046

Feature Oracle 10046 pg_trace Ultimate
SQL text
Bind variables
Execution plan
CPU time
Buffer gets (cr)
Physical reads (pr)
Per-block I/O
I/O timing
OS cache detection BETTER!
Per-node stats
File paths
Root required
eBPF required
Score 8/12 10/12 🏆

💡 Key Innovation

We discovered that PostgreSQL's track_io_timing + timing analysis can distinguish three cache tiers without eBPF:

  • Tier 1 (PG cache): shared_blks_hit - Instant access
  • Tier 2 (OS cache): Fast I/O (< 500us) - Syscall but no disk
  • Tier 3 (Disk): Slow I/O (> 500us) - Actual physical I/O

The 10-100x timing difference makes detection trivial!

📈 Performance Impact

  • Total overhead: 2-4% (production-safe)
  • Breakdown:
    • track_io_timing: 0.1-2% (usually <0.5%)
    • Extension hooks: 1-2%
    • Trace file writes: 0.5-1%
    • /proc reads: <0.1%

🔧 Requirements

  • PostgreSQL 12+ (tested on 12-16)
  • Linux (for /proc filesystem)
  • track_io_timing = on (required for per-block I/O timing)

📝 License

PostgreSQL License (same as PostgreSQL)

🙏 Credits

Based on Oracle 10046 trace format and PostgreSQL instrumentation infrastructure.

📞 Support


Start tracing: SELECT pg_trace_start_trace(); 🚀

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published