t1nkl/postgres-pgbouncer-extension
Composer 安装命令:
composer require t1nkl/postgres-pgbouncer-extension
包简介
Laravel PostgreSQL pgbouncer extension fix for PDO::ATTR_EMULATE_PREPARES option
README 文档
README
A tiny Laravel package that makes working with PostgreSQL through PgBouncer safe and predictable when PDO::ATTR_EMULATE_PREPARES is enabled.
Why you might need it:
- PgBouncer in transaction/session pooling mode does not support server‑side prepared statements.
- The common workaround is to enable PDO emulated prepares (
PDO::ATTR_EMULATE_PREPARES => true). - With emulation enabled, default binding behavior in Laravel/PDO can cause booleans and some types to be sent in ways PgBouncer/PostgreSQL may not expect.
This package provides a custom PostgresConnection that:
- Formats
DateTimeInterfacevalues using your connection grammar date format. - Converts PHP booleans to the PostgreSQL-native textual values
'true'and'false'during binding. - Binds values using explicit PDO parameter types: integers as
PDO::PARAM_INT, resources asPDO::PARAM_LOB, and everything else asPDO::PARAM_STRfor consistency with emulated prepares.
The package is automatically discovered by Laravel and is only activated for the pgsql connection when PDO::ATTR_EMULATE_PREPARES is present and enabled in your connection options.
Requirements
- PHP: ^8.0
- Laravel Framework: ^10.0 | ^11.0 | ^12.0 | ^13.0
- PostgreSQL + PgBouncer (transaction or session pooling scenarios)
Installation
composer require t1nkl/postgres-pgbouncer-extension
Laravel package auto-discovery will register the service provider; no manual configuration is needed beyond the database option below.
Configuration
Enable emulated prepares on your PostgreSQL connection. You can declare it either as an associative map or as a numeric flag — this package supports both styles.
config/database.php
'connections' => [ 'pgsql' => [ 'driver' => 'pgsql', 'host' => env('DB_HOST', '127.0.0.1'), 'port' => env('DB_PORT', '5432'), 'database' => env('DB_DATABASE', 'forge'), 'username' => env('DB_USERNAME', 'forge'), 'password' => env('DB_PASSWORD', ''), 'charset' => 'utf8', 'prefix' => '', 'search_path' => 'public', 'sslmode' => 'prefer', // EITHER associative option style (recommended): 'options' => [ PDO::ATTR_EMULATE_PREPARES => true, ], // OR numeric option style (also supported): // 'options' => [ // PDO::ATTR_EMULATE_PREPARES, // ], ], ],
Make sure your default connection is pgsql or that you add the options to the specific PostgreSQL connection you actually use.
If you modify configuration, remember to clear caches:
php artisan config:clear php artisan cache:clear
How it works
- The service provider hooks into Laravel’s connection resolver for
pgsqlonly whenPDO::ATTR_EMULATE_PREPARESis detected in the connectionoptions. - It swaps the default
Illuminate\Database\PostgresConnectionwithPostgresPgbouncerExtension\Database\PostgresConnection. - The custom connection overrides:
prepareBindings: convertsDateTimeInterfaceto the grammar date format and casts booleans to'true'/'false'strings.bindValues: binds integers asPDO::PARAM_INT, resources asPDO::PARAM_LOB, and all other values asPDO::PARAM_STRto keep behavior stable with emulated prepares.
Usage
There is nothing special to do. Once installed and the option is enabled, all queries through the pgsql connection will use the safer binding strategy.
Example: boolean filters are sent as 'true'/'false' which PostgreSQL parses natively.
User::where('is_active', true)->get();
Troubleshooting
- The behavior does not change:
- Ensure
PDO::ATTR_EMULATE_PREPARESis present in youroptionsand set totrue(or listed numerically). - Confirm your app uses the
pgsqlconnection you configured (config('database.default')). - Clear config/cache (
php artisan config:clear && php artisan cache:clear). - Make sure the package is installed and not excluded from auto-discovery.
- Ensure
- Still seeing unexpected parameter types?
- Double-check any custom connection/resolver code that might override this package.
- Try a minimal repro calling the query builder and logging SQL + bindings.
Testing locally
This repository includes a test suite. To run it:
composer install
composer test
Related work
Original gist/package inspiration: https://github.com/t1nkl/Laravel-PostgreSQL-pgbouncer-Fix
License
MIT License. See LICENSE for details.
t1nkl/postgres-pgbouncer-extension 适用场景与选型建议
t1nkl/postgres-pgbouncer-extension 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 104.86k 次下载、GitHub Stars 达 8, 最近一次更新时间为 2022 年 06 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「postgresql」 「postgres」 「laravel」 「pgbouncer」 「ATTR_EMULATE_PREPARES」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 t1nkl/postgres-pgbouncer-extension 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 t1nkl/postgres-pgbouncer-extension 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 t1nkl/postgres-pgbouncer-extension 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
SilverStripe now has tentative support for PostgreSQL ('Postgres')
Dibi is Database Abstraction Library for PHP
Store your language lines in the database, yaml or other sources
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
A PSR-7 compatible library for making CRUD API endpoints
Sql dumps containing databases for the linna apps.
统计信息
- 总下载量: 104.86k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 8
- 点击次数: 17
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-06-14