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:
@@ -0,0 +1,34 @@
|
||||
-- Restore the 000036 (WITH-based) evaluator.
|
||||
CREATE OR REPLACE FUNCTION filename_is_filtered(
|
||||
p_connection_id VARCHAR(36), p_relative_path TEXT, p_name TEXT
|
||||
) RETURNS BOOLEAN AS $$
|
||||
WITH nearest_allow AS (
|
||||
SELECT rule.excluded_extensions AS extensions
|
||||
FROM filename_filter_rules rule
|
||||
WHERE rule.source_connection_id = p_connection_id
|
||||
AND rule.mode = 'allow'
|
||||
AND (rule.directory_path = ''
|
||||
OR p_relative_path LIKE rule.directory_path || '/%')
|
||||
ORDER BY LENGTH(rule.directory_path) DESC
|
||||
LIMIT 1
|
||||
)
|
||||
SELECT CASE
|
||||
WHEN filename_extension(p_name) = '' THEN FALSE
|
||||
WHEN EXISTS (
|
||||
SELECT 1
|
||||
FROM filename_filter_rules rule
|
||||
CROSS JOIN LATERAL UNNEST(rule.excluded_extensions) extension
|
||||
WHERE rule.source_connection_id = p_connection_id
|
||||
AND rule.mode = 'exclude'
|
||||
AND (rule.directory_path = ''
|
||||
OR p_relative_path LIKE rule.directory_path || '/%')
|
||||
AND extension = filename_extension(p_name)
|
||||
) THEN TRUE
|
||||
WHEN EXISTS (SELECT 1 FROM nearest_allow)
|
||||
THEN NOT EXISTS (SELECT 1 FROM nearest_allow
|
||||
WHERE filename_extension(p_name) = ANY(extensions))
|
||||
ELSE FALSE
|
||||
END
|
||||
$$ LANGUAGE SQL STABLE PARALLEL SAFE;
|
||||
|
||||
DROP INDEX IF EXISTS idx_source_catalog_nodes_snapshot_parent;
|
||||
@@ -0,0 +1,42 @@
|
||||
-- Faster evaluator: no WITH blocks (scalar subqueries inline better and keep a
|
||||
-- single prepared plan per call). Semantics identical to 000036:
|
||||
-- extensionless names pass; exclude rules block; the nearest allow rule, when
|
||||
-- one matches, wins over everything not in it.
|
||||
CREATE OR REPLACE FUNCTION filename_is_filtered(
|
||||
p_connection_id VARCHAR(36), p_relative_path TEXT, p_name TEXT
|
||||
) RETURNS BOOLEAN AS $$
|
||||
SELECT CASE
|
||||
WHEN filename_extension(p_name) = '' THEN FALSE
|
||||
WHEN EXISTS (
|
||||
SELECT 1
|
||||
FROM filename_filter_rules rule
|
||||
CROSS JOIN LATERAL UNNEST(rule.excluded_extensions) extension
|
||||
WHERE rule.source_connection_id = p_connection_id
|
||||
AND rule.mode = 'exclude'
|
||||
AND (rule.directory_path = ''
|
||||
OR p_relative_path LIKE rule.directory_path || '/%')
|
||||
AND extension = filename_extension(p_name)
|
||||
) THEN TRUE
|
||||
WHEN EXISTS (
|
||||
SELECT 1 FROM filename_filter_rules rule
|
||||
WHERE rule.source_connection_id = p_connection_id
|
||||
AND rule.mode = 'allow'
|
||||
AND (rule.directory_path = ''
|
||||
OR p_relative_path LIKE rule.directory_path || '/%')
|
||||
) THEN NOT (filename_extension(p_name) = ANY(COALESCE((
|
||||
SELECT rule.excluded_extensions
|
||||
FROM filename_filter_rules rule
|
||||
WHERE rule.source_connection_id = p_connection_id
|
||||
AND rule.mode = 'allow'
|
||||
AND (rule.directory_path = ''
|
||||
OR p_relative_path LIKE rule.directory_path || '/%')
|
||||
ORDER BY LENGTH(rule.directory_path) DESC
|
||||
LIMIT 1
|
||||
), '{}'::TEXT[])))
|
||||
ELSE FALSE
|
||||
END
|
||||
$$ LANGUAGE SQL STABLE PARALLEL SAFE;
|
||||
|
||||
-- The subtree walk needs (snapshot, parent); make sure it is indexed.
|
||||
CREATE INDEX IF NOT EXISTS idx_source_catalog_nodes_snapshot_parent
|
||||
ON source_catalog_nodes(snapshot_id, parent_node_id);
|
||||
@@ -0,0 +1 @@
|
||||
ALTER TABLE source_catalog_nodes DROP COLUMN IF EXISTS ext;
|
||||
@@ -0,0 +1,4 @@
|
||||
-- Catalog reads must not run the rule-table probe + regex per row.
|
||||
-- Precompute the parsed extension on every node row (generated, immutable).
|
||||
ALTER TABLE source_catalog_nodes
|
||||
ADD COLUMN IF NOT EXISTS ext TEXT GENERATED ALWAYS AS (filename_extension(name)) STORED;
|
||||
@@ -0,0 +1,35 @@
|
||||
DROP FUNCTION IF EXISTS catalog_filter_context(VARCHAR);
|
||||
CREATE OR REPLACE FUNCTION filename_is_filtered(
|
||||
p_connection_id VARCHAR(36), p_relative_path TEXT, p_name TEXT
|
||||
) RETURNS BOOLEAN AS $$
|
||||
SELECT CASE
|
||||
WHEN filename_extension(p_name) = '' THEN FALSE
|
||||
WHEN EXISTS (
|
||||
SELECT 1
|
||||
FROM filename_filter_rules rule
|
||||
CROSS JOIN LATERAL UNNEST(rule.excluded_extensions) extension
|
||||
WHERE rule.source_connection_id = p_connection_id
|
||||
AND rule.mode = 'exclude'
|
||||
AND (rule.directory_path = ''
|
||||
OR p_relative_path LIKE rule.directory_path || '/%')
|
||||
AND extension = filename_extension(p_name)
|
||||
) THEN TRUE
|
||||
WHEN EXISTS (
|
||||
SELECT 1 FROM filename_filter_rules rule
|
||||
WHERE rule.source_connection_id = p_connection_id
|
||||
AND rule.mode = 'allow'
|
||||
AND (rule.directory_path = ''
|
||||
OR p_relative_path LIKE rule.directory_path || '/%')
|
||||
) THEN NOT (filename_extension(p_name) = ANY(COALESCE((
|
||||
SELECT rule.excluded_extensions
|
||||
FROM filename_filter_rules rule
|
||||
WHERE rule.source_connection_id = p_connection_id
|
||||
AND rule.mode = 'allow'
|
||||
AND (rule.directory_path = ''
|
||||
OR p_relative_path LIKE rule.directory_path || '/%')
|
||||
ORDER BY LENGTH(rule.directory_path) DESC
|
||||
LIMIT 1
|
||||
), '{}'::TEXT[])))
|
||||
ELSE FALSE
|
||||
END
|
||||
$$ LANGUAGE SQL STABLE PARALLEL SAFE;
|
||||
@@ -0,0 +1,18 @@
|
||||
-- Hot catalog reads resolve a connection's filter context ONCE per query and
|
||||
-- evaluate n.ext against arrays; the exact row-by-row filename_is_filtered is
|
||||
-- only used when directory-scoped rules exist.
|
||||
CREATE OR REPLACE FUNCTION catalog_filter_context(p_connection_id VARCHAR(36))
|
||||
RETURNS TABLE(has_dir BOOLEAN, src_excl TEXT[], src_allow TEXT[]) AS $$
|
||||
SELECT coalesce(bool_or(rule.directory_path <> ''), FALSE),
|
||||
coalesce((SELECT array_agg(extension) FROM (
|
||||
SELECT unnest(excluded_extensions) AS extension
|
||||
FROM filename_filter_rules r2
|
||||
WHERE r2.source_connection_id = p_connection_id
|
||||
AND r2.mode = 'exclude' AND r2.directory_path = ''
|
||||
) e), '{}'::text[]),
|
||||
(SELECT r3.excluded_extensions FROM filename_filter_rules r3
|
||||
WHERE r3.source_connection_id = p_connection_id
|
||||
AND r3.mode = 'allow' AND r3.directory_path = '' LIMIT 1)
|
||||
FROM filename_filter_rules rule
|
||||
WHERE rule.source_connection_id = p_connection_id
|
||||
$$ LANGUAGE SQL STABLE PARALLEL SAFE;
|
||||
Reference in New Issue
Block a user