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 schema_change;
23#[cfg(feature = "schemas")]
24pub mod schemas;
25#[cfg(feature = "schemas")]
26pub use schemas::{get_schemas, publication_info};
27#[cfg(feature = "tunnel")]
28pub mod tunnel;
29#[cfg(feature = "tunnel")]
30pub use tunnel::{Client, Config, DEFAULT_SNAPSHOT_STATEMENT_TIMEOUT, TunnelConfig};
31
32pub mod query;
33pub use mz_ore::sql;
34pub use query::{
35    Sql, SqlFormatError, batch_execute, execute, execute_prepared, query, query_one,
36    query_one_prepared, query_opt, query_opt_prepared, query_prepared, simple_query,
37    simple_query_opt,
38};
39
40/// An error representing pg, ssh, ssl, and other failures.
41#[derive(Debug, thiserror::Error)]
42pub enum PostgresError {
43    /// Any other error we bail on.
44    #[error(transparent)]
45    Generic(#[from] anyhow::Error),
46    /// Error using ssh.
47    #[cfg(feature = "tunnel")]
48    #[error("error setting up ssh: {0}")]
49    Ssh(#[source] anyhow::Error),
50    /// Error doing io to setup an ssh connection.
51    #[error("error communicating with ssh tunnel: {0}")]
52    SshIo(std::io::Error),
53    /// Error doing io to setup a connection.
54    #[error("IO error in connection: {0}")]
55    Io(#[from] std::io::Error),
56    /// A postgres error.
57    #[error(transparent)]
58    Postgres(#[from] tokio_postgres::Error),
59    /// Error setting up postgres ssl.
60    #[error(transparent)]
61    PostgresSsl(#[from] openssl::error::ErrorStack),
62    #[error("query returned more rows than expected")]
63    UnexpectedRow,
64    /// Cannot find publication
65    ///
66    /// This error is more specific than the others because its occurrence has
67    /// differing semantics from other types of PG errors.
68    #[error("publication {0} does not exist")]
69    PublicationMissing(String),
70    #[error("one or more tables requires BYPASSRLS: {0:?}")]
71    BypassRLSRequired(Vec<String>),
72}