Most PostgreSQL performance advice ends with "add an index". That advice is incomplete, because PostgreSQL has six index types, and picking the wrong one means the planner ignores it entirely.
B-tree covers most cases, and you rarely need to think about it. For the rest, GIN, partial, and covering indexes fit the query better: GIN searches inside a value, a partial index holds only the rows you query, and a covering index answers the query without visiting the table.
Here's how to recognize those cases, with the EF Core configuration to match.
The SQL in this article uses snake_case names from UseSnakeCaseNamingConvention() in the EFCore.NamingConventions package.
B-tree: The Default That Usually Wins
A B-tree index supports equality, range comparisons, ORDER BY, and uniqueness.
That's why it's the default, and why it handles almost everything you throw at it.
In EF Core, every HasIndex call produces a B-tree unless you say otherwise:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Order>(builder =>
{
builder.HasIndex(o => o.CustomerId);
builder.HasIndex(o => new { o.Status, o.CreatedAt });
});
}
In the composite index on (Status, CreatedAt), column order matters.
PostgreSQL can use it for WHERE status = 'Pending' and for WHERE status = 'Pending' AND created_at > ..., but not efficiently for a query that filters only on created_at.
PostgreSQL 18 added B-tree skip scan, which narrows that gap when the leading column has few distinct values (like a status), but earlier versions have to read the whole index or the table for that query.
The rule I use: equality columns first, range columns last.
Before adding anything more exotic, confirm the query is actually slow and see what the planner does with EXPLAIN ANALYZE. Most "missing index" problems end here, with a B-tree.
GIN: When One Row Holds Many Values
B-tree indexes one value per row.
GIN (Generalized Inverted Index) indexes many values per row, which is exactly what you need for jsonb, arrays, and full-text search.
Say you store order metadata as a JSON column:
public class Order
{
public Guid Id { get; set; }
public Guid CustomerId { get; set; }
public string Status { get; set; } = string.Empty;
public DateTime CreatedAt { get; set; }
public string Metadata { get; set; } = "{}";
}
modelBuilder.Entity<Order>(builder =>
{
builder.Property(o => o.Metadata).HasColumnType("jsonb");
builder.HasIndex(o => o.Metadata).HasMethod("gin");
});
HasMethod("gin") is an Npgsql provider extension, so make sure you have the provider package:
dotnet add package Npgsql.EntityFrameworkCore.PostgreSQL
With that index in place, containment queries stop scanning the table:
SELECT * FROM orders
WHERE metadata @> '{"source": "mobile"}';
A B-tree on a jsonb column is nearly useless for this query.
The planner can't use it because you're searching inside the document, not comparing the whole value.
The same logic applies to full-text search, where a GIN index over a tsvector turns seconds into milliseconds.
One caveat: GIN indexes are expensive to maintain on write-heavy tables. Each inserted row can produce dozens of index entries, so don't add one "just in case".
Partial Indexes: Index Only the Rows You Query
A partial index covers a subset of rows defined by a WHERE clause.
It's smaller, cheaper to maintain, and often faster than a full index.
The classic case is a work queue table where 99% of rows are Completed and you only ever query the Pending ones:
modelBuilder.Entity<OutboxMessage>(builder =>
{
builder
.HasIndex(m => m.OccurredOnUtc)
.HasFilter("processed_on_utc IS NULL");
});
That generates:
CREATE INDEX ix_outbox_messages_occurred_on_utc
ON outbox_messages (occurred_on_utc)
WHERE processed_on_utc IS NULL;
If the table holds 50 million processed messages and 200 pending ones, the index contains 200 entries. The full B-tree alternative would contain 50 million.
The scaling the outbox pattern benchmark uses a partial covering index with this same filter, and it cut the unprocessed-message query from about 70 ms to 1 ms.
Two things to remember:
- The query's
WHEREclause must match (or imply) the index predicate, or the planner won't use it. - Write the filter in raw SQL using the actual column names, not the C# property names.
Partial indexes also give you conditional uniqueness, which plain unique constraints can't express:
modelBuilder.Entity<User>(builder =>
{
builder
.HasIndex(u => u.Email)
.IsUnique()
.HasFilter("deleted_at IS NULL");
});
One active account per email, while soft-deleted rows keep their old address.
Covering Indexes: Skip the Table Entirely
Normally, PostgreSQL uses the index to find matching rows, then visits the table (the heap) to fetch the remaining columns.
A covering index stores extra columns in the index itself with INCLUDE, so the heap visit disappears.
This produces an index-only scan, and it's the tool for hot, narrow queries:
modelBuilder.Entity<Product>(builder =>
{
builder
.HasIndex(p => p.CategoryId)
.IncludeProperties(p => new { p.Name, p.Price });
});
CREATE INDEX ix_products_category_id
ON products (category_id)
INCLUDE (name, price);
Now this query can skip the table:
SELECT name, price FROM products WHERE category_id = 42;
The INCLUDE columns aren't part of the search key, so they don't bloat the tree structure or affect uniqueness.
The trade-off is a larger index and slightly slower writes, so reserve it for queries you've measured.
Index-only scans need reasonably fresh visibility maps, so tables with heavy churn benefit less until autovacuum catches up.
The Other Types, Briefly
You'll rarely need the remaining index types, but you should know they exist:
- GiST handles geometric data, ranges, and nearest-neighbor searches. PostGIS is built on it.
- BRIN stores min/max values per block range. It's tiny and works well for huge append-only tables queried by time, but only if the physical row order correlates with the column.
- Hash supports only equality. Since PostgreSQL 10 it's crash-safe, but a B-tree handles the same equality lookups and also supports ranges, sorting, and uniqueness, so it rarely pays to choose hash.
- SP-GiST covers partitioned search trees for specialized data like IP ranges.
If a table is so large that a BRIN index sounds attractive, also consider table partitioning, which solves the retention problem at the same time.
How Do You Verify an Index Gets Used?
An index that the planner ignores is pure write overhead.
After adding one, verify it with EXPLAIN ANALYZE:
EXPLAIN ANALYZE
SELECT * FROM orders
WHERE metadata @> '{"source": "mobile"}';
You want to see Bitmap Index Scan or Index Scan on your new index, not Seq Scan.
And check for dead weight periodically:
SELECT s.relname, s.indexrelname, s.idx_scan,
pg_size_pretty(pg_relation_size(s.indexrelid)) AS index_size
FROM pg_stat_user_indexes s
JOIN pg_index i ON i.indexrelid = s.indexrelid
WHERE NOT i.indisunique
ORDER BY s.idx_scan;
The query skips unique and primary-key indexes, because they enforce constraints even when no query scans them.
A non-unique index with idx_scan = 0 after weeks in production is a candidate for review.
Before you drop it, confirm the counters weren't reset recently (stats_reset in pg_stat_database) and check every replica, because each server counts only its own scans.
An index that no server uses still has to be updated on every insert.
Indexing pairs with the query-side techniques I cover in how I optimized an API endpoint to make it 15x faster.
Summary
Start with B-tree and stay there until you have evidence you need something else. The signals for the other types are specific:
- Searching inside a value (jsonb, arrays, full-text): GIN.
- Querying a small, stable subset of a large table (queues, soft delete, active flags): partial index.
- A hot query that reads few columns: covering index with
INCLUDE. - Huge append-only tables queried by time: BRIN, or better, partitioning.
Configure all of them through EF Core's HasIndex, HasMethod, HasFilter, and IncludeProperties so they're tracked in your migrations, not in someone's undocumented psql session.
Then prove each one with EXPLAIN ANALYZE, and drop the non-unique ones that no production server uses.
Frequently Asked Questions
What is the default index type in PostgreSQL?
B-tree is the default index type in PostgreSQL. It supports equality and range comparisons, sorting, and uniqueness constraints, which is why it handles the vast majority of workloads.
When should I use a GIN index instead of a B-tree index?
Use a GIN index when a single row contains multiple values you need to search inside, such as jsonb documents, arrays, or full-text search vectors. B-tree can only index the column value as a whole.
How do I create a PostgreSQL index with EF Core?
Use the HasIndex method in your model configuration. The Npgsql provider adds extensions like HasMethod for the index type, HasFilter for partial indexes, and IncludeProperties for covering indexes. The index is then created by a regular EF Core migration.
Do indexes slow down writes in PostgreSQL?
Yes. Every index on a table must be updated on INSERT and often on UPDATE, and GIN indexes are notably more expensive to maintain than B-tree. Only keep indexes that a real query uses.

