Client and backend. This class is shared: the same code is compiled into your app and into your server, so everything on this page works in both.
public final class Postgres
- Object
- ServerSession
- Postgres
A PostgreSQL client speaking the v3 frontend/backend protocol directly.
Written rather than wrapped because there is nothing to wrap: JDBC needs a
driver manager, a class loader and reflection, none of which a translated
server binary has. The protocol is small, stable (v3 has been the wire format
since 7.4) and documented, so the honest option is to speak it. The same source
runs on both targets because it is built on SqlSocket, which each target
implements.
Three decisions worth stating:
- The extended query protocol, always. Simple Query would be fewer round
trips, but it has no parameters, and a client with no way to bind a value is
a client whose users concatenate SQL. Parse/Bind/Execute is what makes
query(sql, params)safe by construction. - Text format for parameters and results. The binary format saves parsing at the cost of a per-type encoder on both sides, and gets subtly wrong for the types nobody tested. Text is what psql sends.
- SCRAM-SHA-256 is verified in both directions. The server’s final message proves it knew the stored key; skipping that check (which a client can do and still connect successfully) leaves the handshake open to a server that only pretends to be PostgreSQL.
Fields
public static final ServerSession.Connector CONNECTOR | Opens Postgres sessions; naming this constant is what puts the engine in an application. |
Methods
Inherited nested types
Inherited methods
Field details
CONNECTOR
public static final ServerSession.Connector CONNECTORServerSession.Connector.Method details
dialect
public Dialect dialect()ownsTransactionControl
public boolean ownsTransactionControl()connect
public static Postgres connect(String host, int port, String database, String user, String password, String sslMode, String caFile, int timeoutMillis, int socketTimeoutMillis)
throws IOExceptionConnects, negotiates TLS when asked, authenticates, and returns a session ready for queries.
sslMode is “require”, “prefer” or “disable”. “prefer” exists because it is
what a local development database usually needs and a managed one usually
forbids; “require” fails rather than falling back, which is the only setting
that means anything against an attacker.
Throws
execute
public int execute(String sql, Object[] params)
throws IOExceptionReturns
Throws
query
public List query(String sql, Object[] params)
throws IOExceptionThrows
close
public void close()begin
public void begin()
throws IOExceptionexecute
where it is traced; these exist so every engine answers the same calls.Throws
beginReadOnly
public void beginReadOnly()
throws IOExceptionThrows
begin
public void begin(boolean readOnly, TransactionIsolation isolation)
throws IOExceptionThrows
commit
public void commit()
throws IOExceptionThrows
rollback
public void rollback()
throws IOExceptionThrows
savepoint
public void savepoint(String verb, String name)
throws IOExceptionThrows
isClosed
public boolean isClosed()lastInsertId
public long lastInsertId()