Skip to content

About

pg_chaos_shuffle is a PostgreSQL extension for Chaos Engineering that enforces random ordering on `SELECT` queries missing an `ORDER BY` clause.

Resources

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

pg_chaos_shuffle

pg_chaos_shuffle is a PostgreSQL extension for Chaos Engineering that enforces random ordering on SELECT queries missing an ORDER BY clause.

It helps developers and SREs detect bugs caused by implicit reliance on default sort orders (e.g., Primary Key order or insertion order).

Caution

USE WITH EXTREME CAUTION IN PRODUCTION.

While Chaos Engineering encourages testing in production, this extension alters query results, which may break application logic (e.g., pagination, UI sorting) and affect user experience.

Recommended Strategy:

  1. CI / Staging: Enable globally to detect hidden bugs early.
  2. Production: If used, limit the blast radius. Do not enable it globally via shared_preload_libraries. Instead, load it per session/role for specific testing.

Motivation

In PostgreSQL, the order of rows returned by a SELECT statement without ORDER BY is undefined. However, in practice, rows are often returned in the order of insertion or by Primary Key.

Many applications accidentally rely on this "coincidental" order. This is a fragile dependency; changes in execution plans or physical data layout can suddenly break the application.

pg_chaos_shuffle acts as a chaos agent by randomizing these results, forcing the system to reveal its hidden dependencies on implicit ordering.

Installation

Option A: From Binary Release (Recommended for CI/Docker)

  1. Download the zip file matching your PostgreSQL version and Architecture from the Releases page.
  2. Extract and copy the files to your PostgreSQL installation directories.
# Example for Linux
cp pg_chaos_shuffle.so $(pg_config --pkglibdir)/
cp pg_chaos_shuffle.control $(pg_config --sharedir)/extension/
cp pg_chaos_shuffle--*.sql  $(pg_config --sharedir)/extension/
  1. Add to postgresql.conf:
shared_preload_libraries = 'pg_chaos_shuffle'
  1. Restart PostgreSQL.

Option B: Build from Source (For Rust Developers)

You need pgcentralfoundation/pgrx installed.

cargo pgrx run pg16  # Replace with your target PG version

Usage

Enable the extension in your database:

CREATE EXTENSION pg_chaos_shuffle;

(Note: If you didn't set shared_preload_libraries, you may need to run LOAD 'pg_chaos_shuffle'; in the session first.)

verification

Run the following SQL to see the chaos in action:

-- 1. Create test table and data
CREATE TEMPORARY TABLE users (
  id SERIAL PRIMARY KEY,
  username TEXT NOT NULL,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Create an index (Normally this encourages sorted results)
CREATE INDEX idx_users_username ON users(username, created_at);

-- Insert 1000 rows
INSERT INTO users (username, created_at)
  SELECT 'user_' || g, CURRENT_TIMESTAMP + (g * interval '1 minute')
  FROM generate_series(1, 1000) AS g;

-- Update statistics
ANALYZE users;

-------------------------------------------------------
-- 2. Test Chaos Mode
-------------------------------------------------------

-- [WITHOUT ORDER BY]
-- Normally returns in PK order, but with this extension, 
-- it returns in RANDOM order.
SELECT * FROM users LIMIT 5;

-- You will see shuffled results
WARNING:  Chaos Mode: Injected 'ORDER BY random()' to the query!
 id  | username |         created_at         
-----+----------+----------------------------
 642 | user_642 | 2025-12-13 09:09:10.342555
 516 | user_516 | 2025-12-13 07:03:10.342555
 110 | user_110 | 2025-12-13 00:17:10.342555
 538 | user_538 | 2025-12-13 07:25:10.342555
 829 | user_829 | 2025-12-13 12:16:10.342555
(5 rows)

-- [WITH ORDER BY]
-- Returns correctly sorted results (Extension does nothing).
SELECT * FROM users ORDER BY id LIMIT 5;
-- You will see the results
 id | username |         created_at         
----+----------+----------------------------
  1 | user_1   | 2025-12-12 23:09:55.598759
  2 | user_2   | 2025-12-12 23:10:55.598759
  3 | user_3   | 2025-12-12 23:11:55.598759
  4 | user_4   | 2025-12-12 23:12:55.598759
  5 | user_5   | 2025-12-12 23:13:55.598759
(5 rows)

How it works

This extension uses planner_hook to inspect the Query Tree. If a SELECT statement has no ORDER BY clause (and no aggregation/GROUP BY), it injects a hidden ORDER BY random() clause into the query plan.

License

This project is licensed under the MIT License.

About

pg_chaos_shuffle is a PostgreSQL extension for Chaos Engineering that enforces random ordering on `SELECT` queries missing an `ORDER BY` clause.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages