Skip to content
Open
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
b68951e
perf(postgres): implement batch insert in dataWriter (#1252)
gcoinstash-cmd Aug 26, 2026
b57ee95
test(validation): add entity tuple validation regression assertions
gcoinstash-cmd Aug 30, 2026
ff18b70
test(validation): add entity tuple validation regression assertions
gcoinstash-cmd Aug 30, 2026
f241fea
test(validation): add entity tuple validation regression assertions
gcoinstash-cmd Aug 30, 2026
d74b5b2
test(validation): add entity tuple validation regression assertions
gcoinstash-cmd Aug 30, 2026
b7832b9
test(validation): add entity tuple validation regression assertions
gcoinstash-cmd Aug 30, 2026
cf628ec
test(validation): add entity tuple validation regression assertions
gcoinstash-cmd Aug 30, 2026
44bcd7d
test(validation): add entity tuple validation regression assertions
gcoinstash-cmd Aug 30, 2026
63a03c1
test(validation): add entity tuple validation regression assertions
gcoinstash-cmd Aug 31, 2026
21f77de
test(validation): add entity tuple validation regression assertions
gcoinstash-cmd Aug 31, 2026
0af7a9b
test(validation): add entity tuple validation regression assertions
gcoinstash-cmd Aug 31, 2026
98e72a3
test(validation): add entity tuple validation regression assertions
gcoinstash-cmd Aug 31, 2026
de70830
test(validation): add entity tuple validation regression assertions
gcoinstash-cmd Sep 1, 2026
313e71f
test(validation): add entity tuple validation regression assertions
gcoinstash-cmd Sep 1, 2026
8399539
test(validation): add entity tuple validation regression assertions
gcoinstash-cmd Sep 1, 2026
b5641b2
test(validation): add entity tuple validation regression assertions
gcoinstash-cmd Sep 2, 2026
b194b01
test(validation): add entity tuple validation regression assertions
gcoinstash-cmd Sep 2, 2026
aa83538
test(validation): add entity tuple validation regression assertions
gcoinstash-cmd Sep 2, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 72 additions & 19 deletions internal/storage/postgres/data_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,19 +429,48 @@ func (w *DataWriter) runOperation(
}

// Batch operations helper functions
const _insertChunkSize = 500

func (w *DataWriter) batchInsertRelationships(batch *pgx.Batch, xid db.XID8, tenantID string, tupleCollection *database.TupleCollection) error {
titer := tupleCollection.CreateTupleIterator()
for titer.HasNext() {
t := titer.GetNext()
srelation := t.GetSubject().GetRelation()
if srelation == tuple.ELLIPSIS {
srelation = ""
tuples := tupleCollection.GetTuples()
if len(tuples) == 0 {
return nil
}

for i := 0; i < len(tuples); i += _insertChunkSize {
end := i + _insertChunkSize
if end > len(tuples) {
end = len(tuples)
}

insertBuilder := w.database.Builder.Insert(RelationTuplesTable).
Columns("entity_type", "entity_id", "relation", "subject_type", "subject_id", "subject_relation", "created_tx_id", "tenant_id").
Suffix("ON CONFLICT ON CONSTRAINT uq_relation_tuple_not_expired DO NOTHING")

for _, t := range tuples[i:end] {
srelation := t.GetSubject().GetRelation()
if srelation == tuple.ELLIPSIS {
srelation = ""
}
insertBuilder = insertBuilder.Values(
t.GetEntity().GetType(),
t.GetEntity().GetId(),
t.GetRelation(),
t.GetSubject().GetType(),
t.GetSubject().GetId(),
srelation,
xid,
tenantID,
)
}

query, args, err := insertBuilder.ToSql()
if err != nil {
return err
}
batch.Queue(
"INSERT INTO relation_tuples (entity_type, entity_id, relation, subject_type, subject_id, subject_relation, created_tx_id, tenant_id) VALUES ($1, $2, $3, $4, $5, $6, $7, $8) ON CONFLICT ON CONSTRAINT uq_relation_tuple_not_expired DO NOTHING",
t.GetEntity().GetType(), t.GetEntity().GetId(), t.GetRelation(), t.GetSubject().GetType(), t.GetSubject().GetId(), srelation, xid, tenantID,
)
batch.Queue(query, args...)
}

return nil
}

Expand Down Expand Up @@ -486,19 +515,43 @@ func buildDeleteClausesForRelationships(tupleCollection *database.TupleCollectio

// Batch insert attributes
func (w *DataWriter) batchInsertAttributes(batch *pgx.Batch, xid db.XID8, tenantID string, attributeCollection *database.AttributeCollection) error {
aiter := attributeCollection.CreateAttributeIterator()
for aiter.HasNext() {
a := aiter.GetNext()
jsonBytes, err := protojson.Marshal(a.GetValue())
attributes := attributeCollection.GetAttributes()
if len(attributes) == 0 {
return nil
}

for i := 0; i < len(attributes); i += _insertChunkSize {
end := i + _insertChunkSize
if end > len(attributes) {
end = len(attributes)
}

insertBuilder := w.database.Builder.Insert(AttributesTable).
Columns("entity_type", "entity_id", "attribute", "value", "created_tx_id", "tenant_id")

for _, a := range attributes[i:end] {
jsonBytes, err := protojson.Marshal(a.GetValue())
if err != nil {
return err
}
jsonStr := string(jsonBytes)
insertBuilder = insertBuilder.Values(
a.GetEntity().GetType(),
a.GetEntity().GetId(),
a.GetAttribute(),
jsonStr,
xid,
tenantID,
)
}

query, args, err := insertBuilder.ToSql()
if err != nil {
return err
}
jsonStr := string(jsonBytes)
batch.Queue(
"INSERT INTO attributes (entity_type, entity_id, attribute, value, created_tx_id, tenant_id) VALUES ($1, $2, $3, $4, $5, $6)",
a.GetEntity().GetType(), a.GetEntity().GetId(), a.GetAttribute(), jsonStr, xid, tenantID,
)
batch.Queue(query, args...)
}

return nil
}

Expand Down
Loading