Geyser plugin postgres
The solana-geyser-plugin-postgres
crate implements a plugin storing account data to a PostgreSQL database to illustrate how a plugin can be developed to work with Solana validators using the Plugin Framework.
The plugin is configured using the input configuration file. An example configuration file looks like the following:
The host
, user
, and port
control the PostgreSQL configuration information. For more advanced connection options, please use the connection_str
field. Please see Rust Postgres Configuration.
To improve the throughput to the database, the plugin supports connection pooling using multiple threads, each maintaining a connection to the PostgreSQL database. The count of the threads is controlled by the threads
field. A higher thread count usually offers better performance.
To further improve performance when saving large numbers of accounts at startup, the plugin uses bulk inserts. The batch size is controlled by the batch_size
parameter. This can help reduce the round trips to the database.
The panic_on_db_errors
can be used to panic the validator in case of database errors to ensure data consistency.
To connect to the PostgreSQL database via SSL, set use_ssl
to true, and specify the server certificate, the client certificate and the client key files in PEM format using the server_ca
, client_cert
and client_key
fields respectively. For example:
The accounts_selector
can be used to filter the accounts that should be persisted.
For example, one can use the following to persist only the accounts with particular Base58-encoded Pubkeys,
Or use the following to select accounts with certain program owners:
To select all accounts, use the wildcard character (*):
transaction_selector
, controls if and what transactions to store. If this field is missing, none of the transactions are stored.
For example, one can use the following to select only the transactions referencing accounts with particular Base58-encoded Pubkeys,
The mentions
field supports wildcards to select all transaction or all 'vote' transactions. For example, to select all transactions:
To select all vote transactions:
Please follow PostgreSQL Ubuntu Installation on instructions to install the PostgreSQL database server. For example, to install postgresql-14,
Modify the pg_hba.conf as necessary to grant the plugin to access the database. For example, in /etc/postgresql/14/main/pg_hba.conf, the following entry allows nodes with IPs in the CIDR 10.138.0.0/24 to access all databases. The validator runs in a node with an ip in the specified range.
It is recommended to run the database server on a separate node from the validator for better performance.
Configure the Database Performance Parameters
Please refer to the PostgreSQL Server Configuration for configuration details. The referential implementation uses the following configurations for better database performance in the /etc/postgresql/14/main/postgresql.conf which are different from the default postgresql-14 installation.
The sample scripts/postgresql.conf can be used for reference.
Create the Database Instance and the Role
Start the server:
Create the database. For example, the following creates a database named 'solana':
Create the database user. For example, the following creates a regular user named 'solana':
Verify the database is working using psql. For example, assuming the node running PostgreSQL has the ip 10.138.0.9, the following command will land in a shell where SQL commands can be entered:
Use the scripts/create_schema.sql
After this, start the validator with the plugin by using the --geyser-plugin-config
argument mentioned above.
To destroy the database objects, created by create_schema.sql
, use drop_schema.sql. For example,
To capture account historical data, in the configuration file, turn store_account_historical_data
to true.
And ensure the database trigger is created to save data in the audit_table
when records in account
are updated, as shown in create_schema.sql
,
The trigger can be dropped to disable this feature, for example,
Over time, the account_audit can accumulate large amount of data. You may choose to limit that by deleting older historical data.
For example, the following SQL statement can be used to keep up to 1000 of the most recent records for an account:
The following are the tables in the Postgres database
account
Account data
block
Block metadata
slot
Slot metadata
transaction
Transaction data
account_audit
Account historical data
When a validator lacks sufficient computing power, the overhead of saving the account data can cause it to fall behind the network especially when all accounts or a large number of accounts are selected. The node hosting the PostgreSQL database needs to be powerful enough to handle the database loads as well. It has been found using GCP n2-standard-64 machine type for the validator and n2-highmem-32 for the PostgreSQL node is adequate for handling transmitting all accounts while keeping up with the network. In addition, it is best to keep the validator and the PostgreSQL in the same local network to reduce latency. You may need to size the validator and database nodes differently if serving other loads.
Last updated