Enable PostgreSQL for Analytics on Aiven for PostgreSQL® Early availability
Connect your PostgreSQL for Analytics service to your storage bucket, create Iceberg tables, and query them with standard SQL.
role:organization:admin or role:project:adminPostgreSQL for Analytics is in early availability. Request access to use it.
Prerequisites
- PostgreSQL for Analytics enabled for your project. Request access, and Aiven enables it for you.
- Access to the Aiven API or the Aiven CLI, which you need to create the service.
- Optionally, to connect the bucket with Terraform, Aiven Provider for Terraform 4.64.0 or later.
- A storage bucket that you own, and an access key with permission to read from and write to it. For the best performance and to avoid data transfer charges, keep the bucket in the same region as your service.
Create your service
Create a PostgreSQL for Analytics service with the API or the CLI. Choose a plan with room for both engines: PostgreSQL and DuckDB, the analytical query engine that runs alongside it, each get roughly half of the service's memory, and DuckDB also caches data on the service's disk.
Set variant to pg_lake on every service you create.
- CLI
- API
avn service create SERVICE_NAME --project PROJECT_NAME \
--service-type pg \
--plan PLAN \
--cloud CLOUD_REGION \
-c pg_version=17 \
-c variant=pg_lake
Use the ServiceCreate endpoint:
curl --request POST \
--url https://api.aiven.io/v1/project/PROJECT_NAME/service \
--header 'Authorization: Bearer BEARER_TOKEN' \
--header 'content-type: application/json' \
--data '{
"service_name": "SERVICE_NAME",
"service_type": "pg",
"plan": "PLAN",
"cloud": "CLOUD_REGION",
"user_config": {
"pg_version": "17",
"variant": "pg_lake"
}
}'
If PostgreSQL for Analytics isn't enabled for your project, the request fails with
Setting variant is not allowed.
Connect your storage bucket
Create an Amazon S3 integration endpoint in your project, then connect that endpoint to your service with a PostgreSQL for Analytics Credentials integration. Use this endpoint type for Google Cloud Storage too: DuckDB connects to Google Cloud Storage through its Amazon S3-compatible API. The endpoint holds the bucket URL and the access credentials, and the integration passes them to DuckDB. The integration's source is your PostgreSQL for Analytics service, and its destination is the endpoint.
Set the endpoint's URL to your bucket:
| Storage | URL |
|---|---|
| Amazon S3 | https://BUCKET_NAME.s3.REGION.amazonaws.com |
| Google Cloud Storage | https://storage.googleapis.com/BUCKET_NAME |
Add a path after the bucket to scope the credentials to a prefix, such as
https://storage.googleapis.com/BUCKET_NAME/analytics. For Google Cloud Storage, use an
HMAC key as the access key and secret.
- Console
- CLI
- API
- Terraform
- In the Aiven Console, go to your project.
- Click Integration endpoints.
- Click Amazon S3, then click Add new endpoint.
- Enter an endpoint name.
- In Url, enter your bucket's URL.
- Enter the Access Key Id and Secret Access Key for a user with read and write access to the bucket, and click Create.
- Open your PostgreSQL for Analytics service page and click Integrations.
- Under Endpoint integrations, click PostgreSQL for Analytics Credentials, and select the endpoint you created.
-
Create the endpoint:
avn service integration-endpoint-create --project PROJECT_NAME \--endpoint-name analytics-storage \--endpoint-type external_aws_s3 \-c url="https://BUCKET_NAME.s3.REGION.amazonaws.com" \-c access_key_id=AWS_ACCESS_KEY_ID \-c secret_access_key=AWS_SECRET_ACCESS_KEY -
Find the endpoint's ID:
avn service integration-endpoint-list --project PROJECT_NAME -
Connect the endpoint to your service:
avn service integration-create --project PROJECT_NAME \--integration-type pg_for_analytics_credentials \--source-service SERVICE_NAME \--dest-endpoint-id ENDPOINT_ID
-
Create the endpoint with ServiceIntegrationEndpointCreate:
curl --request POST \--url https://api.aiven.io/v1/project/PROJECT_NAME/integration_endpoint \--header 'Authorization: Bearer BEARER_TOKEN' \--header 'content-type: application/json' \--data '{"endpoint_name": "analytics-storage","endpoint_type": "external_aws_s3","user_config": {"url": "https://BUCKET_NAME.s3.REGION.amazonaws.com","access_key_id": "AWS_ACCESS_KEY_ID","secret_access_key": "AWS_SECRET_ACCESS_KEY"}}'Copy the
endpoint_idfrom the response. -
Connect the endpoint to your service with ServiceIntegrationCreate:
curl --request POST \--url https://api.aiven.io/v1/project/PROJECT_NAME/integration \--header 'Authorization: Bearer BEARER_TOKEN' \--header 'content-type: application/json' \--data '{"integration_type": "pg_for_analytics_credentials","source_service": "SERVICE_NAME","dest_endpoint_id": "ENDPOINT_ID"}'
Replace the placeholders with your project name, bearer token, bucket URL, access credentials, service name, and endpoint ID.
Requires Aiven Provider for Terraform 4.64.0 or later. Terraform connects the bucket to a service you created with the API or the CLI, so reference that service by name.
resource "aiven_service_integration_endpoint" "analytics_storage" {
project = "PROJECT_NAME"
endpoint_name = "analytics-storage"
endpoint_type = "external_aws_s3"
external_aws_s3_user_config {
url = "https://BUCKET_NAME.s3.REGION.amazonaws.com"
access_key_id = var.access_key_id
secret_access_key = var.secret_access_key
}
}
resource "aiven_service_integration" "analytics_credentials" {
project = "PROJECT_NAME"
integration_type = "pg_for_analytics_credentials"
source_service_name = "SERVICE_NAME"
destination_endpoint_id = aiven_service_integration_endpoint.analytics_storage.id
}
If you later rotate the access key on the endpoint, your service picks up the new credentials without any further action.
Use a bucket dedicated to this one service, and keep the integration in place once you have Iceberg tables. Queries expect those tables in the bucket the integration points to, so they fail if it changes.
Aiven doesn't validate the URL or the credentials when you create the integration. If either is wrong, the service still starts and PostgreSQL works normally, but any statement against an Iceberg table fails later:
ERROR: HTTP Error: HTTP GET error reading 's3://BUCKET_NAME/orders_analytics' in region
'REGION' (HTTP 403 Forbidden)
AccessDenied: Access Denied
Verify the endpoint's Url, Access Key Id, and Secret Access Key.
Enable the extension
Connect as the owner of the database where you plan to keep Iceberg tables, and run:
CREATE EXTENSION pg_lake CASCADE;
Your primary service user, avnadmin, owns the databases it creates.
Grant access to Iceberg tables
Your service comes with three group roles. As avnadmin, grant membership to the
database users who need it:
| Role | Members can |
|---|---|
lake_read | Read data in Iceberg tables |
lake_write | Write data in Iceberg tables |
lake_read_write | Read and write data in Iceberg tables, and create them |
GRANT lake_read_write TO analytics_user;
lake_read_write is the only role with access to the pg_lake_iceberg foreign server, so
anyone who creates Iceberg tables needs it. Granting lake_read and lake_write together
isn't equivalent.
Create an Iceberg table
Create a table that stores its data as Iceberg files in your bucket. Set location
to a path in your bucket:
CREATE TABLE orders_analytics (
order_id bigint,
customer_id bigint,
order_total numeric(18,4),
created_at timestamptz
) USING iceberg WITH (location = 's3://BUCKET_NAME/orders_analytics');
Set location on every Iceberg table, and keep it inside the bucket and path of the
endpoint you connected. Aiven scopes the storage credentials to that path, so a table
outside it fails with an access error.
Iceberg can't represent a numeric column with no declared precision and scale as an
exact decimal. If you create an Iceberg table from a numeric column without one,
PostgreSQL for Analytics converts it to double precision, an inexact binary
floating-point type, and only reports this with a low-severity NOTICE, which many
clients don't show by default:
NOTICE: column "order_total" has type that cannot be stored as an Iceberg
decimal, converting to double precision
HINT: Use numeric(P,S) with precision <= 38 to preserve exact decimal
semantics.
A single converted value usually looks correct, but summing many of them accumulates rounding error, so totals can silently stop matching the source data. This is particularly risky for financial, billing, or audit workloads.
Declare numeric(P,S) with a precision of 38 or less on every numeric column. If you
can't change the source table, cast the column in your SELECT, for example
total::numeric(18,4).
To fail instead of converting, turn the conversion off for your session:
SET pg_lake_iceberg.unsupported_numeric_as_double = off;
Load data into Iceberg tables
You can write into an Iceberg table directly, which keeps a single copy of the data in your bucket:
INSERT INTO orders_analytics VALUES (1001, 42, 99.9500, now());
You can also copy rows from a regular PostgreSQL table in the same service. This leaves you with two copies, and the Iceberg copy is only as current as your last load:
INSERT INTO orders_analytics SELECT * FROM orders;
Load in batches rather than row by row. Each write creates files in your bucket, so many small writes leave many small files and slow your queries down.
To load new data on a schedule, run your INSERT ... SELECT from
pg_cron. Define what counts as new
for your data, such as a timestamp or an ID watermark, and select on that.
Query Iceberg tables
Query Iceberg tables with standard SQL, from the same connection you use for your regular tables:
SELECT customer_id, sum(order_total)
FROM orders_analytics
GROUP BY customer_id
ORDER BY sum(order_total) DESC
LIMIT 10;
You can also join an Iceberg table with a regular PostgreSQL table in the same query.
Maintain Iceberg tables
An autovacuum worker maintains Iceberg tables for you. It compacts data files, expires old snapshots, and removes files that no snapshot references any more, much as autovacuum does for regular PostgreSQL tables.
To compact a table immediately, for example after a large batch load, run VACUUM
yourself:
VACUUM orders_analytics;
Related pages