Comments (10)
OK, I think I finally understand what's going on.
For the leak to happen you have to:
- Send
LISTEN
query - Send
NOTIFY
query - Send a query with extended query protocol with a parameter written as binary (and not text, like what psql does)
What's leaking is the array for parameter's OID (and in some cases, a string for the query) because they're allocated in TopMemoryContext
instead of MessageContext
how they're supposed to.
Now what's happening: is
MessageContext
is set afterNOTIFY
query is donenotifyInterruptPending
flag is set because there is a notification, so pg has to send notification- Afterwards ProcessIncomingNotify is called, which starts a transaction and changes the current memory context to CurTransactionContext
- When transaction is commited it calls AtCommit_Memory, which changes current context to TopMemoryContext (instead of
MessageContext
in which we've started) - This makes it so all of the allocations for
Parse
command (at the very least inpostgres.c
) are going to happen inTopMemoryContext
, which is going to stay until pg process is destroyed.
Now, I'm afraid I'm going to be busy with other stuff for at least the next few weeks, so I wouldn't be able to notify pg's maintainers. If anyone is interested in raising this with pg folks and potentially fixing it themselves, please feel free to do so.
from npgsql.
Can also be reproed with psql:
- Change client encoding to a different one from pg (
SET client_encoding = 'LATIN1'
) - Subscribe to a channel (
LISTEN any_channel
) - Do
NOTIFY
/SELECT 1
loop
This way it leaks query text string and in my case it's 80 bytes per loop.
from npgsql.
I filed a Postgres bug report for this. Here is a link to the development thread:
https://www.postgresql.org/message-id/3478884.1718656625%40sss.pgh.pa.us
Tom, if you're reading this, next time I'll submit bug details instead of linking to a GitHub page, sorry about that.
Thanks everyone for your help.
from npgsql.
@WizardBrony great, thank you!
from npgsql.
@WizardBrony just to be sure, what makes you think there's a leak here (where memory continues to be referenced), rather than Npgsql simply allocating memory that will be garbage-collected eventually? If a real leak is involved, program memory should continuously always grow, and eventually cause an OutOfMemoryException; is that really what you're seeing?
from npgsql.
@WizardBrony just to be sure, what makes you think there's a leak here (where memory continues to be referenced), rather than Npgsql simply allocating memory that will be garbage-collected eventually? If a real leak is involved, program memory should continuously always grow, and eventually cause an OutOfMemoryException; is that really what you're seeing?
Just to clarify, I'm seeing the actual Postgres backend process leak memory, not Npgsql, which means this could in fact be a bug in Postgres itself. However, I do not know the details of the LISTEN/NOTIFY or command parameter protocols nor Npgsql's implementation of them to know for a fact that it's not an issue in Npgsql, so I wanted to bring the issue up here first.
from npgsql.
Yeah, sorry - @vonzshik and @NinoFloris pointed that out to me offline. @vonzshik is taking a look at it, but as you say, this is most likely an issue in PG rather than Npgsql.
from npgsql.
Sounds good, thanks for looking into it. I appreciate it.
from npgsql.
Is there a usage pattern which npgsql could adopt which would avoid this pg leakage?
from npgsql.
Is there a usage pattern which npgsql could adopt which would avoid this pg leakage?
You could try sending SELECT 1
query after each NOTIFY
, and as long as there is no encoding mismatch between frontend (npgsql) and backend (pg) it should help. But from the example above, this can be easily reproduced with psql, and that means that leak can happen with any pg provider as long as they have different encodings or they're using extended query protocol.
from npgsql.
Related Issues (20)
- Received backend message CommandComplete while expecting ParseCompleteMessage HOT 2
- CommandTimeout still affects BeginTextImport (and maybe BeginTextExport)
- Support exporting data via LogicalReplicationConnection
- Support Logical Streaming Replication Protocol V4
- Value was either too large or too small for a Decimal. HOT 3
- Increasing CPU usage with Multiplexing and OpenTelemetry Tracing HOT 4
- Npgsql.NpgsqlException: No suitable host was found. HOT 5
- NodaTime.Period is not supported for parameters having NpgsqlDbType Interval HOT 8
- Add a loop for connecting during failover
- Add SCRAM support to https://www.npgsql.org/doc/security.html?tabs=tabid-1#password-management
- Evaluate skipped macOS tests
- Support the PG17 ALTER_REPLICATION_SLOT syntax and the failover option in ReplicationConnection
- Improve error message when connection to Postgres fails HOT 11
- Retrieving an index of new positional paramer HOT 3
- Error messages should not have built-in formatting HOT 4
- Npgsql.NpgsqlDataSourceBuilder threw an exception HOT 2
- Connection leak with unpooled connections and TransactionScope and Multihost HOT 4
- Checkbox not showing for Boolean field in dataGridView HOT 3
- How Open Telemetry traces for query latency is measured? HOT 1
- Connection pool blocked while refreshing hosts state cache? HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from npgsql.