graph-node installation grows beyond what a single Postgres instance can handle, you can scale the system horizontally by adding more Postgres instances. This is called sharding, and each Postgres instance is called a shard.
Overview
The resultinggraph-node system uses all Postgres instances together, essentially forming a distributed database. Sharding relies heavily on the fact that in almost all cases:
- Traffic for a single subgraph can be handled by a single Postgres instance
- Load can be distributed by storing different subgraphs in different shards
Sharding requires using a configuration file rather than environment variables. It cannot be configured through CLI options alone.
The Primary Shard
In a sharded setup, one shard is special and is called the primary. The primary is used to store:- System-wide metadata
- Mapping of subgraph names to IPFS hashes
- Directory of all subgraphs and the shards in which each is stored
- List of configured chains
- Metadata that rarely changes
Frequently changing metadata (such as subgraph head pointers) is stored in the individual shards, not the primary.
Setting Up Sharding
Configuration File Setup
Add additional[store.<name>] entries to your configuration file:
Prerequisites: Inter-Shard Communication
Sharding uses thepostgres_fdw extension for inter-shard communication. Graph Node automatically sets up foreign servers and foreign tables.
Initialization
When a new shard is added to the configuration file:- Graph Node initializes the database schema during startup
- Foreign data wrappers are automatically configured
- Metadata synchronization begins
Verifying Inter-Shard Connectivity
After schema initialization, manually verify connectivity:The query result doesn’t matter - success means connectivity works. Failures indicate network or authentication issues.
Metadata Synchronization
With multiple shards, Graph Node periodically copies metadata from the primary to all other shards:- Copied metadata is needed to respond to queries
- Each query needs the primary to find which shard stores the subgraph’s data
- Metadata copies enable queries when the primary is down
Sharding Strategies
There are many ways to split data across shards. One recommended setup:Small Primary
Minimal primary shard storing only system metadata
Low-Traffic Shards
Multiple shards for low-traffic subgraphs with many subgraphs per shard
High-Traffic Shards
One or few shards for high-traffic subgraphs with few subgraphs per shard
Block Cache Shards
Dedicated shards storing only blockchain block caches
Strategy: By Traffic Level
Strategy: By Blockchain Network
Store different networks in different shards:Strategy: Dedicated Block Cache Shards
Separate block cache storage from subgraph data:Copying and Moving Between Shards
Besides deployment rules for new subgraphs, you can copy and move existing subgraphs between shards.Creating a Copy
Start copying a subgraph from one shard to another:A deployment is identified by its IPFS hash. Multiple copies of the same deployment can exist across shards, but only one copy per shard. Only one copy is marked as
active for query responses.Copy Behavior
By default,graphman copy create:
- Copies source subgraph data up to the copy initiation point
- Starts indexing independently from the source
- Both source and destination continue indexing
Copy with Activation
Automatically activate the copy when it catches up:- Copies data from source
- Indexes independently until caught up to chain head
- Marks copy as
activewhen synced - Routes queries to the new copy
Copy with Replacement
Replace the source with the copy:- Copies data from source
- Indexes independently until caught up
- Marks copy as
active - Marks source as
unused - Source is deleted ~8 hours after copy syncs (default reaper configuration)
Monitoring Copy Progress
Check copy operation status:- Copy phase (copying data, building indexes, counting entities)
- Progress percentage
- Estimated completion time
Listing Copy Operations
The number of active copy operations is limited to 5 per source/destination shard pair to limit load on the shards.
Copy Process Details
During copying:- Namespace Creation: Creates temporary
sgdNNNnamespace in destination matching source’s identifier - Table Mapping: Maps all tables from source into destination shard via foreign data wrapper
- Data Transfer: Copies data in batches
- Index Building: Rebuilds indexes on destination (can be slow)
- Entity Counting: Counts all entities to update
entity_count(can be very slow with minimal output) - Cleanup: Automatically deletes temporary namespace when complete
Deleting Inactive Copies
Make non-active copies eligible for deletion:- Unassign the copy from all indexing nodes
- The unused deployment reaper will delete it automatically (~8 hours by default)
Namespaces
Sharding creates Postgres schemas (namespaces) for inter-shard data access:primary_public: Maps important tables from primary into each shardshard_<name>_subgraphs: Maps important tables from each shard into every other shard
Rebuilding Mappings
If foreign data wrapper mappings become corrupted or out of sync:The mapping setup code is in
ForeignServer::map_primary and ForeignServer::map_metadata in the connection_pool.rs file.Best Practices
Start with Single Database
Start with Single Database
Begin with a single database. Add sharding only when you hit resource limits. The original database becomes the primary shard, and existing data can remain there.
Plan Network Topology
Plan Network Topology
Ensure all shards can communicate before configuring sharding. Test network connectivity and authentication between all shard pairs.
Use Deployment Rules
Use Deployment Rules
Set up deployment rules to automatically route new subgraphs to appropriate shards. This is simpler than manually moving subgraphs later.
Monitor Copy Operations
Monitor Copy Operations
When copying subgraphs between shards, actively monitor progress. Copy operations can fail silently or take much longer than expected.
Separate Block Caches
Separate Block Caches
Consider storing blockchain block caches in dedicated shards separate from subgraph data. This isolates block ingestion load.
Balance Shard Load
Balance Shard Load
Use the
shards array (instead of single shard) in deployment rules to automatically distribute subgraphs to the least-loaded shard.Keep Primary Small
Keep Primary Small
The primary shard should primarily store metadata. Route most subgraph data to other shards.
Test Before Production
Test Before Production
Test sharding setup in a non-production environment first. Verify inter-shard connectivity and deployment placement.
Removing a Shard
When a shard is no longer needed:- Ensure no references: Verify no deployment is stored in the shard and no chain is stored in it
- Remove from config: Delete the shard declaration from the configuration file
- Restart nodes: Restart all Graph Node instances with the updated configuration
Removing a shard leaves behind:
- Foreign tables in
shard_<name>_subgraphson other shards - User mapping and foreign server definitions
DROP commands in psql if desired.Troubleshooting
Copy Operation Stuck
Copy Operation Stuck
If a copy operation appears stuck:Long pauses during entity counting or index building are normal.
Foreign Data Wrapper Errors
Foreign Data Wrapper Errors
If you see FDW-related errors:Check network connectivity:
Shard Connection Issues
Shard Connection Issues
Deployment Placed in Wrong Shard
Deployment Placed in Wrong Shard
Test deployment placement:Review your deployment rules - rules are evaluated in order, first match wins.
Complete Example
Production Sharding Configuration
Production Sharding Configuration

