Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .schema/pgdog.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
"pub_sub_channel_size": 0,
"query_cache_limit": 1000,
"query_log": null,
"query_log_stdout": false,
"query_parser": "auto",
"query_parser_enabled": false,
"query_parser_engine": "pg_query_protobuf",
Expand Down Expand Up @@ -972,6 +973,11 @@
],
"default": null
},
"query_log_stdout": {
"description": "Log queries to stdout. Format: `query [database: db, user: user]`",
"type": "boolean",
"default": false
},
"query_parser": {
"description": "Toggle the query parser to enable/disable query parsing and all of its benefits. By default, the query parser is turned on automatically, so only disable it if you know what you're doing.\n\n_Default:_ `auto`\n\nhttps://docs.pgdog.dev/configuration/pgdog.toml/general/#query_parser",
"$ref": "#/$defs/QueryParserLevel",
Expand Down
18 changes: 18 additions & 0 deletions pgdog-config/src/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,10 @@ pub struct General {
#[serde(default)]
pub query_log: Option<PathBuf>,

/// Log queries to stdout. Format: `query [database: db, user: user]`
#[serde(default = "General::query_log_stdout")]
pub query_log_stdout: bool,

/// Minimum parse duration in milliseconds that triggers a warning log with the query text.
/// Queries whose parsing takes longer than this value are logged at WARN level.
/// Set to `0` or omit to disable.
Expand Down Expand Up @@ -800,6 +804,7 @@ impl Default for General {
broadcast_address: Self::broadcast_address(),
broadcast_port: Self::broadcast_port(),
query_log: Self::query_log(),
query_log_stdout: Self::query_log_stdout(),
log_min_duration_parse: Self::default_log_min_duration_parse(),
log_query_sample_length: Self::log_query_sample_length(),
openmetrics_port: Self::openmetrics_port(),
Expand Down Expand Up @@ -1188,6 +1193,10 @@ impl General {
Self::env_option_string("PGDOG_QUERY_LOG").map(PathBuf::from)
}

fn query_log_stdout() -> bool {
Self::env_bool_or_default("PGDOG_QUERY_LOG_STDOUT", false)
}

fn default_log_min_duration_parse() -> Option<u64> {
Self::env_option("PGDOG_LOG_MIN_DURATION_PARSE")
}
Expand Down Expand Up @@ -1587,6 +1596,15 @@ mod tests {
assert_eq!(General::query_log(), None);
}

#[test]
fn test_query_log_stdout_env() {
env::set_var("PGDOG_QUERY_LOG_STDOUT", "true");
assert!(General::query_log_stdout());

env::remove_var("PGDOG_QUERY_LOG_STDOUT");
assert!(!General::query_log_stdout());
}

#[test]
fn test_env_numeric_fields() {
env::set_var("PGDOG_BROADCAST_PORT", "7432");
Expand Down
5 changes: 5 additions & 0 deletions pgdog/src/frontend/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ pub struct Client {
sticky: Sticky,
/// Client database.
database: String,
/// Log queries to stdout.
query_log_stdout: bool,
}

impl Client {
Expand Down Expand Up @@ -379,6 +381,7 @@ impl Client {
stream_buffer: MessageBuffer::new(config.config.memory.message_buffer),
sticky: Sticky::from_params(&params),
database: database.to_string(),
query_log_stdout: false,
}))
}

Expand Down Expand Up @@ -410,6 +413,7 @@ impl Client {
sticky: Sticky::from_params(&connect_params),
params: connect_params,
database: "pgdog".to_string(),
query_log_stdout: false,
}
}

Expand Down Expand Up @@ -580,6 +584,7 @@ impl Client {
// Configure prepared statements cache.
self.prepared_statements.level = config.prepared_statements();
self.timeouts = Timeouts::from_config(&config.config.general);
self.query_log_stdout = config.config.general.query_log_stdout;

while !self.client_request.is_complete() {
let idle_timeout = self
Expand Down
4 changes: 4 additions & 0 deletions pgdog/src/frontend/client/query_engine/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ pub struct QueryEngineContext<'a> {
pub(super) sticky: Sticky,
/// Rewrite result.
pub(super) rewrite_result: Option<RewriteResult>,
/// Log queries to stdout.
pub(super) query_log_stdout: bool,
}

impl<'a> QueryEngineContext<'a> {
Expand All @@ -60,6 +62,7 @@ impl<'a> QueryEngineContext<'a> {
rollback: false,
sticky: client.sticky,
rewrite_result: None,
query_log_stdout: client.query_log_stdout,
}
}

Expand All @@ -86,6 +89,7 @@ impl<'a> QueryEngineContext<'a> {
rollback: false,
sticky: Sticky::new(),
rewrite_result: None,
query_log_stdout: false,
}
}

Expand Down
4 changes: 4 additions & 0 deletions pgdog/src/frontend/client/query_engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub mod multi_step;
pub mod notify_buffer;
pub mod pub_sub;
pub mod query;
mod query_log_stdout;
pub mod rewrite;
pub mod route_query;
pub mod set;
Expand All @@ -39,6 +40,7 @@ pub mod two_pc;
pub mod unknown_command;

use self::query::ExplainResponseState;
use self::query_log_stdout::log_query_stdout;
pub(crate) use advisory_lock::AdvisoryLocks;
pub use context::QueryEngineContext;
use notify_buffer::NotifyBuffer;
Expand Down Expand Up @@ -108,6 +110,8 @@ impl QueryEngine {
.received(context.client_request.total_message_len());
self.set_state(State::Active); // Client is active.

log_query_stdout(context);

// Rewrite prepared statements.
self.rewrite_extended(context)?;

Expand Down
21 changes: 21 additions & 0 deletions pgdog/src/frontend/client/query_engine/query_log_stdout.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use tracing::info;

use super::QueryEngineContext;
use crate::util::user_database_from_params;

pub(super) fn log_query_stdout(context: &QueryEngineContext<'_>) {
if !context.query_log_stdout {
return;
}

if let Ok(Some(query)) = context.client_request.query() {
let (user, database) = user_database_from_params(context.params);
let query_one_line = query.query().replace(['\r', '\n'], " ");
info!(
"{} [database: {}, user: {}]",
query_one_line.trim(),
database,
user
);
}
}
Loading