Skip to Content
DatabasePostgreSQL on Zenifra

How Zenifra PostgreSQL works

This page shows how Zenifra managed PostgreSQL is configured: the workload it is tuned for, how memory and connections are split in each plan, how data is protected and what happens when the database has more than one instance. To create and connect, see Database and Connect your application to the database.

Summary

ItemHow it works
WorkloadOLTP: short transactions and many small concurrent reads and writes
Versions15, 16, 17 and 18 (default 18), created on the latest minor version
EngineCommunity PostgreSQL, no fork
Data page (block size)8 KiB (8,192 bytes), also in the WAL
Memory25% of the plan's RAM for the database cache (shared_buffers)
Memory per operation (work_mem)limited per plan: 4 MB up to db-premium and 16 MB on db-enterprise
Connectionslimit per plan and per instance, from 15 to 598
WritesCOMMIT only returns after the data is on the primary's disk; with the Write to 2 instances option, also on a replica
Encryptiondata and backups encrypted at rest; TLS 1.3 in transit
Userapp, owner of the app database, no superuser
Instances1 to 6; with 2 or more, automatic failover and a read URI on the replicas
Query statisticspg_stat_statements on in every database

Built for OLTP

Zenifra PostgreSQL is tuned for OLTP, the typical application workload: sign-ups, orders, sessions, job queues, APIs. That means many short transactions, each reading or writing a few rows through indexes, with many connections at the same time.

The settings follow that profile:

  • a quarter of the memory goes to the database cache, and the rest serves the connections and the operating system cache;
  • the working memory of each operation (work_mem) is small, so many connections fit in the plan without running out of memory;
  • every committed transaction is already on disk.

Heavy reports, aggregations over millions of rows and event analytics run better on a columnar database. For that, use Analytics with ClickHouse and keep PostgreSQL for the application's transactions.

8 KiB data pages

PostgreSQL stores tables and indexes in 8 KiB pages (8,192 bytes), the community default. The WAL also uses 8 KiB blocks. In practice:

  • a row must fit in a page; large values (text, JSON, bytea) above about 2 KB are compressed and stored separately automatically (TOAST);
  • extensions, tools and dumps made for standard PostgreSQL work without changes;
  • pg_dump and pg_restore from another provider with the default block size work as is.

To check it on your database:

SHOW block_size;  -- 8192

Memory per plan

The values below are set when the database is created and applied again on every plan change. They apply to each instance.

PlanRAMshared_bufferswork_memmaintenance_work_memeffective_cache_size
db-starter and db-free256 MB64 MB4 MB16 MB192 MB
db-basic1 GB256 MB4 MB64 MB768 MB
db-premium2 GB512 MB4 MB128 MB1,536 MB
db-enterprise8 GB2 GB16 MB512 MB6 GB
  • work_mem is limited on purpose: each sort or join of each connection can use up to this value, so a high limit with many connections would exhaust the plan's memory. The rule is 1/512 of the RAM, with a minimum of 4 MB and a maximum of 32 MB. A query that goes over it uses temporary files on disk and gets slower. If a specific query needs more, raise it only for the session or the transaction: SET LOCAL work_mem = '32MB';.
  • maintenance_work_mem is used by CREATE INDEX, VACUUM and ALTER TABLE.
  • WAL: the maximum WAL size between checkpoints is a quarter of the disk, up to 1 GB, so the write log does not take the space of the data on small disks.

Server parameters are managed by Zenifra and follow the plan. Settings you can change per session or transaction with SET (such as work_mem, statement_timeout and lock_timeout) are up to you.

Connections

Each instance accepts a maximum number of connections from your application, depending on the plan:

PlanConnections per instance
db-free15
db-starter15
db-basic72
db-premium147
db-enterprise598
  • The limit exists because each PostgreSQL connection is a process with its own memory. It is sized so that all connections fit in the plan's memory together with the cache.
  • A few extra connections are reserved for the platform (administration and metrics). They are not counted above and keep the database monitored and operable even when every application connection is in use.
  • Above the limit, a new connection is refused with remaining connection slots are reserved.
  • Use a connection pool in the driver or ORM, with a maximum size equal to or below the limit, adding up every instance of your application. For example: on db-basic, 3 application instances with a pool of 20 use 60 of the 72 connections.
  • Orphaned connections are closed automatically. If the network drops or the application process dies without closing the connection, the database notices in about 2 minutes and frees the slot.
  • With 2 or more instances, each replica has its own limit. Queries sent through the read-only URI use the replicas' connections, not the primary's.

To see how many connections are open:

SELECT count(*) FROM pg_stat_activity WHERE usename = 'app';

Writes and durability

  • Commit on disk: when COMMIT returns, the transaction is already on the primary instance's disk and survives a restart.
  • Write to 2 instances: with 2 or more instances, you can require each COMMIT to also wait for a replica, for every write or only in the transactions that need it. See Write to 2 instances.
  • Persistent disk: data lives on persistent storage. With 1 instance, the database disk is replicated in 3 copies on different servers, so a server failure does not lose the data.
  • Automatic backups included in the plan, 1 to 3 per day, kept for 3 to 30 days. See Automatic backups.

Encryption

  • At rest: PostgreSQL database disks and backups are encrypted with AES-XTS. Data is never stored in the clear, not even in the copies.
  • In transit: connections use TLS 1.3, the minimum accepted version. The console URI already includes sslmode=require. TLS is required by default and only stops being required if you turn it off in the TLS card (see Require TLS on PostgreSQL).
  • Password: login uses SCRAM-SHA-256, and the password generated by Zenifra is shown only on creation or after a renewal.

