Copy to/from Parquet in S3, Azure Blob Storage, Google Cloud Storage, http(s) stores, local files or standard inout stream from within PostgreSQL
Copy to/from Parquet in S3, Azure Blob Storage, Google Cloud Storage, http(s) stores, local files or standard inout stream from within PostgreSQL
Copy from/to Parquet files in PostgreSQL!
pg_parquetis a PostgreSQL extension that allows you to read and write Parquet files, which are located inS3,Azure Blob Storage,Google Cloud Storage,http(s) endpointsorfile system, from PostgreSQL viaCOPY TO/FROMcommands. It depends on Apache Arrow project to read and write Parquet files and pgrx project to extend PostgreSQL'sCOPYcommand.
-- Copy a query result into Parquet in S3
COPY (SELECT * FROM table) TO 's3://mybucket/data.parquet' WITH (format 'parquet');
-- Load data from Parquet in S3
COPY table FROM 's3://mybucket/data.parquet' WITH (format 'parquet');After installing Postgres, you need to set up rustup, cargo-pgrx to build the extension.
…There are mainly 3 things that you can do with pg_parquet:
You can use PostgreSQL's COPY command to read and write from/to Parquet files. Below is an example of how to write a PostgreSQL table, with complex types, into a Parquet file and then to read the Parquet file content back into the same table.
…You can use COPY command to read and write Parquet stream from/to standard input and output. Below is an example usage (you have to specify format = parquet):
psql -d pg_parquet -p 28817 -h localhost -c "create table product_example_reconstructed (like product_example);"
CREATE TABLE
psql -d pg_parquet -p 28817 -h localhost -c "copy product_example to stdout (format parquet);" | psql -d pg_parquet -p 28817 -h localhost -c "copy product_example_reconstructed from stdin (format parquet);"
COPY 2You can use COPY command to read and write Parquet stream from/to a program's input and output. Below is an example usage (you have to specify format = parquet):
psql -d pg_parquet -p 28817 -h localhost -c "copy product_example_reconstructed to program 'cat > /tmp/test.parquet' (format parquet);"
COPY 2
psql -d pg_parquet -p 28817 -h localhost -c "copy product_example_reconstructed from program 'cat /tmp/test.parquet' (format parquet);"
COPY 2You can call SELECT * FROM parquet.schema() to discover the schema of the Parquet file at given uri.
…You can call SELECT * FROM parquet.metadata() to discover the detailed metadata of the Parquet file, such as column statistics, at given uri.
……You can call SELECT * FROM parquet.file_metadata() to discover file level metadata of the Parquet file, such as format version, at given uri.
SELECT * FROM parquet.file_metadata('/tmp/product_example.parquet')
uri | created_by | num_rows | num_row_groups | format_version
------------------------------+------------+----------+----------------+----------------
/tmp/product_example.parquet | pg_parquet | 1 | 1 | 1
(1 row)You can call SELECT * FROM parquet.kv_metadata() to query custom key-value metadata of the Parquet file at given uri.
SELECT uri, encode(key, 'escape') as key, encode(value, 'escape') as value FROM parquet.kv_metadata('/tmp/product_example.parquet');
uri | key | value
------------------------------+--------------+---------------------
/tmp/product_example.parquet | ARROW:schema | /////5gIAAAQAAAA ...
(1 row)You can call SELECT * FROM parquet.column_stats() to discover the column statistics of the Parquet file, such as min and max value for the column, at given uri.
…You can call SELECT * FROM parquet.list() to see all uris that matches with the uri pattern.
Uri pattern can resolve ** for directories and * for words in the uri.
COPY (SELECT i FROM generate_series(1, 1000000) i) TO '/tmp/some/test.parquet' with (file_size_bytes '1MB');
COPY 1000000
SELECT * FROM parquet.list('/tmp/some/**/*.parquet');
uri | size
---------------------------------------+---------
/tmp/some/test.parquet/data_4.parquet | 100162
/tmp/some/test.parquet/data_3.parquet | 1486916
/tmp/some/test.parquet/data_2.parquet | 1486916
/tmp/some/test.parquet/data_0.parquet | 1486920
/tmp/some/test.parquet/data_1.parquet | 1486916
(5 rows)
Uri pattern is also supported by COPY FROM for all supported object stores except http(s) endpoints.
COPY (SELECT i FROM generate_series(1, 1000000) i) TO 's3://testbucket/some/test.parquet' with (file_size_bytes '1MB');
COPY 1000000
CREATE TABLE test(a int);
CREATE TABLE
COPY test FROM 's3://testbucket/some/**/*.parquet';
COPY 1000000pg_parquet supports reading and writing Parquet files from/to S3, Azure Blob Storage, http(s) and Google Cloud Storage object stores.
[!NOTE] To be able to write into a object store location, you need to grant
parquet_object_store_writerole to your current postgres user. Similarly, to read from an object store location, you need to grantparquet_object_store_readrole to your current postgres user.
The simplest way to configure object storage is by creating the standard ~/.aws/credentials and ~/.aws/config files:
$ cat ~/.aws/credentials
[default]
aws_access_key_id = AKIAIOSFODNN7EXAMPLE
aws_secret_access_key = wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
$ cat ~/.aws/config
[default]
region = eu-central-1Alternatively, you can use the following environment variables when starting postgres to configure the S3 client:
AWS_ACCESS_KEY_ID: the access key ID of the AWS accountAWS_SECRET_ACCESS_KEY: the secret access key of the AWS accountAWS_SESSION_TOKEN: the session token for the AWS accountAWS_REGION: the default region of the AWS accountAWS_ENDPOINT_URL: the endpointAWS_SHARED_CREDENTIALS_FILE: an alternative location for the credentials file (only via environment variables)AWS_CONFIG_FILE: an alternative location for the config file (only via environment variables)AWS_PROFILE: the name of the profile from the credentials and config file (default profile name is default) (only via environment variables)AWS_ALLOW_HTTP: allows http endpoints (only via environment variables)Config source priority order is shown below:
Supported S3 uri formats are shown below:
Supported authorization methods' priority order is shown below:
The simplest way to configure object storage is by creating the standard ~/.azure/config file:
$ cat ~/.azure/config
[storage]
account = devstoreaccount1
key = Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==Alternatively, you can use the following environment variables when starting postgres to configure the Azure Blob Storage client:
AZURE_STORAGE_ACCOUNT: the storage account name of the Azure BlobAZURE_STORAGE_KEY: the storage key of the Azure BlobAZURE_STORAGE_CONNECTION_STRING: the connection string for the Azure Blob (overrides any other config)AZURE_STORAGE_SAS_TOKEN: the storage SAS token for the Azure BlobAZURE_TENANT_ID: the tenant id for client secret or workload identity auth (only via environment variables)AZURE_CLIENT_ID: the client id for client secret or workload identity auth (only via environment variables)AZURE_FEDERATED_TOKEN_FILE: the file system path to the federated token file to use for workload identity auth (only via environment variables)AZURE_CLIENT_SECRET: the client secret for client secret auth (only via environment variables)AZURE_STORAGE_ENDPOINT: the endpoint (only via environment variables)AZURE_CONFIG_FILE: an alternative location for the config file (only via environment variables)AZURE_ALLOW_HTTP: allows http endpoints (only via environment variables)Config source priority order is shown below:
Supported Azure Blob Storage uri formats are shown below:
Supported authorization methods' priority order is shown below:
Only https uris are supported by default. You can set ALLOW_HTTP environment variable to allow http uris.
The simplest way to configure object storage is by creating a json config file like [~/.config/gcloud/application_default_credentials.json] (can be generated by gcloud auth application-default login):
$ cat ~/.config/gcloud/application_default_credentials.json
{
"gcs_base_url": "http://localhost:4443",
"disable_oauth": true,
"client_email": "",
"private_key_id": "",
"private_key": ""
}Alternatively, you can use the following environment variables when starting postgres to configure the Google Cloud Storage client:
GOOGLE_SERVICE_ACCOUNT_KEY: json serialized service account key (only via environment variables)GOOGLE_SERVICE_ACCOUNT_PATH: an alternative location for the config file (only via environment variables)Supported Google Cloud Storage uri formats are shown below:
pg_parquet supports the following options in the COPY TO command:
format parquet: you need to specify this option to read or write Parquet files which does not end with .parquet[.] extension,file_size_bytes : the total file size per Parquet file. When set, the parquet files, with target size, are created under parent directory (named the same as file name). By default, when not specified, a single file is generated without creating a parent folder. You can specify total bytes without unit like file_size_bytes 2000000 or with unit (KB, MB, or GB) like file_size_bytes '1MB',field_ids : fields ids that are assigned to the fields in Parquet file schema. By default, no field ids are assigned. Pass auto to let pg_parquet generatNo open issues yet, or sync has not completed.