- Notifications
You must be signed in to change notification settings - Fork1.1k
Migrate from lib/pq to pgx to fix context cancellation data race#18492
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Closed
Uh oh!
There was an error while loading.Please reload this page.
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
This migration addresses issue#6201 by replacing the lib/pq PostgreSQLdriver with pgx, which has better context cancellation handling and ismore actively maintained.Key changes:- Replace lib/pq imports with github.com/jackc/pgx/v5- Update error handling to use pgconn.PgError instead of pq.Error- Migrate LISTEN/NOTIFY implementation in pubsub package- Replace pq.Array() calls with direct slice usage- Update PostgreSQL error code checks to use numeric codes- Remove custom lib/pq replace directive from go.modBenefits:- Fixes data race issues with context cancellation- Better performance and PostgreSQL feature support- More actively maintained driver- Improved error handling and loggingTested: Database package builds successfully with new driverCo-authored-by: ibetitsmike <203725896+ibetitsmike@users.noreply.github.com>
Member
Emyrk commentedJun 24, 2025
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes#6201
This PR migrates the Coder codebase from the
lib/pqPostgreSQL driver topgxto address data race issues with context cancellation and improve overall database performance.Problem
The
lib/pqlibrary has a longstanding issue with data races during context cancellation (lib/pq#1021), which can cause instability in applications that heavily use contexts like Coder. Additionally,lib/pqis in maintenance mode and not actively developed.Solution
Migrate to
pgx, which:Changes Made
Core Database Layer
pq.Errortopgconn.PgErrorpq.Array()calls with direct Go slice usage (pgx handles this automatically)LISTEN/NOTIFY (PubSub)
pq.Listenerwith custompgxListenerShimusingpgx.Conn.WaitForNotification()ExtratoPayloadforpgconn.NotificationDependencies
github.com/jackc/pgx/v5andgithub.com/jackc/pgx/v5/stdlibgithub.com/lib/pqdependency and custom replace directivegithub.com/jackc/pgx/v5/stdlibfordatabase/sqlcompatibilityTesting
Benefits
lib/pqdue do data race in context cancellation #6201Migration Notes
This is a significant but necessary change that touches the core database layer. The migration maintains full compatibility with existing database operations while providing the benefits of the more modern pgx driver.
The most complex part was migrating the pubsub LISTEN/NOTIFY implementation, which required a complete rewrite due to the different APIs between lib/pq and pgx. The new implementation is simpler and more robust.