Incident brief
ORDER BY position out of range
ORDER BY and GROUP BY can reference output columns by position, but the position must be within the select list; a position beyond the number of selected columns raises this error.
What lands in your log
ERROR: ORDER BY position 5 is not in select list
In 10 seconds
- What triggers it
- Write ORDER BY <n> where <n> is larger than the number of columns in the select list.
- Fix
- Use a position within the select list, or order by the column name or expression directly.
- Proof
- Reproduced on PostgreSQL 18.4 → A single query reproduces SQLSTATE 42P10; the out-of-range ORDER BY position is rejected and the LINE points at it.
Fix
What to do right now
Application-level steps for this error.
- Use a position within the select list, or order by the column name or expression directly.
- Recount the select list after edits so positional references stay valid.
-- Reference a position that exists in the select list.
SELECT label FROM ref_demo ORDER BY 1;For this error
See this error live on the server
Run these against the affected instance to confirm the diagnosis before you act.
ORDER BY position out of range is fixed in the statement text. PostgreSQL stores no failed parse tree, so use the error POSITION against the exact SQL sent by the driver and inspect real column/function names.
Candidate columns on the referenced relations
Replace the relation array with the tables/aliases in the failing statement.
SELECT attrelid::regclass AS table_name, attnum, attname,
format_type(atttypid, atttypmod) AS data_type
FROM pg_attribute
WHERE attrelid = ANY (ARRAY[
to_regclass('<first_relation>'),
to_regclass('<second_relation>')
]::regclass[])
AND attnum > 0 AND NOT attisdropped
ORDER BY attname, table_name;Why it happens
What PostgreSQL is telling you
The mechanism behind the error, grounded in the official manual, not paraphrased.
PostgreSQL 18 Documentation, Appendix A. PostgreSQL Error Codes (Table A.1, Class 42, Syntax Error or Access Rule Violation)
42P10 → invalid_column_referenceRead the full section on postgresql.org →
Ordering by a non-existent position
ORDER BY 5 refers to the fifth output column, but the select list is shorter, so PostgreSQL raises 'ORDER BY position 5 is not in select list'.The session continues normally
The ORDER BY position check failed at parse time, outside any transaction, so session B's next statement runs unaffected.Reproduce & verify
A real, single-session PostgreSQL reproduction
A literal transcript of SQL run against a live PostgreSQL instance in an isolated lab. The commands below are exactly what was executed.
- 1Write ORDER BY <n> where <n> is larger than the number of columns in the select list.
- 2PostgreSQL maps the positional reference to an output column.
- 3Because no such column exists, the statement aborts with SQLSTATE 42P10.
A query ordered by a positional index that drifted out of range after the select list was trimmed.
CREATE TABLE ref_demo(label text);
INSERT INTO ref_demo VALUES ('a');SELECT label FROM ref_demo ORDER BY 5;SELECT 'ok' AS session_after_error;What PostgreSQL actually returned
CREATE TABLE
INSERT 0 1ERROR: ORDER BY position 5 is not in select list
LINE 1: SELECT label FROM ref_demo ORDER BY 5;
^ session_after_error
---------------------
ok
(1 row)The same query runs once the position exists in the select list.
Without this
Before: an out-of-range position aborts
With this, tested
After: ORDER BY 1 sorts by the first column
- A second operational test: exact SQL, raw output, measured result, and engineer notes
Card required. Cancel before day 7 and you are not charged.
Connected
Everything this error touches
Every page this SQLSTATE connects to: the concept that explains it, the runbooks that fix it, the parameters you tune to prevent it, and the sibling errors it travels with. All real cross-references. Jump straight in, or open the full interactive map.
Verification
- Last verified
- 2026-07-24 (isolated lab, PostgreSQL 18.4)
- Verification scope
- Verified against PostgreSQL 18.4 in an isolated lab environment
- Audit status
- reviewed
Went further?
Pro unlocks the second lab proof
Free page stops the bleeding. Pro adds the operational test, SQLSTATE audit, and deeper evidence, same error, more certainty.