Sub topic 17
Testing and Inspecting Partition Behavior
Assume you know basic SQL, PostgreSQL table definitions, indexes, constraints, and the basics of query plans. Testing partition behavior means checking both where PostgreSQL stores rows and which partitions it scans.
Confirm the partition layout
Start by inspecting the hierarchy. pg_partition_tree reports the parent, each partition, its depth, and whether a relation is a leaf partition.
SELECT
relid::regclass AS relation_name,
parentrelid::regclass AS parent_name,
isleaf,
level
FROM pg_partition_tree('orders')
ORDER BY level, relation_name;This query helps detect missing partitions, unexpected nesting, or a default partition that receives more rows than expected.
You can also inspect partition bounds:
SELECT
child.relname AS partition_name,
pg_get_expr(child.relpartbound, child.oid) AS partition_bound
FROM pg_inherits AS inheritance
JOIN pg_class AS parent
ON parent.oid = inheritance.inhparent
JOIN pg_class AS child
ON child.oid = inheritance.inhrelid
WHERE parent.relname = 'orders'
ORDER BY child.relname;The pg_get_expr function converts PostgreSQL's internal bound expression into readable SQL.
Verify write routing
Rows inserted through the parent table are routed to a leaf partition. The system column tableoid identifies the physical table that stores each row.
INSERT INTO orders (order_id, customer_id, order_date, amount)
VALUES
(10, 42, DATE '2026-01-12', 25.00),
(11, 42, DATE '2026-02-03', 30.00);
SELECT
order_id,
order_date,
tableoid::regclass AS physical_partition
FROM orders
WHERE order_id IN (10, 11)
ORDER BY order_id;The two rows should appear in different monthly partitions. Repeat this test at every boundary. For a range defined as FROM ('2026-01-01') TO ('2026-02-01'), January 31 belongs to that partition, but February 1 does not.
Updates can also cause routing. If an update changes the partition key, PostgreSQL moves the row from one partition to another. Test this explicitly, especially when triggers, foreign keys, or application code depend on row movement.
UPDATE orders
SET order_date = DATE '2026-02-15'
WHERE order_id = 10;Verify partition pruning
Partition pruning lets PostgreSQL skip partitions that cannot contain matching rows. Use EXPLAIN with the same predicates your application sends.
EXPLAIN (COSTS OFF)
SELECT *
FROM orders
WHERE order_date >= DATE '2026-01-10'
AND order_date < DATE '2026-01-15';A plan that lists only the January partition demonstrates pruning. A plan that lists every partition may indicate a missing or indirect partition-key predicate, a parameterized query plan, or a planner limitation. Check the actual plan on your PostgreSQL version rather than relying on a fixed node name.
A repeatable inspection path
Test normal values, lower and upper boundaries, values outside the declared range, and values intended for a default partition. Include an update that crosses a boundary.
Exercise
Which expression identifies the physical partition containing a row, and which command checks whether a query skips partitions?
Show answer
Use tableoid::regclass to identify the physical partition. Use EXPLAIN, usually with COSTS OFF or ANALYZE, to inspect partition pruning.