Archiving a Ledger Database
Prerequisites
- You are an audit administrator or a role that has the audit administrator permissions.
- The database is running properly, and a series of addition, deletion, and modification operations are performed on the tamper-proof database to ensure that operation records are generated in the ledger for query.
- The storage path audit_directory of audit files has been correctly configured in the database.
Context
Currently, the ledger database provides two archiving interfaces: ledger_hist_archive(text… and ledger_gchain_archive(vo…. Only the audit administrator can invoke the ledger database interfaces.
The interface for archiving the user history table is pg_catalog.ledger_hist_archive. To archive the table, run the following command:
SELECT pg_catalog.ledger_hist_archive(schema_name text,table_name text);
If the archiving is successful, the function returns t. Otherwise, the function returns f.
The interface for archiving the global blockchain table is pg_catalog.ledger_gchain_archive. To archive the table, run the following command:
SELECT pg_catalog.ledger_gchain_archive();
If the archiving is successful, the function returns t. Otherwise, the function returns f.
Procedure
Archive a specified user history table.
openGauss=# SELECT pg_catalog.ledger_hist_archive('ledgernsp', 'usertable');
The command output is as follows:
ledger_hist_archive --------------------- t (1 row)
The user history table is archived as a record:
openGauss=# SELECT * FROM blockchain.ledgernsp_usertable_hist; rec_num | hash_ins | hash_del | pre_hash ---------+------------------+------------------+---------------------------------- 3 | e78e75b00d396899 | 8fcd74a8a6a4b484 | fd61cb772033da297d10c4e658e898d7 (1 row)
The command output indicates that the user history table of the current node is exported successfully.
Export the global blockchain table.
openGauss=# SELECT pg_catalog.ledger_gchain_archive();
The command output is as follows:
ledger_gchain_archive ----------------------- t (1 row)
The global history table will be archived to n (number of user tables) data records by user table:
openGauss=# SELECT * FROM gs_global_chain; blocknum | dbname | username | starttime | relid | relnsp | relname | relhash | globalhash | txcommand ----------+----------+----------+-------------------------------+-------+-----------+-----------+------------------+----------------------------------+----------- 1 | postgres | libc | 2021-05-10 19:59:38.619472+08 | 16388 | ledgernsp | usertable | 57c101076694b415 | be82f98ee68b2bc4e375f69209345406 | Archived. (1 row)
The command output indicates that the global blockchain table of the current node is successfully exported.