rryqszq4 / ngx-php

ngx-php - Embedded php7 or php8 scripting language for nginx module. Mainline development version of the ngx-php.
BSD 2-Clause "Simplified" License
582 stars 56 forks source link

pg_pconnect safe? #190

Open klaussilveira opened 1 month ago

klaussilveira commented 1 month ago

Since the README documents that globals are unsafe, and so are singletons, I was wondering if pg_pconnect is also considered unsafe for ngx-php code. Is a connection pooler like pgbouncer a preferred alternative?

joanhey commented 4 weeks ago

It's safe to use pg_pconnect(), but it's more useful in a shared nothing mode, like PHP-FPM.

Ngx-php boots your application once, keeps it in memory, and then feeds it requests. So if you don't close a normal pg_connect(), it'll be open and don't need a "p" connect(). Only be careful that all databases have a timeout for connections, so you need to send a simple "select 1" to maintain it open or use a reconnect method if it's closed. The same happen with any persistent app.

In persistent apps you can use globals, singletons and static properties, but you need to understand that any change will be persistent. So you need to clear before new requests and use closures in the singletons.

In the Techempower benchmark we use normal PDO without persistent connection. But we have always that connection open. Not only we have the connection open, we also have the prepared statements open and ready to call.