Skip to content

Add PostgreSQL schema hashing - #802

Merged
staabm merged 1 commit into
staabm:mainfrom
oiahoon:fix/pgsql-schema-hasher
Jul 16, 2026
Merged

Add PostgreSQL schema hashing#802
staabm merged 1 commit into
staabm:mainfrom
oiahoon:fix/pgsql-schema-hasher

Conversation

@oiahoon

@oiahoon oiahoon commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

Summary

  • add a PostgreSQL schema hasher based on relation identity, column name, effective type, and nullability
  • select the PostgreSQL hasher automatically for PDO pgsql data sources
  • cover stable hashing plus column rename, type, and nullability changes with a real PostgreSQL regression test

The previous fallback selected the MySQL schema hasher for every data source, so PostgreSQL recording mode attempted to execute SET SESSION group_concat_max_len. The dedicated hasher now lets result-cache invalidation follow PostgreSQL schema changes without invoking MySQL-specific SQL.

Closes #795

Validation

  • PostgreSQL PHPUnit configurations: 357 tests, 356 assertions, 6 expected skips
  • focused schema-hasher regression: 1 test, 5 assertions
  • PHP parallel lint on all changed files
  • ECS on all changed files
  • PHPStan on both newly added files and all six test configurations
  • git diff --check

@oiahoon
oiahoon force-pushed the fix/pgsql-schema-hasher branch from 29ee378 to 862c1fe Compare July 15, 2026 16:54
@staabm

staabm commented Jul 16, 2026

Copy link
Copy Markdown
Owner

Did you try this patch on a real world project?

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

Adds first-class PostgreSQL support for schema hashing so phpstan-dba can invalidate result caches based on PostgreSQL schema changes (instead of using the MySQL hasher and executing MySQL-specific SQL on pgsql connections).

Changes:

  • Introduce a new SchemaHasherPgsql implementation that hashes PostgreSQL schema metadata (schema/table/column name, effective type, nullability).
  • Automatically select the PostgreSQL hasher when the datasource is a PDO connection using the pgsql driver.
  • Add a PostgreSQL regression test covering stable hashing and hash changes on column rename, type change, and nullability change.

Reviewed changes

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

File Description
tests/default/SchemaHasherPgsqlTest.php Adds a PostgreSQL integration/regression test verifying schema-hash stability and sensitivity to column changes.
src/QueryReflection/QueryReflection.php Switches hasher selection to use SchemaHasherPgsql automatically for PDO pgsql datasources.
src/DbSchema/SchemaHasherPgsql.php New PostgreSQL-specific schema hasher based on pg_catalog metadata.

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

Comment on lines +32 to +45
SELECT MD5(
STRING_AGG(
CONCAT_WS(
CHR(31),
namespace.nspname,
relation.relname,
attribute.attname,
FORMAT_TYPE(attribute.atttypid, attribute.atttypmod),
attribute.attnotnull::text
),
CHR(30)
ORDER BY namespace.nspname, relation.relname, attribute.attnum
)
) AS dbsignature

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Addressed in 11feff1. Empty user schemas now hash the empty aggregate deterministically as md5('').

Comment thread tests/default/SchemaHasherPgsqlTest.php Outdated
$port = ';port=' . $port;
}

return new PDO(sprintf('pgsql:dbname=%s;host=%s', $database, $host) . $port, $user, $password);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Addressed in 11feff1. The DDL connection now uses PDO::ERRMODE_EXCEPTION, so fixture setup failures surface immediately.

@oiahoon
oiahoon force-pushed the fix/pgsql-schema-hasher branch from 862c1fe to 11feff1 Compare July 16, 2026 14:59
@oiahoon

oiahoon commented Jul 16, 2026

Copy link
Copy Markdown
Contributor Author

I have not tried it in an external application. I exercised the production routing path against a local PostgreSQL 16.14 instance, including schema rename, type, and nullability changes. After this update I also verified that an empty user schema returns d41d8cd98f00b204e9800998ecf8427e, and reran all six PostgreSQL PHPUnit configurations: 357 tests / 356 assertions passed, with 6 expected skips.

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 3 out of 3 changed files in this pull request and generated no new comments.

@staabm
staabm merged commit 39cb48e into staabm:main Jul 16, 2026
28 checks passed
@staabm

staabm commented Jul 16, 2026

Copy link
Copy Markdown
Owner

thank you!

@eiriksm

eiriksm commented Aug 12, 2026

Copy link
Copy Markdown

Hey, thanks for working on this! I was just bumping into a bug in our CI pipeline based on how we don't have PostgreSQL schema hashing. I verified that bumping to the latest dev version fixes the issue. Would be super nice to have a new tagged release with this fix in it 🙏 ❤️

@staabm

staabm commented Aug 12, 2026

Copy link
Copy Markdown
Owner

Tagged a new release

@eiriksm

eiriksm commented Aug 13, 2026

Copy link
Copy Markdown

Thank you, wow what a rapid response 🚀

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.

implement PGSQL schema hasher

4 participants