# Segmented databases

You can distribute partitioned tables across multiple storage devices to:

- Give them more space
- Support parallelization

The root of a segmented database contains only the `sym` list and a file `par.txt`, which unifies the partitions of a database, presenting them as a single database for querying.

## `par.txt`

File `par.txt` defines the top-level partitioning of the database into directories. Each row of `par.txt` is a directory path. Each such directory follows the usual partitioning, typically by date. The directories must not be empty.

In the following layout, `par.txt` and `sym` sit on the first device, and each remaining device holds whole date partitions, each with the same table directories:
```txt
DISK 0             DISK 1                     DISK 2
db                 db                        db
├── par.txt        ├── 2020.10.03            ├── 2020.10.04
└── sym            │   ├── quotes            │   ├── quotes
                   │   │   ├── price         │   │   ├── price
                   │   │   ├── sym           │   │   ├── sym
                   │   │   └── time          │   │   └── time
                   │   └── trades            │   └── trades
                   │       ├── price         │       ├── price
                   │       ├── sym           │       ├── sym
                   │       ├── time          │       ├── time
                   │       └── vol           │       └── vol
                   ├── 2020.10.05            ├── 2020.10.06
                   │   ├── quotes            │   ├── quotes
                   ..                        ..
```

The matching `par.txt`:
```txt
/1/db
/2/db
```

> **Danger — Do not end the paths with a directory delimiter**
>
>
>   Write `/1/db`, not `/1/db/`. Depending on the file system, the trailing slash can leave q unable to resolve the segment.
>
## Work with segmented databases

You create and modify segmented databases in exactly the same way as [partitioned tables](partition.md). The only addition is the `par.txt` file, which is a plain text file you can maintain by hand.

### Find the segment of a partition

[`.Q.pd`](../../ref/dotq.md#pd-modified-partition-locations) lists the directory that holds each partition, conformant to the partition values in [`.Q.pv`](../../ref/dotq.md#pv-modified-partition-values). In a segmented database, it shows which segment holds each partition. For example, with a `par.txt` that lists the segments `../s1` and `../s2`:
```q
q).Q.pv!.Q.pd
2026.04.10| :./../s1
2026.04.11| :./../s2
2026.04.12| :./../s1
```

## Considerations

- Partition data correctly: the data for a date must be in the partition for that date. [`.Q.par`](../../ref/dotq.md#par-get-expected-partition-location) returns the segment where q expects a partition to be.
- Segments apply to both reads and writes.
- The directories listed in `par.txt` can contain only database subdirectories. Any other file or directory in them causes an error.
- The same partition name can appear in more than one segment. For example, you can split the symbols for a date, with A–M in `/1/db` and N–Z in `/2/db`. Aggregations give correct results as long as the data is split, not duplicated.

## Using symlinks

Security-related options such as [`reval`](../../ref/eval.md#reval) or the command-line option [`-u 1`](../../ref/cmdline.md#-u-usr-pwd) restrict access to within the current directory. That restriction can prevent users from reaching a segmented database when `par.txt` references partitions outside the current directory.

To provide access, use symbolic links (symlinks). Create one per segment:
```bash
ln -s /db1 db1$
ln -s /db2 db2$
```

The database root then holds the links alongside `par.txt` and `sym`:
```txt
total 16
lrwxr-xr-x  1 user  kx   6 18 Sep 12:30 db1$ -> /db1
lrwxr-xr-x  1 user  kx   6 18 Sep 12:30 db2$ -> /db2
-rw-r--r--  1 user  kx  10 18 Sep 12:30 par.txt
-rw-r--r--  1 user  kx  48 18 Sep 12:30 sym
```

and `par.txt` names the links rather than the absolute paths:
```txt
db1$
db2$
```

A trailing `$` in the directory name makes KDB-X skip the symbolic links themselves and use the directories they point to.

## Next steps

- [Loading a database](loading-db.md)
- [Compression](file-compression.md)
