Skip to content

fix(postgres): read the sequence only for a document created without one - #1021

Merged
abnegate merged 1 commit into
mainfrom
fix-postgres-explicit-sequence
Oct 15, 2026
Merged

abnegate merged 1 commit into
mainfrom
fix-postgres-explicit-sequence

Conversation

@abnegate

Copy link
Copy Markdown
Member

Problem

Postgres::createDocument() calls lastInsertId() after every insert, and PostgreSQL answers that with lastval(). A document that brings its own $sequence writes it into the identity column without calling nextval(). On a session that has not called nextval() yet, lastval() raises:

SQLSTATE[55000]: Object not in prerequisite state: 7 ERROR:  lastval is not yet defined in this session

So creating a document with its own sequence on a fresh connection fails. Restores and migrations re-create documents with their sequence. MariaDB, MySQL and SQLite answer 0 in the same case instead of raising, and they already read the sequence only when it is missing.

It surfaced as a SharedTables/Postgres failure in testDeleteDocument on #1019 (job). That test re-creates its fixture with the fixture's own sequence, and paratest --functional gives each test a fresh process.

Fix

Read lastInsertId() only when the document has no sequence. When it has one, the value was already kept by the old ??=.

Test

DocumentTests::testDocumentRecreatedWithItsSequenceInAFreshSessionKeepsIt creates a document, deletes it, and re-creates it with its sequence. On PostgreSQL it first runs DISCARD SEQUENCES, which clears the session's lastval(), so the failure no longer depends on test order. The test checks that the re-created document and a fresh read keep the original sequence.

Run locally against PostgreSQL 18, the test fails on main with the error above and passes with the fix. testCreateDocument*/testDeleteDocument* (33 tests) pass on PostgreSQL, and the new test passes on SQLite. Pint and PHPStan are clean on the changed files.

🤖 Generated with Claude Code

createDocument() called lastInsertId(), which PostgreSQL answers with
lastval(), after every insert. A document that brings its own $sequence
inserts it into the identity column without calling nextval(), so on a
session that has not called nextval() yet lastval() raises 55000 "lastval
is not yet defined in this session" and the create fails. MariaDB, MySQL
and SQLite answer 0 there instead of raising.

It showed up as a SharedTables/Postgres CI failure in testDeleteDocument,
which re-creates its fixture with the fixture's sequence on a fresh paratest
process. The sequence is now read only when the document has none, as the
value was already kept in that case.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Oct 15, 2026

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 9 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

⚙️ Run configuration
  • Configuration used: Repository: utopia-php/database/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: ac16ac59-e71d-4197-9cef-27cf60294925
📥 Commits

Reviewing files that changed from the base of the PR and between 107dbf5 and 1d38a16.

📒 Files selected for processing: 2
  • src/Database/Adapter/Postgres.php
  • tests/e2e/Adapter/Scopes/DocumentTests.php
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@abnegate
abnegate merged commit 0555ec9 into main Oct 15, 2026
24 checks passed
@abnegate
abnegate deleted the fix-postgres-explicit-sequence branch October 15, 2026 11:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant