Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

perf(entity_properties): drop the unused values GIN (#7589)

teo52e200c2c5ff
diff --git a/.sqlx/query-6a18325302f4c68619c189edc7c73d1c36945d9ec3c9cac0a6bacb85aeb1a50e.json b/.sqlx/query-6a18325302f4c68619c189edc7c73d1c36945d9ec3c9cac0a6bacb85aeb1a50e.json
new file mode 100644
index 0000000000..35d033dd1b
--- /dev/null
+++ b/.sqlx/query-6a18325302f4c68619c189edc7c73d1c36945d9ec3c9cac0a6bacb85aeb1a50e.json
@@ -0,0 +1,16 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n INSERT INTO entity_properties (id, entity_id, entity_type, property_definition_id, values)\n VALUES (\n gen_random_uuid(), 'two', 'TASK', $1,\n jsonb_build_object('type', 'EntityReference', 'value', jsonb_build_array(\n jsonb_build_object('entity_id', $2::text, 'entity_type', 'INITIATIVE'),\n jsonb_build_object('entity_id', $3::text, 'entity_type', 'INITIATIVE')\n ))\n ), (\n gen_random_uuid(), 'extra', 'TASK', $1,\n jsonb_build_object('type', 'EntityReference', 'value', jsonb_build_array(\n jsonb_build_object(\n 'entity_id', $2::text,\n 'entity_type', 'INITIATIVE',\n 'specific_message_id', 'x'\n )\n ))\n ), (\n gen_random_uuid(), 'empty', 'TASK', $1,\n '{\"type\": \"EntityReference\", \"value\": []}'::jsonb\n )\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text",
+ "Text"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "6a18325302f4c68619c189edc7c73d1c36945d9ec3c9cac0a6bacb85aeb1a50e"
+}
diff --git a/.sqlx/query-d7bad4039582741fcae82df8c90a5148a76cfd4567fcef8e0432ee746a9a2e3d.json b/.sqlx/query-d7bad4039582741fcae82df8c90a5148a76cfd4567fcef8e0432ee746a9a2e3d.json
deleted file mode 100644
index 484e28e4af..0000000000
--- a/.sqlx/query-d7bad4039582741fcae82df8c90a5148a76cfd4567fcef8e0432ee746a9a2e3d.json
+++ /dev/null
@@ -1,23 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n SELECT entity_id\n FROM entity_properties\n WHERE property_definition_id = $1\n AND entity_type = 'TASK'\n AND values @> jsonb_build_object(\n 'value', jsonb_build_array(jsonb_build_object(\n 'entity_id', $2::text, 'entity_type', 'INITIATIVE'\n ))\n )\n ORDER BY entity_id\n ",
- "describe": {
- "columns": [
- {
- "ordinal": 0,
- "name": "entity_id",
- "type_info": "Text"
- }
- ],
- "parameters": {
- "Left": [
- "Uuid",
- "Text"
- ]
- },
- "nullable": [
- false
- ]
- },
- "hash": "d7bad4039582741fcae82df8c90a5148a76cfd4567fcef8e0432ee746a9a2e3d"
-}
diff --git a/.sqlx/query-f8066d61cdf87dd9d2ce727952599eaf0da6670d335ad74e2fe8ef07b8df61d3.json b/.sqlx/query-f8066d61cdf87dd9d2ce727952599eaf0da6670d335ad74e2fe8ef07b8df61d3.json
new file mode 100644
index 0000000000..49fd0a51bf
--- /dev/null
+++ b/.sqlx/query-f8066d61cdf87dd9d2ce727952599eaf0da6670d335ad74e2fe8ef07b8df61d3.json
@@ -0,0 +1,22 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT entity_id\n FROM entity_properties\n WHERE property_definition_id = '00000001-0000-0000-0000-000000000014'\n AND entity_type = 'TASK'\n AND values -> 'value' @> jsonb_build_array(jsonb_build_object(\n 'entity_id', $1::text, 'entity_type', 'INITIATIVE'\n ))\n ORDER BY entity_id\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "entity_id",
+ "type_info": "Text"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Text"
+ ]
+ },
+ "nullable": [
+ false
+ ]
+ },
+ "hash": "f8066d61cdf87dd9d2ce727952599eaf0da6670d335ad74e2fe8ef07b8df61d3"
+}
diff --git a/crates/macro_db_client/migrations/20261006133403_drop_entity_properties_values_gin.sql b/crates/macro_db_client/migrations/20261006133403_drop_entity_properties_values_gin.sql
new file mode 100644
index 0000000000..e74725bc72
--- /dev/null
+++ b/crates/macro_db_client/migrations/20261006133403_drop_entity_properties_values_gin.sql
@@ -0,0 +1,12 @@
+-- no-transaction
+-- Inserts maintain this GIN on every property row, and flushing its pending
+-- list is most of the entity_properties insert time. Nothing in the measured
+-- week scanned it. Filters use the expression indexes on values->'value'
+-- (assignee, status, project). project_task_ids in this change reads that
+-- expression with the Project id as a literal, so it uses
+-- idx_ep_project_value_gin.
+--
+-- This migration deploys first. Until the service rolls, the previous
+-- whole-value query uses the property_definition_id btree. It returns the
+-- same rows.
+DROP INDEX CONCURRENTLY IF EXISTS idx_entity_properties_values_gin;
diff --git a/crates/system_properties/src/domain/model/constants/system_property_key.rs b/crates/system_properties/src/domain/model/constants/system_property_key.rs
index f56aab049e..ee7342b817 100644
--- a/crates/system_properties/src/domain/model/constants/system_property_key.rs
+++ b/crates/system_properties/src/domain/model/constants/system_property_key.rs
@@ -206,6 +206,15 @@ mod tests {
assert!(all_keys.contains(&SystemPropertyKey::PROJECT_UUID));
}
+ #[test]
+ fn project_uuid_matches_the_partial_gin_predicate() {
+ // project_task_ids and idx_ep_project_value_gin both spell this literal.
+ assert_eq!(
+ SystemPropertyKey::PROJECT_UUID.to_string(),
+ "00000001-0000-0000-0000-000000000014"
+ );
+ }
+
#[test]
fn test_required_property_ids_for_company() {
let required = SystemPropertyKey::required_property_ids_for_entity(EntityType::Company);
diff --git a/crates/system_properties/src/outbound/pgpool.rs b/crates/system_properties/src/outbound/pgpool.rs
index abe3dc59bf..32b619be29 100644
--- a/crates/system_properties/src/outbound/pgpool.rs
+++ b/crates/system_properties/src/outbound/pgpool.rs
@@ -284,21 +284,22 @@ impl SystemPropertiesRepository for PgSystemPropertiesRepository {
}
async fn project_task_ids(&self, project_id: Uuid) -> Result<Vec<String>, SystemPropertyError> {
- // Whole-value containment uses the `values` GIN index.
+ // The Project id is a literal so a generic plan can prove the partial
+ // predicate of idx_ep_project_value_gin. A bound parameter cannot.
+ // Containment on values->'value' matches whole-value containment of
+ // {"value": [ref]}: null, non-object, and non-array rows are excluded
+ // either way. The literal must stay equal to SystemPropertyKey::PROJECT_UUID.
let rows = sqlx::query_scalar!(
r#"
SELECT entity_id
FROM entity_properties
- WHERE property_definition_id = $1
+ WHERE property_definition_id = '00000001-0000-0000-0000-000000000014'
AND entity_type = 'TASK'
- AND values @> jsonb_build_object(
- 'value', jsonb_build_array(jsonb_build_object(
- 'entity_id', $2::text, 'entity_type', 'INITIATIVE'
- ))
- )
+ AND values -> 'value' @> jsonb_build_array(jsonb_build_object(
+ 'entity_id', $1::text, 'entity_type', 'INITIATIVE'
+ ))
ORDER BY entity_id
"#,
- SystemPropertyKey::PROJECT_UUID,
project_id.to_string(),
)
.fetch_all(&self.pool)
diff --git a/crates/system_properties/src/outbound/pgpool/test.rs b/crates/system_properties/src/outbound/pgpool/test.rs
index 7f430c694e..37c5f234a3 100644
--- a/crates/system_properties/src/outbound/pgpool/test.rs
+++ b/crates/system_properties/src/outbound/pgpool/test.rs
@@ -522,8 +522,42 @@ async fn project_membership_reads_the_project_property(pool: Pool<Postgres>) ->
"DOCUMENT",
)
.await?;
+ // A second reference in the array, an extra key, and an empty array. Containment
+ // keeps the first two and drops the empty array, same as whole-value containment.
+ sqlx::query!(
+ r#"
+ INSERT INTO entity_properties (id, entity_id, entity_type, property_definition_id, values)
+ VALUES (
+ gen_random_uuid(), 'two', 'TASK', $1,
+ jsonb_build_object('type', 'EntityReference', 'value', jsonb_build_array(
+ jsonb_build_object('entity_id', $2::text, 'entity_type', 'INITIATIVE'),
+ jsonb_build_object('entity_id', $3::text, 'entity_type', 'INITIATIVE')
+ ))
+ ), (
+ gen_random_uuid(), 'extra', 'TASK', $1,
+ jsonb_build_object('type', 'EntityReference', 'value', jsonb_build_array(
+ jsonb_build_object(
+ 'entity_id', $2::text,
+ 'entity_type', 'INITIATIVE',
+ 'specific_message_id', 'x'
+ )
+ ))
+ ), (
+ gen_random_uuid(), 'empty', 'TASK', $1,
+ '{"type": "EntityReference", "value": []}'::jsonb
+ )
+ "#,
+ project,
+ first.to_string(),
+ second.to_string(),
+ )
+ .execute(&pool)
+ .await?;
- assert_eq!(repo.project_task_ids(first).await?, ["task-a", "task-b"]);
+ assert_eq!(
+ repo.project_task_ids(first).await?,
+ ["extra", "task-a", "task-b", "two"]
+ );
let mut projects = repo
.task_projects(&[
"task-a".to_string(),