Redrock Postgres 文档
主页 切换暗/亮/自动模式 切换暗/亮/自动模式 切换暗/亮/自动模式 返回首页

其他顶级对象

连接信息

ConnectionInfo 类

class psycopg.ConnectionInfo(pgconn)

允许访问有关连接的信息。

该对象通常由 Connection.info返回。

ConnectionInfo.dsn

dsn

返回连接到数据库的连接字符串。

该字符串包含设置为非默认值的所有参数,这些参数可能来自连接字符串和传入connect()方法的参数,或者来自环境变量。永远不会返回密码(您可以使用password属性读取它)。

get_parameters() 方法以 dict 类型返回相同的信息。

ConnectionInfo.status

status

连接的状态。参见 PQstatus()

状态可以是多个值之一。但是,在异步连接过程之外只能看到其中的两个:OKBAD。与数据库的良好连接具有状态OK。通常情况下,OK状态会一直保持到Connection.close(),但通信故障可能会导致状态过早变为BAD

ConnectionInfo.transaction_status

transaction_status

The current in-transaction status of the session. See PQtransactionStatus().

The status can be IDLE (currently idle), ACTIVE (a command is in progress), INTRANS (idle, in a valid transaction block), or INERROR (idle, in a failed transaction block). UNKNOWN is reported if the connection is bad. ACTIVE is reported only when a query has been sent to the server and not yet completed.

ConnectionInfo.pipeline_status

pipeline_status

The current pipeline status of the client. See PQpipelineStatus().

ConnectionInfo.backend_pid

backend_pid

The process ID (PID) of the backend process handling this connection. See PQbackendPID().

ConnectionInfo.vendor

vendor

A string representing the database vendor connected to.

Normally it is PostgreSQL; it may be different if connected to a different database.

New in version 3.1.

ConnectionInfo.server_version

server_version

An integer representing the server version. See PQserverVersion().

The number is formed by converting the major, minor, and revision numbers into two-decimal-digit numbers and appending them together. Starting from PostgreSQL 10 the minor version was dropped, so the second group of digits is always 00. For example, version 9.3.5 is returned as 90305, version 10.2 as 100002.

ConnectionInfo.error_message

error_message

The error message most recently generated by an operation on the connection. See PQerrorMessage().

ConnectionInfo.get_parameters()

get_parameters()

Return the connection parameters values.

Return all the parameters set to a non-default value, which might come either from the connection string and parameters passed to ~Connection.connect() or from environment variables. The password is never returned (you can read it using the password attribute).

RETURN TYPE:

Dict[str, str]

The dsn attribute returns the same information in the form as a string.

ConnectionInfo.timezone

timezone

The Python timezone info of the connection’s timezone.

>>> conn.info.timezone
zoneinfo.ZoneInfo(key='Europe/Rome')

ConnectionInfo.host

host

The server host name of the active connection. See PQhost().

This can be a host name, an IP address, or a directory path if the connection is via Unix socket. (The path case can be distinguished because it will always be an absolute path, beginning with /.)

ConnectionInfo.hostaddr

hostaddr

The server IP address of the connection. See PQhostaddr().

Only available if the libpq used is at least from PostgreSQL 12. Raise ~psycopg.NotSupportedError otherwise.

ConnectionInfo.port

port

The port of the active connection. See PQport().

ConnectionInfo.dbname

dbname

The database name of the connection. See PQdb().

ConnectionInfo.user

user

The user name of the connection. See PQuser().

ConnectionInfo.password

password

The password of the connection. See PQpass().

ConnectionInfo.options

options

The command-line options passed in the connection request. See PQoptions.

ConnectionInfo.parameter_status()

parameter_status(param_name)

Return a parameter setting of the connection.

Return None is the parameter is unknown.

返回类型:

Optional[str]

Example of parameters are server_version, standard_conforming_strings… See PQparameterStatus() for all the available parameters.

ConnectionInfo.encoding

encoding

连接的客户端编码的 Python 编解码器名称。

返回的值总是标准化为 Python 编解码器 ~codecs.CodecInfo.name:

conn.execute("SET client_encoding TO LATIN9")
conn.info.encoding
'iso8859-15'

一些 PostgreSQL 编码在 Python 中不可用,无法被选用(目前包括EUC_TW, MULE_INTERNAL)。PostgreSQL 的SQL_ASCII编码有“无编码”的特殊含义:详见字符串适配

列描述对象

Column 类

