---
title: "Implementing a Kalman Filter in Postgres to Smooth GPS Data"
description: "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."
url: "https://swe.quest/articles/implementing-a-kalman-filter-in-postgres-to-smooth-gps-data-fd49bdbe30ebf6c9"
canonical: "https://neon.com/blog/implementing-a-kalman-filter-in-postgres-to-smooth-gps-data"
source: "Neon"
published: "2025-09-26"
difficulty: "medium"
kind: "How-to"
reading_minutes: 5
tags: ["postgres","geospatial","sql","algorithms","performance"]
---

# Implementing a Kalman Filter in Postgres to Smooth GPS Data

A post on [Neon](https://swe.quest/sources/neon.md), published 2025-09-26, 5 min read, How-to. Original: [https://neon.com/blog/implementing-a-kalman-filter-in-postgres-to-smooth-gps-data](https://neon.com/blog/implementing-a-kalman-filter-in-postgres-to-smooth-gps-data)

## Summary

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.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [PostgreSQL](https://swe.quest/tags/postgres.md) (Data & databases)
- [Maps & geospatial](https://swe.quest/tags/geospatial.md) (Domains)
- [SQL](https://swe.quest/tags/sql.md) (Languages)
- [Algorithms](https://swe.quest/tags/algorithms.md) (Architecture & design)
- [Performance engineering](https://swe.quest/tags/performance.md) (Reliability & operations)

## Prerequisites

- SQL functions and recursive CTEs ([easy posts on SQL](https://swe.quest/tags/sql.md?difficulty=easy))
- Postgres custom aggregates ([easy posts on PostgreSQL](https://swe.quest/tags/postgres.md?difficulty=easy))
- basic Kalman filter concepts

## Keywords

Kalman Filter, PostgreSQL, pgbench, HDOP, Neon, traconiq

## Related posts

- [What's new in pg\_clickhouse v0.10.0: Subqueries, TPC-H Speedups, C Driver, and Aggregates](https://swe.quest/articles/what-s-new-in-pg-clickhouse-v0-10-0-subqueries-tpc-h-speedups-c-driver-and-178ced76a7784be7.md): ClickHouse · 2026-08-11 · Hard · 11 min read · PostgreSQL, Query engines, Analytics & OLAP, SQL, Performance engineering
  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.
- [pg\_stat\_ch: a PostgreSQL extension that exports every metric to ClickHouse](https://swe.quest/articles/pg-stat-ch-a-postgresql-extension-that-exports-every-metric-to-clickhouse-44a394f2889686fc.md): ClickHouse · 2026-02-13 · Hard · 16 min read · PostgreSQL, Observability, Performance engineering, Concurrency, Analytics & OLAP
  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.
- [ClickHouse and PostgreSQL - a Match Made in Data Heaven - part 1](https://swe.quest/articles/clickhouse-and-postgresql-a-match-made-in-data-heaven-part-1-7c7668b9b4913b70.md): ClickHouse · 2022-12-20 · Medium · 18 min read · Analytics & OLAP, PostgreSQL, Query engines, Performance engineering, SQL
  ClickHouse can query PostgreSQL data in place through the postgresql table function and the PostgreSQL table engine.
- [Postgres on NVMe: performance and the convergence of transactions and analytics](https://swe.quest/articles/postgres-on-nvme-performance-and-the-convergence-of-transactions-and-analytics-6553677f279f656e.md): ClickHouse · 2026-09-21 · Medium · 8 min read · PostgreSQL, Performance engineering, Storage systems, Replication, Analytics & OLAP
  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.
- [PostgreSQL Stories: From slow query to fast—via stats](https://swe.quest/articles/postgresql-stories-from-slow-query-to-fast-via-stats-ac7001ab0fde8224.md): Render · 2024-05-14 · Medium · 13 min read · PostgreSQL, Query engines, Performance engineering, Debugging
  A simple LIMIT 1 join between a ~100k-row postgres\_dbs table and a ~90M-row events table ran for hours.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
