Enable Seqscan Off For Plan Debugging

Set enable_seqscan = off so the planner prefers index paths, compare the plans, then RESET; it is session-level debugging, not a production setting.

SqlData Fetching

Code

snippet
-- 小表或低选择性时计划器可能选顺序扫描,让"索引到底有没有被用上"
-- 难以观察。SET enable_seqscan = off 把顺序扫描的代价抬高,强制计划器
-- 改走索引路径,用来对比验证计划差异;它只是会话级调试手段,不是生产开关
SET enable_seqscan = off;

EXPLAIN (COSTS OFF)
SELECT *
FROM users
WHERE email = '[email protected]';

-- 恢复默认,只影响当前会话
RESET enable_seqscan;

-- 生产排查用 EXPLAIN ANALYZE:真实执行并给出实际行数与耗时
EXPLAIN ANALYZE
SELECT id
FROM users
WHERE email = '[email protected]';

What the code does

Several statements share this file — SELECT / EXPLAIN, with users among the objects involved.

What the statements actually assemble: a WHERE clause narrowing which rows one statement affects.

Using it

Run the statement against the users table.

Key statements

EXPLAIN (COSTS OFF)
SELECT *
FROM users
WHERE email = '[email protected]';
EXPLAIN ANALYZE
SELECT id
FROM users
WHERE email = '[email protected]';
SET enable_seqscan = off;

Interactive Tools for This Topic

FAQ

What relations does the SQL touch?
`users` — referenced directly, no join needed.