Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

ClickHouse 13 min read Deep dive mediumAdept

Hints and hazards: pg_plan_advice in PG19

Postgres 19 adds two contrib modules for query hints: pg_plan_advice, which applies advice per session via SET, and pg_stash_advice, which attaches advice to specific query identifiers. Advice works by excluding plan options rather than forcing them, so unusable hints are ignored and harmful ones are accepted without checks. Benchmarks on artificial workloads show the effect: a BITMAP_HEAP_SCAN hint cut a clustered ORDER BY ... LIMIT query from 1.46s to under 10ms, and a JOIN_ORDER hint fixed a correlated-filter misestimate for a roughly 200x speedup. In production, use SET LOCAL inside a transaction, and check the new EXPLAIN PLAN_ADVICE output to confirm advice was matched.

Assumes: Postgres EXPLAIN plansquery planner statisticsindex scan typesjoin methods

Related posts

  • Postgres week in the Netherlands: PGDay Lowlands & Percona Live 2026

    ClickHouse easyNovice

    Notes from PGDay Lowlands in Utrecht and Percona Live in Amsterdam, covering two ClickHouse-maintained Postgres extensions and several PostgreSQL 19 features. pg_clickhouse is a foreign data wrapper that lets unmodified Postgres queries and ORMs run against ClickHouse via pushdown, with 15 of 22 TPC-H queries fully pushed down. pg_stat_ch streams per-query execution events through a shared-memory ring buffer to ClickHouse without back-pressure. Also covered: the new PostgreSQL 19 plan-stabilization modules pg_plan_advice and pg_stash_advice, and PostgreSQL 19 monitoring changes including WAIT FOR, pg_stat_lock, wal_fpi_bytes, and log_lock_waits on by default.

  • Easier Postgres fine-tuning with online_advisor

    Neon mediumAdept

    online_advisor is a Postgres extension that hooks into the executor (like auto_explain) to watch the live workload and recommend indexes, extended statistics, or prepared statements without applying anything automatically. It suggests indexes when a predicate filters more than 1,000 rows (filtered_threshold), multivariate statistics when actual rows exceed estimates by 10x (misestimation_threshold), and prepared statements when planning time exceeds execution time (prepare_threshold 1.0). Recommendations are queried from proposed_indexes and proposed_statistics views with call counts and elapsed time, and the reader then creates the object and runs VACUUM ANALYZE. It supports Postgres 14–17, though Neon offers it only on 17.

  • PostgreSQL Stories: From slow query to fast—via stats

    Render mediumAdept

    A simple LIMIT 1 join between a ~100k-row postgres_dbs table and a ~90M-row events table ran for hours. The cause was a merge join that scanned ~79M non-matching events sorted ahead of the 'dpg-' serviceId prefix. Adding manual lower and upper bounds on the indexed JSON expression brought the query down to milliseconds, but the real fault was that PostgreSQL only kept statistics on whole JSON objects, not on data->>'serviceId'. Running CREATE STATISTICS on that expression gave the planner accurate distributions, and the original query then finished in under a millisecond without rewrites.

  • Postgres 19 is out: try it on Neon

    Neon mediumAdept

    Postgres 19 is an operations-focused release that adds REPACK (CONCURRENTLY) for online table rebuilding and clustering via logical decoding, pg_plan_advice/pg_stash_advice for stabilizing bad plans by query ID, parallel autovacuum with priority scoring, WAIT FOR LSN for read-your-writes on replicas, and logical replication sequence sync. REPACK CONCURRENTLY needs a replication slot, temporary disk for a full copy, and fails if more than ~104M tuples change during the run, and it is not MVCC-safe for older snapshots. Other additions include postgres_fdw statistics import, ON CONFLICT DO SELECT, JSON output for COPY TO, and new stats views. SQL/PGQ, FOR PORTION OF, partition merge/split and GROUP BY ALL were reverted before GA.

  • Introducing Postgres Query Insights in ClickHouse Cloud

    ClickHouse mediumAdept

    Postgres Query Insights, now in preview for ClickHouse Cloud Managed Postgres, ranks every normalized query pattern by impact and aggregates per-execution counters (CPU, cache vs disk reads, temp spills, unlaunched parallel workers, WAL) to explain why a pattern is slow. A worked example traces a p99 regression to a sort spilling to disk, confirmed with EXPLAIN (ANALYZE, BUFFERS) and fixed with an index or work_mem tuning. Telemetry comes from pg_stat_ch, an Apache 2.0 extension that normalizes literals at parse-analyze time so values and PII never leave the database. It enqueues raw per-statement events via a non-blocking try-lock on a shared-memory ring buffer at about 3% overhead, dropping events under pressure rather than back-pressuring Postgres.

The quest for software engineering knowledge.

Look:
Layout: