# 3.4 Case Folding

_Peios / Reference / loregd / Storage_

> Names are case-insensitive but case-preserving, implemented by storing both the written form and a folded one.

Key names and value names are case-insensitive but case-preserving.
[*fold.key-and-value-names-are-case-insensitive-but-case-preserving]
loregd implements this by storing both forms: the name as written, and a
folded form alongside it in a `_folded` column.
[*fold.both-the-written-and-folded-forms-are-stored]

The folded form is computed once, when the name is written, and it is the
folded column that appears in every `WHERE` clause and every primary key.
[*fold.the-folded-form-is-computed-once-and-is-what-every-comparison-uses]
Lookups are therefore plain binary comparisons
[*fold.lookups-are-plain-binary-comparisons]
:

```sql
SELECT * FROM path_entries
WHERE parent_guid = ? AND child_name_folded = ?
```

This is why no custom SQLite collation is registered anywhere in loregd
the case-insensitivity has already happened by the time SQLite sees the
query.
[*fold.no-custom-sqlite-collation-is-registered]

The canonical name is what comes back in responses, so callers see the
case they originally supplied while storage compares the folded form.
[*fold.responses-return-the-case-the-caller-supplied]

Hive names are folded too, though not stored
[*fold.hive-names-are-folded-but-never-stored]
: routing a request to a
hive and rejecting duplicate hive declarations (§2.1) both compare folded
names.
[*fold.hive-routing-and-duplicate-rejection-compare-folded-names]

Layer names are **not** folded. They are compared as binary, so layer
names are case-sensitive.
[*fold.layer-names-are-not-folded]

## 3.4.1 How the folded form is computed

The folded form is Unicode Simple Case Folding: each codepoint is
replaced by its status C or S mapping from `CaseFolding.txt`, Unicode
16.0.0, or left alone when it has none.
[*fold.folding-is-unicode-simple-case-folding-status-c-and-s]
 This is the
rule used for case-insensitive LCS names. loregd and the kernel use
byte-identical folded forms because a
name the two fold differently is a key that exists for one side and not
the other.
[*fold.loregd-must-fold-byte-identically-to-the-kernel]

Identity is guaranteed by construction rather than by care. loregd does
not derive the fold from the Go standard library, whose case tables
implement lowercasing (a different operation) at a different Unicode
version.
[*fold.the-fold-is-not-derived-from-the-go-standard-library]
Its table is generated by the kernel's own generator,
`peios/generate-casefold-table.py` in the kernel repository, from the same
`CaseFolding.txt`
that produces the kernel's table; the generated Go file records the source
version and checksum, and the lookup mirrors the kernel's binary search
over the same ranges and pairs.
[*fold.the-table-is-generated-by-the-kernels-own-generator]
 A Unicode
version bump regenerates both outputs together.

## 3.4.2 Compatibility with older folded columns

Simple folding differs from lowercasing. For example, Greek final sigma
`ς` folds to `σ`; Cherokee folds toward uppercase. A database populated by
the lowercasing implementation used before September 2026 can contain
folded columns that differ from Unicode 16.0.0 simple folding.
[*fold.columns-written-before-september-2026-used-lowercasing]

Schema version 1 does not distinguish those tables and startup does not
recompute existing folded columns. A successful version check therefore
does not establish that a database uses the same folding rules.
