# Transactions and Types: vector, halfvec — Vector Databases

Source: https://www.geekswithgeeks.com/en/vector-databases/p-tx

> Use ACID updates and smaller storage types.

## Vectors get database guarantees

Since vectors are ordinary rows, you get **transactions**: update a document's text and its embedding atomically, roll back a failed batch, delete rows and have indexes follow, and take consistent **backups** with the tools you already use (`pg_dump`, base backups, point-in-time recovery). pgvector also offers compact types: **`halfvec`** stores 16-bit floats (half the size of `vector`), and there are **sparse** and **bit** vector types for other uses; check the extension's documentation for the limits in your version, such as the maximum dimensions you can index. Smaller types reduce memory and disk, usually with little recall loss, but measure on your data.

## A rolled-back delete, run

I ran this SQL on PostgreSQL 16 with the pgvector extension, version 0.8.6, in a Docker container. Inside the transaction the tenant `beta` row is gone (4 rows); after `ROLLBACK` all 5 rows are back. The vector column takes part in the transaction like any other.

```sql
BEGIN;
DELETE FROM docs WHERE tenant = 'beta';
SELECT count(*) AS inside_transaction FROM docs;
ROLLBACK;
SELECT count(*) AS after_rollback FROM docs;
```

Output:

```
 inside_transaction 
--------------------
                  4
(1 row)

 after_rollback 
----------------
              5
(1 row)
```

## halfvec is smaller, run

I ran this SQL on PostgreSQL 16 with the pgvector extension, version 0.8.6, in a Docker container. A 4-dimension `vector` value takes 24 bytes in this measurement and `halfvec` takes 16, because 16-bit floats are half the size of 32-bit ones (the fixed header is the same).

```sql
SELECT pg_column_size('[0.1,0.2,0.3,0.4]'::vector(4))  AS vector_bytes,
       pg_column_size('[0.1,0.2,0.3,0.4]'::halfvec(4)) AS halfvec_bytes;
```

Output:

```
 vector_bytes | halfvec_bytes 
--------------+---------------
           24 |            16
(1 row)
```

**Quiz:** What benefit do transactions give vector data?

- [ ] Higher recall automatically
- [ ] Faster embeddings
- [x] Atomic updates of text and embedding, and rollback
- [ ] No need for backups

*Answer:* Atomic updates of text and embedding, and rollback. Row-level ACID semantics apply to the vector column too.
