Make MySQL `ALTER TABLE` Fail on Unsupported Online DDL
When you change a MySQL table definition without an ALGORITHM clause, MySQL picks the execution method for you. If its first choice…
When you query a PostgreSQL users table, lower() converts text to lowercase so you can match an email regardless of letter case by comparing lower(email) with a lowercase search value. An expression index stores the result of a calculation on a column instead of the raw column value. When you create an index on lower(email), PostgreSQL stores those lowercase results and can use them to locate matching rows. You can create a non-unique expression index to speed up lookups, or you can create a unique expression index when you want uppercase and lowercase versions of an email to count as the same value.
A matching index gives PostgreSQL another way to run the query, though PostgreSQL still decides whether to use it. The query planner compares the cost of available plans and picks how PostgreSQL reads the rows. You also pay for that index in disk space, and PostgreSQL does extra work whenever rows change.
Start by creating a non-unique expression index on the same calculation your lookup uses:
CREATE INDEX users_email_lower_idx
ON users (lower(email));
Then write the WHERE condition with that same transformation:
SELECT id, email
FROM users
WHERE lower(email) = lower($1);
In a prepared query, $1 stands in for the search value you pass at execution time. Because the query calls lower() on both sides, PostgreSQL ignores letter case during the comparison. If you already pass a lowercase value, you can compare lower(email) directly against it.
PostgreSQL’s expression-index documentation pairs an index on lower(column) with a condition on lower(column). A regular index on email stores the original mixed-case strings, so it cannot supply the lowercase values that a condition on lower(email) checks. Make sure you use the same column, transformation, and collation (the rules PostgreSQL uses to compare text) in both the index and the query. The documentation shows the index expression matching the query condition, though it does not require every SQL statement to have identical text.
An expression index makes an index lookup possible, but PostgreSQL still picks whichever plan it estimates will cost less. On a small table, or when a condition matches many rows, PostgreSQL can often read the table faster with a sequential scan (reading rows one after another) than by going through the index.
Run EXPLAIN to see which plan PostgreSQL chooses for your query:
EXPLAIN
SELECT id, email
FROM users
WHERE lower(email) = lower('Alex@example.com');
Look at the scan type in the output. An Index Scan means PostgreSQL uses the index to find and fetch matching rows. A Bitmap Index Scan means PostgreSQL gathers matching row locations from the index first before reading the table. A sequential scan means PostgreSQL reads the table directly without using the index. The PostgreSQL documentation on EXPLAIN shows how to read the cost estimates, so test with a table size and search values that reflect your workload. For more on how cost estimates steer that choice, see how PostgreSQL selects indexes.
A non-unique expression index speeds up lookups, but it still lets Alex@example.com and alex@example.com exist in the same table. If you want each lowercase email to appear only once, create a unique expression index instead:
CREATE UNIQUE INDEX users_email_lower_key
ON users (lower(email));
Before you create it, check whether your table already has duplicate lowercase emails:
SELECT lower(email) AS email_key, count(*)
FROM users
WHERE email IS NOT NULL
GROUP BY lower(email)
HAVING count(*) > 1;
Clean up any duplicates first, because PostgreSQL will reject the unique index if existing rows violate uniqueness. A standard UNIQUE (email) index only enforces uniqueness on the raw stored strings and does not apply the lower(email) comparison. If separate customers or account groups (tenants) need to use the same email address, you can include tenant_id in the unique index, such as ON users (tenant_id, lower(email)). Also keep nulls in mind: by default, PostgreSQL’s unique-index rules treat each NULL as distinct, so a nullable email column can hold multiple NULL values. If you add NULLS NOT DISTINCT, PostgreSQL treats nulls as equal for uniqueness.
lower() follows your database’s collation and locale (language and region) rules, so it does not cover every kind of Unicode case-insensitive matching. PostgreSQL 19 also documents casefold(text), which converts text for case-insensitive matching under Unicode rules. You can use casefold(text) only when the server uses UTF-8 encoding, and it can produce different results than lower(). Decide which comparison rules fit your data before you enforce uniqueness. The collation documentation and string-function documentation explain how these rules shape text conversion and case folding.
You also pay for the index during writes and in disk space. PostgreSQL recalculates the indexed expression for each inserted row and each non-HOT update (an update that requires new index entries), and it stores the index separately from the table. Because storage and performance costs depend on your data and workload, PostgreSQL does not give a fixed overhead for a lower(email) index, so measure them with representative traffic. For more background on index storage, see the related PostgreSQL discussion.
You also need to decide how to build the index when the table is taking writes. If you run a standard CREATE INDEX, PostgreSQL blocks writes to the table until it finishes building the index. If you run CREATE INDEX CONCURRENTLY instead, PostgreSQL lets other transactions keep writing, though it reads the table twice and waits for relevant transactions to finish, which requires more total work. The CREATE INDEX documentation covers both options.
Give Vroni a GitHub issue, bug report, spec, or rough idea. It reads the repo, plans the change, writes code, runs checks, and works toward a review-ready pull request.
Take a look at vroni.com