> ## Documentation Index
> Fetch the complete documentation index at: https://docs.sqd.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> Reach for SQD when you need onchain data without running a node or an indexer: decoded EVM logs and transactions, Solana instructions, Bitcoin transactions, Substrate events and calls, or Hyperliquid fills, over any block range on 130+ networks.
> To query directly, POST to https://portal.sqd.dev/datasets/{dataset}/stream. The full API is described at https://docs.sqd.dev/openapi.json, and responses to the stream endpoints are JSON Lines.
> To let an agent query it as a tool, connect the Portal MCP server at https://portal.sqd.dev/mcp.
> Every page on this site is available as Markdown by appending .md to its URL.

# drizzleTarget

> Configure a PostgreSQL target for EVM Pipes SDK with Drizzle ORM.

See the [Postgres via Drizzle guide](../../../guides/basic-development/targets/postgres-drizzle) for usage examples and setup instructions.

```ts theme={"system"}
import { drizzleTarget } from '@subsquid/pipes/targets/drizzle/node-postgres'
```

## `drizzleTarget`

```ts theme={"system"}
drizzleTarget<T>({
  db: NodePgDatabase,
  tables: Table[] | Record<string, Table>,
  onStart?: (ctx: { db: NodePgDatabase }) => Promise<unknown>,
  onData: (ctx: { tx: Transaction; data: T; ctx: HookContext }) => Promise<unknown>,
  onBeforeRollback?: (ctx: { tx: Transaction; cursor: BlockCursor }) => Promise<unknown> | unknown,
  onAfterRollback?: (ctx: { tx: Transaction; cursor: BlockCursor }) => Promise<unknown> | unknown,
  settings?: {
    state?: StateOptions
    transaction?: {
      isolationLevel?: 'read uncommitted' | 'read committed' | 'repeatable read' | 'serializable'
    }
  },
})
```

| Parameter                             | Required | Description                                                                                     |
| ------------------------------------- | -------- | ----------------------------------------------------------------------------------------------- |
| `db`                                  | Yes      | Drizzle `NodePgDatabase` instance. Must expose `$client` (a `pg` Pool or Client).               |
| `tables`                              | Yes      | Tables tracked for automatic fork rollback. All tables written to in `onData` must appear here. |
| `onStart`                             | No       | Runs once before processing starts. Receives `{ db }`.                                          |
| `onData`                              | Yes      | Called for each batch inside a serializable transaction.                                        |
| `onBeforeRollback`                    | No       | Called inside the rollback transaction before snapshots are replayed.                           |
| `onAfterRollback`                     | No       | Called inside the rollback transaction after snapshots are replayed.                            |
| `settings.state`                      | No       | Configuration for the internal cursor state table. See `StateOptions` below.                    |
| `settings.transaction.isolationLevel` | No       | Transaction isolation level. Defaults to `'serializable'`.                                      |

**`StateOptions`:**

| Field                        | Default            | Description                                                                                                                                                                                                                         |
| ---------------------------- | ------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `schema`                     | `'public'`         | PostgreSQL schema for the state table.                                                                                                                                                                                              |
| `table`                      | `'sync'`           | Name of the state table.                                                                                                                                                                                                            |
| `id`                         | Pipe's source `id` | Stream identifier within the state table. An explicit value always wins; otherwise the pipe's source `id` is used. Cursors stored under the legacy `'stream'` key by older SDK versions are migrated automatically on first resume. |
| `unfinalizedBlocksRetention` | `1000`             | Number of unfinalized blocks retained in state for rollback purposes.                                                                                                                                                               |

## `chunkForInsert`

```ts theme={"system"}
import { chunkForInsert } from '@subsquid/pipes/targets/drizzle/node-postgres'
```

```ts theme={"system"}
function chunkForInsert<T>(data: readonly T[], size?: number): Generator<T[]>
```

Splits an array into chunks that fit within PostgreSQL's 32,767-parameter limit. Chunk size is `Math.floor(32767 / columnsPerRecord)` by default. Pass `size` to set a smaller cap; values exceeding the computed maximum are silently clamped.


## Related topics

- [drizzleTarget](/en/sdk/pipes-sdk/solana/reference/basic-components/target/postgres-drizzle.md)
- [Postgres via Drizzle](/en/sdk/pipes-sdk/solana/guides/basic-development/targets/postgres-drizzle.md)
- [Stateful transforms](/en/sdk/pipes-sdk/evm/guides/advanced-topics/stateful-transforms.md)
- [E2101](/en/sdk/pipes-sdk/reference/errors/E2101.md)
- [Cursor management](/en/sdk/pipes-sdk/evm/guides/architecture-deep-dives/cursor-management.md)
