Using procopy

Consider the following step-by-step scenario to load data from a source database into the analytical Postgres Pro AXE DBMS with procopy.

PostgreSQL → Postgres Pro AXE Transfer

Postgres Pro AXE is an analytical DBMS that stores data in Parquet files and makes it available through the SQL interface. To load data into Postgres Pro AXE, specify axe in the destination.driver_name configuration parameter. In this case, procopy creates Parquet files from the source data, transfers them to the storage specified in the configuration, and registers them in the Postgres Pro AXE metadata catalog.

If you set destination.driver_name to parquet instead of axe, data is saved as Parquet files without being registered in the Postgres Pro AXE metadata catalog.

procopy cannot create Postgres Pro AXE analytical tables automatically. Before starting the transfer, create the storage and the analytical tables in Postgres Pro AXE, as described in the subsections below. After the transfer is complete, the analytical tables are queried directly.

  1. Prepare Postgres Pro AXE

    Create the Postgres Pro AXE storage and analytical tables. The exact sequence depends on whether you use a local directory or an S3 bucket as the storage for Parquet files.

    1. Prepare Postgres Pro AXE for the S3 storage

      Create an S3 bucket for storing Parquet files. Then, connect to the Postgres Pro AXE database and run the following queries:

      CREATE EXTENSION pgpro_axe;
      
      CREATE ROLE axe_admin WITH LOGIN PASSWORD '_password_';
      GRANT ALL ON SCHEMA axe TO axe_admin;
      ALTER SYSTEM SET axe.postgres_role TO 'axe_admin';
      
      -- Restart the server, then configure the metadata catalog:
      
      SELECT axe.create_catalog_secret(
          'localhost', '5432', current_database(), 'axe_admin', '_password_'
      );
      
      SELECT axe.create_simple_secret(
          type      := 'S3',
          key_id    := 'access_key_ID_example',
          secret    := 'secret',
          url_style := 'path',
          endpoint  := 'localhost:9000',
          use_ssl   := 'false'
      );
      
      SELECT axe.create_storage('axe_local_storage', 's3://axe/axe/');
      SELECT axe.create_folder('axe', 'axe_local_storage', 'in');

      The pgpro_axe extension lets you manage the metadata catalog. The axe.create_simple_secret stored procedure stores the credentials for accessing the S3 bucket, and axe.create_storage registers the S3 storage that will store Parquet files. The axe.create_folder stored procedure creates the axe shared directory inside the storage. The in directory path must correspond to the destination.folder_path value that you set in the configuration file.

    2. Prepare Postgres Pro AXE for the local storage

      Create a local directory for storing Parquet files on the host where procopy and Postgres Pro AXE are deployed. Then, connect to the Postgres Pro AXE database and run the following queries:

      CREATE EXTENSION pgpro_axe;
      
      CREATE ROLE axe_admin WITH LOGIN PASSWORD '_password_';
      GRANT ALL ON SCHEMA axe TO axe_admin;
      ALTER SYSTEM SET axe.postgres_role TO 'axe_admin';
      
      -- Restart the server, then configure the metadata catalog:
      
      SELECT axe.create_catalog_secret(
          'localhost', '5432', current_database(), 'axe_admin', '_password_'
      );
      
      SELECT axe.create_storage('axe_local_storage', 'file:///var/lib/postgresql/axe/');
      SELECT axe.create_folder('axe', 'axe_local_storage', 'in');

      If procopy is deployed on a separate server from Postgres Pro AXE, specify the path on the procopy server for the directory of the Postgres Pro AXE shared directory. For example, through NFS.

      After the storage and the shared directory are set up, create the analytical table based on a heap table:

      CREATE TABLE main.olap_products (
      id integer PRIMARY KEY,
      name text NOT NULL,
      price numeric(10,2) NOT NULL,
      stock integer DEFAULT 0
      ) USING axe WITH (storage = 'axe_local_storage');

      The CREATE TABLE …​ USING axe statement creates the olap_products analytical table based on the public.products heap table, without loading data (WITH NO DATA). Analytical tables are logical objects that represent Parquet files in the metadata catalog. The table is created in the main schema, which matches the default schema used by procopy for Postgres Pro AXE tables, and its name must match the destination_table value that you set in the configuration file.

  2. Create the source table

    In the source database, create the table from which the data will be transferred:

    CREATE TABLE public.products (
        id    integer PRIMARY KEY,
        name  text NOT NULL,
        price numeric(10,2) NOT NULL,
        stock integer DEFAULT 0
    );

    Insert data into the source table:

    INSERT INTO public.products VALUES (1, 'Widget',  9.99, 100);
    INSERT INTO public.products VALUES (2, 'Gadget', 19.99,  50);
    INSERT INTO public.products VALUES (3, 'Gizmo',  29.99,  25);
  3. Prepare the configuration file

    Generate the configuration file:

    ./procopy config generate > progate_axe.yaml

    Edit the configuration file so that it looks as follows. Specify axe as the destination driver:

    version: 1
    source:
        database: progate
        driver_name: postgresql
        host: localhost
        max_conn: 50
        options:
            - sslmode=disable
        password: ""
        port: 5431
        username: progate
    destination:
        driver_name: axe
        host: localhost
        port: 5430
        database: axedb
        username: progate
        password: ""
        max_conn: 50
        path: axe
        folder_name: axe
        folder_path: in
        parquet_connection:
            local_path: /var/lib/postgresql/axe
    tasks:
        - id: tab1
          table:
            source_table: public.products
            destination_table: main.olap_products

    The destination.path parameter defines the path to the Postgres Pro AXE files inside the bucket or in the local file system. The destination.folder_name parameter defines the name of the Postgres Pro AXE shared directory, and destination.folder_path defines the directory path relative to destination.path. The destination.parquet_connection parameter defines where procopy writes Parquet files: to the local directory (local_path) or to an S3 bucket.

    If you use an S3 bucket to store the Parquet files, replace the destination.parquet_connection section with the following one:

        parquet_connection:
            s3:
                bucket: axe
                endpoint: http://localhost:9000
                access_key: access_key_ID_example
                secret_key: secret
                tmp_path: /var/lib/procopy/tmp

    For the S3 storage, the destination.path and destination.folder_path values form the path to the files inside the bucket, and destination.parquet_connection.s3.tmp_path defines the directory where procopy temporarily stores the files before uploading them to the bucket.

  4. Run the data load

    Run the command:

    ./bin/procopy load -f ./bin/progate_axe.yaml

    procopy reads data from the source table, creates Parquet files, transfers them to the specified storage, and registers them in the Postgres Pro AXE metadata catalog.

  5. Check the destination

    After the transfer is complete, the data in the analytical table becomes available for reading through the SQL interface if procopy calls the axe.add_files stored procedure. Query the analytical table:

    SELECT * FROM main.olap_products ORDER BY id;
     id |  name  | price | stock
    ----+--------+-------+-------
      1 | Widget |  9.99 |   100
      2 | Gadget | 19.99 |    50
      3 | Gizmo  | 29.99 |    25
    (3 rows)