Skip to content

fix(postgres): read inserted sequences from INSERT ... RETURNING - #995

Open
HarshMN2345 wants to merge 1 commit into
mainfrom
fix/postgres-insert-returning-sequence
Open

HarshMN2345 wants to merge 1 commit into
mainfrom
fix/postgres-insert-returning-sequence

Conversation

@HarshMN2345

Copy link
Copy Markdown
Member

On Postgres, createDocuments appends RETURNING "_uid", "_id" (plus "_tenant" on shared tables) to the insert and sets $sequence from the result, mapped by tenant and _uid. After that getSequences() has nothing to look up, so the extra SELECT _uid, _id ... WHERE _uid IN (...) after every batch goes away. Rows skipped by ON CONFLICT DO NOTHING are not returned and still go through getSequences() as before.

The change is behind a new getSupportForInsertReturning() capability, which is only enabled for Postgres. MariaDB, MySQL and SQLite behave as before. Upserts are not changed.

Mapping by tenant also fixes tenant-per-document batches where the same _uid appears under two tenants. Before, both documents got the same $sequence.

Verification: I ran createDocuments with $onNext through the library against Postgres 16 with log_statement=all.

  • Before: every batch was followed by SELECT _uid, _id ... WHERE "_uid" IN (...).
  • After: there is no SELECT for plain or shared-table batches. With skipDuplicates, only the skipped id is looked up (IN ('dup')). Sequences match the stored rows.

tests/e2e/Adapter/PostgresTest.php and SharedTables/PostgresTest.php pass, except testCacheFallback, which needs to stop a utopia-redis container and can't run in my setup.

Refs appwrite/appwrite#14078

createDocuments now appends RETURNING "_uid", "_id" (plus "_tenant" on shared
tables) when the adapter supports it and sets $sequence from the result, mapped by
tenant and _uid. getSequences() then has nothing left to look up, except for rows
skipped by ON CONFLICT DO NOTHING, which still go through it as before.

Gated by getSupportForInsertReturning(), enabled for Postgres only.
@coderabbitai

coderabbitai Bot commented Oct 5, 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 43 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: 23c8d965-555b-4550-9e69-ada8d3f85c17
📥 Commits

Reviewing files that changed from the base of the PR and between 1c99c21 and 0c30116.

📒 Files selected for processing (2)
  • src/Database/Adapter/Postgres.php
  • src/Database/Adapter/SQL.php
  • Autopilot · 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.

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