Updated on 2025-05-29 GMT+08:00

Archiving a Ledger Database

Prerequisites

  • You are an audit administrator or a role that has the AUDITADMIN permission.
  • 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 APIs: ledger_hist_archive(text, text) and ledger_gchain_archive(text, text). Only audit administrators can call the ledger database APIs.
  • The pg_catalog.ledger_hist_archive API archives the user history table data of the current DN. The operation is as follows:
    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 and the cause of failure.

  • The pg_catalog.ledger_gchain_archive API archives the global historical table data of the current CN. The operation is as follows:

    SELECT pg_catalog.ledger_gchain_archive();

    If the archiving is successful, the function returns t. Otherwise, the function returns f and the cause of failure.

Procedure

  1. Run the EXECUTE DIRECT statement to archive data on a DN.

    1
    gaussdb=# EXECUTE DIRECT ON (datanode1) 'select pg_catalog.ledger_hist_archive(''ledgernsp'', ''usertable'');';
    
    The query result is as follows:
     ledger_hist_archive
    ---------------------
     t
    (1 row)
    The user history table is archived as a record:
    gaussdb=# EXECUTE DIRECT ON (datanode1) '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 datanode1 node is exported successfully.

  2. Connect to the CN to export the global blockchain table.

    1
    gaussdb=# SELECT pg_catalog.ledger_gchain_archive();
    

    The query result 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:

    gaussdb=# SELECT * FROM gs_global_chain;
     blocknum |  dbname  | username |           starttime           | relid |  relnsp   |  relname  |     relhash      |            globalhash            | txcommand
    ----------+----------+----------+-------------------------------+-------+-----------+-----------+------------------+----------------------------------+-----------
            1 | testdb | 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 CN is successfully exported.