ENG-1433 The database views are too slow#783
Conversation
|
Updates to Preview Branch (eng-1433-the-database-views-are-too-slow) ↗︎
Tasks are run on every commit but only new migration files are pushed.
View logs for this Workflow Run ↗︎. |
There was a problem hiding this comment.
Devin Review found 1 new potential issue.
🐛 1 issue in files not directly in the diff
🐛 upsert_account_in_space references dropped column sa.editor (packages/database/supabase/schemas/account.sql:213-215)
The upsert_account_in_space function in the schema references sa.editor on the ON CONFLICT DO UPDATE path, but the editor column was dropped from SpaceAccess in migration 20260124215014_PartialSpaceAccess.sql (line 306: ALTER TABLE public."SpaceAccess" DROP COLUMN editor). This causes a runtime error whenever the function takes the conflict path (i.e., when an account already has a SpaceAccess entry for the given space).
This function is actively called by upsert_accounts_in_space, which is invoked from the Roam sync path in syncDgNodesToSupabase.ts:360.
Root Cause
In the migration 20260124215014_PartialSpaceAccess.sql, the function is created (line 13–55) with this SQL in the ON CONFLICT handler:
DO UPDATE SET permissions = CASE
WHEN COALESCE(local_account.space_editor, sa.editor, true) THEN 'editor'
ELSE 'reader' END;Later in the same migration (line 306), the column is dropped:
ALTER TABLE public."SpaceAccess" DROP COLUMN editor;Because plpgsql functions are validated lazily (at execution time, not creation time), the function creation succeeds. But at runtime, when PostgreSQL re-plans the statement after the column drop, it encounters sa.editor referencing a non-existent column and raises an error.
The schema file account.sql line 214 reflects this same broken state. sa.editor should be removed from the COALESCE expression entirely, since the editor boolean has been superseded by the permissions enum column.
Impact: Any call to upsert_account_in_space (or upsert_accounts_in_space) that hits the ON CONFLICT path will fail with a column-not-found error, breaking account syncing for existing users.
View 8 additional findings in Devin Review.
b609191 to
e1bde6c
Compare
There was a problem hiding this comment.
Devin Review found 1 new potential issue.
🐛 1 issue in files not directly in the diff
🐛 upsert_account_in_space references dropped column sa.editor (packages/database/supabase/schemas/account.sql:213-215)
The upsert_account_in_space function in the schema references sa.editor on the ON CONFLICT DO UPDATE path, but the editor column was dropped from SpaceAccess in migration 20260124215014_PartialSpaceAccess.sql (line 306: ALTER TABLE public."SpaceAccess" DROP COLUMN editor). This causes a runtime error whenever the function takes the conflict path (i.e., when an account already has a SpaceAccess entry for the given space).
This function is actively called by upsert_accounts_in_space, which is invoked from the Roam sync path in syncDgNodesToSupabase.ts:360.
Root Cause
In the migration 20260124215014_PartialSpaceAccess.sql, the function is created (line 13–55) with this SQL in the ON CONFLICT handler:
DO UPDATE SET permissions = CASE
WHEN COALESCE(local_account.space_editor, sa.editor, true) THEN 'editor'
ELSE 'reader' END;Later in the same migration (line 306), the column is dropped:
ALTER TABLE public."SpaceAccess" DROP COLUMN editor;Because plpgsql functions are validated lazily (at execution time, not creation time), the function creation succeeds. But at runtime, when PostgreSQL re-plans the statement after the column drop, it encounters sa.editor referencing a non-existent column and raises an error.
The schema file account.sql line 214 reflects this same broken state. sa.editor should be removed from the COALESCE expression entirely, since the editor boolean has been superseded by the permissions enum column.
Impact: Any call to upsert_account_in_space (or upsert_accounts_in_space) that hits the ON CONFLICT path will fail with a column-not-found error, breaking account syncing for existing users.
View 11 additional findings in Devin Review.
e1bde6c to
426b2c9
Compare
There was a problem hiding this comment.
Devin Review found 1 new potential issue.
🐛 1 issue in files not directly in the diff
🐛 upsert_account_in_space references dropped column sa.editor (packages/database/supabase/schemas/account.sql:213-215)
The upsert_account_in_space function in the schema references sa.editor on the ON CONFLICT DO UPDATE path, but the editor column was dropped from SpaceAccess in migration 20260124215014_PartialSpaceAccess.sql (line 306: ALTER TABLE public."SpaceAccess" DROP COLUMN editor). This causes a runtime error whenever the function takes the conflict path (i.e., when an account already has a SpaceAccess entry for the given space).
This function is actively called by upsert_accounts_in_space, which is invoked from the Roam sync path in syncDgNodesToSupabase.ts:360.
Root Cause
In the migration 20260124215014_PartialSpaceAccess.sql, the function is created (line 13–55) with this SQL in the ON CONFLICT handler:
DO UPDATE SET permissions = CASE
WHEN COALESCE(local_account.space_editor, sa.editor, true) THEN 'editor'
ELSE 'reader' END;Later in the same migration (line 306), the column is dropped:
ALTER TABLE public."SpaceAccess" DROP COLUMN editor;Because plpgsql functions are validated lazily (at execution time, not creation time), the function creation succeeds. But at runtime, when PostgreSQL re-plans the statement after the column drop, it encounters sa.editor referencing a non-existent column and raises an error.
The schema file account.sql line 214 reflects this same broken state. sa.editor should be removed from the COALESCE expression entirely, since the editor boolean has been superseded by the permissions enum column.
Impact: Any call to upsert_account_in_space (or upsert_accounts_in_space) that hits the ON CONFLICT path will fail with a column-not-found error, breaking account syncing for existing users.
View 11 additional findings in Devin Review.
https://linear.app/discourse-graphs/issue/ENG-1433/the-database-views-are-too-slow
Optimize the database view queries.
https://www.loom.com/share/698ca41d993f47519568b06188ed1b1e
Note: I factored out the WITH query into a function after the loom, not worth re-filming, functionally equivalent.
Also added embeddings, and permissions on embeddings.
Small discrepancy noted by Devin re partial in policies, can be ignored in practice because resource access only added in that case