---
title: "How to take random samples from big PostgreSQL tables"
description: "Random sampling from billion-row PostgreSQL tables with ORDER BY random() scans every row and runs a top-N heapsort, taking over a minute."
url: "https://swe.quest/articles/how-to-take-random-samples-from-big-postgresql-tables-9724e6f9679558c6"
canonical: "https://render.com/blog/postgresql-random-samples-big-tables"
source: "Render"
published: "2024-06-13"
difficulty: "medium"
kind: "How-to"
reading_minutes: 10
tags: ["postgres","sql","performance","query-engines"]
---

# How to take random samples from big PostgreSQL tables

A post on [Render](https://swe.quest/sources/render.md), published 2024-06-13, 10 min read, How-to. Original: [https://render.com/blog/postgresql-random-samples-big-tables](https://render.com/blog/postgresql-random-samples-big-tables)

## Summary

Random sampling from billion-row PostgreSQL tables with ORDER BY random() scans every row and runs a top-N heapsort, taking over a minute. A WHERE random() \< p filter (Bernoulli sampling) drops the sort and halves runtime, but it still reads every heap page. TABLESAMPLE SYSTEM picks whole 8kB heap blocks and returns about 10k rows in roughly 50ms, at the cost of a nondeterministic result count and correlated rows within each block. A 10k-trial simulation showed result sizes and runtimes are normally distributed with modest row overlap. For joins, sample one side (the "many" side) before joining, and never sample both tables independently.

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)
- [SQL](https://swe.quest/tags/sql.md) (Languages)
- [Performance engineering](https://swe.quest/tags/performance.md) (Reliability & operations)
- [Query engines](https://swe.quest/tags/query-engines.md) (Data & databases)

## Prerequisites

- SQL queries ([easy posts on SQL](https://swe.quest/tags/sql.md?difficulty=easy))
- PostgreSQL query plans ([easy posts on PostgreSQL](https://swe.quest/tags/postgres.md?difficulty=easy))
- basic probability

## Keywords

PostgreSQL, TABLESAMPLE, Bernoulli sampling, ORDER BY random(), heap blocks, EXPLAIN

## Related posts

- [ClickHouse Newsletter February 2023: A ‘Sample’ of What’s Happening in the Community](https://swe.quest/articles/clickhouse-newsletter-february-2023-a-sample-of-what-s-happening-in-the-9cf1bfd9f4573dfb.md): ClickHouse · 2023-02-15 · Medium · 9 min read · Analytics & OLAP, SQL, Performance engineering
  ClickHouse's SAMPLE BY clause trades exact answers for speed on very large tables, illustrated with a 425-billion-row Wikistat dataset.
- [Native random sampling in ClickHouse](https://swe.quest/articles/native-random-sampling-in-clickhouse-b04e18e64b8c1545.md): ClickHouse · 2026-05-22 · Medium · 11 min read · Analytics & OLAP, Query engines, Performance engineering, SQL
  ClickHouse's SAMPLE clause runs aggregations over a deterministic subset of rows selected by a hash-based sample key, which must be part of the primary key so granules above the hash threshold can be skipped.
- [ClickHouse and The One Billion Row Challenge](https://swe.quest/articles/clickhouse-and-the-one-billion-row-challenge-a492ebece6e75794.md): ClickHouse · 2024-01-23 · Medium · 8 min read · Analytics & OLAP, SQL, Performance engineering
  ClickHouse Local solves the One Billion Row Challenge (min/mean/max temperature per station from a 13GB text file) in about 20 seconds on an 8-core Hetzner VM, versus nearly 3 minutes for the Java baseline.
- [PostgresBench: A Reproducible Benchmark for Postgres Services](https://swe.quest/articles/postgresbench-a-reproducible-benchmark-for-postgres-services-3a30213211ea6b71.md): ClickHouse · 2026-04-02 · Medium · 9 min read · PostgreSQL, Performance engineering, Storage systems, Cloud platforms
  PostgresBench is an open, reproducible benchmark for managed Postgres services built on pgbench's TPC-B-like workload (256 clients, 16 threads, 10-minute runs, three runs each) at ~100 GB and ~500 GB scale factors, with default configs, HA disabled and all services in us-east-2.
- [Implementing "seen by" functionality with Postgres](https://swe.quest/articles/implementing-seen-by-functionality-with-postgres-42ef1db52a88742a.md): Supabase · 2022-07-18 · Medium · 21 min read · PostgreSQL, Data structures, Performance engineering, Databases
  Four ways to track post views in Postgres are benchmarked: a naive counter column, an hstore of viewer IDs per post, an association table, and the HyperLogLog extension.

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)
