@@ -25,6 +25,7 @@ const mongo_functions = require('./mongo_functions');
25
25
const { RpcError } = require ( '../rpc' ) ;
26
26
const SensitiveString = require ( './sensitive_string' ) ;
27
27
const time_utils = require ( './time_utils' ) ;
28
+ const config = require ( '../../config' ) ;
28
29
mongodb . Binary . prototype [ util . inspect . custom ] = function custom_inspect_binary ( ) {
29
30
return `<mongodb.Binary ${ this . buffer . toString ( 'base64' ) } >` ;
30
31
} ;
@@ -181,7 +182,7 @@ let query_counter = 0;
181
182
async function log_query ( pg_client , query , tag , millitook , should_explain ) {
182
183
const log_obj = {
183
184
tag,
184
- took : millitook ,
185
+ took : millitook . toFixed ( 1 ) + 'ms' ,
185
186
query,
186
187
clients_pool : { total : pg_client . totalCount , waiting : pg_client . waitingCount , idle : pg_client . idleCount } ,
187
188
stack : ( new Error ( ) ) . stack . split ( '\n' ) . slice ( 1 ) ,
@@ -198,7 +199,11 @@ async function log_query(pg_client, query, tag, millitook, should_explain) {
198
199
}
199
200
}
200
201
201
- dbg . log0 ( 'QUERY_LOG:' , JSON . stringify ( log_obj ) ) ;
202
+ if ( millitook > config . LONG_DB_QUERY_THRESHOLD ) {
203
+ dbg . warn ( `QUERY_LOG: LONG QUERY (OVER ${ config . LONG_DB_QUERY_THRESHOLD } ms ` , JSON . stringify ( log_obj ) ) ;
204
+ } else {
205
+ dbg . log0 ( 'QUERY_LOG:' , JSON . stringify ( log_obj ) ) ;
206
+ }
202
207
}
203
208
204
209
function convert_sort ( sort ) {
@@ -239,8 +244,9 @@ async function _do_query(pg_client, q, transaction_counter) {
239
244
// dbg.log0(`postgres_client: ${tag}: ${q.text}`, util.inspect(q.values, { depth: 6 }));
240
245
const millistart = time_utils . millistamp ( ) ;
241
246
const res = await pg_client . query ( q ) ;
242
- const millitook = time_utils . millitook ( millistart ) ;
243
- if ( process . env . PG_ENABLE_QUERY_LOG === 'true' ) {
247
+ const milliend = time_utils . millistamp ( ) ;
248
+ const millitook = milliend - millistart ;
249
+ if ( process . env . PG_ENABLE_QUERY_LOG === 'true' || millitook > config . LONG_DB_QUERY_THRESHOLD ) {
244
250
// noticed that some failures in explain are invalidating the transaction.
245
251
// myabe did something wrong but for now don't try to EXPLAIN the query when in transaction.
246
252
await log_query ( pg_client , q , tag , millitook , /*should_explain*/ transaction_counter === 0 ) ;
@@ -762,7 +768,7 @@ class PostgresTable {
762
768
* https://github.com/thomas4019/mongo-query-to-postgres-jsonb/blob/e0bb65eafc39458da30e4fc3c5f47ffb5d509fcc/test/filter.js#L229
763
769
*/
764
770
function calculateOptionsAndArrayFields ( q ) {
765
- const ops = [ '$all' ] ;
771
+ const ops = [ '$all' ] ;
766
772
const l = [ ] ;
767
773
for ( const p of Object . keys ( q ) ) {
768
774
for ( const o of ops ) {
0 commit comments