gsql/ Accumulators
Last Updated: October 20, 2018

Understanding 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).

gsqlterminal
SumAccum<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.

gsqlterminal
SumAccum<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

TypeBehavior
SumAccumComputes running sum or string concatenation.
MaxAccum / MinAccumKeeps track of the largest or smallest value.
AvgAccumAutomatically tracks sum and count to provide the average.
AndAccum / OrAccumTracks collective boolean state.

4. Collection Accumulators

TypeBehavior
ListAccumAn ordered list of elements.
SetAccumA collection of unique elements.
MapAccumStores Key -> Value pairs.
HeapAccumA sorted collection with a fixed size (top-K).
GroupByAccumComplex 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.

  1. Parallelism: Accumulators allow threads to work independently.
  2. Performance: They avoid the "locking" overhead of traditional databases.
  3. 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;