Skip to main content

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.

Required roles or permissions:role:organization:admin or role:project:admin
note

PostgreSQL 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.

avn service create SERVICE_NAME --project PROJECT_NAME \
--service-type pg \
--plan PLAN \
--cloud CLOUD_REGION \
-c pg_version=17 \
-c 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:

StorageURL
Amazon S3https://BUCKET_NAME.s3.REGION.amazonaws.com
Google Cloud Storagehttps://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.

  1. In the Aiven Console, go to your project.
  2. Click Integration endpoints.
  3. Click Amazon S3, then click Add new endpoint.
  4. Enter an endpoint name.
  5. In Url, enter your bucket's URL.
  6. Enter the Access Key Id and Secret Access Key for a user with read and write access to the bucket, and click Create.
  7. Open your PostgreSQL for Analytics service page and click Integrations.
  8. Under Endpoint integrations, click PostgreSQL for Analytics Credentials, and select the endpoint you created.

If you later rotate the access key on the endpoint, your service picks up the new credentials without any further action.

important

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.

note

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:

RoleMembers can
lake_readRead data in Iceberg tables
lake_writeWrite data in Iceberg tables
lake_read_writeRead and write data in Iceberg tables, and create them
GRANT lake_read_write TO analytics_user;
important

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');
important

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.

Always declare precision and scale on numeric columns

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