Beyond Execution Hooks
Designing Low-Overhead Telemetry and Diagnostic Architectures
Speaker : Kanishk Verma
Agenda
00:00 - 03:00 | The Hook Paradigm: Power and Ubiquity |
06:00 - 12:00 | The Anti-Pattern: Inline Processing & Network I/O (With simple use case) |
12:00 - 17:00 | Simple on demand signal and poll architecture. |
17:00 - 23:00 | Custom Signals and Interrupts |
23:00 - 27:00 | Case study: pg_query_state |
27:00 - 30:00 | Summary & Takeaways |
What is a hook?
The Definition
In Postgres, a "hook" is fundamentally a global function pointer found in the core server code (e.g., src/backend/executor/execMain.c).
Execution Logic
Postgres checks if the pointer is set. If null, it runs the standard function. If set, it redirects to the custom function registered by the extension.
Lifecycle Stages
Parsing
Planning
Exec Start
Exec Run
Exec End
warning
Crucial Detail: Synchronous Execution
Execution runs inline within the backend process handling the client connection. If your hook takes time, the client blocks.
The Hook Paradigm: Power and Ubiquity
The Success Pattern:
Hook Chaining Internals & "Broken Chains"
Registration & Setup
// Global pointer to previous hook
static ExecutorRun_hook_type prev_ExecutorRun = NULL;
void _PG_init(void) {
/* Save the next step in the chain */
prev_ExecutorRun = ExecutorRun_hook;
/* Insert our custom hook at the head */
ExecutorRun_hook = my_custom_ExecutorRun;
}
Execution Logic
static void my_custom_ExecutorRun(...) {
// 1. Pre-execution logic (CPU bound only!)
...
// 2. Call next link in chain
if (prev_ExecutorRun)
prev_ExecutorRun(...);
else
standard_ExecutorRun(...);
// 3. Post-execution logic
...
}
The Anti-Pattern: Inline Processing & Network I/O
dangerous
Real-World Use Case: Telemetry to Remote Store
How to send the telemetry data such as the query execution time or the query statistics to the remote store such as Spanner or BigQuery or Another DataStore.
Operationalizing Telemetry: Design Solution
settings_input_component
Solution 1: Integrated Inline Telemetry
Implementation of a network call for telemetry directly within the inline hook to persist metrics to highly available datastores.
warning
Note: Inline processing introduces network I/O latency hazards.
Direct network calls in hooks can cause 100x query latency spikes.
Mathematical Proof of Connection Exhaustion
Scenario A: Baseline
Latency: 0.1 ms
10,000 QPS
Max throughput per connection
Scenario B: 10ms Inline Block
Latency: 10.1 ms
~99 QPS
Max throughput per connection
With a max_connections pool of 100, the total system capacity suffers a catastrophic drop:
1,000,000 QPS -> 9,900 QPS
warning
Result: Denial of Service (DoS)
Connection pool exhaustion occurs. Clients timeout, backends saturate, and the database becomes unavailable.
Solution 2: Asymmetric Offloading
Instead of sending metrics over the network inline, the backend process writes metrics into a shared memory segment.
Specific Implementation
Ring Buffer
Ensures high-performance write operations without the overhead of traditional locking mechanisms.
What happens when the system is under extreme load and the Background-worker is too slow?
1. Data Store Slow
The remote log data store slows down, causing the Background Worker (BGW) to block on network write calls.
2. High Qps
Client queries execute quickly, writing metrics to shared memory. The write pointer catches up to the BGW read pointer.
priority_high
The Critical Decision
The system must choose between two suboptimal outcomes:
Solution : Lossy Drop (Load-Shedding)
Solution 2.1: On-Demand Diagnostics
Beyond Telemetry Logging: Solving for Interactive Diagnostics
The Challenge
A query is stuck (e.g., running for 10 minutes), and you need to identify the specific join node causing the delay.
Why existing methods fail:
The Requirement
Inspect execution state on-demand
Zero Overhead Goal:
Eliminate performance penalties when diagnostics are not actively being performed.
"We need a way to inspect the execution state of an active backend on-demand, without paying any overhead when we aren't looking."
Custom Signals and Interrupts Architecture
Let’s understand the real world example of pg_query_state But what is it?
The pg_query_state module provides facility to know the current state of query execution on working backend.
To enable this extension, you have to patch the stable version of PostgreSQL, recompile it, and deploy new binaries. All patch files are located in the patches/ directory.
Key Use Cases
Note: Extension requires patching stable PostgreSQL versions based on the specific version number suffix.
Production case study :
pg_query_state
Summary & Takeaways
References and further Readings