Given a table orders with columns id, status, and amount, a partial index is created as:
CREATE INDEX idx_pending_orders ON orders(amount) WHERE status = 'pending';
What will be the result of this query?
EXPLAIN SELECT * FROM orders WHERE status = 'pending' AND amount > 100;
Partial indexes only include rows matching the WHERE condition.
The partial index idx_pending_orders includes only rows where status = 'pending'. The query filters on the same condition, so PostgreSQL can use the index to efficiently find rows with amount > 100 among pending orders.
Which of the following is the correct syntax to create a partial index on the users table for rows where active = true?
Partial indexes use the WHERE clause after the index definition.
The correct syntax uses CREATE INDEX followed by the index name, table, columns, and a WHERE clause specifying the condition. There is no CREATE PARTIAL INDEX command.
You have a transactions table with millions of rows. Most rows have status = 'completed', but you often query for status = 'failed' transactions. Which partial index would best optimize queries filtering WHERE status = 'failed'?
Partial indexes should match the query filter condition to be effective.
Since queries filter for status = 'failed', creating a partial index on only those rows improves performance by indexing fewer rows and speeding up lookups.
A partial index was created as:
CREATE INDEX idx_active_users ON users(id) WHERE active = true;
But the query below does not use the index:
SELECT * FROM users WHERE active = false AND id = 123;
Why?
Partial indexes only include rows matching their WHERE condition.
The partial index only includes rows where active = true. The query filters for active = false, so the index cannot be used.
Consider a table employees with a partial unique index:
CREATE UNIQUE INDEX idx_unique_email_active ON employees(email) WHERE active = true;
What is the effect of this index on enforcing uniqueness of email values?
Partial unique indexes apply uniqueness only to rows matching the WHERE condition.
The unique constraint applies only to rows where active = true. Rows where active = false can have duplicate email values without violating the index.