---
title: "Dynamically estimating and scaling Postgres’ working set size"
description: "Neon autoscales Postgres compute to keep the workload's working set in memory, using a modified HyperLogLog whose register bits are replaced with timestamps so the number of distinct pages accessed can be estimated for any lookback window."
url: "https://swe.quest/articles/dynamically-estimating-and-scaling-postgres-working-set-size-9625ea29ef0bf2ef"
canonical: "https://neon.com/blog/dynamically-estimating-and-scaling-postgres-working-set-size"
source: "Neon"
published: "2024-09-05"
difficulty: "medium"
kind: "Deep dive"
reading_minutes: 12
tags: ["postgres","algorithms","capacity-planning","caching","serverless"]
---

# Dynamically estimating and scaling Postgres’ working set size

A post on [Neon](https://swe.quest/sources/neon.md), published 2024-09-05, 12 min read, Deep dive. Original: [https://neon.com/blog/dynamically-estimating-and-scaling-postgres-working-set-size](https://neon.com/blog/dynamically-estimating-and-scaling-postgres-working-set-size)

## Summary

Neon autoscales Postgres compute to keep the workload's working set in memory, using a modified HyperLogLog whose register bits are replaced with timestamps so the number of distinct pages accessed can be estimated for any lookback window. A heuristic scans windows from a 5-minute offset up to one hour and treats the point where the estimate jumps sharply as the end of the current workload's plateau. Scaling then projects working set growth to avoid repeated cache evictions. In benchmarks this matched fixed-size compute performance at 30% lower cost, roughly tripled throughput on a bursty pgbench workload, and raised cache hit rate from 30-70% to 100% on a batch workload.

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)
- [Algorithms](https://swe.quest/tags/algorithms.md) (Architecture & design)
- [Capacity planning](https://swe.quest/tags/capacity-planning.md) (Reliability & operations)
- [Caching](https://swe.quest/tags/caching.md) (Backend & APIs)
- [Serverless](https://swe.quest/tags/serverless.md) (Backend & APIs)

## Prerequisites

- Postgres buffer cache basics ([easy posts on PostgreSQL](https://swe.quest/tags/postgres.md?difficulty=easy))
- HyperLogLog cardinality estimation
- autoscaling concepts ([easy posts on Capacity planning](https://swe.quest/tags/capacity-planning.md?difficulty=easy))

## Keywords

Neon, HyperLogLog, Local File Cache, pgbench, sql\_exporter, Aurora Serverless v2

## Related posts

- [Autoscaling Lakebase Postgres](https://swe.quest/articles/autoscaling-lakebase-postgres-63257ef0753db067.md): Neon · 2026-08-31 · Hard · 12 min read · PostgreSQL, Capacity planning, Virtualization, Algorithms, Kubernetes
  Neon's Lakebase Postgres resizes compute in place about once every 81 seconds by taking the maximum of three target sizes: CPU load (one-minute average kept under 90%), memory (polled every 100 ms by an in-VM monitor and kept under 75%), and the working set of the compute cache.
- [1 Year of Autoscaling Postgres: How it’s going, and what’s next](https://swe.quest/articles/1-year-of-autoscaling-postgres-how-it-s-going-and-what-s-next-a75ce75d92118903.md): Neon · 2024-04-15 · Hard · 8 min read · PostgreSQL, Virtualization, Capacity planning, Memory management, Kubernetes
  Neon autoscales Postgres by running each instance as a VM in Kubernetes (NeonVM), with a per-node autoscaler-agent, a modified scheduler, and an in-VM vm-monitor that resizes the Local File Cache.
- [Monitor These Key Neon Metrics Via Datadog](https://swe.quest/articles/monitor-these-key-neon-metrics-via-datadog-4c621c1afdac4161.md): Neon · 2024-11-12 · Medium · 5 min read · PostgreSQL, Observability, Caching, Performance engineering
  Neon now exports database metrics to Datadog via an API-key integration covering all computes in a project.
- [Autoscaling in Action: Postgres Load Testing with pgbench](https://swe.quest/articles/autoscaling-in-action-postgres-load-testing-with-pgbench-9e7339241ec6e0af.md): Neon · 2024-02-23 · Medium · 5 min read · PostgreSQL, Capacity planning, Performance engineering, Serverless
  A pgbench load test against Neon serverless Postgres runs a CPU-heavy query, SELECT log(factorial(32000)) / log(factorial(20000)), to trigger autoscaling.
- [Performance tips for Neon Postgres](https://swe.quest/articles/performance-tips-for-neon-postgres-9739985c47432424.md): Neon · 2024-04-04 · Medium · 8 min read · PostgreSQL, Performance engineering, Serverless, SQL
  Neon's serverless Postgres follows mostly standard Postgres tuning, plus a few Neon-specific checks.

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)
