Gabriel
07/22/2025, 9:12 AMINDEX ix_relation_tuple_by_subject_relation (userset_namespace ASC, userset_relation ASC, namespace ASC, relation ASC)
When checking the db we saw that the cardinality of tuples (userset_namespace, userset_relation, namespace, relation)
was very small. It seems that cockroachdb then assigns the same page to all data with the same tuple and creates a hot tuple.
We solved the incident by doing
create INDEX ix_relation_tuple_by_subject_relation_v2 on relation_tuple (userset_namespace ASC, userset_relation ASC, namespace ASC, relation ASC, object_id);
and
alter index ix_relation_tuple_by_subject_relation set not visible;
This way the index has a high cardinality key (object_id) and cockroachdb can split between different nodes.
However, even though the old index is not visible, it is still maintained, so we still have a couple of nodes with high cpu.
Do you foresee any problem from dropping the index?
Also, are you aware in what queries this is used? We couldn't find it in the database console.
Finally, would it make sense to open a pr with the change to spicedb?
Cheersyetitwo
07/22/2025, 1:46 PMGabriel
07/22/2025, 1:55 PMyetitwo
07/22/2025, 2:03 PMyetitwo
07/22/2025, 2:07 PMyetitwo
07/22/2025, 2:07 PMyetitwo
07/22/2025, 2:07 PMGabriel
07/22/2025, 2:10 PMGabriel
07/22/2025, 2:13 PM*
having a lot of object ids (26% of the volume), which is expected since it is the any user wildcard
next is some metadata userset_object_id that represents 2.5% of relation_tupleGabriel
07/22/2025, 2:14 PMGabriel
07/22/2025, 2:17 PMyetitwo
07/22/2025, 2:26 PMyetitwo
07/22/2025, 2:27 PMyetitwo
07/22/2025, 2:27 PMyetitwo
07/22/2025, 2:27 PMyetitwo
07/22/2025, 2:27 PMGabriel
07/22/2025, 2:28 PMJoey
07/22/2025, 4:05 PMJoey
07/22/2025, 4:05 PMJoey
07/22/2025, 4:06 PMJoey
07/22/2025, 4:06 PMJoey
07/22/2025, 4:06 PMJoey
07/22/2025, 4:07 PMJoey
07/22/2025, 4:07 PMGabriel
07/23/2025, 6:41 AMSELECT
namespace,
object_id,
relation,
userset_namespace,
userset_object_id,
userset_relation,
caveat_name,
caveat_context
FROM
relation_tuple AS OF SYSTEM TIME 1753239450000000000
WHERE
((userset_namespace = $1 AND userset_object_id IN ($2,) AND userset_relation = $3))
AND namespace = $4
AND relation = $5
ORDER BY
userset_namespace, userset_object_id, userset_relation, namespace, object_id, relation
LIMIT
9223372036854775807
With our data distribution and some specific parameters it was using `ix_relation_tuple_by_subject_relation`instead of ix_relation_tuple_by_subject
Gabriel
07/23/2025, 6:41 AMGabriel
07/23/2025, 7:26 AM"could not find valid split key"
, this is what prompted us to introduce the new index with a high cardinality subfield.
@Joey or @yetitwo Are you aware why this split could be failing? It is still happening once we removed the visibility and it is consuming a lot of CPU and network.
According to [CRDB](https://cockroachdb.slack.com/archives/CP4D9LD5F/p1753170168422649), when there are a lot of duplicates in a secondary index, the primary key is stored with it and used for splitting. They claim that the reason behind a key not being capable of splitting would be because the PK is constantly updating. Does this make sense to you? Looking at relation_tuple I cannot see what would be updating, except maybe the timestamp fieldJoey
07/23/2025, 3:36 PMJoey
07/23/2025, 3:36 PMJoey
07/23/2025, 3:36 PMGabriel
07/28/2025, 12:56 PM