Skip to content

Add postgres Store - #975

Open
ChrisSchinnerl wants to merge 1 commit into
masterfrom
chris/postgres-init
Open

Add postgres Store#975
ChrisSchinnerl wants to merge 1 commit into
masterfrom
chris/postgres-init

Conversation

@ChrisSchinnerl

Copy link
Copy Markdown
Member

This PR is the first of probably quite a few to introduce a Postgres driver to hostd.
It starts by adding a init.sql which is similar to SQLite's but with Postgres types as well as similar boilerplate code we use in indexd. The only difference between indexd and hostd is that this PR uses an embedded Postgres db for testing which makes running tests more convenient.

Since we have 100+ store interface methods the goal is to follow up with at least 1 PR per store interface in the host but considering some of those are quite large probably a lot more than that.
I really want to avoid this being a dumb translation and instead make sure it's making good use of Postgres' capabilities.

@ChrisSchinnerl ChrisSchinnerl self-assigned this Jul 8, 2026
@socket-security

socket-security Bot commented Jul 8, 2026

Copy link
Copy Markdown

Review the following changes in direct dependencies. Learn more about Socket for GitHub.

Diff Package Supply Chain
Security
Vulnerability Quality Maintenance License
Addedgithub.com/​jackc/​pgx/​v5@​v5.10.073100100100100
Addedgithub.com/​fergusstrange/​embedded-postgres@​v1.34.097100100100100

View full report

@github-project-automation github-project-automation Bot moved this to In Progress in Sia Jul 8, 2026
@ChrisSchinnerl
ChrisSchinnerl marked this pull request as ready for review July 9, 2026 11:52
Copilot AI review requested due to automatic review settings July 9, 2026 11:52

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 95c5278ae6

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread persist/postgres/store.go Outdated

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces the initial PostgreSQL-backed persistence layer for hostd, including schema initialization, migration scaffolding, and embedded-Postgres-based tests to validate schema/migration consistency.

Changes:

  • Added a new persist/postgres package with store open/init logic, transaction retry/backoff handling, and SQL helpers for slow-query logging.
  • Introduced an initial Postgres schema (init.sql) plus migration scaffolding (migrations.go) and a migration consistency test using an embedded Postgres instance.
  • Added new dependencies for pgx and embedded Postgres in go.mod/go.sum.

Note: this repo contains knope.toml; consider adding a knope changeset for this PR to document the addition of the Postgres store.

Reviewed changes

Copilot reviewed 7 out of 8 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
persist/postgres/store.go Store open/close, database creation, and transaction retry/backoff wrapper.
persist/postgres/sql.go *sql.Tx/*sql.Stmt wrappers for slow-query logging and DB version helpers.
persist/postgres/migrations.go Migration list placeholder (starts at v1, currently no migrations).
persist/postgres/migrations_test.go Embedded Postgres test harness and schema consistency checks.
persist/postgres/init.sql Initial Postgres schema for hostd persistence tables.
persist/postgres/init.go Init/upgrade flow, schema application, db_version management, host key generation.
go.mod Adds pgx + embedded-postgres dependencies.
go.sum Dependency checksums for the new modules.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread persist/postgres/store.go Outdated
Comment thread persist/postgres/store.go
Comment thread persist/postgres/store.go Outdated
Comment thread persist/postgres/sql.go Outdated

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 7 out of 8 changed files in this pull request and generated 4 comments.

Comment thread persist/postgres/store.go Outdated
Comment thread persist/postgres/store.go Outdated
Comment thread persist/postgres/migrations_test.go
Comment thread go.mod

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 7 out of 8 changed files in this pull request and generated 3 comments.

Comment thread persist/postgres/store.go
Comment thread persist/postgres/store.go
Comment thread persist/postgres/migrations_test.go Outdated

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 7 out of 8 changed files in this pull request and generated 3 comments.

Comment thread persist/postgres/store.go
Comment thread persist/postgres/init.go Outdated
Comment thread persist/postgres/sql.go

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 7 out of 8 changed files in this pull request and generated 2 comments.

Comment thread persist/postgres/init.go
Comment thread persist/postgres/store.go
Comment thread persist/postgres/init.sql
sector_writes BIGINT NOT NULL DEFAULT 0,
UNIQUE (volume_id, volume_index)
);
CREATE INDEX volume_sectors_sector_writes_volume_id_sector_id_volume_index_compound ON volume_sectors(sector_writes ASC, volume_id, sector_id, volume_index) WHERE sector_id IS NULL;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we care this index name exceeds the 63 char limit postgres imposes? (there's 3 in total)

Comment thread persist/postgres/sql.go

// getDBVersion returns the current version of the database.
func getDBVersion(ctx context.Context, pool *pgxpool.Pool) (version int64) {
// error is ignored -- the database may not have been initialized yet.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is kind of odd? we do this for s3d and indexd too but wouldn't it be better to check explicitly and return an error if it's not initialized? or perhaps even retry with a small backoff and error out after a second or so

Comment thread persist/postgres/init.sql
cached_subtree_roots BYTEA,
last_access_timestamp TIMESTAMP WITH TIME ZONE NOT NULL
);
CREATE INDEX stored_sectors_sector_root ON stored_sectors(sector_root);

@chris124567 chris124567 Jul 13, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need an index here because we already have UNIQUE on that column. It seems there are a few more places this happens

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

4 participants