question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

PostgreSQL connections spike

See original GitHub issue

Describe the bug In production we have a large invoices flow. Every ~10-30 minutes the number of connections to the database increases dramatically (from ~10 to maximum of connection pool size).

I suppose the problem is that there is no limit on the number of parallel threads. With a large number of pending invoices, the application will open a large number of database connections. At the moment when too many connections to the database are open, other operations with invoices take too long or fall with a HTTP timeout. It is critical for our payments flow.

In my opinion, using System.Linq.ParallelEnumerable or Microsoft.TPL.DataFlow will limit the number of parallel threads and database connections.

To Reproduce Generate a large number of rows in database table “PendingInvoices”.

Expected behavior BtcPay server does not take unlimited number of connections.

Screenshots image

Your BTCPay Environment (please complete the following information): Deployment method: Docker Browser: all browsers Bitcoin Network: mainnet NBXplorer version: v2.1.36 BtcPay server version: v1.0.5.7 Database engine: PostgreSQL v11.8 Database hosting: AWS RDS Pending invoices count: 3162 Connection pool size: 255

SQL Query that runs in each connection SELECT i."Id", i."Archived", i."Blob", i."Created", i."CurrentRefundId", i."CustomerEmail", i."ExceptionStatus", i."ItemCode", i."OrderId", i."Status", i."StoreDataId", p."Id", p."Accounted", p."Blob", p."InvoiceDataId" FROM "Invoices" AS i LEFT JOIN "Payments" AS p ON i."Id" = p."InvoiceDataId" WHERE i."Id" = $1 ORDER BY i."Id", p."Id"

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:33 (21 by maintainers)

github_iconTop GitHub Comments

1reaction
Kukkscommented, Oct 29, 2020

@NicolasDorier There were updates of invoice status. I’ve created 500 invoices simultaneously for testing.

SQL query: INSERT INTO "InvoiceEvents" ("InvoiceDataId", "UniqueId", "Message", "Severity", "Timestamp") VALUES ($1, $2, $3, $4, $5)

On production environment spikes are gone but I continue to monitor the number of connections.

These are less important, we can create a batching tool to schedule a mass insert every x interval

1reaction
Rheopyrincommented, Oct 28, 2020

@pavlenex I’ll ask our customer support team to provide actual feedback.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is it normal for database connections to spike like this?
I am running a Postgres database on Lightsail, and I am connecting to it ... I am a little confused by the spike...
Read more >
Sudden connection and load average spikes with ...
Hello all, This is my very first message to the Postgresql community, and I really hope you can help me solve the trouble...
Read more >
Sudden connection and load average spikes with postgresql 9.3
I'm monitoring the pg_stat_activity table and when connections raise there's no query being processed that could block a table and cause the other...
Read more >
Analyzing the Limits of Connection Scalability in Postgres
Why connection scalability in Postgres is important · Central state and spikey load require large numbers of connections · Latency and result ...
Read more >
How to Manage Connections Efficiently in Postgres, or Any ...
How to Manage Connections Efficiently in Postgres, or Any Database ... continues to ramp up, when suddenly a big spike of failures appears....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found