class psycopg.Column(cursor, index)

An object describing a column of data from a database result, as described by the DBAPI, so it can also be unpacked as a 7-items tuple.

The object is returned by Cursor.description.

Column.name

name

The name of the column.

Column.type_code

type_code

The numeric OID of the column.

Column.display_size

display_size

The field size, for varchar(n), None otherwise.

Column.internal_size

internal_size

The internal field size for fixed-size types, None otherwise.

Column.precision

precision

The number of digits for fixed precision types.

Column.scale

scale

The number of digits after the decimal point if available.

通知

Notify 类

class psycopg.Notify

An asynchronous notification received from the database.

The object is usually returned by Connection.notifies().

Notify.channel

channel: str

The name of the channel on which the notification was received.

Notify.payload

payload: str

The message attached to the notification.

Notify.pid

pid: int

The PID of the backend process which sent the notification.

管道相关对象

有关详细信息,请参阅管道模式支持

Pipeline 类

class psycopg.Pipeline(conn)

Handler for connection in pipeline mode.

This objects is returned by Connection.pipeline().

Pipeline.sync()

sync()

Sync the pipeline, send any pending command and receive and process all available results.

Pipeline.is_supported()

classmethod is_supported()

Return !True if the psycopg libpq wrapper supports pipeline mode.

RETURN TYPE:

bool

AsyncPipeline 类

class psycopg.AsyncPipeline(conn)

Handler for async connection in pipeline mode.

This objects is returned by AsyncConnection.pipeline().

AsyncPipeline.sync()

async sync()

事务相关对象

有关这些对象的详细信息,请参阅事务管理

IsolationLevel 类

class psycopg.IsolationLevel(value)

表示事务隔离级别的枚举值。

该值通常与 Connection.isolation_level 属性一起使用。

可以查看 PostgreSQL 文档,以了解不同事务隔离级别的影响。

IsolationLevel.READ_UNCOMMITTED

READ_UNCOMMITTED = 1

IsolationLevel.READ_COMMITTED

READ_COMMITTED = 2

IsolationLevel.REPEATABLE_READ

REPEATABLE_READ = 3

IsolationLevel.SERIALIZABLE

SERIALIZABLE = 4

Transaction 类

class psycopg.Transaction(connection, savepoint_name=None, force_rollback=False)

Returned by Connection.transaction() to handle a transaction block.

Transaction.savepoint_name

savepoint_name

The name of the savepoint; !None if handling the main transaction.

Transaction.connection

connection

The connection the object is managing.

AsyncTransaction 类

class psycopg.AsyncTransaction(connection, savepoint_name=None, force_rollback=False)

Returned by AsyncConnection.transaction() to handle a transaction block.

AsyncTransaction.connection

connection

Rollback 异常

exception psycopg.Rollback(transaction=None)

Exit the current Transaction context immediately and rollback any changes made within this context.

If a transaction context is specified in the constructor, rollback enclosing transactions contexts up to and including the one specified.

It can be used as:

  • raise Rollback: roll back the operation that happened in the current transaction block and continue the program after the block.
  • raise Rollback(): same effect as above
  • raise Rollback(tx): roll back any operation that happened in the Transaction !tx (returned by a statement such as with conn.transaction() as tx: and all the blocks nested within. The program will continue after the !tx block.

两阶段提交相关对象

Xid 类

class psycopg.Xid(format_id, gtrid, bqual, prepared=None, owner=None, database=None)

两阶段提交事务的标识符。

该对象也可以拆解为 3 元素的元组 (format_id, gtrid, bqual) 。

有关详细信息,请参阅两阶段提交协议支持

Xid.format_id

format_id: Optional[int]

Format Identifier of the two-phase transaction.

Xid.gtrid

gtrid: str

Global Transaction Identifier of the two-phase transaction.

If the Xid doesn’t follow the XA standard, it will be the PostgreSQL ID of the transaction (in which case format_id and bqual will be !None).

Xid.bqual

bqual: Optional[str]

Branch Qualifier of the two-phase transaction.

Xid.prepared

prepared: Optional[datetime] = None

Timestamp at which the transaction was prepared for commit.

Only available on transactions recovered by ~Connection.tpc_recover().

Xid.owner

owner: Optional[str] = None

Named of the user that executed the transaction.

Only available on recovered transactions.

Xid.database

database: Optional[str] = None

Named of the database in which the transaction was executed.

Only available on recovered transactions.