Implementing a Kalman Filter in Postgres to Smooth GPS Data
A Kalman Filter can run entirely inside Postgres to smooth noisy GPS tracks, with per-device state (estimate plus covariance matrix) kept in a devices table and a kalman_step SQL function applying predict/update steps that can use HDOP or speed. Online filtering via an upsert wrapper on insert cuts throughput about 35-40% (8.5 ms vs 13.6 ms average latency in pgbench). For offline batch filtering, custom aggregates beat recursive CTEs (4419 vs 3443 tps), so aggregates are recommended for production and recursive CTEs for debugging and transparency.
Related posts
- What's new in pg_clickhouse v0.10.0: Subqueries, TPC-H Speedups, C Driver, and Aggregates
ClickHouse hardMaster
pg_clickhouse v0.10.0 pushes correlated scalar subqueries, IN and NOT IN down to ClickHouse as remote subqueries and anti/semi-joins, raising full TPC-H pushdown from 12 to 16 of 22 queries. Q17 drops from 32.7 s to 37 ms, beating native PostgreSQL's 2.1 s. A NULL-semantics layer tracks how each expression's result is consumed and injects CASE guards only when operands may be NULL, so ClickHouse's two-valued IN matches PostgreSQL's three-valued logic; transform_null_in is pinned to 0 so a server profile can't break those guards. The release also switches from clickhouse-cpp to the plain-C clickhouse-c driver, which fixes setjmp/longjmp-vs-exception crashes and streams results, and gives each concurrent foreign scan its own connection. Statistical, ordered-set and partitionwise aggregates now push down, and builtin function pushdown is opt-in to preserve Postgres semantics.
- pg_stat_ch: a PostgreSQL extension that exports every metric to ClickHouse
ClickHouse hardMaster
pg_stat_ch is an open-source (Apache 2.0) PostgreSQL 16–18 extension that captures every statement execution, including failed queries and DDL, as a fixed-size ~4.6KB event with 45 fields and streams it to ClickHouse for APM-style analysis. On the hot path each backend memcpys the event into a cache-line-padded shared-memory ring buffer. A background worker flushes batches once a second over ClickHouse's native protocol with LZ4 compression, and materialized views handle all aggregation on the ClickHouse side. Key design choices are fixed-size events for predictable memory (query text is truncated at 2KB), dropping events instead of applying back-pressure, and a try-lock path with per-backend batching that cuts lock acquisitions about 5x. On pgbench TPC-B with 32 clients, extension code costs about 2% CPU but throughput drops about 11% because of contention on the enqueue lock.
- ClickHouse and PostgreSQL - a Match Made in Data Heaven - part 1
ClickHouse mediumAdept
ClickHouse can query PostgreSQL data in place through the postgresql table function and the PostgreSQL table engine. Simple WHERE clauses are pushed down to Postgres, while aggregation, joins, sorting and LIMIT run in ClickHouse. On the 28M-row UK house price dataset, a low-selectivity filter makes Postgres choose a parallel sequential scan over the index, taking about 28s, and querying through ClickHouse gives similar times because Postgres still does the scan. Queries using ClickHouse-specific functions such as toYear are not pushed down and can run much slower. Rewriting joins with conditional aggregates like medianIf, or copying the data into local MergeTree tables, is what delivers the large speedups.
- Postgres on NVMe: performance and the convergence of transactions and analytics
ClickHouse mediumAdept
Running Postgres on local NVMe instead of gp3 EBS gave 9.2× more throughput (16,030 vs 1,734 TPS) and UPDATE latency of 4.0 ms vs 36.9 ms in a pgbench benchmark with a 482 GiB dataset about 30× larger than RAM. Profiling showed EBS backends spending 89% of wall time blocked on I/O. VACUUM ran 2.6× faster on NVMe, while logical decoding gained only 1.7× because it is single-threaded. To offset NVMe's ephemerality, durability moves into Postgres via quorum synchronous replication across AZs plus continuous WAL archival to S3 with WAL-G. Analytics still needs a columnar engine, so the recommended pattern is CDC (PeerDB, or the new WalShadow) into ClickHouse, queried through the pg_clickhouse extension.
- PostgreSQL Stories: From slow query to fast—via stats
Render mediumAdept
A simple LIMIT 1 join between a ~100k-row postgres_dbs table and a ~90M-row events table ran for hours. The cause was a merge join that scanned ~79M non-matching events sorted ahead of the 'dpg-' serviceId prefix. Adding manual lower and upper bounds on the indexed JSON expression brought the query down to milliseconds, but the real fault was that PostgreSQL only kept statistics on whole JSON objects, not on data->>'serviceId'. Running CREATE STATISTICS on that expression gave the planner accurate distributions, and the original query then finished in under a millisecond without rewrites.