Skip to main content

mz_postgres_util/
lib.rs

1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Use of this software is governed by the Business Source License
4// included in the LICENSE file.
5//
6// As of the Change Date specified in that file, in accordance with
7// the Business Source License, use of this software will be governed
8// by the Apache License, Version 2.0.
9
10//! PostgreSQL utility library.
11
12#[cfg(feature = "replication")]
13pub mod replication;
14#[cfg(feature = "replication")]
15pub use replication::{
16    available_replication_slots, drop_replication_slots, fetch_max_lsn, get_is_in_recovery,
17    get_max_wal_senders, get_timeline_id, get_wal_level, validate_no_rls_policies,
18};
19#[cfg(feature = "schemas")]
20pub mod desc;
21#[cfg(feature = "schemas")]
22pub mod schemas;
23#[cfg(feature = "schemas")]
24pub use schemas::{get_schemas, publication_info};
25#[cfg(feature = "tunnel")]
26pub mod tunnel;
27#[cfg(feature = "tunnel")]
28pub use tunnel::{Client, Config, DEFAULT_SNAPSHOT_STATEMENT_TIMEOUT, TunnelConfig};
29
30pub mod query;
31pub use mz_ore::sql;
32pub use query::{
33    Sql, SqlFormatError, batch_execute, execute, execute_prepared, query, query_one,
34    query_one_prepared, query_opt, query_opt_prepared, query_prepared, simple_query,
35    simple_query_opt,
36};
37
38/// An error representing pg, ssh, ssl, and other failures.
39#[derive(Debug, thiserror::Error)]
40pub enum PostgresError {
41    /// Any other error we bail on.
42    #[error(transparent)]
43    Generic(#[from] anyhow::Error),
44    /// Error using ssh.
45    #[cfg(feature = "tunnel")]
46    #[error("error setting up ssh: {0}")]
47    Ssh(#[source] anyhow::Error),
48    /// Error doing io to setup an ssh connection.
49    #[error("error communicating with ssh tunnel: {0}")]
50    SshIo(std::io::Error),
51    /// Error doing io to setup a connection.
52    #[error("IO error in connection: {0}")]
53    Io(#[from] std::io::Error),
54    /// A postgres error.
55    #[error(transparent)]
56    Postgres(#[from] tokio_postgres::Error),
57    /// Error setting up postgres ssl.
58    #[error(transparent)]
59    PostgresSsl(#[from] openssl::error::ErrorStack),
60    #[error("query returned more rows than expected")]
61    UnexpectedRow,
62    /// Cannot find publication
63    ///
64    /// This error is more specific than the others because its occurrence has
65    /// differing semantics from other types of PG errors.
66    #[error("publication {0} does not exist")]
67    PublicationMissing(String),
68    #[error("one or more tables requires BYPASSRLS: {0:?}")]
69    BypassRLSRequired(Vec<String>),
70}