gsql/ Accumulators
Last Updated: October 20, 2018Understanding GSQL's unique variable types for high-performance parallel graph computation.
Accumulators
Accumulators are TigerGraph's "secret sauce." They are special, thread-safe variables that "accumulate" data during graph traversal.
1. Types of Accumulators
Global Accumulators (@@)
Shared across the entire query. Used to track global metrics (e.g., total count, max value in the whole graph).
gsqlterminalSumAccum<INT> @@total_population;
Local/Vertex-Attached Accumulators (@)
Attached to every vertex in the graph for the duration of the query. Think of them as "temporary attributes" used for calculations.
gsqlterminalSumAccum<FLOAT> @rank;
2. The Accumulation Operator (+=)
Unlike standard assignment (=), accumulators use += to handle concurrent updates from multiple threads without locks.
SumAccum<INT>: Adds the new value to the current sum.MaxAccum<INT>: Updates the value only if the new value is larger.ListAccum<INT>: Appends the new value to a list.OrAccum: Performs a logical OR.
3. Scalar Accumulators
| Type | Behavior |
|---|---|
SumAccum | Computes running sum or string concatenation. |
MaxAccum / MinAccum | Keeps track of the largest or smallest value. |
AvgAccum | Automatically tracks sum and count to provide the average. |
AndAccum / OrAccum | Tracks collective boolean state. |
4. Collection Accumulators
| Type | Behavior |
|---|---|
ListAccum | An ordered list of elements. |
SetAccum | A collection of unique elements. |
MapAccum | Stores Key -> Value pairs. |
HeapAccum | A sorted collection with a fixed size (top-K). |
GroupByAccum | Complex grouping for multi-dimensional aggregation. |
5. Why use Accumulators?
In a distributed system, hundreds of threads might try to update the same vertex at once.
- Parallelism: Accumulators allow threads to work independently.
- Performance: They avoid the "locking" overhead of traditional databases.
- Graph Algorithms: Most advanced algorithms (PageRank, Shortest Path) rely heavily on
@accumulators to propagate values across hops.
gsqlterminal# Example: Counting neighbors for every vertex V = SELECT s FROM Start:s -(Link:e)-> :t ACCUM s.@neighbor_count += 1;