Skip to main content

PgExtensions

Trait PgExtensions 

Source
pub trait PgExtensions: Send + Sync {
    // Required methods
    fn listen<'life0, 'life1, 'async_trait>(
        &'life0 mut self,
        channel: &'life1 str,
    ) -> Pin<Box<dyn Future<Output = Result<(), DbError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn notify<'life0, 'life1, 'life2, 'async_trait>(
        &'life0 mut self,
        channel: &'life1 str,
        payload: &'life2 str,
    ) -> Pin<Box<dyn Future<Output = Result<(), DbError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait;
    fn copy_from_stdin<'life0, 'life1, 'life2, 'async_trait>(
        &'life0 mut self,
        sql: &'life1 str,
        data: &'life2 [u8],
    ) -> Pin<Box<dyn Future<Output = Result<u64, DbError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait;
}
Expand description

PostgreSQL 扩展功能 trait

提供 PostgreSQL 特有的 LISTEN/NOTIFY 通道通信与 COPY FROM STDIN 批量导入功能。 这些功能在其他数据库(MySQL/SQLite)中没有对应实现,因此单独定义为扩展 trait, 仅由 SqlxPgConnection 实现。

§LISTEN/NOTIFY

PostgreSQL 的轻量级进程间通信机制:

  • LISTEN channel:订阅通道
  • NOTIFY channel, payload:向通道发送通知

接收通知需配合 PgListener 或轮询 pg_notification 系列函数。

§COPY FROM STDIN

PostgreSQL 的高性能批量导入协议,比逐行 INSERT 快 10~100 倍。 数据通过专用协议流式传输,绕过 SQL 解析器。

Required Methods§

Source

fn listen<'life0, 'life1, 'async_trait>( &'life0 mut self, channel: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<(), DbError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

LISTEN 通道:订阅指定通道的通知

§参数
  • channel: 通道名(仅允许字母、数字、下划线,防止 SQL 注入)
§错误
  • 通道名包含非法字符时返回 DbError::Internal
  • 连接已关闭时返回 DbError::Internal
Source

fn notify<'life0, 'life1, 'life2, 'async_trait>( &'life0 mut self, channel: &'life1 str, payload: &'life2 str, ) -> Pin<Box<dyn Future<Output = Result<(), DbError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

NOTIFY 通道:向指定通道发送通知

§参数
  • channel: 通道名(仅允许字母、数字、下划线)
  • payload: 通知载荷字符串(单引号自动转义)
§错误
  • 通道名包含非法字符时返回 DbError::Internal
Source

fn copy_from_stdin<'life0, 'life1, 'life2, 'async_trait>( &'life0 mut self, sql: &'life1 str, data: &'life2 [u8], ) -> Pin<Box<dyn Future<Output = Result<u64, DbError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

COPY FROM STDIN:批量导入数据

§参数
  • sql: COPY 语句,如 COPY mytable (col1, col2) FROM STDIN WITH (FORMAT csv, HEADER true)
  • data: 完整的导入数据字节流
§返回

返回受影响的行数。

§性能

比逐行 INSERT 快 10~100 倍,适用于大批量数据导入(10 万行以上)。

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§