Skip to content

SCHEMA003 fires on PRIMARY KEY columns (false positive) #146

Open

Description

@RAprogramm

Problem

SCHEMA003 ("ORDER BY column could benefit from index") fires on a column that is the table's PRIMARY KEY. The primary key already provides a B-tree index, and Postgres satisfies ORDER BY id DESC with a backward index scan, so the suggestion CREATE INDEX idx_id ON table(id) is redundant.

Reproduction

schema.sql:

CREATE TABLE campaigns (
    id SERIAL PRIMARY KEY,
    name VARCHAR(255) NOT NULL
);

queries.sql:

SELECT id, name FROM campaigns ORDER BY id DESC LIMIT 100;

Actual: [INFO] SCHEMA003: ORDER BY column 'id' could benefit from index
Expected: no finding — id is the primary key.

Suspected cause

The schema parser appears to collect only explicit CREATE INDEX statements as indexed columns and does not register PRIMARY KEY (and likely UNIQUE) constraints as indexes.

Suggested fix

Register PRIMARY KEY and UNIQUE columns as indexed (single-column) when building the schema index map, and skip SCHEMA003/SCHEMA001/SCHEMA004 findings for columns already covered by a unique/PK index prefix.

Activity

  1. added
    bug
    on Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions