Skip to content

Commit 53602fe

Browse files
committed
save
1 parent 85da50f commit 53602fe

11 files changed

Lines changed: 243 additions & 34 deletions

File tree

pgdog/src/admin/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pub mod setup_schema;
2222
pub mod show_client_memory;
2323
pub mod show_clients;
2424
pub mod show_config;
25+
pub mod show_fdw;
2526
pub mod show_instance_id;
2627
pub mod show_lists;
2728
pub mod show_mirrors;

pgdog/src/admin/parser.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ use super::{
55
prelude::Message, probe::Probe, reconnect::Reconnect, reload::Reload,
66
reset_query_cache::ResetQueryCache, set::Set, setup_schema::SetupSchema,
77
show_client_memory::ShowClientMemory, show_clients::ShowClients, show_config::ShowConfig,
8-
show_instance_id::ShowInstanceId, show_lists::ShowLists, show_mirrors::ShowMirrors,
9-
show_peers::ShowPeers, show_pools::ShowPools, show_prepared_statements::ShowPreparedStatements,
10-
show_query_cache::ShowQueryCache, show_replication::ShowReplication,
11-
show_server_memory::ShowServerMemory, show_servers::ShowServers, show_stats::ShowStats,
12-
show_transactions::ShowTransactions, show_version::ShowVersion, shutdown::Shutdown, Command,
13-
Error,
8+
show_fdw::ShowFdw, show_instance_id::ShowInstanceId, show_lists::ShowLists,
9+
show_mirrors::ShowMirrors, show_peers::ShowPeers, show_pools::ShowPools,
10+
show_prepared_statements::ShowPreparedStatements, show_query_cache::ShowQueryCache,
11+
show_replication::ShowReplication, show_server_memory::ShowServerMemory,
12+
show_servers::ShowServers, show_stats::ShowStats, show_transactions::ShowTransactions,
13+
show_version::ShowVersion, shutdown::Shutdown, Command, Error,
1414
};
1515

1616
use tracing::debug;
@@ -44,6 +44,7 @@ pub enum ParseResult {
4444
Probe(Probe),
4545
MaintenanceMode(MaintenanceMode),
4646
Healthcheck(Healthcheck),
47+
ShowFdw(ShowFdw),
4748
}
4849

4950
impl ParseResult {
@@ -79,6 +80,7 @@ impl ParseResult {
7980
Probe(probe) => probe.execute().await,
8081
MaintenanceMode(maintenance_mode) => maintenance_mode.execute().await,
8182
Healthcheck(healthcheck) => healthcheck.execute().await,
83+
ShowFdw(show_fdw) => show_fdw.execute().await,
8284
}
8385
}
8486

