drizzle-orm icon indicating copy to clipboard operation
drizzle-orm copied to clipboard

[FEATURE]: indexes on materialized views

Open AlexDaniel opened this issue 1 year ago β€’ 12 comments

Currently it is not possible to add indexes on materialized views.

The documentation explains quite well how to create indexes on tables. This is done by providing the third argument to pgTable call, like this:

export const user = pgTable("user", {
  id: serial("id").primaryKey(),
  name: text("name"),
  email: text("email"),
}, (table) => {
  return {
    nameIdx: index("name_idx").on(table.name),
    emailIdx: uniqueIndex("email_idx").on(table.email),
  };
});

However, pgMaterializedView does not accept a third argument at all, even though it is possible to add indexes on materialized views in postgresql. Please add a possibility to define indexes on materialized views. πŸ™πŸΌ

AlexDaniel avatar Sep 16 '24 14:09 AlexDaniel

Currently we are just keeping an eye on if Drizzle Kit migrations decides to drop and recreate the view, to add the indexes back manually.

May even add a GitHub action to check for us and flag it.

PostgreSQL requires a unique index to refresh the view concurrently.

dakdevs avatar Oct 31 '24 07:10 dakdevs

bump

DoseOfCode avatar Nov 02 '24 03:11 DoseOfCode

Any updates here?

Acorn221 avatar Nov 13 '24 07:11 Acorn221

This would be really helpful!

s3ththompson avatar Apr 03 '25 23:04 s3ththompson

would love to see this

spencerperley avatar May 31 '25 22:05 spencerperley

Any updates here? I’ve had to use pgMaterializedView with .existing() and create the view manually for now. So I can create the indexes.

CostaFernando avatar Jul 13 '25 00:07 CostaFernando

Is there any workaround for the moment?

pencilcheck avatar Jul 30 '25 08:07 pencilcheck

Hey everyone!

I've created this message to send in a batch to all opened issues we have, just because there are a lot of them and I want to update all of you with our current work, why issues are not responded to, and the amount of work that has been done by our team over ~8 months.

I saw a lot of issues with suggestions on how to fix something while we were not responding – so thanks everyone. Also, thanks to everyone patiently waiting for a response from us and continuing to use Drizzle!

We currently have 4 major branches with a lot of work done. Each branch was handled by different devs and teams to make sure we could make all the changes in parallel.


First branch is drizzle-kit rewrite

All of the work can be found on the alternation-engine branch. Here is a PR with the work done: https://github.com/drizzle-team/drizzle-orm/pull/4439

As you can see, it has 167k added lines of code and 67k removed, which means we've completely rewritten the drizzle-kit alternation engine, the way we handle diffs for each dialect, together with expanding our test suite from 600 tests to ~9k test units for all different types of actions you can do with kit. More importantly, we changed the migration folder structure and made commutative migrations, so you won't face complex conflicts on migrations when working in a team.

What's left here:

  • We are finishing handling defaults for Postgres, the last being geometry (yes, we fixed the srid issue here as well).
  • We are finishing commutative migrations for all dialects.
  • We are finishing up the command, so the migration flow will be as simple as drizzle-kit up for you.

Where it brings us:

  • We are getting drizzle-kit into a new good shape where we can call it [email protected]!

Timeline:

  • We need ~2 weeks to finish all of the above and send this branch to beta for testing.

Second big branch is a complex one with several HUGE updates

  • Bringing Relational Queries v2 finally live. We've done a lot of work here to actually make it faster than RQBv1 and much better from a DX point of view. But in implementing it, we had to make another big rewrite, so we completely rewrote the drizzle-orm type system, which made it much simpler and improved type performance by ~21.4x:
(types instantiations for 3300 lines production drizzle schema + 990 lines relations)

TS v5.8.3: 728.8k -> 34.1k
TS v5.9.2: 553.7k -> 25.4k

You can read more about it here.

What's left here:

Where it brings us:

  • We are getting drizzle-orm into a new good shape where we can call it [email protected]!

Breaking changes:

  • We will have them, but we will have open channels for everyone building on top of drizzle types, so we can guide you through all the changes.

Third branch is adding support for CockroachDB and MSSQL dialects

Support for them is already in the alternation-engine branch and will be available together with the drizzle-kit rewrite.

Summary

All of the work we are doing is crucial and should be done sooner rather than later. We've received a lot of feedback and worked really hard to find the best strategies and decisions for API, DX, architecture, etc., so we can confidently mark it as v1 and be sure we can improve it and remain flexible for all the features you are asking for, while becoming even better for everyone building on top of the drizzle API as well.

We didn't want to stay with some legacy decisions and solutions we had, and instead wanted to shape Drizzle in a way that will be best looking ahead to 2025–2026 trends (v1 will get proper effect support, etc.).

