Product8 min read

ByCostGraph Team

How to split a shared PostgreSQL bill across the teams that use it

Connect a shared PostgreSQL instance, label its databases and roles with their owners, and show each team its share of the bill.

Ask about this postClaudeChatGPTPerplexity

Most teams run fewer PostgreSQL instances than they run applications. Checkout, accounts and the analytics jobs all sit on one shared instance, because one instance is cheaper to run than five. Your cloud bill then shows that instance as one line. Nobody can tell what checkout costs, and the instance's owner ends up paying for everyone.

The usual fix is a spreadsheet: pull database sizes once a quarter, guess at a split, and argue about it.

With CostGraph you can split a shared PostgreSQL instance's bill across the databases and roles that use it, and show each team its own share. CostGraph reads the instance's own usage statistics every hour, so the split follows what each tenant actually stores and runs. This post walks through the setup, from the login you create to the breakdown in Showback.

How the split works

  1. Your cloud bill lists the instance on its own line. Any provider works, as long as the bill names the instance by its resource ID.
  2. CostGraph signs in to the instance with a read-only login. Every hour it reads each database's size, each database's activity, and each role's query time. It reads statistics, not the contents of your tables.
  3. Each day, CostGraph splits the instance's charges. Storage follows each database's size. Compute follows each database's activity.
  4. A labelled database or role carries its share to its owner. You label it in CostGraph or with a comment in PostgreSQL, using the key you already group spend by, such as app or team.
  5. Showback shows each owner's share. The shares add up to the instance's bill, so your totals stay the same.

Activity means active time on PostgreSQL 14 and later, and transactions on older versions. CostGraph's own hourly reads are not counted. Databases your provider runs for its own maintenance, such as rdsadmin on Amazon RDS and cloudsqladmin on Cloud SQL, are left out.

Before you start

You need:

  • The instance's cost in CostGraph, on its own line. Connect the billing data for the provider that runs the instance, and check that the bill lists the instance by its resource ID. Some providers only report cost per project or service by default.
  • The instance's resource ID, exactly as it appears on the bill. On AWS this is the instance's ARN.
  • An owner, admin or billing admin role in CostGraph, to connect the instance.
  • A route from CostGraph to the instance. CostGraph Cloud reaches public addresses only, and verifies the instance's certificate with the SSL mode verify-full or verify-ca. A self-hosted install can reach instances on your private network.

Step 1: create a login for CostGraph

Run this on the instance, in the database CostGraph will sign in to:

CREATE ROLE costgraph LOGIN PASSWORD 'choose-a-password';
GRANT pg_monitor TO costgraph;
CREATE EXTENSION IF NOT EXISTS pg_stat_statements;

pg_monitor lets the login read sizes and activity without reading any table. pg_stat_statements provides each role's query time, which is what splits a shared database by role.

pg_stat_statements must also be listed in the instance's shared_preload_libraries. On a managed instance, turn it on in your provider's settings, for example the parameter group on Amazon RDS. Changing shared_preload_libraries takes effect only after the instance restarts, so plan a restart if the module is not loaded yet. Without it, CostGraph still splits the instance by database, but cannot split a shared database by role.

If the extension lives in a schema other than public, let the login use that schema:

GRANT USAGE ON SCHEMA extensions TO costgraph;

Step 2: connect the instance

Open Settings > Integrations and pick PostgreSQL. Choose the tenant whose bill contains the instance and enter its Instance ID. Then enter the host, port, database and the login from step 1, and choose the SSL mode. If the instance's certificate is signed by a private CA, paste the CA certificate. Select Connect.

CostGraph signs in before it saves anything. If the check fails, the error names the cause: a wrong login, a missing pg_monitor role, a certificate that cannot be verified, or an instance it cannot reach. A warning tells you when pg_stat_statements is missing, not loaded or in a schema the login cannot use, or when the bill has no cost for that instance ID yet.

The warnings stay on the connection under Settings > Integrations > PostgreSQL until you fix them, along with the reason for any failed hourly read. Connect each shared instance separately.

Step 3: label each database or role with its owner

Pick a layout for each database. Most instances mix them:

There are two ways to label.

In CostGraph

Open Cost breakdown and select the Showback tab. For a shared database, group by postgres:role instead to label its roles.

This needs no access to the instance. CostGraph saves the owner as a virtual tag rule, applies it to the whole split straight away, and lets you edit it under Settings > Virtual tags. The rule's preview may show no matches for a database or role, but the owner still applies.

In PostgreSQL

Add a key=value comment to the database:

COMMENT ON DATABASE orders IS 'app=checkout';
COMMENT ON DATABASE acme_prod IS 'customer=acme, team=enterprise';

For a shared database, comment on the role each tenant signs in with:

COMMENT ON ROLE ml_service IS 'app=ml';
COMMENT ON ROLE bi_reporting IS 'app=bi';

Separate several labels with commas. CostGraph reads comments every hour and applies them to the last 35 days of the split. To apply a new comment straight away, select Resync on the connection.

If a database has an owner from CostGraph and a comment for the same key, the owner from CostGraph wins.

Step 4: see each tenant's share

Open Cost breakdown, select the Showback tab, then Database allocation. Under Group by, pick the key from your labels, such as app, to see each tenant's share of your shared instances. Expand a row to see the services its share comes from.

Group by postgres:database to see every database by name, labelled or not, and by postgres:role to see the roles of a shared database. Spend that no label claims shows as Unallocated, so it is easy to see which databases or roles still need an owner.

The Billed tab keeps showing the instance as one line, so the split never changes your totals.

Fix common problems

  • Nothing is split. Check the connection for a warning that the bill has no cost for the instance ID. The bill for the provider that runs the instance must be connected and list the instance under that ID.
  • The instance ID matches several resources. Enter the full resource ID as it appears on the bill. CostGraph does not split a cost it cannot tie to one instance.
  • A shared database is not split by role. The warning on the connection says whether pg_stat_statements is missing, not loaded, or in a schema the login cannot use.

Get started

Database showback uses parts of CostGraph you may already have:

If you want help splitting your shared instances, book a demo and we will walk through it with you.

FAQ

Put your bill to work

Connect a provider, watch your spend attribute itself, ask GraphAI where the money’s going.