我正在尝试使用Go 中的pq 驱动程序连接到 postresql 数据库。当我在数据库的本地副本上执行此操作时,使用类似的连接字符串
DB, err = sql.Open("postgres", "user=user password=pwd dbname=mydb sslmode=disable")
Run Code Online (Sandbox Code Playgroud)
这一切都很好。
但是,当我切换到连接通过 pgbouncer 的生产服务器时:
DB, err = sql.Open("postgres", "user=user password=pwd host=/var/run/pgbouncer port=port dbname=mydb sslmode=disable")
Run Code Online (Sandbox Code Playgroud)
对于所有查询,我一直收到相同的错误,无论多么简单:
Database error: pq: S:"ERROR" M:"prepared statement \"1\" does not exist" C:"26000" F:"prepare.c" L:"519" R:"FetchPreparedStatement"
Run Code Online (Sandbox Code Playgroud)
(它总是“准备好的语句\”1\“”,独立于我试图通过的查询)
两种情况下的查询都运行如下:
res_rows, err := DB.Query(query)
if err != nil {
log.Printf("Database error: %s\n", err)
}
for res_rows.Next() {
...
}
Run Code Online (Sandbox Code Playgroud)
谷歌搜索建议关闭准备好的语句,但我不知道如何在 Go 中做到这一点,我不确定它是否受支持。任何帮助(甚至建议完全使用其他东西)将不胜感激。
pgbouncer版本1.7.2
psql(9.5.6)
我尝试在PgBouncer中使用auth_hba_file(/var/lib/pgsql/9.5/data/pg_hba.conf).
配置pgbouncer.ini
postgres = host=localhost port=5432 dbname=postgres user=postgres
test = host=localhost port=5432 dbname=test user=test
[pgbouncer]
logfile = /var/log/pgbouncer/pgbouncer.log
pidfile = /var/run/pgbouncer/pgbouncer.pid
listen_addr = *
listen_port = 6432
auth_type = hba
auth_hba_file = /var/lib/pgsql/9.5/data/pg_hba.conf
admin_users = postgres
stats_users = stats, postgres
pool_mode = session
server_reset_query = DISCARD ALL
max_client_conn = 100
default_pool_size = 20
Run Code Online (Sandbox Code Playgroud)
cat pg_hba.conf | grep -v"#"| grep -v"^ $"
local all all trust
host all all 127.0.0.1/32 trust
host all all ::1/128 trust
host test test …Run Code Online (Sandbox Code Playgroud) 我目前尝试将pgbouncer连接到postgresql,docker-compose但pgbouncer无法连接到postgresql.
我尝试连接到pgbouncer整个Postgresqlbash:
$ sudo docker-compose ps
Name Command State Ports
--------------------------------------------------------------------------------------
postgres_pgbouncer_1 /opt/pgbouncer/entrypoint.sh Up 0.0.0.0:6432->6432/tcp
postgres_postgres_1 docker-entrypoint.sh postg ... Up 0.0.0.0:5432->5432/tcp
$ sudo docker-compose exec postgres bash
& psql -h pgbouncer -p 6432 -U postgre
psql: ERROR: no such user: postgres
Run Code Online (Sandbox Code Playgroud)
我在日志中收到此警告pgbouncer:
$ sudo docker-compose logs -f pgbouncer
2019-12-20 08:11:31.802 UTC [1] WARNING C-0x5591f3329fb0: (nodb)/(nouser)@192.168.96.2:40270 pooler error: no such user: postgres
Run Code Online (Sandbox Code Playgroud)
docker-compose.yml
version: '3.4' …Run Code Online (Sandbox Code Playgroud) 在Windows Azure VM中的Ubuntu 12.04(精确)上,我在同一台机器上运行了postgres和pgbouncer.一切都设置好并且有效但是当重新启动VM时,pgbouncer不会自动启动.
我开始运行它的命令如下.注意:我需要成为'postgres'用户才能启动服务,否则会失败.还详细的答案preferrend.Linux不是我的普通操作系统.
sudo su postgres
pgbouncer -d -v /etc/pgbouncer/pgbouncer.iniRun Code Online (Sandbox Code Playgroud)
如果有用,这就是pgbouncer的安装方式:
sudo apt-get install postgresql-9.3 pgbouncerRun Code Online (Sandbox Code Playgroud)
注意:我只能在第一次运行pgbouncer -d -v /etc/pgbouncer/pgbouncer.ini命令后与pgbouncer服务(强制重新加载,状态,启动,停止)进行交互.
我正在开发与 postgresql (npgsql) 连接的 .net 应用程序。由于大量空闲连接(npgsql 无法终止它),我不得不安装 pgbouncer。我编写了一个配置文件并设置了一个 auth_user_file。
pgbouncer配置:
[databases]
dbr = host=HOST_DNS_NAME port=16252 dbname=app_database user=PGB_USER password=PGB_PASSWORD
[pgbouncer]
listen_port = 16252
listen_addr = *
auth_type = md5
auth_file = /URL_TO_AUTH_FILE/users.txt
logfile = pgbouncer.log
pidfile = pgbouncer.pid
admin_users = root
stats_users = root
default_pool_size = 50
max_client_conn = 100
log_connections = 1
log_disconnections = 1
log_pooler_errrors = 1
unix_socket_dir = /tmp
Run Code Online (Sandbox Code Playgroud)
我允许在 16252 端口上进行通信 (iptables)
-A INPUT -m state --state NEW,ESTABLISHED -m tcp -p tcp --dport 16252 -j ACCEPT
netstat …Run Code Online (Sandbox Code Playgroud) 我目前正在增强一个使用spring和hibernate的应用程序.有多个实例,应用程序通过预准备语句与db(postgres)进行通信.应用程序到现在为止,通过dbcp与postgres通信.
更改:应用程序现在通过pgbouncer传达给postgres.
即:应用程序 - > dbcp - > pgbouncer - > postgres
我知道这不是最理想的解决方案,即:有两个普通人.但由于目前的架构,我们需要它们.
要求:pgbouncer不支持事务模式中的预准备语句,因此必须将其删除.
更改以消除准备好的声明.
1)psql:VERSION = 9.2.6
没变
2)pgbouncer:在配置文件中设置以下属性
ignore_startup_parameters=extra_float_digits
pool_mode=transaction
server_reset_query=
Run Code Online (Sandbox Code Playgroud)
3)jdbc:已相应地设置准备的阈值.即:jdbc:postgresql://localhost:6541/postgres?prepareThreshold=0
JAVA VERSION = 1.7.0_51
JDBC DRIVER = postgresql-9.3-1102.jdbc41-3.jar
Run Code Online (Sandbox Code Playgroud)
4)dbcp:poolPreparedStatements = false maxOpenPreparedStatements = 0
5)休眠:没有变化
6)春天:没有变化
问题:
尽管所有这些变化,我仍然看到准备好的陈述试图创建和交易失败的原因.
"错误:预处理语句"S_21"不存在;嵌套异常是org.postgresql.util.PSQLException:错误:预处理语句"S_21"不存在"
我删除了使用预准备语句的所有逻辑更改.
如何防止创建其他预准备语句?spring或hibernate是否在内部为其使用创建了准备好的语句?如果是,我该如何禁用它们?
问题我有一个运行几百个sidekiq后台进程的rails应用程序.它们都连接到一个PostgreSQL数据库,它对提供250个连接并不十分满意 - 它可以,但如果所有sidekiq进程意外地向db发送查询,它就会崩溃.
选项1我一直在考虑在数据库前添加pgBouncer,但我目前无法使用它的事务模式,因为我高度依赖于search_path在每个作业处理的开头设置以确定哪个"国家"(PostgreSQL模式)工作(公寓宝石).在这种情况下,我将不得不使用基于会话的连接池模式.然而,据我所知,这会要求我在每次作业处理后断开连接,将连接释放回池中,这样做性能真的很高,不是吗?我错过了什么吗?
选项2使用基于应用程序层的连接池也是一个选项,但是我不确定我如何能够使用sidekiq为PostgreSQL做到这一点?
选项3我没有想到的东西?
postgresql connection-pooling pgbouncer sidekiq apartment-gem
我遇到了一个问题,我有一个功能,根据某些情况需要序列化访问.这似乎是使用咨询锁的好例子.但是,在相当繁重的负载下,我发现序列化访问没有发生,我看到并发访问该函数.
该功能的目的是为事件提供"库存控制".这意味着,它旨在限制特定事件的并发票购买,以使事件不被超卖.这些是应用程序/数据库中使用的唯一建议锁.
我发现偶尔会有比eventTicketMax值更多的门票.由于咨询锁定,这似乎不可能.在以低音量进行测试时(或在获取锁定后手动引入延迟,例如pg_sleep),事情按预期工作.
CREATE OR REPLACE FUNCTION createTicket(
userId int,
eventId int,
eventTicketMax int
) RETURNS integer AS $$
DECLARE insertedId int;
DECLARE numTickets int;
BEGIN
-- first get the event lock
PERFORM pg_advisory_lock(eventId);
-- make sure we aren't over ticket max
numTickets := (SELECT count(*) FROM api_ticket
WHERE event_id = eventId and status <> 'x');
IF numTickets >= eventTicketMax THEN
-- raise an exception if this puts us over the max
-- and bail
PERFORM pg_advisory_unlock(eventId);
RAISE EXCEPTION 'Maximum …Run Code Online (Sandbox Code Playgroud) 试图在亚马逊上安装pgbouncer遇到了这个:
[root@somehost ~]# uname -a
Linux somehost 4.4.35-33.55.amzn1.x86_64 #1 SMP Tue Dec 6 20:30:04 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
[root@somehost ~]# yum install pgbouncer
Loaded plugins: priorities, update-motd, upgrade-helper
No package pgbouncer available.
Error: Nothing to do
Run Code Online (Sandbox Code Playgroud)
去了github - 发现只有compilable版本.谷歌搜索并决定在下面发布解决方案
如果我"显示服务器",它会返回40个结果.如果我同时连接到数据库并使用pg_stat_activity进行连接计数(仅计算与pgbouncer的连接),则会返回85个连接.我认为那些计数应该匹配.我有什么误会?CentOS 7,Pgbouncer 1.8.1,Postgresql 9.6.10.
pgbouncer ×10
postgresql ×6
.net ×1
amazon-linux ×1
azure ×1
concurrency ×1
django ×1
docker ×1
firewall ×1
go ×1
hibernate ×1
jdbc ×1
linux ×1
networking ×1
npgsql ×1
sidekiq ×1
spring ×1
ubuntu ×1