@@ -114,6 +116,7 @@ impl ParseResult {
114116
Probe(probe) => probe.name(),
115117
MaintenanceMode(maintenance_mode) => maintenance_mode.name(),
116118
Healthcheck(healthcheck) => healthcheck.name(),
119+
ShowFdw(show_fdw) => show_fdw.name(),
117120
}
118121
}
119122
}
@@ -163,6 +166,7 @@ impl Parser {
163166
"lists" => ParseResult::ShowLists(ShowLists::parse(&sql)?),
164167
"prepared" => ParseResult::ShowPrepared(ShowPreparedStatements::parse(&sql)?),
165168
"replication" => ParseResult::ShowReplication(ShowReplication::parse(&sql)?),
169+
"fdw" => ParseResult::ShowFdw(ShowFdw::parse(&sql)?),
166170
command => {
167171
debug!("unknown admin show command: '{}'", command);
168172
return Err(Error::Syntax);
@@ -229,4 +233,10 @@ mod tests {
229233
let result = Parser::parse("SHOW CLIENT MEMORY;");
230234
assert!(matches!(result, Ok(ParseResult::ShowClientMemory(_))));
231235
}
236+
237+
#[test]
238+
fn parses_show_fdw_command() {
239+
let result = Parser::parse("SHOW FDW;");
240+
assert!(matches!(result, Ok(ParseResult::ShowFdw(_))));
241+
}
232242
}

pgdog/src/admin/show_fdw.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
use crate::backend::databases::databases;
2+
3+
use super::prelude::*;
4+
5+
pub struct ShowFdw;
6+
7+
#[async_trait]
8+
impl Command for ShowFdw {
9+
fn name(&self) -> String {
10+
"SHOW FDW".into()
11+
}
12+
13+
fn parse(_sql: &str) -> Result<Self, Error> {
14+
Ok(ShowFdw)
15+
}
16+
17+
async fn execute(&self) -> Result<Vec<Message>, Error> {
18+
let rd = RowDescription::new(&[
19+
Field::bigint("id"),
20+
Field::text("database"),
21+
Field::text("user"),
22+
Field::text("database_name"),
23+
Field::text("addr"),
24+
Field::numeric("port"),
25+
Field::text("role"),
26+
Field::numeric("sv_idle"),
27+
Field::numeric("sv_active"),
28+
Field::numeric("sv_total"),
29+
Field::numeric("total_xact_count"),
30+
Field::numeric("total_query_count"),
31+
Field::numeric("avg_xact_count"),
32+
Field::numeric("avg_query_count"),
33+
]);
34+
let mut messages = vec![rd.message()?];
35+
36+
for (user, cluster) in databases().all() {
37+
if let Some(pools) = cluster.fdw_pools() {
38+
for (role, _ban, pool) in pools {
39+
let state = pool.state();
40+
let stats = state.stats;
41+
let mut row = DataRow::new();
42+
row.add(pool.id() as i64)
43+
.add(user.database.as_str())
44+
.add(user.user.as_str())
45+
.add(pool.addr().database_name.as_str())
46+
.add(pool.addr().host.as_str())
47+
.add(pool.addr().port as i64)
48+
.add(role.to_string())
49+
.add(state.idle)
50+
.add(state.checked_out)
51+
.add(state.total)
52+
.add(stats.counts.xact_count)
53+
.add(stats.counts.query_count)
54+
.add(stats.averages.xact_count)
55+
.add(stats.averages.query_count);
56+
messages.push(row.message()?);
57+
}
58+
}
59+
}
60+
61+
Ok(messages)
62+
}
63+
}

pgdog/src/admin/tests/mod.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::net::messages::{DataRow, DataType, FromBytes, Protocol, RowDescriptio
66

77
use super::show_client_memory::ShowClientMemory;
88
use super::show_config::ShowConfig;
9+
use super::show_fdw::ShowFdw;
910
use super::show_lists::ShowLists;
1011
use super::show_mirrors::ShowMirrors;
1112
use super::show_pools::ShowPools;
@@ -465,3 +466,59 @@ async fn show_client_memory_reports_memory_stats() {
465466
assert_eq!(field.data_type(), *expected_type);
466467
}
467468
}
469+
470+
#[tokio::test(flavor = "current_thread")]
471+
async fn show_fdw_returns_correct_columns_with_no_fdw_configured() {
472+
let context = TestAdminContext::new();
473+
474+
let mut config = ConfigAndUsers::default();
475+
config.config.databases.push(Database {
476+
name: "app".into(),
477+
host: "127.0.0.1".into(),
478+
role: Role::Primary,
479+
shard: 0,
480+
..Default::default()
481+
});
482+
config.users.users.push(ConfigUser {
483+
name: "alice".into(),
484+
database: "app".into(),
485+
password: Some("secret".into()),
486+
..Default::default()
487+
});
488+
489+
context.set_config(config);
490+
491+
let command = ShowFdw;
492+
let messages = command.execute().await.expect("show fdw execution failed");
493+
494+
assert_eq!(
495+
messages.len(),
496+
1,
497+
"expected only row description when no FDW configured"
498+
);
499+
500+
let row_description = RowDescription::from_bytes(messages[0].payload())
501+
.expect("row description message should parse");
502+
let actual_names: Vec<&str> = row_description
503+
.fields
504+
.iter()
505+
.map(|field| field.name.as_str())
506+
.collect();
507+
let expected_names = vec![
508+
"id",
509+
"database",
510+
"user",
511+
"database_name",
512+
"addr",
513+
"port",
514+
"role",
515+
"sv_idle",
516+
"sv_active",
517+
"sv_total",
518+
"total_xact_count",
519+
"total_query_count",
520+
"avg_xact_count",
521+
"avg_query_count",
522+
];
523+
assert_eq!(actual_names, expected_names);
524+
}

pgdog/src/backend/databases.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ pub fn reload_from_existing() -> Result<(), Error> {
8686

8787
// Reconfigure FDW with new schema.
8888
if config.config.general.cross_shard_backend.need_fdw() {
89-
PostgresLauncher::get().reconfigure();
89+
PostgresLauncher::get().reconfigure()?;
9090
}
9191

9292
Ok(())
@@ -98,7 +98,7 @@ pub fn init() -> Result<(), Error> {
9898

9999
// Start postgres_fdw compatibility engine.
100100
if config.config.general.cross_shard_backend.need_fdw() {
101-
PostgresLauncher::get().launch();
101+
PostgresLauncher::get().launch()?;
102102
}
103103

104104
replace_databases(from_config(&config), false)?;
@@ -133,15 +133,15 @@ pub fn reload() -> Result<(), Error> {
133133
new_config.config.general.cross_shard_backend.need_fdw(),
134134
) {
135135
(true, true) => {
136-
PostgresLauncher::get().reconfigure();
136+
PostgresLauncher::get().reconfigure()?;
137137
}
138138

139139
(false, true) => {
140-
PostgresLauncher::get().launch();
140+
PostgresLauncher::get().launch()?;
141141
}
142142

143143
(true, false) => {
144-
PostgresLauncher::get().shutdown();
144+
PostgresLauncher::get().shutdown()?;
145145
}
146146

147147
(false, false) => {}

pgdog/src/backend/error.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,15 @@ pub enum Error {
137137

138138
#[error("{0}")]
139139
ForeignTable(#[from] crate::backend::schema::postgres_fdw::Error),
140+
141+
#[error("fdw: {0}")]
142+
Fdw(Box<crate::backend::fdw::Error>),
143+
}
144+
145+
impl From<crate::backend::fdw::Error> for Error {
146+
fn from(value: crate::backend::fdw::Error) -> Self {
147+
Self::Fdw(Box::new(value))
148+
}
140149
}
141150

142151
impl From<crate::frontend::Error> for Error {

pgdog/src/backend/fdw/error.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,7 @@ pub enum Error {
3333

3434
#[error("postgres process exited unexpectedly")]
3535
ProcessExited,
36+
37+
#[error("fdw launcher channel closed")]
38+
ChannelClosed,
3639
}

0 commit comments

Comments
 (0)