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

# Port Miniflux to CockroachDB

> Miniflux is an open-source feed reader written in Go with lib/pq, and written for PostgreSQL.

> [!NOTE]
>
> **Migration** · **Works on:** Cockroach Continuum, CockroachDB Cloud, self-hosted · **Tested:** weekly in CI against the latest CockroachDB · **Stack:** Go, lib/pq, MOLT · **Code:** [`migrations/miniflux-port`](https://github.com/cockroachlabs/cockroach-cookbook/tree/migrations/miniflux-port/v1.0.0/migrations/miniflux-port) v1.0.0

[Miniflux](https://github.com/miniflux/v2) is an open-source feed reader written in Go with `lib/pq`, and
written for PostgreSQL. This project ports Miniflux 2.3.5 to CockroachDB as 13 patches, one change each, and
checks the result with Miniflux's own tests: all 53 unit-test packages and all 106 API integration tests pass
on CockroachDB. It goes with the Cockroach University course **Port a PostgreSQL App to CockroachDB: What Changes**.

The port changes 10 files: 273 lines added and 131 removed, most of them in the migrations. The driver,
the HTTP API, the web UI and almost all of the SQL stay as they were. The patches modify Miniflux; Miniflux's
own source isn't in the Cookbook repository, and `scripts/get-miniflux.sh` downloads it.

## Get the code

The code is in the [Cockroach Cookbook](https://github.com/cockroachlabs/cockroach-cookbook) repository. To fetch only this project at the version this page describes:

```shell theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
git clone --filter=blob:none --sparse https://github.com/cockroachlabs/cockroach-cookbook.git
cd cockroach-cookbook
git sparse-checkout set migrations/miniflux-port
git checkout migrations/miniflux-port/v1.0.0
cd migrations/miniflux-port
```

## Run it locally

You need Go 1.26 or later, git and the [`cockroach` binary](https://docs.cockroachlabs.com/docs/stable/install-cockroachdb), v26.1 or later.

```sh theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
sh scripts/get-miniflux.sh      # clones Miniflux 2.3.5 into miniflux/ and applies the patches as commits
sh scripts/local-db.sh          # starts an insecure local node and creates the miniflux database
cd miniflux && go build -o miniflux .
DATABASE_URL='postgresql://root@127.0.0.1:26257/miniflux?sslmode=disable' \
  RUN_MIGRATIONS=1 CREATE_ADMIN=1 ADMIN_USERNAME=admin ADMIN_PASSWORD=<a password> ./miniflux
```

Miniflux is then on [http://127.0.0.1:8080](http://127.0.0.1:8080). `git log -p 2.3.5..` in `miniflux/` shows each change with its reason.

## Run it on CockroachDB Basic

> [!NOTE]
>
> These steps use a CockroachDB Basic cluster, which existing CockroachDB Cloud organizations can create. New organizations are on Cockroach Continuum: create a cluster with the [Continuum quickstart](https://docs.cockroachlabs.com/docs/continuum/tutorial-quickstart.md) and use its connection string in the same steps. Continuum pricing differs from the Basic prices on this page; see [Continuum pricing](https://docs.cockroachlabs.com/docs/continuum/pricing.md).

1. [Create a Basic cluster](https://docs.cockroachlabs.com/docs/cockroachcloud/create-a-basic-cluster.md) and a SQL
   user, then run `CREATE DATABASE miniflux;` in a SQL shell on it.
2. Set `DATABASE_URL` to that user's [connection string](https://docs.cockroachlabs.com/docs/cockroachcloud/connect-to-your-cluster.md)
   with `/miniflux` as the database and `sslmode=verify-full`.
3. Start Miniflux with `RUN_MIGRATIONS=1` once, as above, to build the schema.

## What it costs

CockroachDB Basic charges for Request Units (RUs) and storage: $0.20 per million
RUs and $0.50 per GiB-month, with a \$15 monthly allowance for pay-as-you-go organizations
([Plan a CockroachDB Basic cluster](https://docs.cockroachlabs.com/docs/cockroachcloud/plan-your-cluster-basic.md), October 2026).
Most of Miniflux's database work is feed refreshes, so the RUs it uses grow with the number of feeds and how often
they're polled (`POLLING_FREQUENCY`, 60 minutes by default); the cluster's **Overview** page shows the RUs yours use.

## Run Miniflux's tests on CockroachDB

```sh theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
cd miniflux && go test ./... && cd ..   # unit tests
sh scripts/integration-test.sh          # starts Miniflux on a fresh miniflux_test database and runs its API tests
```

CI runs both, and `go vet` and `gofmt`, against `cockroachdb/cockroach:latest` on every change and weekly.

## Assess Miniflux

### The schema

[`schema/miniflux-2.3.5-schema.sql`](https://github.com/cockroachlabs/cockroach-cookbook/blob/migrations/miniflux-port/v1.0.0/migrations/miniflux-port/schema/miniflux-2.3.5-schema.sql) is Miniflux 2.3.5's schema as `pg_dump -s --no-owner --no-privileges` (pg\_dump 18.6) writes it, from a database built by Miniflux's own migrations: 14 tables,
20 indexes, 7 sequences and 5 enum types. Upload it to the
[Schema Conversion Tool](https://docs.cockroachlabs.com/docs/cockroachcloud/migrations-page.md) (**Schema Conversion**
under **Data** on any CockroachDB Cloud cluster, then **Add Schema**, PostgreSQL) to see what it reports:

* 2 Required Fixes: the `\restrict` and `\unrestrict` lines pg\_dump 18 adds, which are `psql` commands, not SQL.
  **Delete** them in the tool and click **Retry Migration**.
* 17 Suggestions: the 7 sequences and the 7 `id` columns that use them, `schema_version` without a primary key,
  and two indexed timestamp columns.

Everything else converts as written. None of the 13 changes below shows up in the report: they're in how the
migrations build the schema, in the application's SQL, and in how it uses IDs and transactions.

### The application

The application finds the rest, starting with its migrations. `scripts/assess.sh` runs unpatched Miniflux 2.3.5's
migrations on a fresh `miniflux_assess` database and prints where they stop; `--replay` does it again after each
commit on the `cockroachdb` branch until they run to the end, so each change shows the failure it fixes
(about 4 minutes):

```sh theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
sh scripts/assess.sh --replay
```

```text theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
Miniflux 2.3.5                                                  [Migration v1] pq: unexpected transaction status idle
+ 14395ac database: keep migration schema changes inside their  [Migration v18] pq: unimplemented: ALTER COLUMN TYPE requiring a rewrite of on-disk data is not supported inside a transaction (0A000)
…
+ e0adafd migrations: don't copy removed entries into a table   all migrations ran
```

The first nine changes come from the migrations. Miniflux's integration tests found two more (0010, 0011),
reading how its web UI and APIs use IDs found one (0012), and the refresh load test found the last (0013).

## What changes, and why

| Patch | What CockroachDB does | The change |
| - | - | - |
| 0001 | Commits an open transaction before a schema change (`autocommit_before_ddl`), and creates tables schema-locked | Each migration turns both off with `SET LOCAL`, so its schema changes stay in its transaction |
| 0002 | Can't change a column's type inside a transaction when that rewrites the data | Two columns are created with their final type, and the migrations that converted them do nothing |
| 0003 | Doesn't let a transaction write a column it has just added | Three backfill `UPDATE`s are removed |
| 0004 | Doesn't implement `setweight()`, `tsvector \|\| tsvector` or `websearch_to_tsquery()` | Entries index `to_tsvector(title \|\| ' ' \|\| content)` |
| 0005 | Can't build a partial index on a column added in the same transaction | Two partial indexes are created at the start of the next migration |
| 0006 | `sha256()` returns hex text, and replacing an expression index in one transaction fails | One unique index on `sha256(url::bytea)`, created once |
| 0007 | Has no extensions, so no `DROP EXTENSION` | The migration that dropped `hstore` does nothing |
| 0008 | Can't drop an index and reuse its name in one transaction | Two migrations that replaced an index and then put it back do nothing |
| 0009 | Can't write to a table with a foreign key in the transaction that created it | The copy of removed entries into the new `entry_tombstones` table is removed |
| 0010 | Doesn't accept an expression as an `ON CONFLICT` target | The enclosure insert uses `ON CONFLICT DO NOTHING`, which skips the same rows |
| 0011 | Splits `2.0.8` into `2`, `0` and `8` when parsing text, and raises an error for a query with only stop words | Searches build a `to_tsquery` string in which a word's pieces must appear in order, guarded by a `CASE` |
| 0012 | Fills `SERIAL` columns with `unique_rowid()` by default: 19-digit IDs | `SERIAL` creates sequences, so IDs stay small enough for JavaScript |
| 0013 | Runs transactions at `SERIALIZABLE` and returns `40001` when concurrent ones conflict | The feed-refresh transaction runs in a retry loop |

Patches 0001 to 0009 were found by running the migrations; 0010 and 0011 by Miniflux's integration tests;
0012 by reading how the web UI and the APIs use IDs; and 0013 by running concurrent refreshes (below).

### Why the migrations can change

The patches change what some historical migrations do. That's safe here because, on CockroachDB, Miniflux's
migrations only ever run from version 0 on a new, empty database: an existing database moves in with MOLT
(below) after the migrations have built the schema. A backfill with no rows to update, or a type conversion
of an empty column, has nothing to do, so the patches create each column with its final type and skip the
step. Migrations added after 2.3.5 will run on databases with data, so they need to follow the rules in
[`AGENTS.md`](https://github.com/cockroachlabs/cockroach-cookbook/blob/migrations/miniflux-port/v1.0.0/migrations/miniflux-port/AGENTS.md).

Patch 0001 keeps each migration in one transaction, as Miniflux's runner expects. CockroachDB gives
`CREATE TABLE` and `CREATE INDEX` in a transaction the same atomicity as other statements, but most other
schema changes in a multi-statement transaction can fail at `COMMIT` after the transaction's other
statements have committed, with error `XXA00`
([Schema changes within transactions](https://docs.cockroachlabs.com/docs/stable/online-schema-changes.md#schema-changes-within-transactions)).
On a new, empty database that's unlikely (a unique index that finds duplicates is one cause), and the fix is
to start again on a new database. On a database with data, a migration that changes an existing table should
make one schema change, and an `XXA00` means checking the schema by hand before running it again.

### IDs

CockroachDB's default for `SERIAL`, `unique_rowid()`, spreads inserts well, but it makes IDs such as
`1224403685310201857`. Miniflux's web UI reads entry IDs with `parseInt`, and a JavaScript number can't hold an
integer above 2^53 exactly, so marking an entry read would send a rounded ID. Patch 0012 sets
[`serial_normalization`](https://docs.cockroachlabs.com/docs/stable/serial) to `sql_sequence_cached` during
migrations, so `SERIAL` creates a sequence whose values each session caches, in blocks of
`sql.defaults.serial_sequences_cache_size` (256 by default):

* Most sequences cache 256 values per session. A sequence without a cache adds a write for every row; on a
  single node it doubled the cost of an insert, and the feed tests timed out. With the cache, inserts cost
  about what they do with `unique_rowid()`, and IDs from different connections aren't in insert order.
* `entries_id_seq` has no cache ([`PER SESSION CACHE 1`](https://docs.cockroachlabs.com/docs/stable/alter-sequence)).
  Fever's `since_id` and the API's `after_entry_id` use entry IDs as a cursor ("give me entries after the
  newest one I have"), so they need to arrive in insert order. That costs time: the integration suite takes
  56 seconds this way, 36 with every sequence cached and 32 with `unique_rowid()`.

### Search

Search matches the same words, and `ts_rank` still orders the results, with two differences from 2.3.5:

* A match in an entry's title no longer ranks above a match in its content, because `setweight()` isn't available.
* The web-search syntax (quoted phrases, `OR`, `-word`) isn't parsed. Every word must match, and a word with
  punctuation, such as `2.0.8`, must match in order.

A search for `2.0.8` finds the one entry that has it, and a search for only stop words, such as `the`,
returns no entries instead of an error.

### Retries

CockroachDB runs every transaction at `SERIALIZABLE` isolation and returns SQLSTATE `40001` when one conflicts
with a concurrent transaction, for the client to run it again ([retry errors](https://docs.cockroachlabs.com/docs/stable/transaction-retry-error-reference.md)).
`scripts/refresh-load.sh` measures this. For 90 seconds, 16 threads (the size of Miniflux's worker pool)
refresh 40 local feeds that gain items every 2 seconds, while 4 more mark entries read and flush history:

| Build | Refreshes | Failed with `40001` | Retries |
| - | - | - | - |
| Patches 0001–0012 | 3,050 | 156 (5.1%) | none |
| All 13 patches | 2,786 | 0 | 284: 259 transactions retried, 234 succeeding on the second attempt and 25 on the third |

Every failure was in the per-entry transaction in `RefreshFeedEntries`, which reads whether an entry exists
and then inserts or updates it. Mark-as-read and flush-history are single statements, and CockroachDB
retried those itself. Patch 0013 adds `inRetryableTransaction` (40001 only, up to 5 attempts, randomized
backoff) and uses it there. Numbers are from one run on a laptop single node; a cluster will differ.

## Move an existing Miniflux database

An existing Miniflux 2.3.5 database on PostgreSQL moves with [MOLT Fetch](https://docs.cockroachlabs.com/docs/molt/molt-fetch.md)
and is checked with [MOLT Verify](https://docs.cockroachlabs.com/docs/molt/molt-verify.md), following the
[Classic Bulk Load](https://docs.cockroachlabs.com/docs/molt/migration-approach-classic-bulk-load.md) approach: Miniflux
stops, the data loads, and Miniflux starts again on CockroachDB. With Miniflux stopped
and the `molt` binary installed:

```sh theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
(cd miniflux && go build -o miniflux .)
SRC='postgres://<user>@<host>:5432/miniflux' DST_DB=miniflux sh molt/move.sh
```

`molt/move.sh` drops and recreates `DST_DB` on the local node, then:

1. Builds the schema with the ported migrations, so the target has the port's sequences, indexes and types.
2. Drops the target's foreign keys, because MOLT loads tables in parallel and a child row can arrive before its parent.
3. Copies the data with MOLT Fetch, leaving out `schema_version` (no primary key; the migrations already set it
   to 135) and `entries.document_vectors` (`molt/transforms.json`).
4. Adds the foreign keys back, which checks every copied row against them.
5. Rebuilds `entries.document_vectors` with the expression the port uses. MOLT doesn't copy `tsvector` values,
   and they need rebuilding anyway: vectors built from `2.0.8` as one word wouldn't match a search for its pieces.
6. Moves each sequence past the highest copied ID.
7. Compares every row on both sides with MOLT Verify.

To try it without a database of your own, `molt/seed.py` fills one through the API of a running, unpatched
Miniflux 2.3.5 (2 users, 42 feeds, 1,095 entries, 364 enclosures), and `molt/check.py` checks the moved copy
through the ported app: the same counts per user, a search for `4.17.1` that finds its one entry, and a
refresh whose new entries get IDs after the copied ones. On that data every row matched in all 13 tables.

MOLT keeps its progress in `_molt_*` tables in the target; Miniflux ignores them, and they can be dropped after the move.

## Start small, grow without switching

Miniflux is a small app: one process and a handful of tables, and a single node is plenty for it. Nothing in
the patches depends on the size of the cluster: the same build connects to a laptop node or to a CockroachDB
Basic cluster, through the same PostgreSQL driver. The ID and retry changes are
the ones that matter once there's more than one node, so they're made for that from the start. The tests in
this project run on a single node.

## License

The patches modify [Miniflux](https://github.com/miniflux/v2) (Copyright The Miniflux Authors) and, like
Miniflux, are licensed under Apache-2.0. See [`NOTICE`](https://github.com/cockroachlabs/cockroach-cookbook/tree/migrations/miniflux-port/v1.0.0/migrations/miniflux-port/NOTICE).
