To accelerate analytical queries and optimize performance for hybrid transactional and analytical processing (HTAP) workloads, you can enable the columnar engine on an AlloyDB for PostgreSQL instance. You can also configure the initial memory size for the column store and adjust memory and storage cache settings to match your workload requirements.

<br />

To use the columnar engine in AlloyDB Omni,
see [Enable the columnar engine in AlloyDB Omni](https://docs.cloud.google.com/alloydb/omni/containers/current/docs/columnar-engine/configure.md.txt).

Each of these actions involves changing the value of a database flag on
one of your AlloyDB instances. For more information about
setting database flags, see [Configure an instance's database
flags](https://docs.cloud.google.com/alloydb/docs/instance-configure-database-flags).


For a conceptual overview of the AlloyDB columnar engine, see
[About the AlloyDB columnar engine](https://docs.cloud.google.com/alloydb/docs/columnar-engine/about.md.txt).

## Enable the columnar engine

To use columnar engine on an instance, set the instance's
[`google_columnar_engine.enabled`](https://docs.cloud.google.com/alloydb/docs/reference/columnar-engine-flags.md.txt#enabled)
flag to `on`.
After you set this flag, the instance automatically restarts.

<br />

To enable [direct Apache Iceberg access](https://docs.cloud.google.com/alloydb/docs/access-iceberg-data-from-alloydb.md.txt)
for querying Iceberg tables, you must also set the
[`google_columnar_engine.enable_iceberg_reads`](https://docs.cloud.google.com/alloydb/docs/reference/columnar-engine-flags.md.txt#enable_iceberg_reads)
flag to `on`.

For more information about setting database flags in
AlloyDB, see [Configure an instance's database flags](https://docs.cloud.google.com/alloydb/docs/instance-configure-database-flags.md.txt).

## Configure the size of the column store

While the columnar engine is enabled on an instance,
AlloyDB allocates a portion of the instance's memory to
store its columnar data. Dedicating high-speed RAM to your column store
ensures that AlloyDB can access the columnar data as
rapidly as possible.

Memory and storage cache together represent the overall capacity of the columnar engine.

### Configure memory for the columnar engine

If the column store becomes larger than the allocated portion of your instance's memory, AlloyDB automatically uses the instance's underlying cache layer to store the additional columnar data.

<br />

In AlloyDB, you can load relations, materialized views, and vector indexes into the columnar engine to accelerate query performance.

By default, AlloyDB allocates 30% of the instance's memory to the column store. For a 1-vCPU instance, the default allocation is 10%. AlloyDB automatically adjusts the total allocation if you resize the instance. You can modify the instance's memory to a recommended maximum of 50%, although 70% is allowed. For a 1-vCPU instance, the maximum allowed is 20%. Regardless of the instance size, the minimum allocation is 128 MB.
To find the recommended
columnar engine memory size for your instance, see
[Recommend column store memory size](https://docs.cloud.google.com/alloydb/docs/columnar-engine/manage-content-recommendations.md.txt#recommend-populate).


You can also set the allocation to a fixed and specific size using the
[`google_columnar_engine.memory_size_in_mb`](https://docs.cloud.google.com/alloydb/docs/reference/columnar-engine-flags.md.txt#memory-size-in-mb) flag.

To have AlloyDB return to the default allocation,
[remove the flag from the instance](https://docs.cloud.google.com/alloydb/docs/instance-configure-database-flags.md.txt).

For more information on setting or deleting flags, see [Configure an
instance's database
flags](https://docs.cloud.google.com/alloydb/docs/instance-configure-database-flags).


### Configure the loading of vector indexes into the columnar engine

The columnar engine accelerates vector search when HNSW or ScaNN vector indexes are populated into it. Columnar engine acceleration for HNSW indexes is generally available (GA), while acceleration for ScaNN indexes is in [Preview](https://cloud.google.com/products#product-launch-stages).

To enable the columnar index cache, set the [`google_columnar_engine.enable_index_caching`](https://docs.cloud.google.com/alloydb/docs/reference/columnar-engine-flags.md.txt#enable_index_caching) flag to `on`.

To disable this feature, set the flag to `off`.

### Configure storage cache for the columnar engine

You must size the `google_columnar_engine.storage_cache_size` relative to the
`google_columnar_engine.memory_size_in_mb` setting because the columnar engine
memory is used for in-memory metadata for the storage cache. The recommended
sizing ratio is approximately 100 to 1000 times the memory size. For example,
with a `google_columnar_engine.memory_size_in_mb` of 1024 (1 GB), you must set
`google_columnar_engine.storage_cache_size` between 100 GB and 1000 GB.

The columnar engine's storage cache is provisioned according to the instance tier.

> [!NOTE]
> **Note:** 1 vCPU instance tiers don't have SSDs and can't use storage cache for the columnar engine. Their columnar engine storage cache size is 0.

| Instance tier | Default columnar engine cache size |||
| Instance tier | N2 || C4A |
| Instance tier | Primary | Read pool | Primary or read pool |
|---|---|---|---|
| 1 vCPU | N/A | N/A | 0 |
| 2 vCPU | 18.75 GB | 37.5 GB | 18.75 GB |
| 4 vCPU | 18.75 GB | 37.5 GB | 18.75 GB |
| 8 vCPU | 18.75 GB | 37.5 GB | 37.5 GB |
| 16 vCPU | 37.5 GB | 37.5 GB | 75 GB |
| 32 vCPU | 75 GB | 75 GB | 112.5 GB |
| 48 vCPU | N/A | N/A | 187.5 GB |
| 64 vCPU | 150 GB | 150 GB | 262.5 GB |
| 72 vCPU | N/A | N/A | 300 GB |
| 96 vCPU | 300 GB | 300 GB | N/A |
| 128 vCPU | 450 GB | 450 GB | N/A |

The `google_columnar_engine.storage_cache_size` flag lets you adjust the columnar engine storage cache, with the limitations of the minimum and maximum values listed in the following table:

| Instance tier | Minimum columnar engine cache size | Maximum columnar engine cache size |||
| Instance tier | Minimum columnar engine cache size | N2 || C4A |
| Instance tier | Minimum columnar engine cache size | Primary | Read pool | Primary or read pool |
|---|---|---|---|---|
| 1 vCPU | N/A | N/A | N/A | 0 |
| 2 vCPU | 0 | 187.5 GB | 375 GB | 187.5 GB |
| 4 vCPU | 0 | 187.5 GB | 375 GB | 187.5 GB |
| 8 vCPU | 0 | 187.5 GB | 375 GB | 375 GB |
| 16 vCPU | 0 | 375 GB | 375 GB | 750 GB |
| 32 vCPU | 0 | 750 GB | 750 GB | 1125 GB |
| 48 vCPU | 0 | N/A | N/A | 1875 GB |
| 64 vCPU | 0 | 1500 GB | 1500 GB | 2625 GB |
| 72 vCPU | 0 | N/A | N/A | 3000 GB |
| 96 vCPU | 0 | 3000 GB | 3000 GB | N/A |
| 128 vCPU | 0 | 4500 GB | 4500 GB | N/A |

## Monitor storage cache data for the columnar engine

To view and monitor data about the storage cache of the columnar engine, you
can use the following commands:

| Command | Description |
|---|---|
| `SELECT google_columnar_engine_storage_cache_used();` | Returns the size of the used columnar engine storage cache. |
| `SELECT google_columnar_engine_storage_cache_available();` | Returns the size of the unused columnar engine storage cache. |
| `SHOW google_columnar_engine.storage_cache_size;` | Returns the total amount of the configured size of storage cache for the columnar engine. |

## Enable vectorized join

The columnar engine has a *vectorized join* feature that can improve
the performance of joins by applying vectorized
processing to qualifying queries.

After you enable the vectorized join, the AlloyDB query
planner has the option to apply the vectorized join operator instead of
the standard PostgreSQL hash join operator. The planner makes this
decision by comparing the cost of executing the query using either
method.

To enable vectorized join on an instance, set the instance's
[`google_columnar_engine.enable_vectorized_join`](https://docs.cloud.google.com/alloydb/docs/reference/columnar-engine-flags.md.txt#enable-vectorized-join)
flag to `on`.
For more information about setting database flags, see [Configure an
instance's database
flags](https://docs.cloud.google.com/alloydb/docs/instance-configure-database-flags).

## Manually refresh your columnar engine

By default, when you enable the columnar engine, it is set to automatically refresh the column store
in the background.

> [!NOTE]
> **Note:** In [preview](https://cloud.google.com/products#product-launch-stages), ScaNN indexes aren't automatically refreshed in the columnar engine.

For workloads with a high rate of data modifications such as frequent `INSERT`, `UPDATE`, or `DELETE` operations, you may manually refresh the columnar store after these changes to keep the index up-to-date.

To manually refresh the columnar engine, run the following SQL query:

```sql
SELECT google_columnar_engine_refresh('TABLE_NAME');
```

Replace `TABLE_NAME` with the name of the table or the materialized view you
want to manually refresh.

## Disable the columnar engine

To disable the columnar engine on an instance, set the `google_columnar_engine.enabled`
flag to `off`.
For more information about setting database flags in AlloyDB, see [Configure an instance's database flags](https://docs.cloud.google.com/alloydb/docs/instance-configure-database-flags.md.txt).

<br />

After you set this flag, the instance automatically restarts.


## Troubleshoot the columnar engine

If columns don't populate in the columnar engine, then one of the following situations
might be true:

- The columns you want to add include an unsupported data type.

- The requirements of the columnar engine
  aren't met.

To find the cause of this issue, try the following:

- Review your instance's
  [audit logs](https://docs.cloud.google.com/alloydb/docs/audit-logging.md.txt#service_name).

- [Confirm](https://docs.cloud.google.com/alloydb/docs/columnar-engine/monitor-tune.md.txt#relations-view)
  that the tables or materialized views in your query are in the columnar engine.

- Verify the usage of the columnar engine
  using the [`EXPLAIN`](https://docs.cloud.google.com/alloydb/docs/columnar-engine/monitor-tune.md.txt)
  statement.

## What's next

- See the [full list of columnar engine database
  flags](https://docs.cloud.google.com/alloydb/docs/reference/columnar-engine-flags).

- Learn about
  [auto-columnarization](https://docs.cloud.google.com/alloydb/docs/columnar-engine/manage-content-recommendations.md.txt).

- Learn how to [accelerate analytical queries using the AlloyDB columnar engine](https://www.cloudskillsboost.google/paths/22/course_templates/642/labs/501234).