perf(catalog): resolve filename filters once per query, not per row

The 000036 evaluator probes the rules table and runs two regexes for every
catalog file row; the first-level page of the 8k-file education source took
2.5-5s per query and the browser hung on 'reading first-level directory'.

- source_catalog_nodes gains a STORED generated ext column (000038)
- catalog_filter_context() resolves the connection's source-level allow/
  exclude sets once; hot queries match node.ext against arrays (000039)
- filename_is_filtered stays the exact fallback, used only when the
  connection has directory-scoped rules
- ListCatalogSources, ListCatalogNodes (current/subtree/total/items) and
  RouteCatalogFiles fan-out adopt the fast path

Education root page: 4770ms -> 12ms; all-source counts: 54ms.
This commit is contained in:
姚章浩
2026-09-15 11:29:28 +08:00
parent e6ef5a4693
commit c84f848849
9 changed files with 182 additions and 13 deletions
+39 -9
View File
@@ -17,8 +17,13 @@ func (s *Store) ListCatalogSources(ctx context.Context) ([]governance.CatalogSou
SELECT connection.id, connection.source_type, snapshot.source_label, root.node_id,
snapshot.folder_count - 1,
(SELECT COUNT(*)::integer FROM source_catalog_nodes file
LEFT JOIN LATERAL catalog_filter_context(connection.id) ctx ON TRUE
WHERE file.snapshot_id=snapshot.id AND file.node_type='file'
AND NOT filename_is_filtered(connection.id,file.relative_path,file.name)),
AND (file.ext=''
OR (coalesce(ctx.has_dir,FALSE) AND NOT filename_is_filtered(connection.id,file.relative_path,file.name))
OR (NOT coalesce(ctx.has_dir,FALSE)
AND (ctx.src_allow IS NULL OR file.ext = ANY(ctx.src_allow))
AND NOT file.ext = ANY(coalesce(ctx.src_excl,'{}'::text[]))))),
head.activated_at,
'ready', COALESCE(refresh.duration_ms,0)
FROM source_catalog_heads head
@@ -66,18 +71,28 @@ func (s *Store) ListCatalogNodes(ctx context.Context, filter governance.CatalogN
page := governance.CatalogNodePage{Items: []governance.CatalogNode{}, Limit: filter.Limit, Offset: filter.Offset}
var parentID *string
err := s.db.QueryRow(ctx, `
WITH filter_ctx AS MATERIALIZED (SELECT * FROM catalog_filter_context($1))
SELECT node.node_id,node.parent_node_id,node.node_type,node.name,
(SELECT COUNT(*)::integer FROM source_catalog_nodes child
LEFT JOIN filter_ctx ctx ON TRUE
WHERE child.snapshot_id=node.snapshot_id AND child.parent_node_id=node.node_id
AND (child.node_type='folder' OR NOT filename_is_filtered($1,child.relative_path,child.name))),
AND (child.node_type='folder' OR child.ext=''
OR (ctx.has_dir AND NOT filename_is_filtered($1,child.relative_path,child.name))
OR (NOT ctx.has_dir AND (ctx.src_allow IS NULL OR child.ext = ANY(ctx.src_allow)) AND NOT child.ext = ANY(ctx.src_excl)))),
(SELECT COUNT(*)::integer FROM source_catalog_nodes file
LEFT JOIN filter_ctx ctx ON TRUE
WHERE file.snapshot_id=node.snapshot_id AND file.node_type='file'
AND (node.relative_path='' OR file.relative_path LIKE node.relative_path||'/%')
AND NOT filename_is_filtered($1,file.relative_path,file.name)),
AND (file.ext=''
OR (ctx.has_dir AND NOT filename_is_filtered($1,file.relative_path,file.name))
OR (NOT ctx.has_dir AND (ctx.src_allow IS NULL OR file.ext = ANY(ctx.src_allow)) AND NOT file.ext = ANY(ctx.src_excl)))),
(SELECT COALESCE(SUM(COALESCE(file.size_bytes,0)),0)::bigint FROM source_catalog_nodes file
LEFT JOIN filter_ctx ctx ON TRUE
WHERE file.snapshot_id=node.snapshot_id AND file.node_type='file'
AND (node.relative_path='' OR file.relative_path LIKE node.relative_path||'/%')
AND NOT filename_is_filtered($1,file.relative_path,file.name)),
AND (file.ext=''
OR (ctx.has_dir AND NOT filename_is_filtered($1,file.relative_path,file.name))
OR (NOT ctx.has_dir AND (ctx.src_allow IS NULL OR file.ext = ANY(ctx.src_allow)) AND NOT file.ext = ANY(ctx.src_excl)))),
node.size_bytes,node.modified_at
FROM source_catalog_heads head
JOIN source_catalog_nodes node ON node.snapshot_id=head.snapshot_id
@@ -125,20 +140,29 @@ SELECT node_id,name FROM trail ORDER BY depth DESC`, filter.ConnectionID, filter
breadcrumbs.Close()
if err := s.db.QueryRow(ctx, `
WITH filter_ctx AS MATERIALIZED (SELECT * FROM catalog_filter_context($1))
SELECT COUNT(*)::integer FROM source_catalog_heads head
JOIN source_catalog_nodes node ON node.snapshot_id=head.snapshot_id
LEFT JOIN filter_ctx ctx ON TRUE
WHERE head.source_connection_id=$1 AND node.parent_node_id=$2
AND (node.node_type='folder' OR NOT filename_is_filtered($1,node.relative_path,node.name))`, filter.ConnectionID, filter.ParentNodeID).Scan(&page.Total); err != nil {
AND (node.node_type='folder' OR node.ext=''
OR (ctx.has_dir AND NOT filename_is_filtered($1,node.relative_path,node.name))
OR (NOT ctx.has_dir AND (ctx.src_allow IS NULL OR node.ext = ANY(ctx.src_allow)) AND NOT node.ext = ANY(ctx.src_excl)))`, filter.ConnectionID, filter.ParentNodeID).Scan(&page.Total); err != nil {
return governance.CatalogNodePage{}, fmt.Errorf("count catalog nodes: %w", err)
}
rows, err := s.db.Query(ctx, `
WITH RECURSIVE current_head AS (
WITH RECURSIVE filter_ctx AS MATERIALIZED (
SELECT * FROM catalog_filter_context($1)
), current_head AS (
SELECT snapshot_id FROM source_catalog_heads WHERE source_connection_id=$1
), selected AS MATERIALIZED (
SELECT node.* FROM current_head head JOIN source_catalog_nodes node ON node.snapshot_id=head.snapshot_id
LEFT JOIN filter_ctx ctx ON TRUE
WHERE node.parent_node_id=$2
AND (node.node_type='folder' OR NOT filename_is_filtered($1,node.relative_path,node.name))
AND (node.node_type='folder' OR node.ext=''
OR (ctx.has_dir AND NOT filename_is_filtered($1,node.relative_path,node.name))
OR (NOT ctx.has_dir AND (ctx.src_allow IS NULL OR node.ext = ANY(ctx.src_allow)) AND NOT node.ext = ANY(ctx.src_excl)))
ORDER BY CASE node.node_type WHEN 'folder' THEN 0 ELSE 1 END,node.sort_order,lower(node.name),node.node_id
LIMIT $3 OFFSET $4
), descendants(ancestor_id,snapshot_id,node_id) AS (
@@ -154,8 +178,11 @@ WITH RECURSIVE current_head AS (
current_version.content_state AS version_content_state,content.content_state AS object_content_state,
content.local_spool_ref,leaf.size_bytes
FROM descendants
CROSS JOIN filter_ctx ctx
JOIN source_catalog_nodes leaf ON leaf.snapshot_id=descendants.snapshot_id AND leaf.node_id=descendants.node_id AND leaf.node_type='file'
AND NOT filename_is_filtered($1,leaf.relative_path,leaf.name)
AND (leaf.ext=''
OR (ctx.has_dir AND NOT filename_is_filtered($1,leaf.relative_path,leaf.name))
OR (NOT ctx.has_dir AND (ctx.src_allow IS NULL OR leaf.ext = ANY(ctx.src_allow)) AND NOT leaf.ext = ANY(ctx.src_excl)))
LEFT JOIN source_items item ON item.id=leaf.source_item_id
LEFT JOIN source_item_versions current_version ON current_version.source_item_id=item.id AND current_version.revision=item.revision
LEFT JOIN content_objects content ON content.id=current_version.content_object_id
@@ -191,8 +218,11 @@ WITH RECURSIVE current_head AS (
SELECT selected.node_id,selected.parent_node_id,selected.node_type,selected.name,
CASE WHEN selected.node_type='file' THEN 0 ELSE (
SELECT COUNT(*)::integer FROM source_catalog_nodes child
LEFT JOIN filter_ctx ctx2 ON TRUE
WHERE child.snapshot_id=selected.snapshot_id AND child.parent_node_id=selected.node_id
AND (child.node_type='folder' OR NOT filename_is_filtered($1,child.relative_path,child.name))
AND (child.node_type='folder' OR child.ext=''
OR (ctx2.has_dir AND NOT filename_is_filtered($1,child.relative_path,child.name))
OR (NOT ctx2.has_dir AND (ctx2.src_allow IS NULL OR child.ext = ANY(ctx2.src_allow)) AND NOT child.ext = ANY(ctx2.src_excl)))
) END,
COALESCE(stats.eligible_file_count,0),COALESCE(stats.eligible_bytes,0),
selected.size_bytes,selected.modified_at,selected.content_sha256,COALESCE(stats.visibility,'unknown'),