---
title: "Maintaining a Running Balance"
description: "How to use CDC Sink patches to incrementally update an account balance as transaction rows arrive from PostgreSQL"
url: "https://docs.ravendb.net/7.2/server/ongoing-tasks/cdc-sink/examples/example-running-balance"
version: "7.2"
language: "csharp"
---

# Maintaining a Running Balance

> Client language: C#.
>
> RavenDB publishes an official agent skill for coding assistants at https://docs.ravendb.net/skills/ravendb/SKILL.md (also installable with `npx skills add ravendb/skills`). The skill describes itself as: "Read this before writing any RavenDB query, client call or /databases/ request. Covers server setup, document modelling, the Node.js, .NET and Python clients, RQL, indexes, vector search and AI agents. Code written from recall tends to run and return the wrong rows, because RQL resembles SQL and the clients take field names as plain strings."

> **Note**
>
> - This example shows how to use CDC Sink patches to **maintain a computed aggregate** on a RavenDB document  
>   as individual event rows arrive from PostgreSQL.
>
> - For detailed instructions on creating a CDC Sink task with the Client API or Studio,  
>   see [Create a CDC Sink task](https://docs.ravendb.net/7.2/server/ongoing-tasks/cdc-sink/manage-cdc-sink-tasks/create-task.md).
>
> - In this article:
>   - [Source schema](#source-schema)
>   - [Goal](#goal)
>   - [REPLICA IDENTITY setup](#replica-identity-setup)
>   - [Task configuration](#task-configuration)
>     - [Via the Client API](#via-the-client-api)
>     - [Via Studio](#via-studio)
>   - [Resulting documents](#resulting-documents)
>   - [Handling deletes](#handling-deletes)

## Source schema {#source-schema}

An accounts table and a transactions table:

```sql
CREATE TABLE accounts (
    account_id SERIAL PRIMARY KEY,
    owner      TEXT NOT NULL,
    currency   TEXT NOT NULL DEFAULT 'USD'
);

CREATE TABLE transactions (
    txn_id     SERIAL PRIMARY KEY,
    account_id INT NOT NULL REFERENCES accounts(account_id),
    amount     NUMERIC(12,2) NOT NULL,
    type       TEXT NOT NULL CHECK (type IN ('credit', 'debit')),
    created_at TIMESTAMPTZ DEFAULT now()
);
```

## Goal {#goal}

Store each account as a RavenDB document with a `Balance` field that reflects the running total of all transactions.  
Transaction rows are embedded as an array for history, and `Balance` is maintained using patch logic.

## REPLICA IDENTITY setup {#replica-identity-setup}

- The `transactions` table uses `txn_id` as its primary key.  
  The join column `account_id` is not part of that primary key.

- By default, PostgreSQL `REPLICA IDENTITY` sends only primary-key columns.  
  Without `REPLICA IDENTITY FULL`, DELETE events for `transactions` rows would not include `account_id`,  
  and CDC Sink could not route the deleted transaction to the correct account document.

- For this example to handle deleted transactions, CDC Sink must receive the `account_id` value in PostgreSQL DELETE events.
  Set `REPLICA IDENTITY FULL` on the `transactions` table:

  ```sql
  ALTER TABLE transactions REPLICA IDENTITY FULL;
  ```

  Learn more in [REPLICA IDENTITY](https://docs.ravendb.net/7.2/server/ongoing-tasks/cdc-sink/source-database-setup/postgres/replica-identity.md).

## Task configuration {#task-configuration}

### Via the Client API

Define the task with the client API:

```csharp
var config = new CdcSinkConfiguration
{
    Name = "Accounts-CDC-Sink",
    ConnectionStringName = "ConnectionStringToPostgreSQL",
    Tables =
    [
        new CdcSinkTableConfig
        {
            CollectionName = "Accounts",
            SourceTableSchema = "public",
            SourceTableName = "accounts",
            PrimaryKeyColumns = ["account_id"],
            Columns =
            [
                new CdcColumnMapping() { Column = "account_id", Name = "AccountId" },
                new CdcColumnMapping() { Column = "owner",      Name = "Owner" },
                new CdcColumnMapping() { Column = "currency",   Name = "Currency" },
            ],
            EmbeddedTables =
            [
                new CdcSinkEmbeddedTableConfig
                {
                    SourceTableSchema = "public",
                    SourceTableName = "transactions",
                    PropertyName = "Transactions",
                    Type = CdcSinkRelationType.Array,
                    JoinColumns = ["account_id"],
                    PrimaryKeyColumns = ["txn_id"],
                    Columns =
                    [
                        new CdcColumnMapping() { Column = "txn_id",     Name = "TxnId" },
                        new CdcColumnMapping() { Column = "amount",     Name = "Amount" },
                        new CdcColumnMapping() { Column = "type",       Name = "Type" },
                        new CdcColumnMapping() { Column = "created_at", Name = "CreatedAt" },
                    ],
                    // Runs after a transaction is inserted or updated;
                    // adjusts Balance by replacing the previous contribution with the new one.
                    Patch = """
                        const oldAmount = $old?.Amount || 0;
                        const newAmount = $row.amount || 0;
                        const sign = $row.type === 'credit' ? 1 : -1;
                        const oldSign = $old?.Type === 'credit' ? 1 : -1;
                        this.Balance = (this.Balance || 0)
                            - (oldSign * oldAmount)
                            + (sign * newAmount);
                        """,
                    // Runs after a transaction is deleted;
                    // uses $old to subtract the removed transaction's last stored contribution.    
                    OnDelete = new CdcSinkOnDeleteConfig
                    {
                        Patch = """
                            const deletedAmount = $old?.Amount || 0;
                            const sign = $old?.Type === 'credit' ? 1 : -1;
                            this.Balance = (this.Balance || 0) - (sign * deletedAmount);
                            """
                    }
                }
            ]
        }
    ]
};

await store.Maintenance.SendAsync(new AddCdcSinkOperation(config));
```

---

### Via Studio

In Studio, configure how the `accounts` source table maps to the `Accounts` target collection,  
then add `transactions` as an embedded table for each account.

---

First, configure the root `accounts` table:

![Configure tables - accounts table](https://docs.ravendb.net/assets/images/example-running-balance-0-7ad8fb4ba5c0e97b54cba860a51d0ef2.png)

1. **Source table → target collection**  
   The `accounts` table maps to the `Accounts` collection.  
   This is a root table, so each row becomes its own document.
2. **Primary key columns**  
   `account_id` is used to build the document ID for each account.
3. **Field mapping**  
   Maps each source column to its target property  
   (`account_id` → `AccountId`, `owner` → `Owner`, `currency` → `Currency`).

---

Then add the embedded `transactions` table:

![Configure tables - transactions table](https://docs.ravendb.net/assets/images/example-running-balance-1-c9a6f020b525cf49e55a6506a3d98400.png)

1. **Embedded table**  
   `transactions` is configured as an embedded table inside the `accounts` document, not as its own collection.
2. **Relation type**  
   Each account stores its transactions as a JSON array (`Type: Array` in the config).
3. **Primary key columns**  
   `txn_id` identifies each array element, so later updates and deletes patch the correct transaction.
4. **Join columns**  
   `account_id` links each transaction row to its parent account document.  
   In this example, PostgreSQL DELETE events need this value to route deleted transactions correctly.
5. **Field mapping**  
   Maps each source column to its target property, matching the `Columns` list in the config.

---

The embedded `transactions` table and its balance patches (`Patch` / `OnDelete.Patch`) are defined under **Advanced settings**.

![Configure patches](https://docs.ravendb.net/assets/images/example-running-balance-2-c652eb1f52f98fd27195a74c59e83568.png)

1. **Case sensitive keys**  
   Controls whether primary-key and map-key matching is case sensitive.  
   It only affects string keys, so it has no effect in this example, where the key is the numeric `txn_id`.
2. **Patch**  
   The INSERT/UPDATE patch that maintains `Balance`;  
   runs on the parent account document after a transaction is added or updated.
3. **Skip deletion**  
   Maps to `IgnoreDeletes`.  
   Leave this off for this example.  
   **Off** = CDC Sink removes the deleted transaction item from the array, and the delete patch updates `Balance`.  
   **On** = CDC Sink skips the automatic removal. If a delete patch is configured, the patch still runs.
4. **Delete patch**  
   The `OnDelete.Patch` script reverses the deleted transaction's contribution to `Balance`.

---

Alternatively, toggle **Raw config** and paste the equivalent JSON:

![Use raw config](https://docs.ravendb.net/assets/images/example-running-balance-3-25654dfc2b9e378567b5dcae4f59f643.png)

```json
{
    "Name": "Accounts-CDC-Sink",
    "ConnectionStringName": "ConnectionStringToPostgreSQL",
    "Tables": [
        {
            "CollectionName": "Accounts",
            "SourceTableSchema": "public",
            "SourceTableName": "accounts",
            "PrimaryKeyColumns": ["account_id"],
            "Columns": [
                { "Column": "account_id", "Name": "AccountId" },
                { "Column": "owner",      "Name": "Owner" },
                { "Column": "currency",   "Name": "Currency" }
            ],
            "EmbeddedTables": [
                {
                    "SourceTableSchema": "public",
                    "SourceTableName": "transactions",
                    "PropertyName": "Transactions",
                    "Type": "Array",
                    "JoinColumns": ["account_id"],
                    "PrimaryKeyColumns": ["txn_id"],
                    "Columns": [
                        { "Column": "txn_id",     "Name": "TxnId" },
                        { "Column": "amount",     "Name": "Amount" },
                        { "Column": "type",       "Name": "Type" },
                        { "Column": "created_at", "Name": "CreatedAt" }
                    ],
                    "Patch": "const oldAmount = $old?.Amount || 0; const newAmount = $row.amount || 0; const sign = $row.type === 'credit' ? 1 : -1; const oldSign = $old?.Type === 'credit' ? 1 : -1; this.Balance = (this.Balance || 0) - (oldSign * oldAmount) + (sign * newAmount);",
                    "OnDelete": {
                        "Patch": "const deletedAmount = $old?.Amount || 0; const sign = $old?.Type === 'credit' ? 1 : -1; this.Balance = (this.Balance || 0) - (sign * deletedAmount);"
                    }
                }
            ]
        }
    ]
}
```

## Resulting documents {#resulting-documents}

After three transactions (credit 100, debit 30, credit 50):

```json
{
    "AccountId": 1,
    "Owner": "Alice",
    "Currency": "USD",
    "Balance": 120.00,
    "Transactions": [
        { "TxnId": 1, "Amount": 100.00, "Type": "credit", "CreatedAt": "..." },
        { "TxnId": 2, "Amount": 30.00,  "Type": "debit",  "CreatedAt": "..." },
        { "TxnId": 3, "Amount": 50.00,  "Type": "credit", "CreatedAt": "..." }
    ],
    "@metadata": { "@collection": "Accounts" }
}
```

## Handling deletes {#handling-deletes}

The patches keep `Balance` correct by applying only the change caused by each transaction event:

- **When a transaction row is inserted**:  
  There is no previous embedded item, so the patch adds the new transaction's contribution to `Balance`.

- **When a transaction row is updated**:  
  The patch subtracts the previous contribution from `$old`, then adds the new contribution from `$row`.

- **When a transaction row is deleted**:  
  There is no new row to add.  
  The `OnDelete.Patch` subtracts the deleted transaction's last stored contribution from `$old`.

Without `OnDelete.Patch`, deleting a transaction row from SQL would remove it from the `Transactions` array  
but leave `Balance` stale.

This also keeps `Balance` correct if the same change is re-applied after failover.  
Learn more in [Failover and consistency](https://docs.ravendb.net/7.2/server/ongoing-tasks/cdc-sink/failover-and-consistency.md).

## See also

- [Simple Migration](https://docs.ravendb.net/7.2/server/ongoing-tasks/cdc-sink/examples/example-simple-migration.md)
- [Denormalization](https://docs.ravendb.net/7.2/server/ongoing-tasks/cdc-sink/examples/example-denormalization.md)
- [Complex Nesting](https://docs.ravendb.net/7.2/server/ongoing-tasks/cdc-sink/examples/example-complex-nesting.md)
- [Patching](https://docs.ravendb.net/7.2/server/ongoing-tasks/cdc-sink/patching.md)
- [Handling Deletes](https://docs.ravendb.net/7.2/server/ongoing-tasks/cdc-sink/handling-deletes.md)
- [Failover and Consistency](https://docs.ravendb.net/7.2/server/ongoing-tasks/cdc-sink/failover-and-consistency.md)
