Quick revision
The short answer
For the jobs table we create a partial index, CREATE INDEX jobs_pending_idx ON jobs (created_at) WHERE status = 'PENDING', and the worker query must repeat that predicate: WHERE status = 'PENDING' ORDER BY created_at LIMIT 10. For logins we create an expression index, CREATE UNIQUE INDEX users_email_lower_idx ON users (lower(email)), and query with the same expression: WHERE lower(email) = lower(?).
What to remember
In this answer, we wrote two specialised PostgreSQL indexes. A partial index on created_at WHERE status = 'PENDING' stays tiny and ordered for the worker poll, as long as the query repeats the same predicate, ideally as a literal. A unique expression index on lower(email) gives case-insensitive, unique logins, as long as the query uses lower(email) too. We also saw citext and nondeterministic collations as alternatives, and that MySQL 8 has functional key parts and generated columns but no partial indexes.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
For the jobs table we create a partial index, CREATE INDEX jobs_pending_idx ON jobs (created_at) WHERE status = 'PENDING', and the worker query must repeat that predicate: WHERE status = 'PENDING' ORDER BY created_at LIMIT 10. For logins we create an expression index, CREATE UNIQUE INDEX users_email_lower_idx ON users (lower(email)), and query with the same expression: WHERE lower(email) = lower(?).
Why this question is asked
These two indexes solve problems we meet in almost every application: a queue table where only a tiny fraction of rows matter, and an email lookup that must ignore case. Interviewers like this coding question because a normal index technically works for both but is either wasteful or ignored, and because the rules for when the planner may use a partial or expression index catch many developers. In this answer, we will recap the two index kinds, write the jobs index and its query, write the email index and its query, look at the alternatives, and compare with MySQL.
1A Quick Recap of Partial and Expression Indexes
A partial index is an index with a WHERE clause: it contains entries only for the rows that match the predicate. An expression index, also called a functional index, stores the result of an expression, such as lower(email), instead of the raw column value. Both are ordinary B-tree indexes in PostgreSQL; they only change what goes into the tree.
The planner, the part of PostgreSQL that chooses how to run a query, uses them only when it can prove they apply. For a partial index, the query's WHERE clause must imply the index predicate. For an expression index, the query must contain the same expression. That proof is done on the text of the query, so small differences matter.
2Need 1: A Partial Index for Pending Jobs
In the jobs table, 99 percent of rows are DONE and workers only ever look for PENDING ones, oldest first. A full index on (status, created_at) would hold an entry for every row, mostly DONE rows that are never searched. The partial index keeps only the pending rows, sorted by created_at.
Notice what makes this fast. With 10 million rows and 1 percent pending, the index holds about 100,000 entries instead of 10 million, so it is a few megabytes and stays in memory. It is already in created_at order, so LIMIT 10 reads ten entries and stops. And it changes only when a pending row is inserted or leaves the pending state; updates to DONE rows never touch it. FOR UPDATE SKIP LOCKED lets several workers take different jobs without waiting for each other.
But there is a catch: the query must say status = 'PENDING'. A query for WHERE status IN ('PENDING', 'RETRY') does not imply the predicate, so the planner cannot use this index and falls back to something slower.
3Need 2: Case-Insensitive Email Lookup
People type Alice@Example.com at sign-up and alice@example.com at login. A plain index on email cannot find the first value when we search for the second. We index the lowercased value instead, and make it unique so two accounts cannot differ only by case.
| Query | Index used? |
|---|---|
| WHERE status = 'PENDING' ORDER BY created_at | yes, jobs_pending_idx |
| WHERE status IN ('PENDING','RETRY') | no, predicate not implied |
| WHERE status = ? (generic plan) | no, value unknown at plan time |
WHERE lower(email) = lower(?) | yes, users_email_lower_idx |
WHERE upper(email) = upper(?) | no, different expression |
The query must use lower(email) on the column side, exactly as the index does. Writing upper(email), ILIKE, or comparing the raw column all produce expressions the planner cannot match to the index. In Java we also lowercase the parameter, or use lower(?) in the SQL as shown, so both sides agree.
4Alternatives for Case-Insensitive Email
PostgreSQL offers two other ways. The citext extension provides a case-insensitive text type, so a plain unique index on a citext column and a plain email = ? query just work. A nondeterministic collation, created with CREATE COLLATION ... (provider = icu, deterministic = false), makes comparisons ignore case for an ordinary text column. Collations are the modern, more general option, but support for pattern matching such as LIKE on nondeterministic collations arrived only in recent releases, so check your version before relying on it.
5What About MySQL?
MySQL 8 supports functional key parts, written with an extra pair of parentheses, so INDEX ((lower(email))) works for need 2. MySQL has no partial indexes, though. The older workaround for both needs is a generated column: a column computed from others, which we can index like any column. For the jobs case, many MySQL users index (status, created_at) and accept the larger index. Note also that MySQL's default collations are already case-insensitive, so a plain unique index on email often behaves case-insensitively there.
6Follow-up questions
What happens if the application binds the status as a parameter instead of a literal?
With WHERE status = ?, the planner has to prove that the index predicate status = 'PENDING' holds, but at planning time it may not know the value. For the first executions of a prepared statement, PostgreSQL builds a custom plan with the actual value and can use the partial index. After about five executions it may switch to a generic plan that must work for any value, and a generic plan cannot use the partial index, so the query suddenly becomes slow. The PostgreSQL JDBC driver also starts using server-side prepared statements after a few executions. The fix is to write the literal 'PENDING' in the SQL, since it never changes, or set plan_cache_mode to force_custom_plan for that query's session.
Why not just add an index on status?
Because status has only a few distinct values, and an index on it alone does not give the created_at order the worker needs. The planner would either ignore it for DONE or fetch all pending rows and sort them. The partial index on created_at solves both: it is small and already in the right order.
7Conclusion
In this answer, we wrote two specialised PostgreSQL indexes. A partial index on created_at WHERE status = 'PENDING' stays tiny and ordered for the worker poll, as long as the query repeats the same predicate, ideally as a literal. A unique expression index on lower(email) gives case-insensitive, unique logins, as long as the query uses lower(email) too. We also saw citext and nondeterministic collations as alternatives, and that MySQL 8 has functional key parts and generated columns but no partial indexes.
Open with
For the jobs table I would create a partial index: CREATE INDEX jobs_pending_idx ON jobs (created_at) WHERE status equals PENDING. Workers poll with WHERE status equals PENDING ORDER BY created_at LIMIT 10, usually with FOR UPDATE SKIP LOCKED so several workers do not collide.
Then, if they want more
Because only about one percent of rows are pending, the index is tiny, it stays in memory, it is already sorted by created_at so the limit stops after ten entries, and it only changes when a job enters or leaves the pending state.
The important rule is that the query must imply the index predicate, so I would write the literal PENDING in the SQL rather than binding it, because a generic prepared plan cannot prove the predicate. For email, I would create a unique index on lower of email, and query with WHERE lower of email equals lower of the parameter.
The expression has to match exactly; upper or ILIKE will not use it. Alternatives in PostgreSQL are the citext type or a case-insensitive nondeterministic collation, where I would check the version for LIKE support.
In MySQL 8 I can use a functional key part with double parentheses, but there are no partial indexes, and the older trick is an indexed generated column.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- MySQL 8
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- PostgreSQL documentation, MySQL reference manual
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Write PostgreSQL indexes for these two needs and show a query that uses each: (1) a jobs...": https://scrivio-interview-library.netlify.app/databases/indexing/0ae2a7827d/