All rules

Performance

postgresql/require-index-on-fk-column

Require an index on every foreign-key column.

  • Type problem
  • Recommended warn
  • Fixable no

Why this matters

An unindexed foreign-key column makes parent-side `DELETE` / `UPDATE` perform a sequential scan of the child table to enforce the constraint. The check is cross-statement: it scans `CREATE TABLE`, `CREATE INDEX`, and `ALTER TABLE ADD CONSTRAINT` in the same file, and only requires an index whose leading column is the FK column. The PRIMARY KEY index already counts.

Examples

Incorrect

Incorrect
CREATE TABLE t (
  id integer PRIMARY KEY,
  fid integer REFERENCES other(id)
);
-- No CREATE INDEX on t.fid in this file.

Correct

Correct
CREATE TABLE t (
  id integer PRIMARY KEY,
  fid integer REFERENCES other(id)
);
CREATE INDEX idx_t_fid ON t (fid);

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/require-index-on-fk-column": "warn",
    },
  },
];

Options

This rule has no options.

Try this rule

Edit the SQL — only require-index-on-fk-column is enabled.

Pre-filled with the first incorrect example. Toggle off in the rule shelf to see how the diagnostic disappears.

0 errors 0 warnings parse 0ms · rules 0ms
Diagnostics

No issues found.

2 rules enabled.

Rule under test require-index-on-fk-column — plus no-syntax-error as a safety net.
eslint-plugin-postgresql

An ESLint plugin that lints .sql files with real PostgreSQL grammar and a curated set of best-practice rules.

© 2026 eslint-plugin-postgresql contributors Built on libpg-query · PostgreSQL 17