pgdo-lib 0.7.1

The convenience of SQLite – but with PostgreSQL (Library package)
Documentation
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
//! A minimal, synchronous PostgreSQL client.
//!
//! This is just enough client for pgdo's own needs: it connects over a
//! Unix-domain socket, supports only [trust authentication][trust] – which is
//! how pgdo creates clusters – and sends queries using the [simple query
//! protocol][simple-query], receiving all values as text.
//!
//! It is synchronous in the most ordinary way: it blocks on socket I/O. Unlike
//! clients that wrap an async implementation, it is safe to call from within an
//! async context, though it will block the calling thread.
//!
//! To work with a cluster from your own code, use a full-featured client
//! library, connecting via [`Cluster::datadir`][`super::Cluster::datadir`]
//! or [`Cluster::url`][`super::Cluster::url`].
//!
//! [trust]: https://www.postgresql.org/docs/current/auth-trust.html
//! [simple-query]:
//!     https://www.postgresql.org/docs/current/protocol-flow.html#PROTOCOL-FLOW-SIMPLE-QUERY

use std::fmt;
use std::io::{self, Read, Write};
use std::os::unix::net::UnixStream;
use std::path::Path;

use bytes::BytesMut;
use fallible_iterator::FallibleIterator;
use postgres_protocol::message::{backend::Message, frontend};

use super::PORT;

/// The path to the Unix-domain socket of a server listening on `port` in
/// `socketdir`.
pub(super) fn socket_path(socketdir: &Path, port: u16) -> std::path::PathBuf {
    socketdir.join(format!(".s.PGSQL.{port}"))
}

/// Does a server accept connections on the Unix-domain socket for `port` in
/// `socketdir`?
///
/// This works across PID namespaces on the same kernel, where PostgreSQL's own
/// check – signalling the PID in `postmaster.pid` – does not. It does not work
/// across kernels, e.g. from macOS into a Linux VM.
///
/// Returns an error if we cannot tell, e.g. because the socket's path is too
/// long for this platform. Treating that as "no" could lead the caller to start
/// a second server on the same data.
pub(super) fn accepts_connections(socketdir: &Path, port: u16) -> io::Result<bool> {
    match UnixStream::connect(socket_path(socketdir, port)) {
        Ok(_) => Ok(true),
        Err(err)
            if matches!(
                err.kind(),
                io::ErrorKind::ConnectionRefused | io::ErrorKind::NotFound
            ) =>
        {
            Ok(false)
        }
        Err(err) => Err(err),
    }
}

/// SQLSTATE for "duplicate database".
pub(super) const DUPLICATE_DATABASE: &str = "42P04";
/// SQLSTATE for "undefined database" (a.k.a. "invalid catalog name").
pub(super) const UNDEFINED_DATABASE: &str = "3D000";

/// Error communicating with a cluster.
#[derive(thiserror::Error, Debug)]
pub enum ClientError {
    #[error("could not communicate with cluster")]
    IoError(#[from] io::Error),
    #[error(transparent)]
    ServerError(Box<ServerError>),
    #[error("cluster requires {0} authentication, which pgdo does not support")]
    AuthenticationError(&'static str),
    #[error("unexpected message from cluster: {0}")]
    ProtocolError(String),
}

impl ClientError {
    /// A suggestion for how to resolve this error, if there is one, e.g. a hint
    /// from the PostgreSQL server.
    pub fn help(&self) -> Option<String> {
        match self {
            Self::AuthenticationError(_) => Some(
                "pgdo creates clusters with trust authentication; check `pg_hba.conf` in the \
                 cluster."
                    .into(),
            ),
            Self::ServerError(err) => err.hint.clone(),
            Self::IoError(_) | Self::ProtocolError(_) => None,
        }
    }
}

impl From<ServerError> for ClientError {
    fn from(error: ServerError) -> Self {
        Self::ServerError(Box::new(error))
    }
}

/// An error reported by the PostgreSQL server.
#[derive(Debug, Clone)]
pub struct ServerError {
    /// e.g. `ERROR`, `FATAL`.
    pub severity: String,
    /// The [SQLSTATE code][sqlstate], e.g. `42P04`.
    ///
    /// [sqlstate]: https://www.postgresql.org/docs/current/errcodes-appendix.html
    pub code: String,
    pub message: String,
    pub detail: Option<String>,
    pub hint: Option<String>,
}

impl std::error::Error for ServerError {}

impl fmt::Display for ServerError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{}: {} (SQLSTATE {})",
            self.severity, self.message, self.code
        )?;
        if let Some(detail) = &self.detail {
            write!(f, "; {detail}")?;
        }
        Ok(())
    }
}

