1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
use std::{borrow::Cow, fmt, str::FromStr};

use postgres_protocol::escape::{escape_identifier, escape_literal};

use super::sqlx;

trait AsSql {
    fn as_sql(&self) -> Cow<'_, str>;
}

/// Reload configuration using `pg_reload_conf`. Equivalent to `SIGHUP` or
/// `pg_ctl reload`.
pub async fn reload(pool: &sqlx::PgPool) -> Result<(), sqlx::Error> {
    sqlx::query("SELECT pg_reload_conf()").execute(pool).await?;
    Ok(())
}

pub enum AlterSystem<'a> {
    Set(&'a Parameter<'a>, &'a Value),
    Reset(&'a Parameter<'a>),
    ResetAll,
}

impl<'a> AlterSystem<'a> {
    /// Alter the system. Changes made by `ALTER SYSTEM` may require a reload or
    /// even a full restart to take effect.
    pub async fn apply(&self, pool: &sqlx::PgPool) -> Result<(), sqlx::Error> {
        sqlx::query(&self.as_sql()).execute(pool).await?;
        Ok(())
    }
}

impl AsSql for AlterSystem<'_> {
    /// Return the SQL to apply this change.
    fn as_sql(&self) -> Cow<'_, str> {
        use AlterSystem::*;
        match self {
            Set(p, v) => format!("ALTER SYSTEM SET {} TO {}", p.as_sql(), v.as_sql()).into(),
            Reset(p) => format!("ALTER SYSTEM RESET {}", p.as_sql()).into(),
            ResetAll => "ALTER SYSTEM RESET ALL".into(),
        }
    }
}

/// A setting as defined in `pg_catalog.pg_settings`.
///
/// This is fairly stringly-typed and mostly informational. For getting and
/// setting values, [`Parameter`] and [`Value`] may be more convenient.
///
/// **Note** that this does not work on PostgreSQL 9.4 and earlier because the
/// `pending_restart` column does not exist. PostgreSQL 9.4 has long been
/// obsolete so a workaround is not provided.
///
/// See the [documentation for
/// `pg_settings`](https://www.postgresql.org/docs/current/view-pg-settings.html).
#[derive(Debug, Clone, sqlx::FromRow)]
pub struct Setting {
    pub name: String,
    pub setting: String,
    pub unit: Option<String>,
    pub category: String,
    pub short_desc: String,
    pub extra_desc: Option<String>,
    pub context: String,
    pub vartype: String,
    pub source: String,
    pub min_val: Option<String>,
    pub max_val: Option<String>,
    pub enumvals: Option<Vec<String>>,
    pub boot_val: Option<String>,
    pub reset_val: Option<String>,
    pub sourcefile: Option<String>,
    pub sourceline: Option<i32>,
    pub pending_restart: bool,
}

impl Setting {
    pub async fn list(pool: &sqlx::PgPool) -> Result<Vec<Self>, sqlx::Error> {
        sqlx::query_as(
            r"
            SELECT
                name,
                setting,
                unit,
                category,
                short_desc,
                extra_desc,
                context,
                vartype,
                source,
                min_val,
                max_val,
                enumvals,
                boot_val,
                reset_val,
                sourcefile,
                sourceline,
                pending_restart
            FROM
                pg_catalog.pg_settings
            ",
        )
        .fetch_all(pool)
        .await
    }

    pub async fn get<N: AsRef<str>>(
        name: N,
        pool: &sqlx::PgPool,
    ) -> Result<Option<Self>, sqlx::Error> {
        sqlx::query_as(
            r"
            SELECT
                name,
                setting,
                unit,
                category,
                short_desc,
                extra_desc,
                context,
                vartype,
                source,
                min_val,
                max_val,
                enumvals,
                boot_val,
                reset_val,
                sourcefile,
                sourceline,
                pending_restart
            FROM
                pg_catalog.pg_settings
            WHERE
                name = $1
            ",
        )
        .bind(name.as_ref())
        .fetch_optional(pool)
        .await
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Parameter<'a>(pub &'a str);

impl<'a> Parameter<'a> {
    /// Get the current [`Value`] for this parameter.
    ///
    /// If you want the full/raw [`Setting`], use [`Setting::get`] instead.
    pub async fn get(&self, pool: &sqlx::PgPool) -> Result<Option<Value>, sqlx::Error> {
        Setting::get(self.0, pool)
            .await?
            .map(|setting| {
                Value::try_from(&setting)
                    .map_err(Into::into)
                    .map_err(sqlx::Error::Decode)
            })
            .transpose()
    }

    /// Set the current value for this parameter.
    pub async fn set<V: Into<Value>>(
        &self,
        pool: &sqlx::PgPool,
        value: V,
    ) -> Result<(), sqlx::Error> {
        let value = value.into();
        AlterSystem::Set(self, &value).apply(pool).await?;
        Ok(())
    }

    /// Reset the value for this parameter.
    pub async fn reset(&self, pool: &sqlx::PgPool) -> Result<(), sqlx::Error> {
        AlterSystem::Reset(self).apply(pool).await?;
        Ok(())
    }
}

impl AsSql for Parameter<'_> {
    /// Return this parameter name escaped as an SQL identifier.
    fn as_sql(&self) -> Cow<'_, str> {
        escape_identifier(self.0).into()
    }
}

impl<'a> fmt::Display for Parameter<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl<'a> AsRef<str> for Parameter<'a> {
    fn as_ref(&self) -> &str {
        self.0
    }
}

impl<'a> From<&'a str> for Parameter<'a> {
    fn from(name: &'a str) -> Self {
        Self(name)
    }
}

impl<'a> From<&'a Setting> for Parameter<'a> {
    fn from(setting: &'a Setting) -> Self {
        Self(&setting.name)
    }
}

#[derive(Debug, PartialEq)]
pub enum Value {
    Boolean(bool),
    String(String), // Or enumerated.
    Number(String),
    Memory(String, MemoryUnit),
    Time(String, TimeUnit),
}

impl AsSql for Value {
    /// Return this parameter value escaped as an SQL literal.
    fn as_sql(&self) -> Cow<'_, str> {
        match self {
            Value::Boolean(true) => "true".into(),
            Value::Boolean(false) => "false".into(),
            Value::String(value) => escape_literal(value).into(),
            Value::Number(value) => value.into(),
            Value::Memory(value, unit) => escape_literal(&format!("{value}{unit}")).into(),
            Value::Time(value, unit) => escape_literal(&format!("{value}{unit}")).into(),
        }
    }
}

impl fmt::Display for Value {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Value::Boolean(value) => write!(f, "{value}"),
            Value::String(value) => write!(f, "{value}"),
            Value::Number(value) => write!(f, "{value}"),
            Value::Memory(value, unit) => write!(f, "{value}{unit}"),
            Value::Time(value, unit) => write!(f, "{value}{unit}"),
        }
    }
}

impl From<bool> for Value {
    fn from(value: bool) -> Self {
        Value::Boolean(value)
    }
}

impl From<&str> for Value {
    fn from(value: &str) -> Self {
        Value::String(value.to_owned())
    }
}

impl From<String> for Value {
    fn from(value: String) -> Self {
        Value::String(value)
    }
}

impl From<&String> for Value {
    fn from(value: &String) -> Self {
        Value::String(value.clone())
    }
}

macro_rules! value_number_from {
    ($($from_type:ty),*) => {
        $(
            impl From<$from_type> for Value {
                fn from(number: $from_type) -> Self {
                    Value::Number(number.to_string())
                }
            }
        )*
    }
}

value_number_from!(i8, i16, i32, i64, i128);
value_number_from!(u8, u16, u32, u64, u128);
value_number_from!(f32, f64);
value_number_from!(usize, isize);

macro_rules! value_memory_from {
    ($($from_type:ty),*) => {
        $(
            impl From<($from_type, MemoryUnit)> for Value {
                fn from((number, unit): ($from_type, MemoryUnit)) -> Self {
                    Value::Memory(number.to_string(), unit)
                }
            }
        )*
    }
}

value_memory_from!(i8, i16, i32, i64, i128);
value_memory_from!(u8, u16, u32, u64, u128);
value_memory_from!(f32, f64);
value_memory_from!(usize, isize);

macro_rules! value_time_from {
    ($($from_type:ty),*) => {
        $(
            impl From<($from_type, TimeUnit)> for Value {
                fn from((number, unit): ($from_type, TimeUnit)) -> Self {
                    Value::Time(number.to_string(), unit)
                }
            }
        )*
    }
}

value_time_from!(i8, i16, i32, i64, i128);
value_time_from!(u8, u16, u32, u64, u128);
value_time_from!(f32, f64);
value_time_from!(usize, isize);

impl TryFrom<&Setting> for Value {
    type Error = String;

    fn try_from(setting: &Setting) -> Result<Self, Self::Error> {
        Ok(match setting.vartype.as_ref() {
            "bool" => match setting.setting.as_ref() {
                "on" | "true" | "tru" | "tr" | "t" => Self::Boolean(true),
                "yes" | "ye" | "y" | "1" => Self::Boolean(true),
                "off" | "of" | "false" | "fals" | "fal" | "fa" | "f" => Self::Boolean(false),
                "no" | "n" | "0" => Self::Boolean(false),
                _ => return Err(format!("invalid boolean value: {setting:?}")),
            },
            "integer" | "real" => match setting.unit.as_deref() {
                None => Self::Number(setting.setting.clone()),
                Some("8kB" | "16MB") => Self::Number(setting.setting.clone()), // Special cases 🤷
                Some(unit) => {
                    if let Ok(unit) = unit.parse::<MemoryUnit>() {
                        Self::Memory(setting.setting.clone(), unit)
                    } else if let Ok(unit) = unit.parse::<TimeUnit>() {
                        Self::Time(setting.setting.clone(), unit)
                    } else {
                        return Err(format!("invalid numeric value: {setting:?}"));
                    }
                }
            },
            "string" => Self::String(setting.setting.clone()),
            "enum" => Self::String(setting.setting.clone()),
            _ => return Err(format!("unrecognised value type: {setting:?}")),
        })
    }
}

/// Memory units recognised in PostgreSQL parameter values.
/// <https://www.postgresql.org/docs/16/config-setting.html#CONFIG-SETTING-NAMES-VALUES>
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum MemoryUnit {
    Bytes,
    Kibibytes,
    Mebibytes,
    Gibibytes,
    Tebibytes,
}

impl fmt::Display for MemoryUnit {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            MemoryUnit::Bytes => write!(f, "B"),
            MemoryUnit::Kibibytes => write!(f, "kB"),
            MemoryUnit::Mebibytes => write!(f, "MB"),
            MemoryUnit::Gibibytes => write!(f, "GB"),
            MemoryUnit::Tebibytes => write!(f, "TB"),
        }
    }
}

impl FromStr for MemoryUnit {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "B" => Ok(MemoryUnit::Bytes),
            "kB" => Ok(MemoryUnit::Kibibytes),
            "MB" => Ok(MemoryUnit::Mebibytes),
            "GB" => Ok(MemoryUnit::Gibibytes),
            "TB" => Ok(MemoryUnit::Tebibytes),
            _ => Err(format!("invalid memory unit: {s:?}")),
        }
    }
}

/// Time units recognised in PostgreSQL parameter values.
/// <https://www.postgresql.org/docs/16/config-setting.html#CONFIG-SETTING-NAMES-VALUES>
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum TimeUnit {
    Microseconds,
    Milliseconds,
    Seconds,
    Minutes,
    Hours,
    Days,
}

impl fmt::Display for TimeUnit {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            TimeUnit::Microseconds => write!(f, "us"),
            TimeUnit::Milliseconds => write!(f, "ms"),
            TimeUnit::Seconds => write!(f, "s"),
            TimeUnit::Minutes => write!(f, "min"),
            TimeUnit::Hours => write!(f, "h"),
            TimeUnit::Days => write!(f, "d"),
        }
    }
}

impl FromStr for TimeUnit {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "us" => Ok(TimeUnit::Microseconds),
            "ms" => Ok(TimeUnit::Milliseconds),
            "s" => Ok(TimeUnit::Seconds),
            "min" => Ok(TimeUnit::Minutes),
            "h" => Ok(TimeUnit::Hours),
            "d" => Ok(TimeUnit::Days),
            _ => Err(format!("invalid time unit: {s:?}")),
        }
    }
}

#[cfg(test)]
mod tests {
    use paste::paste;

    use super::{
        AsSql,
        MemoryUnit::{self, *},
        Parameter,
        TimeUnit::{self, *},
        Value,
    };

    #[test]
    fn test_parameter_as_sql() {
        assert_eq!(Parameter("foo").as_sql(), "\"foo\"");
        assert_eq!(Parameter("foo \\bar").as_sql(), "\"foo \\bar\"");
        assert_eq!(Parameter("foo\"bar").as_sql(), "\"foo\"\"bar\"");
    }

    #[test]
    fn test_value_as_sql_bool() {
        assert_eq!(Value::Boolean(false).as_sql(), "false");
        assert_eq!(Value::Boolean(true).as_sql(), "true");
    }

    #[test]
    fn test_value_as_sql_string() {
        assert_eq!(Value::from("foo").as_sql(), "'foo'");
        assert_eq!(Value::from("foo \\bar").as_sql(), " E'foo \\\\bar'");
        assert_eq!(Value::from("foo'\"'bar").as_sql(), "'foo''\"''bar'");
    }

    #[test]
    fn test_value_as_sql_number() {
        // Numbers are represented as strings, and displayed verbatim, with no
        // escaping. Not ideal. An alternative would be to have signed/unsigned
        // integers (as i128/u128) and floating points (as f64) separately. But
        // PostgreSQL also has arbitrary precision numbers. For now, we'll live
        // with this.
        assert_eq!(Value::Number("123".into()).as_sql(), "123");
        assert_eq!(Value::Number("123.456".into()).as_sql(), "123.456");
    }

    #[test]
    fn test_value_as_sql_memory() {
        assert_eq!(
            Value::Memory("123.4".into(), Gibibytes).as_sql(),
            "'123.4GB'",
        );
    }

    #[test]
    fn test_value_as_sql_time() {
        assert_eq!(Value::Time("123.4".into(), Hours).as_sql(), "'123.4h'",);
    }

    macro_rules! test_value_number_from {
        ($($from_type:ty),*) => {
            $(
                paste! {
                    #[test]
                    fn [< test_value_number_from_ $from_type >]() {
                        assert_eq!(Value::from(42 as $from_type), Value::Number("42".into()));
                    }
                }
            )*
        }
    }

    test_value_number_from!(i8, i16, i32, i64, i128);
    test_value_number_from!(u8, u16, u32, u64, u128);
    test_value_number_from!(f32, f64);
    test_value_number_from!(usize, isize);

    #[test]
    fn test_memory_unit_roundtrip() {
        let units = &[Bytes, Kibibytes, Mebibytes, Gibibytes, Tebibytes];
        for unit in units {
            assert_eq!(format!("{unit}").parse::<MemoryUnit>(), Ok(*unit));
        }
    }

    #[test]
    fn test_time_unit_roundtrip() {
        let units = &[Microseconds, Milliseconds, Seconds, Minutes, Hours, Days];
        for unit in units {
            assert_eq!(format!("{unit}").parse::<TimeUnit>(), Ok(*unit));
        }
    }
}