Skip to content

refactor: upgrade to psycopg3[binary]==3.3.3#12212

Draft
bhardwajparth51 wants to merge 10 commits intointernetarchive:masterfrom
bhardwajparth51:10258/refactor/upgrade-psycopg3
Draft

refactor: upgrade to psycopg3[binary]==3.3.3#12212
bhardwajparth51 wants to merge 10 commits intointernetarchive:masterfrom
bhardwajparth51:10258/refactor/upgrade-psycopg3

Conversation

@bhardwajparth51
Copy link
Copy Markdown
Contributor

@bhardwajparth51 bhardwajparth51 commented Mar 28, 2026

Description

Part of #10258 upgrade.

refactor

This PR upgrades Open Library's underlying PostgreSQL driver from psycopg2
to psycopg[binary]==3.3.3. Following the successful migration of the Open
Library database from PostgreSQL 9.3 to 18, this update ensures our application
bindings are modernized and can leverage psycopg3 natively for better
performance and future async execution.

Changes

  1. Dependencies: Updated requirements.txt to use psycopg[binary]==3.3.3.
  2. Config: Added driver: psycopg explicitly to db_parameters in
    conf/coverstore.yml, conf/infobase.yml, and conf/openlibrary.yml.
  3. Namespaces: Refactored all module exception imports to use psycopg.errors
    across core/db.py, core/edits.py, core/imports.py, and scripts/migrations/.
  4. Solr Builder: Upgraded driver instantiations and typings natively in
    solr_builder.py. Removed cur.itersize which is unsupported in psycopg3.
  5. Config Patch (openlibrary/config.py): Added a temporary compatibility
    shim for infogami's parse_db_parameters(), which currently strips the
    driver key. The patch preserves it until the submodule is updated natively.
    Marked with # TODO

Known Limitation

infogami is a submodule that still internally references psycopg2. The config
patch in openlibrary/config.py is a deliberate stopgap —to be tracked in #10258.

Future Proofing for FastAPI Async

Since psycopg3 natively supports asyncio, this sets up future FastAPI
endpoints to use AsyncConnectionPool without additional driver changes.

Testing

  1. Run docker compose build home to pull in the new dependencies.
  2. Run docker compose run --rm home make test-py successfully against the
    revised stack (3008 passed, 0 failures).
  3. Boot up the local webserver (docker compose up) and verify that standard
    database components resolve properly.
  4. Confirmed driver: psycopg is present in web.config.db_parameters
    after config load.

Stakeholders

@mekarpeles @RayBB

@bhardwajparth51 bhardwajparth51 changed the title feat: upgrade to psycopg3[binary]==3.3.3 refactor: upgrade to psycopg3[binary]==3.3.3 Mar 28, 2026
@bhardwajparth51
Copy link
Copy Markdown
Contributor Author

vendor/infogami/requirements_test.txt still has psycopg2. I've left vendored test requirements untouched for now as part of the transitional phase, but I've added shims in config.py to ensure OL remains fully compatible with psycopg3 throughout

@RayBB
Copy link
Copy Markdown
Collaborator

RayBB commented Mar 30, 2026

Maybe the approach we should take here is to use it in our application in places that we currently are using it directly, and then do a separate pull request for Infogami. Because I don't think that we are going to be able to just patch in Kogami as a long-term solution. We need to fully improve it at its source.

@bhardwajparth51
Copy link
Copy Markdown
Contributor Author

Agreed, patching config.py is definitely a code smell. We should fix it at the source in the infogami repo instead.

Just to make sure we're on the same page for this PR: I am thinking to do a hybrid setup where we have both psycopg2 and psycopg[binary]==3.3.3 in requirements.txt. That way infogami can keep using v2 for now, while we upgrade direct usages like solr_builder to v3.

Or should we first, fix the internetarchive/infogami first to update its driver parsing, and then we do a single, clean switch later?"

@RayBB
Copy link
Copy Markdown
Collaborator

RayBB commented Mar 30, 2026 via email

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.

2 participants