Skip to content

ENG-1433 The database views are too slow#783

Merged
trangdoan982 merged 7 commits intomainfrom
eng-1433-the-database-views-are-too-slow
Feb 16, 2026
Merged

ENG-1433 The database views are too slow#783
trangdoan982 merged 7 commits intomainfrom
eng-1433-the-database-views-are-too-slow

Conversation

@maparent
Copy link
Copy Markdown
Collaborator

@maparent maparent commented Feb 14, 2026

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


Open with Devin

@linear
Copy link
Copy Markdown

linear Bot commented Feb 14, 2026

@supabase
Copy link
Copy Markdown

supabase Bot commented Feb 14, 2026

Updates to Preview Branch (eng-1433-the-database-views-are-too-slow) ↗︎

Deployments Status Updated
Database Mon, 16 Feb 2026 15:15:12 UTC
Services Mon, 16 Feb 2026 15:15:12 UTC
APIs Mon, 16 Feb 2026 15:15:12 UTC

Tasks are run on every commit but only new migration files are pushed.
Close and reopen this PR if you want to apply changes from existing seed or migration files.

Tasks Status Updated
Configurations Mon, 16 Feb 2026 15:15:13 UTC
Migrations ⚠️ Mon, 16 Feb 2026 15:15:13 UTC
Seeding Mon, 16 Feb 2026 15:15:13 UTC
Edge Functions Mon, 16 Feb 2026 15:15:15 UTC

⚠️ Warning — Applied out-of-order migrations: [packages/database/supabase/migrations/20260213155959_upsert_user_permissions.sql]


View logs for this Workflow Run ↗︎.
Learn more about Supabase for Git ↗︎.

devin-ai-integration[bot]

This comment was marked as resolved.

Copy link
Copy Markdown
Contributor

@devin-ai-integration devin-ai-integration Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Open in Devin Review

devin-ai-integration[bot]

This comment was marked as resolved.

@maparent maparent force-pushed the eng-1433-the-database-views-are-too-slow branch from b609191 to e1bde6c Compare February 16, 2026 14:19
Copy link
Copy Markdown
Contributor

@devin-ai-integration devin-ai-integration Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Open in Devin Review

@trangdoan982 trangdoan982 self-requested a review February 16, 2026 15:12
@maparent maparent force-pushed the eng-1433-the-database-views-are-too-slow branch from e1bde6c to 426b2c9 Compare February 16, 2026 15:14
Copy link
Copy Markdown
Contributor

@devin-ai-integration devin-ai-integration Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Open in Devin Review

@trangdoan982 trangdoan982 merged commit 52389ff into main Feb 16, 2026
8 checks passed
@trangdoan982 trangdoan982 deleted the eng-1433-the-database-views-are-too-slow branch February 16, 2026 15:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants