← Back to PostgreSQL, page 4

postgresql-0080 · Hard senior

Why does EXPLAIN report Subplans Removed for WHERE day >= current_date - 7, and how does that differ from plan-time pruning?

Plan-time pruning needs constants. current_date and now() are STABLE, not IMMUTABLE, so the planner keeps every partition the constant parts allow and PostgreSQL prunes the rest at executor start-up, reported as Subplans Removed. Generic prepared plans rely on the same start-up pruning.

Full answer

Sample schema and data:

sql
CREATE TABLE metrics (day date, value int) PARTITION BY RANGE (day);
CREATE TABLE metrics_old PARTITION OF metrics FOR VALUES FROM ('2000-01-01') TO ('2026-01-01');
CREATE TABLE metrics_2026 PARTITION OF metrics FOR VALUES FROM ('2026-01-01') TO ('2027-01-01');
CREATE TABLE metrics_future PARTITION OF metrics FOR VALUES FROM ('2027-01-01') TO ('2100-01-01');
INSERT INTO metrics VALUES ('2025-06-01', 1), ('2026-11-30', 2), ('2027-03-01', 3);
sql
EXPLAIN (COSTS OFF) SELECT count(*) FROM metrics WHERE day >= date '2026-11-27';

Result:

text
Aggregate
  ->  Append
        ->  Seq Scan on metrics_2026 metrics_1
              Filter: (day >= '2026-11-27'::date)
        ->  Seq Scan on metrics_future metrics_2
              Filter: (day >= '2026-11-27'::date)
sql
EXPLAIN (COSTS OFF) SELECT count(*) FROM metrics WHERE day >= current_date - 7 AND day < date '2027-01-01';

Output:

text
Aggregate
  ->  Append
        Subplans Removed: 1
        ->  Seq Scan on metrics_2026 metrics_1
              Filter: ((day < '2027-01-01'::date) AND (day >= (CURRENT_DATE - 7)))
sql
EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF) SELECT count(*) FROM metrics WHERE day >= current_date - 7 AND day < date '2027-01-01';

PostgreSQL returns:

text
Aggregate (actual rows=1 loops=1)
  ->  Append (actual rows=1 loops=1)
        Subplans Removed: 1
        ->  Seq Scan on metrics_2026 metrics_1 (actual rows=1 loops=1)
              Filter: ((day < '2027-01-01'::date) AND (day >= (CURRENT_DATE - 7)))

With a constant date, the plan lists only the 2026 and future partitions. With current_date, the planner can use only the constant upper bound, so the plan keeps the old and 2026 partitions; the old one is then removed at executor start-up, shown as 'Subplans Removed: 1'. Plain EXPLAIN shows this too because it initialises the executor, and EXPLAIN ANALYZE confirms that only metrics_2026 was scanned. The output depends on the box's date only through which partition holds current_date - 7; this was run on 4 December 2026. Run-time pruning makes such queries efficient, but planning still considers every partition, so very high partition counts still cost planning time.

sql
CREATE TABLE metrics (day date, value int) PARTITION BY RANGE (day);
CREATE TABLE metrics_old PARTITION OF metrics FOR VALUES FROM ('2000-01-01') TO ('2026-01-01');
CREATE TABLE metrics_2026 PARTITION OF metrics FOR VALUES FROM ('2026-01-01') TO ('2027-01-01');
CREATE TABLE metrics_future PARTITION OF metrics FOR VALUES FROM ('2027-01-01') TO ('2100-01-01');
INSERT INTO metrics VALUES ('2025-06-01', 1), ('2026-11-30', 2), ('2027-03-01', 3);

EXPLAIN (COSTS OFF) SELECT count(*) FROM metrics WHERE day >= date '2026-11-27';

EXPLAIN (COSTS OFF) SELECT count(*) FROM metrics WHERE day >= current_date - 7 AND day < date '2027-01-01';

EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF) SELECT count(*) FROM metrics WHERE day >= current_date - 7 AND day < date '2027-01-01';

Run on PostgreSQL 17.12 (Debian 17.12-0+deb13u1) in a fresh schema, twice, with identical output.

Key points

  • Plan-time pruning needs constants
  • STABLE functions prune at executor start
  • EXPLAIN ANALYZE shows Subplans Removed
  • Generic plans rely on run-time pruning
  • Many partitions still cost planning time

Common mistakes

  • Concluding that pruning failed from plain EXPLAIN
  • Thousands of partitions on latency-sensitive tables

Follow-ups

  • How does enable_partition_pruning affect this?
  • Can pruning happen during a join?

Tips

  • Contrast the two EXPLAIN forms