User and permissions

  • The application user is app, owner of the app database. It creates and changes tables, indexes, views, functions and schemas in its own database.
  • There is no superuser access. Server administration (parameters, replication, backups and versions) is handled by Zenifra.
  • app has the pg_read_all_stats role, which lets it see statistics and every query in pg_stat_activity and pg_stat_statements.
  • pg_stat_statements comes enabled and records up to 10,000 distinct queries, including the ones inside functions. To find the slowest queries:
SELECT calls, round(mean_exec_time::numeric, 1) AS avg_ms, query
FROM pg_stat_statements
ORDER BY mean_exec_time DESC
LIMIT 10;
  • Replication connections from outside Zenifra are not accepted. To bring data from another provider, use pg_dump and pg_restore.

I/O performance

  • PostgreSQL 16 or newer: read-ahead with up to 16 concurrent operations (effective_io_concurrency and maintenance_io_concurrency).
  • PostgreSQL 17 or newer: sequential reads combined into blocks of up to 128 kB.
  • PostgreSQL 18: asynchronous I/O on (io_method = worker), which fetches disk reads ahead while the query processes the data already read.

With 2 or more instances

With 2 to 6 instances, the database has a primary instance and replicas:

PrimaryReplicas
Acceptswrites and readsreads only
Addressconsole host and port (main URI)console read-only URI
Address on the private network (private connections)<project>.<organization-id>.zenifra.localro.<project>.<organization-id>.zenifra.local
Connectionsplan limitplan limit on each replica

How replication works

  • Every change written on the primary is continuously sent to the replicas through the WAL (streaming replication). In normal use, a replica is a fraction of a second behind the primary.
  • Default: the primary confirms COMMIT as soon as it writes to its own disk, without waiting for the replicas. It is the fastest mode, and writes do not get slower when you add instances.
  • With the Write to 2 instances option: each COMMIT is only confirmed after a replica has also received the transaction. See Write to 2 instances.
  • Read what you just wrote from the primary. A query through the read-only URI right after a write may not see the change yet, even with Write to 2 instances: the replica confirms it received the transaction, and it becomes visible there a moment later. Use the read-only URI for reports, listings and searches that tolerate this small delay.
  • On plans with metrics (db-premium and db-enterprise), each replica's lag shows in the Metrics tab.

Write to 2 instances

The option is off by default. Turn it on in the Write to 2 instances card, on the database edit page, or through the API, with PATCH /v1/project/:id/database/synchronous-writes.

  • On: every COMMIT waits for a replica to confirm it received the transaction. The committed transaction is on 2 instances and, while a replica is available, a primary failure does not lose a committed write.
  • Cost: each transaction that waits gets slower by the round trip to the replica. Reads do not change.
  • No replica available: if no replica is available, writes continue on the primary only, without blocking the application. Waiting resumes when a replica comes back.
  • With 1 instance: the option has no effect, because there is no replica. In the console, the switch is available from 2 instances.
  • No restart: the change applies to the next transactions, without restarting the database. It requires the database to be running and the database.exposure.update permission.

Per transaction

You can also choose the guarantee in each transaction, with SET LOCAL synchronous_commit. The setting applies only to the current transaction:

  • Option off: ask for the guarantee in the transactions that need it, such as a payment, with SET LOCAL synchronous_commit = on.
  • Option on: skip the wait in the transactions that do not need it, such as an access log, with SET LOCAL synchronous_commit = local.
BEGIN;
SET LOCAL synchronous_commit = on;
INSERT INTO payments (order_id, amount) VALUES (42, 199.90);
UPDATE orders SET status = 'paid' WHERE id = 42;
COMMIT; -- only returns after a replica received the transaction

To apply it to every transaction on a connection, use SET synchronous_commit = on (or local), without LOCAL.

Where queries go

  • The main URI always points to the primary, including after a failover: the address stays the same and leads to the new primary.
  • The read-only URI spreads connections across the replicas. It does not reach the primary.
  • A write sent through the read-only URI fails with cannot execute INSERT in a read-only transaction.
  • Many ORMs accept two connections, one for writes and one for reads (for example, read replicas in Prisma, DATABASES with a router in Django and read/write in Laravel). Set the write connection to the main URI and the read connection to the read-only URI.

Failover

  • If the primary fails, the most up-to-date replica is promoted to new primary automatically, with no action on your side.
  • Connections open to the old primary drop. Configure the application to reconnect and retry idempotent operations.
  • With Write to 2 instances off, a transaction committed at the very last moment before the failure may not have reached the promoted replica. With it on, or with SET LOCAL synchronous_commit = on in the transaction, the committed transaction was already on a replica.
  • Instances always run on different servers, close to each other on the network, so a single server failure does not take down the primary and the replicas together.

Disk and billing with several instances

  • Each instance has its own disk, of the chosen size. On hourly billing, storage is charged per GB on each instance.
  • Each instance is charged at the plan price. For example, 3 db-basic instances cost 3 times the plan price, plus the storage of all 3.
  • Adding instances creates new replicas from a copy of the primary. The primary keeps serving during the copy.
  • Removing instances removes replicas. When going back to 1 instance, the primary moves once to an instance with a replicated disk, and open connections drop at that moment.
  • The number of instances can change on hourly billing, from 1 to 6. On monthly and yearly, it stays fixed for the contract. See Database configuration.

Next steps

Last updated on

On this page