We believe that all of the effort we've put in will boost Drizzle and benefit everyone using it.

Thanks everyone, as we said, we are here to stay for a long time to build a great tool together!

Timelines

We are hoping to get v1 for drizzle in beta this fall and same timeline for latest. Right after that we can go through all of the issues and PRs and resond everyone. v1 for drizzle should close ~70% of all the bug tickets we have, so on beta release we will start marking them as closed!

AndriiSherman avatar Aug 30 '25 18:08 AndriiSherman

As a temporary solution, I use

export const exampleMV = pgMaterializedView(
    "example_materialized_view",
    {
        col: text()
    }
)
.as(sql`
    SELECT 'test' as col);
    COMMIT;
    CREATE UNIQUE INDEX idx_col ON example_materialized_view (col
`)

And the result will be the following SQL query

CREATE MATERIALIZED VIEW "public"."example_materialized_view" AS (
    SELECT 'test' as col);
    COMMIT;
    CREATE UNIQUE INDEX idx_col ON example_materialized_view (col
);

Deyvan avatar Oct 16 '25 06:10 Deyvan

TL;DRβ€”if you use Postgres, triggers + custom SQL migrations might help.

I found a certain escape hatch.

Say I have a materialized view some_mview with its definition in schema.ts (pgMaterializedView + queryBuilder used in my case).

In my case, the problem was that I couldn't execute the materialized with CONCURRENTLY on some_mview before:

db=> REFRESH MATERIALIZED VIEW CONCURRENTLY some_mview;
ERROR:  cannot refresh materialized view "public.some_mview" concurrently
HINT:  Create a unique index with no WHERE clause on one or more columns of the materialized view.

Therefore, while already having the materialized view migration applied, I created another one with drizzle-kit generate --custom ...:

CREATE OR REPLACE FUNCTION fire_trigger_create_idx_for_some_mview()
RETURNS event_trigger
LANGUAGE plpgsql
AS $$
DECLARE
    obj record;
BEGIN
    FOR obj IN
        SELECT *
        FROM pg_event_trigger_ddl_commands()
        WHERE object_type = 'materialized view'
    LOOP
        IF obj.object_identity = 'public.some_mview' THEN
            RAISE NOTICE 'Recreating index for materialized view %', obj.object_identity;
            EXECUTE 'CREATE UNIQUE INDEX IF NOT EXISTS idx_some_mview_id ON public.some_mview (id);';
        END IF;
    END LOOP;
END;
$$;

DROP EVENT TRIGGER IF EXISTS trigger_create_idx_for_some_mview;
CREATE EVENT TRIGGER trigger_create_idx_for_some_mview
ON ddl_command_end
WHEN TAG IN ('CREATE MATERIALIZED VIEW')
EXECUTE FUNCTION fire_trigger_create_idx_for_some_mview();

β€”and then, upon regenerating the migration for some_view and applying it:

db=> REFRESH MATERIALIZED VIEW CONCURRENTLY some_mview;
REFRESH MATERIALIZED VIEW

Yeah, not the cleanest solution, but at least it worked for my problems, that is (1) being able to refresh the view concurrently, and (2) having at least a minimum degree of reliability when it comes to the drizzle-kit migration workflow (and I can switch to a dedicated solution when it is ready).

Without the trigger (using only a custom migration that adds an index), schema changes related to some_mview would wipe off the index (in my case drizzle-kit generates DROP ... VIEW ... + CREATE ... VIEW ... statements). With the trigger, index recreation is ensured.

powd avatar Oct 22 '25 18:10 powd

This is extremely disappointing. Basically makes the support for materialized views useless, no?

Will try @powd solution. But it only works, if your unique key (no compound primary key etc.) is id, right?

boredland avatar Nov 06 '25 08:11 boredland

Not completely useless, but it does mean that you have to manage indexes yourself.

On Thu, 6 Nov 2025, 10:40 Jonas Strassel, @.***> wrote:

boredland left a comment (drizzle-team/drizzle-orm#2976) https://github.com/drizzle-team/drizzle-orm/issues/2976#issuecomment-3495887898

This is extremely disappointing. Basically makes the support for materialized views useless, no?

Will try @powd https://github.com/powd solution. But it only works, if your unique key (no compound primary key etc.) is id, right?

β€” Reply to this email directly, view it on GitHub https://github.com/drizzle-team/drizzle-orm/issues/2976#issuecomment-3495887898, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABKATL5ABWVF3O53KE46TTL33MCQVAVCNFSM6AAAAABOJM3LLWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZTIOJVHA4DOOBZHA . You are receiving this because you authored the thread.Message ID: @.***>

AlexDaniel avatar Nov 06 '25 08:11 AlexDaniel

I added a post-migration hook as a workaround. Yet pretty akward lol

MichielMAnalytics avatar Dec 02 '25 11:12 MichielMAnalytics