- Notifications
You must be signed in to change notification settings - Fork924
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
Open
blink-so wants to merge1 commit intomainChoose a base branch frommigrate-lib-pq-to-pgx
base:main
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
+908 −200
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>
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Labels
None yet
0 participants
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/pq
PostgreSQL driver topgx
to address data race issues with context cancellation and improve overall database performance.Problem
The
lib/pq
library 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/pq
is in maintenance mode and not actively developed.Solution
Migrate to
pgx
, which:Changes Made
Core Database Layer
pq.Error
topgconn.PgError
pq.Array()
calls with direct Go slice usage (pgx handles this automatically)LISTEN/NOTIFY (PubSub)
pq.Listener
with custompgxListenerShim
usingpgx.Conn.WaitForNotification()
Extra
toPayload
forpgconn.Notification
Dependencies
github.com/jackc/pgx/v5
andgithub.com/jackc/pgx/v5/stdlib
github.com/lib/pq
dependency and custom replace directivegithub.com/jackc/pgx/v5/stdlib
fordatabase/sql
compatibilityTesting
Benefits
lib/pq
due 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.