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 a row in orders refers to a row in customers through orders.customer_id, PostgreSQL uses a foreign key, a rule that requires the referenced customer to exist. PostgreSQL enforces that rule, but it does not automatically create an index (a data structure it uses to find rows quickly) on orders.customer_id.
If you add an index on orders.customer_id, PostgreSQL can find matching orders quickly when someone deletes a customer or changes that customer's key. PostgreSQL can also use that index to speed up queries that filter or join on customer_id. Whether you should add one depends on how large orders is, how often you delete or update rows in customers, and whether another index already covers those lookups.
PostgreSQL requires the referenced parent columns to have a primary key, a unique constraint, or a non-partial unique index. PostgreSQL uses that parent-side index to check whether a value in a child row matches a row in the parent table. That check works in the opposite direction from a lookup on the child table's foreign-key column.
As the current PostgreSQL constraint documentation explains, PostgreSQL leaves child-side foreign-key indexes to you because some workloads never need them, and other indexes can often handle the same lookups. For example, an index you already created for a common query might cover the foreign-key lookup as well.
CREATE TABLE customers (
id bigint PRIMARY KEY,
name text NOT NULL
);
CREATE TABLE orders (
id bigint PRIMARY KEY,
customer_id bigint REFERENCES customers(id),
placed_at timestamptz NOT NULL
);
-- Optional, depending on the workload:
CREATE INDEX orders_customer_id_idx ON orders (customer_id);
Here, PostgreSQL creates an index for the primary key on customers.id, but it does not create one for the foreign key on orders.customer_id.
When someone deletes a customer or changes that customer's id, PostgreSQL has to find every row in orders whose customer_id matches the old value. Without a supporting index on orders, PostgreSQL scans the referencing table to locate those rows. With an index, it can jump straight to the matching rows instead. The CREATE TABLE documentation highlights this benefit when referenced columns change often.
Once PostgreSQL finds the matching orders, the foreign key's action decides what happens next. Under the default NO ACTION, PostgreSQL rejects the delete if any orders still point to that customer. Under ON DELETE CASCADE, PostgreSQL deletes those orders too. Either way, an index helps PostgreSQL locate the affected rows faster, though it still has to check, delete, or update every row it finds.
PostgreSQL can also use that same index for queries that look up orders by customer_id, such as:
SELECT *
FROM orders
WHERE customer_id = 42;
If a foreign key spans multiple columns, check whether an existing index already covers the lookup PostgreSQL needs. A B-tree index supports queries that match every indexed column to an exact value, no matter what order the index lists those columns in. If a query filters on only some of the columns, though, the leading columns of the index matter most. PostgreSQL's multicolumn index guidance covers these rules in detail.
Every index takes up disk space and gives PostgreSQL extra work during writes. Whenever you insert, delete, or update rows in orders, PostgreSQL has to add, remove, or update the corresponding index entries. As the PostgreSQL index documentation notes, indexes speed up lookups at the cost of extra write overhead.
You get little benefit from a dedicated foreign-key index if the child table is small, you rarely delete or update parent keys, and your queries on the child table do not filter or join on that column. If another index already covers those lookups, adding a separate one only duplicates the work. PostgreSQL has no universal rule for table size or delete frequency, so you need to measure your own workload.
Before you add an index, inspect the indexes that already exist on the table, then compare query plans (the steps PostgreSQL chooses to run a query) and timings on representative data. Keep in mind that EXPLAIN ANALYZE runs the statement it measures. If you test a statement that modifies data, run it inside a transaction (a group of changes you can commit or roll back together) and roll the transaction back afterward to undo the changes. You can read more about this in PostgreSQL's EXPLAIN documentation and its guide to examining index usage. For a closer look at the write overhead foreign keys introduce, see how foreign keys affect PostgreSQL inserts.
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