All rules
Style
postgresql/
Flag `CASE WHEN x IS NULL THEN y ELSE x END` and recommend `COALESCE(x, y)`.
Why this matters
`COALESCE` is shorter, evaluates `x` once, and is the form every PostgreSQL planner optimizes directly. Also catches the mirrored `IS NOT NULL` form. Multi-arm CASEs that go beyond a single null-fallback are not flagged.
Examples
Incorrect
SELECT CASE WHEN nickname IS NULL THEN full_name ELSE nickname END FROM users;SELECT CASE WHEN nickname IS NOT NULL THEN nickname ELSE full_name END FROM users;Correct
SELECT COALESCE(nickname, full_name) FROM users;Configure it
// eslint.config.js
import postgresql from "eslint-plugin-postgresql";
export default [
{
files: ["**/*.sql"],
languageOptions: {
parser: postgresql.configs.recommended.languageOptions.parser,
},
plugins: { postgresql },
rules: {
"postgresql/prefer-coalesce-over-case": "warn",
},
},
]; Options
Edit the SQL — only prefer-coalesce-over-case is enabled.
Pre-filled with the first incorrect example. Toggle off in the rule shelf to see how the diagnostic disappears.
Diagnostics
No issues found.
2 rules enabled.
Rule under test
prefer-coalesce-over-case — plus no-syntax-error as a safety net.