jtv / libpqxx

The official C++ client API for PostgreSQL.
http://pqxx.org/libpqxx/
BSD 3-Clause "New" or "Revised" License
1.03k stars 241 forks source link

Getting parameterised query text without placeholders #902

Open GoldAndPurple opened 3 days ago

GoldAndPurple commented 3 days ago

Is there a way to know the proper text of the executed query, with placeholder text replaced with bound parameters? Relevant for exception handling and logging purposes, where you might not know the parameters used, but need information the most. Right now there is a weird pattern for more complicated queries. It's better to format query-strings with escaped parameters in the application itself, "on your own", and only then execute the resulting statement with no parameters.

jtv commented 3 days ago

Alas, I don't know if any way to get that version of the query string.

I guess it would be possible to reconstruct it, but that can go wrong in exactly the kind of situation where you need it most.

GoldAndPurple commented 2 days ago

I suppose the main problem is that postgres itself doesn't always log parameters, so we have to rely on either the library, or the application to remember them. But what are the pitfalls of reconstructing the query post-execution? The only thing that wouldn't be exactly the same is raw binary data (and its user-defined equivalents), right? Everything else is passed as text, so what's the problem with replacing placeholders with pqxx-escaped parameters?