Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

blink-so[bot]
Copy link
Contributor

@blink-so blink-so bot commented Jun 23, 2025

Fixes #6201

This PR migrates the Coder codebase from the lib/pq PostgreSQL driver to pgx 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:

  • Has better context cancellation handling (fixes the data race)
  • Is actively maintained with regular updates
  • Provides better performance and more PostgreSQL features
  • Has improved error handling and logging

Changes Made

Core Database Layer

  • Error Handling: Updated all error handling from pq.Error to pgconn.PgError
  • Error Codes: Migrated from named error codes to PostgreSQL numeric codes (e.g., "23505" for unique_violation)
  • Array Handling: Replaced pq.Array() calls with direct Go slice usage (pgx handles this automatically)

LISTEN/NOTIFY (PubSub)

  • Complete Rewrite: Replaced pq.Listener with custom pgxListenerShim using pgx.Conn.WaitForNotification()
  • Notification Fields: Updated field names from Extra to Payload for pgconn.Notification
  • Connection Management: Simplified connection handling using pgx's built-in connection management

Dependencies

  • Added: github.com/jackc/pgx/v5 and github.com/jackc/pgx/v5/stdlib
  • Removed: github.com/lib/pq dependency and custom replace directive
  • Driver Registration: Updated to use github.com/jackc/pgx/v5/stdlib for database/sql compatibility

Testing

  • ✅ All database packages build successfully
  • ✅ PubSub LISTEN/NOTIFY functionality migrated and working
  • ✅ Error handling properly converted to pgx error types
  • ✅ Driver registration working correctly

Benefits

  1. Fixes Data Race: Resolves the context cancellation data race issue mentioned in Consider migrating away from lib/pq due do data race in context cancellation #6201
  2. Better Performance: pgx is generally faster than lib/pq
  3. Active Development: pgx is actively maintained vs lib/pq in maintenance mode
  4. Enhanced Features: Better PostgreSQL feature support and error handling
  5. Future-Proof: Ensures Coder stays on a supported, evolving database driver

Migration 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.

This migration addresses issue #6201 by replacing the lib/pq PostgreSQL
driver with pgx, which has better context cancellation handling and is
more 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.mod

Benefits:
- Fixes data race issues with context cancellation
- Better performance and PostgreSQL feature support
- More actively maintained driver
- Improved error handling and logging

Tested: Database package builds successfully with new driver

Co-authored-by: ibetitsmike <[email protected]>
@Emyrk
Copy link
Member

Emyrk commented Jun 24, 2025

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.

Consider migrating away from lib/pq due do data race in context cancellation
1 participant