Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

perf: index documents by company and check document groups once per query (#1813)

Sidd0b9e04710a2
diff --git a/apps/erp/app/modules/documents/documents.service.ts b/apps/erp/app/modules/documents/documents.service.ts
index bd5fa0ae03..2ea2105d90 100644
--- a/apps/erp/app/modules/documents/documents.service.ts
+++ b/apps/erp/app/modules/documents/documents.service.ts
@@ -105,8 +105,11 @@ export async function getDocuments(
}
/** @mcp read */
-export async function getDocumentExtensions(client: SupabaseClient<Database>) {
- return client.from("documentExtensions").select("extension");
+export async function getDocumentExtensions(
+ client: SupabaseClient<Database>,
+ companyId: string
+) {
+ return client.rpc("get_document_extensions", { company_id: companyId });
}
/** @mcp read */
diff --git a/apps/erp/app/routes/api+/mcp+/lib/tool-manifest.digest.json b/apps/erp/app/routes/api+/mcp+/lib/tool-manifest.digest.json
index 92c4a83094..26530619dc 100644
--- a/apps/erp/app/routes/api+/mcp+/lib/tool-manifest.digest.json
+++ b/apps/erp/app/routes/api+/mcp+/lib/tool-manifest.digest.json
@@ -138,7 +138,7 @@
{"name":"documents_createDocumentUploadUrl","classification":"WRITE","paramCount":3,"schema":"9034f525c97e","response":"4ad76afe0777","injectAuth":"companyId+createdBy+updatedBy","permission":"documents:create","paginates":false},
{"name":"documents_deleteDocument","classification":"DESTRUCTIVE","paramCount":1,"schema":"3797626bd17e","response":"bcde375ebd4c","injectAuth":"companyId","permission":"documents:delete","paginates":false},
{"name":"documents_getDocument","classification":"READ","paramCount":1,"schema":"e7617eb689c8","response":"88539143f954","injectAuth":"companyId","permission":"documents:view","paginates":false},
- {"name":"documents_getDocumentExtensions","classification":"READ","paramCount":0,"schema":"efddc7bd8bbc","response":"b24f77557f86","injectAuth":"companyId","permission":"documents:view","paginates":false},
+ {"name":"documents_getDocumentExtensions","classification":"READ","paramCount":0,"schema":"efddc7bd8bbc","response":"a07a74ca3dd1","injectAuth":"companyId","permission":"documents:view","paginates":false},
{"name":"documents_getDocumentLabels","classification":"READ","paramCount":0,"schema":"efddc7bd8bbc","response":"7e2eeb0066ab","injectAuth":"companyId","permission":"documents:view","paginates":false},
{"name":"documents_getDocuments","classification":"READ","paramCount":6,"schema":"848cac7bea79","response":"806ff23e9acc","injectAuth":"companyId","permission":"documents:view","paginates":true},
{"name":"documents_insertDocumentExtraction","classification":"WRITE","paramCount":4,"schema":"790f2b883984","response":"d181256b2684","injectAuth":"companyId+createdBy+updatedBy","permission":"documents:create","paginates":false},
diff --git a/apps/erp/app/routes/x+/documents+/search.tsx b/apps/erp/app/routes/x+/documents+/search.tsx
index 3573606dc4..beeee08811 100644
--- a/apps/erp/app/routes/x+/documents+/search.tsx
+++ b/apps/erp/app/routes/x+/documents+/search.tsx
@@ -13,6 +13,7 @@ import type {
ShouldRevalidateFunction
} from "react-router";
import { Outlet, redirect, useLoaderData } from "react-router";
+import { useResolved } from "~/hooks/useResolved";
import type { Document } from "~/modules/documents";
import {
DocumentsTable,
@@ -46,7 +47,13 @@ export async function loader({ request }: LoaderFunctionArgs) {
const { limit, offset, sorts, filters } =
getGenericQueryFilters(searchParams);
- const [documents, labels, extensions] = await Promise.all([
+ // Only the file-type filter needs this, so it streams rather than holding
+ // the page.
+ const extensions = getDocumentExtensions(client, companyId).then(
+ (result) => result.data?.map(({ extension }) => extension) ?? []
+ );
+
+ const [documents, labels] = await Promise.all([
getDocuments(client, companyId, {
search,
favorite,
@@ -58,8 +65,7 @@ export async function loader({ request }: LoaderFunctionArgs) {
sorts,
filters
}),
- getDocumentLabels(client, userId),
- getDocumentExtensions(client)
+ getDocumentLabels(client, userId)
]);
if (documents.error) {
@@ -89,13 +95,13 @@ export async function loader({ request }: LoaderFunctionArgs) {
count: documents.count ?? 0,
documents: documentsWithDownloadTokens,
labels: labels.data ?? [],
- extensions: extensions.data?.map(({ extension }) => extension) ?? []
+ extensions
};
}
export default function DocumentsAllRoute() {
- const { count, documents, labels, extensions } =
- useLoaderData<typeof loader>();
+ const { count, documents, labels, ...data } = useLoaderData<typeof loader>();
+ const extensions = useResolved(data.extensions, []);
return (
<VStack spacing={0} className="h-full ">
diff --git a/packages/database/src/authz/manifest.ts b/packages/database/src/authz/manifest.ts
index e24f93fea3..22d8651c28 100644
--- a/packages/database/src/authz/manifest.ts
+++ b/packages/database/src/authz/manifest.ts
@@ -425,13 +425,16 @@ export const manifest = {
depreciationRunLine: company("accounting", { read: "accounting_view" }),
dimension: group("accounting"),
dimensionValue: group("accounting"),
+ // groups_for_user is wrapped in a SELECT so Postgres runs it once per query
+ // rather than once per row (it is VOLATILE), and it comes first so the
+ // API-key lookup only runs for rows outside the caller's groups.
document: custom(
"bespoke: readGroups/writeGroups arrays via groups_for_user, plus has_valid_api_key_for_company",
(t) => `
- CREATE POLICY "DELETE" ON ${t} AS PERMISSIVE FOR DELETE TO public USING ((("companyId" = ANY (( SELECT get_companies_with_employee_permission('documents_delete'::text) AS get_companies_with_employee_permission)::text[])) AND (has_valid_api_key_for_company("companyId") OR ((groups_for_user((( SELECT auth.uid() AS uid))::text) && "writeGroups") = true))));
- CREATE POLICY "INSERT" ON ${t} AS PERMISSIVE FOR INSERT TO public WITH CHECK ((("companyId" = ANY (( SELECT get_companies_with_employee_permission('documents_create'::text) AS get_companies_with_employee_permission)::text[])) AND (has_valid_api_key_for_company("companyId") OR ((groups_for_user((( SELECT auth.uid() AS uid))::text) && "writeGroups") = true))));
- CREATE POLICY "SELECT" ON ${t} AS PERMISSIVE FOR SELECT TO public USING ((("companyId" = ANY (( SELECT get_companies_with_employee_permission('documents_view'::text) AS get_companies_with_employee_permission)::text[])) AND (has_valid_api_key_for_company("companyId") OR ((groups_for_user((( SELECT auth.uid() AS uid))::text) && "readGroups") = true))));
- CREATE POLICY "UPDATE" ON ${t} AS PERMISSIVE FOR UPDATE TO public USING ((("companyId" = ANY (( SELECT get_companies_with_employee_permission('documents_update'::text) AS get_companies_with_employee_permission)::text[])) AND (has_valid_api_key_for_company("companyId") OR ((groups_for_user((( SELECT auth.uid() AS uid))::text) && "writeGroups") = true))));
+ CREATE POLICY "DELETE" ON ${t} AS PERMISSIVE FOR DELETE TO public USING ((("companyId" = ANY (( SELECT get_companies_with_employee_permission('documents_delete'::text) AS get_companies_with_employee_permission)::text[])) AND (("writeGroups" && ( SELECT groups_for_user((( SELECT auth.uid() AS uid))::text) AS groups_for_user)) OR has_valid_api_key_for_company("companyId"))));
+ CREATE POLICY "INSERT" ON ${t} AS PERMISSIVE FOR INSERT TO public WITH CHECK ((("companyId" = ANY (( SELECT get_companies_with_employee_permission('documents_create'::text) AS get_companies_with_employee_permission)::text[])) AND (("writeGroups" && ( SELECT groups_for_user((( SELECT auth.uid() AS uid))::text) AS groups_for_user)) OR has_valid_api_key_for_company("companyId"))));
+ CREATE POLICY "SELECT" ON ${t} AS PERMISSIVE FOR SELECT TO public USING ((("companyId" = ANY (( SELECT get_companies_with_employee_permission('documents_view'::text) AS get_companies_with_employee_permission)::text[])) AND (("readGroups" && ( SELECT groups_for_user((( SELECT auth.uid() AS uid))::text) AS groups_for_user)) OR has_valid_api_key_for_company("companyId"))));
+ CREATE POLICY "UPDATE" ON ${t} AS PERMISSIVE FOR UPDATE TO public USING ((("companyId" = ANY (( SELECT get_companies_with_employee_permission('documents_update'::text) AS get_companies_with_employee_permission)::text[])) AND (("writeGroups" && ( SELECT groups_for_user((( SELECT auth.uid() AS uid))::text) AS groups_for_user)) OR has_valid_api_key_for_company("companyId"))));
`
),
documentExtraction: policies({ all: inCompany("companyId", "employee") }),
diff --git a/packages/database/src/swagger-docs-schema.ts b/packages/database/src/swagger-docs-schema.ts
index e6976a4202..48712f3ddf 100644
--- a/packages/database/src/swagger-docs-schema.ts
+++ b/packages/database/src/swagger-docs-schema.ts
@@ -96050,6 +96050,63 @@ export default {
tags: ["(rpc) workflow_merge_custom_fields"]
}
},
+ "/rpc/get_document_extensions": {
+ get: {
+ parameters: [
+ {
+ format: "text",
+ in: "query",
+ name: "company_id",
+ required: true,
+ type: "string"
+ }
+ ],
+ produces: [
+ "application/json",
+ "application/vnd.pgrst.object+json;nulls=stripped",
+ "application/vnd.pgrst.object+json"
+ ],
+ responses: {
+ "200": {
+ description: "OK"
+ }
+ },
+ tags: ["(rpc) get_document_extensions"]
+ },
+ post: {
+ parameters: [
+ {
+ in: "body",
+ name: "args",
+ required: true,
+ schema: {
+ properties: {
+ company_id: {
+ format: "text",
+ type: "string"
+ }
+ },
+ required: ["company_id"],
+ type: "object"
+ }
+ },
+ {
+ $ref: "#/parameters/preferParams"
+ }
+ ],
+ produces: [
+ "application/json",
+ "application/vnd.pgrst.object+json;nulls=stripped",
+ "application/vnd.pgrst.object+json"
+ ],
+ responses: {
+ "200": {
+ description: "OK"
+ }
+ },
+ tags: ["(rpc) get_document_extensions"]
+ }
+ },
"/rpc/get_jobs_by_date_range": {
post: {
parameters: [
diff --git a/packages/database/src/types.ts b/packages/database/src/types.ts
index 9f4f55b8b1..207e7814c0 100644
--- a/packages/database/src/types.ts
+++ b/packages/database/src/types.ts
@@ -83446,6 +83446,12 @@ export type Database = {
trackedActivityId: string
}[]
}
+ get_document_extensions: {
+ Args: { company_id: string }
+ Returns: {
+ extension: string
+ }[]
+ }
get_effective_work_center_id: {
Args: { p_storage_unit_id: string }
Returns: string
diff --git a/packages/database/supabase/functions/lib/types.ts b/packages/database/supabase/functions/lib/types.ts
index 9f4f55b8b1..207e7814c0 100644
--- a/packages/database/supabase/functions/lib/types.ts
+++ b/packages/database/supabase/functions/lib/types.ts
@@ -83446,6 +83446,12 @@ export type Database = {
trackedActivityId: string
}[]
}
+ get_document_extensions: {
+ Args: { company_id: string }
+ Returns: {
+ extension: string
+ }[]
+ }
get_effective_work_center_id: {
Args: { p_storage_unit_id: string }
Returns: string
diff --git a/packages/database/supabase/migrations/20261002075441_document-company-index.sql b/packages/database/supabase/migrations/20261002075441_document-company-index.sql
new file mode 100644
index 0000000000..f25d3d7098
--- /dev/null
+++ b/packages/database/supabase/migrations/20261002075441_document-company-index.sql
@@ -0,0 +1,21 @@
+-- Every documents query filters by company, but "document" had no index on
+-- "companyId", so each one scanned every company's documents.
+CREATE INDEX IF NOT EXISTS "document_companyId_active_idx"
+ ON "document" ("companyId", "active");
+
+-- The file-type filter's options for one company. Replaces reading the
+-- "documentExtensions" view, a DISTINCT over every document the caller can
+-- read in any company. SECURITY INVOKER: the document policies still apply.
+CREATE OR REPLACE FUNCTION public.get_document_extensions(company_id TEXT)
+RETURNS TABLE (extension TEXT)
+LANGUAGE sql
+STABLE
+SECURITY INVOKER
+SET search_path = public
+AS $$
+ SELECT DISTINCT d.extension
+ FROM "document" d
+ WHERE d."companyId" = company_id
+ AND d.extension IS NOT NULL
+ ORDER BY d.extension
+$$;
diff --git a/packages/database/supabase/migrations/20261002075503_document-policy-initplan.sql b/packages/database/supabase/migrations/20261002075503_document-policy-initplan.sql
new file mode 100644
index 0000000000..b1ef7d8d3d
--- /dev/null
+++ b/packages/database/supabase/migrations/20261002075503_document-policy-initplan.sql
@@ -0,0 +1,20 @@
+-- Generated by `pnpm --filter @carbon/database authz migration` from packages/database/src/authz. Do not edit.
+
+-- Ships these rules to databases where `authz sync` does not run after migrations yet.
+
+-- document
+DO $authz$
+DECLARE p record;
+BEGIN
+ FOR p IN SELECT policyname FROM pg_policies WHERE schemaname = 'public' AND tablename = 'document' LOOP
+ EXECUTE format('DROP POLICY %I ON public.%I', p.policyname, 'document');
+ END LOOP;
+END
+$authz$;
+ALTER TABLE public."document" ENABLE ROW LEVEL SECURITY;
+
+ CREATE POLICY "DELETE" ON "public"."document" AS PERMISSIVE FOR DELETE TO public USING ((("companyId" = ANY (( SELECT get_companies_with_employee_permission('documents_delete'::text) AS get_companies_with_employee_permission)::text[])) AND (("writeGroups" && ( SELECT groups_for_user((( SELECT auth.uid() AS uid))::text) AS groups_for_user)) OR has_valid_api_key_for_company("companyId"))));
+ CREATE POLICY "INSERT" ON "public"."document" AS PERMISSIVE FOR INSERT TO public WITH CHECK ((("companyId" = ANY (( SELECT get_companies_with_employee_permission('documents_create'::text) AS get_companies_with_employee_permission)::text[])) AND (("writeGroups" && ( SELECT groups_for_user((( SELECT auth.uid() AS uid))::text) AS groups_for_user)) OR has_valid_api_key_for_company("companyId"))));
+ CREATE POLICY "SELECT" ON "public"."document" AS PERMISSIVE FOR SELECT TO public USING ((("companyId" = ANY (( SELECT get_companies_with_employee_permission('documents_view'::text) AS get_companies_with_employee_permission)::text[])) AND (("readGroups" && ( SELECT groups_for_user((( SELECT auth.uid() AS uid))::text) AS groups_for_user)) OR has_valid_api_key_for_company("companyId"))));
+ CREATE POLICY "UPDATE" ON "public"."document" AS PERMISSIVE FOR UPDATE TO public USING ((("companyId" = ANY (( SELECT get_companies_with_employee_permission('documents_update'::text) AS get_companies_with_employee_permission)::text[])) AND (("writeGroups" && ( SELECT groups_for_user((( SELECT auth.uid() AS uid))::text) AS groups_for_user)) OR has_valid_api_key_for_company("companyId"))));
+