impl ServerError {
    fn from_fields(body: &postgres_protocol::message::backend::ErrorResponseBody) -> Self {
        let mut error = ServerError {
            severity: String::new(),
            code: String::new(),
            message: String::new(),
            detail: None,
            hint: None,
        };
        let mut fields = body.fields();
        // Stop at the first malformed field; we'll use what we have so far.
        while let Ok(Some(field)) = fields.next() {
            let value = String::from_utf8_lossy(field.value_bytes()).into_owned();
            match field.type_() {
                // Non-localised severity is preferred; it's only present in
                // PostgreSQL 9.6 and later, but that's everything we support.
                b'V' => error.severity = value,
                b'C' => error.code = value,
                b'M' => error.message = value,
                b'D' => error.detail = Some(value),
                b'H' => error.hint = Some(value),
                _ => (),
            }
        }
        error
    }
}

/// A row returned from a query. All values are text, or `None` for `NULL`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) struct Row(Vec<Option<String>>);

impl Row {
    /// Get the value in the given column. Panics if the column is out of
    /// range; that's a programming error.
    pub(super) fn get(&self, column: usize) -> Option<&str> {
        self.0[column].as_deref()
    }
}

/// A connection to a cluster.
pub(super) struct Client {
    stream: UnixStream,
    buf: BytesMut,
}

impl Client {
    /// Connect to the cluster listening in `socketdir`, with trust
    /// authentication.
    pub(super) fn connect(
        socketdir: &Path,
        user: &str,
        database: &str,
    ) -> Result<Self, ClientError> {
        let stream = UnixStream::connect(socket_path(socketdir, PORT))?;
        let mut client = Client { stream, buf: BytesMut::new() };

        let mut message = BytesMut::new();
        frontend::startup_message(
            [
                ("user", user),
                ("database", database),
                ("application_name", "pgdo"),
                // We decode values as UTF-8; ask the server to send them so.
                ("client_encoding", "UTF8"),
            ],
            &mut message,
        )?;
        client.stream.write_all(&message)?;

        // Authentication.
        match client.receive()? {
            Message::AuthenticationOk => (),
            Message::ErrorResponse(body) => Err(ServerError::from_fields(&body))?,
            Message::AuthenticationCleartextPassword => {
                Err(ClientError::AuthenticationError("password"))?;
            }
            Message::AuthenticationMd5Password(_) => Err(ClientError::AuthenticationError("MD5"))?,
            Message::AuthenticationSasl(_) => Err(ClientError::AuthenticationError("SASL"))?,
            Message::AuthenticationGss | Message::AuthenticationSspi => {
                Err(ClientError::AuthenticationError("GSSAPI/SSPI"))?;
            }
            message => Err(unexpected(&message))?,
        }

        // Wait for the server to be ready, ignoring parameter statuses, etc.
        loop {
            match client.receive()? {
                Message::ReadyForQuery(_) => break Ok(client),
                Message::ErrorResponse(body) => Err(ServerError::from_fields(&body))?,
                Message::ParameterStatus(_)
                | Message::BackendKeyData(_)
                | Message::NoticeResponse(_) => continue,
                message => Err(unexpected(&message))?,
            }
        }
    }

    /// Execute the given SQL and return all rows from all statements.
    ///
    /// This uses the simple query protocol, so `sql` may contain several
    /// statements, but cannot have parameters: escape values into it with
    /// [`postgres_protocol::escape`]. `COPY` is not supported.
    pub(super) fn query(&mut self, sql: &str) -> Result<Vec<Row>, ClientError> {
        let mut message = BytesMut::new();
        frontend::query(sql, &mut message)?;
        self.stream.write_all(&message)?;

        let mut rows = Vec::new();
        let mut error = None;
        loop {
            match self.receive()? {
                Message::ReadyForQuery(_) => break,
                Message::DataRow(body) => {
                    let buffer = body.buffer();
                    let row = body
                        .ranges()
                        .map(|range| {
                            Ok(range
                                .map(|range| String::from_utf8_lossy(&buffer[range]).into_owned()))
                        })
                        .collect()?;
                    rows.push(Row(row));
                    continue;
                }
                Message::ErrorResponse(body) => {
                    error = Some(ServerError::from_fields(&body));
                    continue;
                }
                Message::RowDescription(_)
                | Message::CommandComplete(_)
                | Message::EmptyQueryResponse
                | Message::NoticeResponse(_)
                | Message::ParameterStatus(_) => continue,
                message => Err(unexpected(&message))?,
            }
        }

        match error {
            Some(error) => Err(error)?,
            None => Ok(rows),
        }
    }

    /// Execute the given SQL, discarding any rows.
    pub(super) fn execute(&mut self, sql: &str) -> Result<(), ClientError> {
        self.query(sql).map(|_| ())
    }

    /// Receive one message from the server.
    fn receive(&mut self) -> Result<Message, ClientError> {
        loop {
            if let Some(message) = Message::parse(&mut self.buf)? {
                return Ok(message);
            } else {
                let mut chunk = [0u8; 8192];
                match self.stream.read(&mut chunk)? {
                    0 => Err(io::Error::from(io::ErrorKind::UnexpectedEof))?,
                    n => {
                        self.buf.extend_from_slice(&chunk[..n]);
                        continue;
                    }
                }
            }
        }
    }
}

impl Drop for Client {
    fn drop(&mut self) {
        // Say goodbye politely. If this fails the server will notice the
        // connection has gone anyway.
        let mut message = BytesMut::new();
        frontend::terminate(&mut message);
        let _ = self.stream.write_all(&message);
    }
}

fn unexpected(message: &Message) -> ClientError {
    // `Message` does not implement `Debug`, so describe it by its variant.
    let name = match message {
        Message::CopyInResponse(_) | Message::CopyOutResponse(_) | Message::CopyData(_) => "COPY",
        Message::NotificationResponse(_) => "notification",
        Message::AuthenticationSaslContinue(_) | Message::AuthenticationSaslFinal(_) => {
            "authentication"
        }
        _ => "other",
    };
    ClientError::ProtocolError(name.into())
}

#[cfg(test)]
mod tests {
    use super::{Client, ClientError, Row, UNDEFINED_DATABASE};
    use crate::cluster::{Cluster, DATABASE_POSTGRES};
    use crate::runtime::strategy::Strategy;

    type TestResult = Result<(), Box<dyn std::error::Error>>;

    fn with_cluster(test: impl FnOnce(&Cluster) -> TestResult) -> TestResult {
        let tempdir = tempfile::tempdir()?;
        let cluster = Cluster::new(tempdir.path().join("data"), Strategy::default())?;
        cluster.start(&[])?;
        let result = test(&cluster);
        cluster.stop()?;
        result
    }

    fn connect(cluster: &Cluster, database: &str) -> Result<Client, ClientError> {
        let user = crate::util::current_user().unwrap();
        Client::connect(&cluster.datadir, &user, database)
    }

    #[test]
    fn query_returns_rows_including_nulls() -> TestResult {
        with_cluster(|cluster| {
            let mut client = connect(cluster, DATABASE_POSTGRES)?;
            let rows = client.query("SELECT 1, NULL, 'three' UNION ALL SELECT 4, '5', NULL")?;
            assert_eq!(
                rows,
                vec![
                    Row(vec![Some("1".into()), None, Some("three".into())]),
                    Row(vec![Some("4".into()), Some("5".into()), None]),
                ]
            );
            Ok(())
        })
    }

    #[test]
    fn query_returns_rows_from_all_statements() -> TestResult {
        with_cluster(|cluster| {
            let mut client = connect(cluster, DATABASE_POSTGRES)?;
            let rows = client.query("SELECT 1; SELECT 2; ; SET application_name = 'foo'")?;
            let values: Vec<_> = rows.iter().map(|row| row.get(0)).collect();
            assert_eq!(values, vec![Some("1"), Some("2")]);
            Ok(())
        })
    }

    #[test]
    fn query_returns_large_results() -> TestResult {
        with_cluster(|cluster| {
            let mut client = connect(cluster, DATABASE_POSTGRES)?;
            // Larger than the read buffer, to exercise reassembly of messages.
            let rows = client.query("SELECT repeat('x', 100000) FROM generate_series(1, 3)")?;
            assert_eq!(rows.len(), 3);
            assert!(rows
                .iter()
                .all(|row| row.get(0).map(str::len) == Some(100_000)));
            Ok(())
        })
    }

    #[test]
    fn query_reports_server_errors_and_client_remains_usable() -> TestResult {
        with_cluster(|cluster| {
            let mut client = connect(cluster, DATABASE_POSTGRES)?;
            match client.query("SELECT * FROM no_such_table") {
                Err(ClientError::ServerError(error)) => {
                    assert_eq!(error.severity, "ERROR");
                    assert_eq!(error.code, "42P01"); // undefined_table.
                }
                other => panic!("unexpected result: {other:?}"),
            }
            assert_eq!(client.query("SELECT 'ok'")?[0].get(0), Some("ok"));
            Ok(())
        })
    }

    #[test]
    fn query_decodes_utf8() -> TestResult {
        with_cluster(|cluster| {
            let mut client = connect(cluster, DATABASE_POSTGRES)?;
            let rows = client.query("SELECT 'Hello, 世界 🐘'")?;
            assert_eq!(rows[0].get(0), Some("Hello, 世界 🐘"));
            Ok(())
        })
    }

    #[test]
    fn connect_reports_missing_database() -> TestResult {
        with_cluster(|cluster| {
            match connect(cluster, "no-such-database") {
                Err(ClientError::ServerError(error)) => {
                    assert_eq!(error.severity, "FATAL");
                    assert_eq!(error.code, UNDEFINED_DATABASE);
                }
                Err(err) => panic!("unexpected error: {err:?}"),
                Ok(_) => panic!("unexpectedly connected"),
            }
            Ok(())
        })
    }

    #[test]
    fn client_works_within_async_context() -> TestResult {
        // The `postgres` crate panics here; this client merely blocks.
        with_cluster(|cluster| {
            let runtime = tokio::runtime::Builder::new_current_thread().build()?;
            runtime.block_on(async {
                let mut client = connect(cluster, DATABASE_POSTGRES)?;
                assert_eq!(client.query("SELECT 1")?[0].get(0), Some("1"));
                Ok(())
            })
        })
    }
}