GIN index: Generalised Inverted iNdex (Postgres) — for columns holding many values per row: arrays, JSONB, full-text. A GIN index, short for Generalised Inverted Index, is a PostgreSQL index for columns that hold many values per row. Arrays, JSONB documents and full-text search vectors are the typical cases. An inverted index maps each value to the rows that contain it, like the index at the back of a book. That makes queries such as "which documents contain this word" or "which products have this tag" fast, even across millions of rows.
GIN indexes are slower to update than normal B-tree indexes, so they suit data that is read often and written less. For JSONB, the jsonb_path_ops variant is smaller and faster when you only need containment queries. MySQL has no direct equivalent; it offers FULLTEXT indexes for text and multi-valued indexes for JSON arrays instead.
What does an example of GIN index look like?
CREATE INDEX idx_docs_body ON docs USING gin (to_tsvector('english', body));
Master SQL query optimization, indexing strategies, and EXPLAIN analysis to dramatically improve database performance and eliminate slow queries. Use when debugging slow queries, designing database schemas, or optimizing application performance.
Postgres best practices maintained by Supabase, for Postgres running anywhere. Load this skill BEFORE writing or changing anything that lives in a Postgres database: creating or altering tables and columns (including choosing column types), schema design, migrations and declarative schema files, RLS policies and the tests that verify them, indexes, triggers, database functions, queues and schedule