Commit Graph
3 Commits
Author SHA1 Message Date
Joshen Lim 5eb68c5df2 Add indexes to cmd k related resources in table editor (#53204)
## Context

We previously added a "Related resources" section in Cmd+K if you're on
the Table Editor with a table opened, this included resources pertaining
to the table such as policies, triggers, and functions.

We're adding indexes as well in this PR - thinking that might also be
appropriate to have in this context
<img width="500" alt="image"
src="https://github.com/user-attachments/assets/5cd383ff-6617-49ba-bdb8-6f65d8fd0680"
/>

## Performance

Benchmarked with Claude against the stress catalog
(`PG_META_STRESS_TABLES=12000`), with master and this branch timed in
the same run on the same database, median of 9.

| Table | master | This PR | Worst of 9 |
|---|---|---|---|
| Hub table — 8 indexes, policy, many FK triggers | 1.6 ms | 2.3 ms |
2.8 ms |
| Policy + user trigger | 1.0 ms | 1.3 ms | 2.0 ms |
| Mid-chain table | 1.1 ms | 1.5 ms | 1.7 ms |
| Deliberately over-indexed table — 20 indexes | 0.8 ms | 2.0 ms | 4.6
ms |
| Table that doesn't exist | 1.1 ms | 1.5 ms | 4.5 ms |
2026-11-11 14:43:07 +08:00
Charis c8aad267cb fix(studio): use read grant variant for table related resources queries (#53231)
Fixes the Studio typecheck failure on \`master\`.

## What

Two PRs merged close together and conflict at the type level. #53079
added the table related resources queries with a plain \`POST
/platform/pg-meta/{ref}/query\` grant. #53185 then made a variant
required for that endpoint. Both queries only read data, so they now
request and accept the \`read\` variant.

## Testing

- \`tsc --noEmit\` in \`apps/studio\` passes.
2026-11-10 11:12:55 -06:00
Joshen Lim f87de2f0d0 Joshenlim/fe 4593 cmd k related resources while in table editor (#53079)
## Context

Building on towards resource-aware Cmd K, the changes done up so far
have all been to allow users to search for database resources from
anywhere in the project, with the intention to get you to the resource
quickly without having to go through the navigation hierarchy of the
dashboard.

This PR explores a way to quickly glance related resources for a table
in the Table Editor through Cmd K for that same intention, but listing
out the policies, triggers or functions that run on the table that
you're currently viewing in the Table Editor. E.g here we've got the
`blog_authors` table opened:
<img width="500" alt="image"
src="https://github.com/user-attachments/assets/78c9b85f-cd30-4564-a264-c1a2a596d5c2"
/>

Policies and triggers are fairly straight forward since we can determine
what's related by their target table. Functions are a bit more fuzzy as
we're trying to find what's related by checking the function definition,
if it does anything to the table. For e.g based on the screenshot above,
`blog_handle_new_user` has this definition which was why it was flagged
<img width="500" alt="image"
src="https://github.com/user-attachments/assets/fb8f3be4-05df-4d87-8440-8b75d95546ae"
/>

We're also deliberately decoupling loading of policies + triggers and
functions since searching functions takes slightly longer, so opting to
load results of policies + triggers first, then lazy load functions
subsequently once results are ready

 ## Performance

As usual, ran a benchmarking session with Claude just to make sure that
the added SQL doesn't cause any performance issues, these are aggregates
of 4–5 runs.

**Catalog size (stress catalog)**

| Query | 2,000 tables | 10,000 tables |
|---|---|---|
| Policies and triggers, hub table (many FKs) | 0.1 ms | 0.5–1.3 ms |
| Policies and triggers, ordinary table | ~0 ms | ~0 ms |
| Functions, every body matches | 1.4 ms | 4–8 ms |
| Functions, no match (full scan) | 1.8 ms | 7.3–7.7 ms |
| Functions, table not visible | 0 ms | 0 ms |

**Large function bodies** (random text, 2,000 tables plus 2,000 small
functions, plus the large ones added)

| Function source | Largest function | Functions: no match (full scan) |
Functions: every body matches |
|---|---|---|---|
| 56 MB (600 functions) | 94 KB | ~50 ms | 2 ms |
| 103 MB (+10 multi-MB) | 4.7 MB | ~92 ms | 2 ms |

## To test

Use a project with a table that has at least one policy, one trigger,
and a function that mentions the table in its body.

- [ ] Open that table in the Table Editor and press Cmd+K. A **Related
resources** section appears at the top with its policies, triggers, and
functions.
- [ ] Click a policy, a trigger, and a function. Each opens its editor
on the right resource.
- [ ] A trigger's function does not also appear under the functions
rows.
- [ ] Type part of one of those names. The section narrows to matching
rows and the project-wide entity results appear below. A resource can
appear in both.
- [ ] Type something that matches nothing in the table. The section
disappears rather than showing empty.
- [ ] Clear the input. The full section comes back without a loading
flicker, since the data is cached.
- [ ] Open the table editor on a table with no policies, triggers, or
referencing functions. Cmd+K shows no empty **Related resources**
section.
- [ ] Switch to a different table and press Cmd+K. The section shows
that table's resources, not the previous table's.
- [ ] Open Cmd+K from a non-Table Editor page, such as the SQL Editor.
The section is not shown.
- [ ] In the network tab, check that opening the menu sends one query
for policies and triggers and one for functions. Typing sends no
additional ones for this section.
2026-11-10 10:46:41 +08:00