Class Postgres


public final class Postgres extends ServerSession

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.
  • Nested Class Summary

    Nested classes/interfaces inherited from class ServerSession

    ServerSession.Connector
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    Opens Postgres sessions; naming this constant is what puts the engine in an application.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Opens a transaction.
    void
    begin(boolean readOnly, TransactionIsolation isolation)
    Opens a transaction at an isolation level.
    void
    Begins a read-only transaction at the server's default isolation.
    void
    Closes the session; closing twice does nothing.
    void
    Commits the transaction.
    static Postgres
    connect(String host, int port, String database, String user, String password, String sslMode, String caFile, int timeoutMillis, int socketTimeoutMillis)
    Connects, negotiates TLS when asked, authenticates, and returns a session ready for queries.
    The SQL this session's server speaks.
    int
    execute(String sql, Object[] params)
    Runs a statement that returns no rows, and returns the number affected.
    boolean
    True once the session is closed or its connection was lost.
    long
    PostgreSQL reports generated keys through RETURNING rows, so there is no last insert id.
    boolean
    True when beginning, committing and rolling back are protocol requests or session state of this client's own, rather than statements a caller could equally run.
    query(String sql, Object[] params)
    Runs a query and returns each row as a column-name to value map, with the SAME value types the SQLite path produces: Long, Double, String, byte[] or null.
    void
    Rolls the transaction back.
    void
    savepoint(String verb, String name)
    Savepoint control; the name must be a plain identifier.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Method Details

    • dialect

      public Dialect dialect()
      Description copied from class: ServerSession
      The SQL this session's server speaks.
      Specified by:
      dialect in class ServerSession
    • ownsTransactionControl

      public boolean ownsTransactionControl()
      Description copied from class: ServerSession
      True when beginning, committing and rolling back are protocol requests or session state of this client's own, rather than statements a caller could equally run.
      Specified by:
      ownsTransactionControl in class ServerSession
    • connect

      public static Postgres connect(String host, int port, String database, String user, String password, String sslMode, String caFile, int timeoutMillis, int socketTimeoutMillis) throws IOException

      Connects, 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:
      IOException
    • execute

      public int execute(String sql, Object[] params) throws IOException
      Runs a statement that returns no rows, and returns the number affected.
      Specified by:
      execute in class ServerSession
      Throws:
      IOException
    • query

      public List query(String sql, Object[] params) throws IOException
      Runs a query and returns each row as a column-name to value map, with the SAME value types the SQLite path produces: Long, Double, String, byte[] or null. A handler must not be able to tell which engine answered it.
      Specified by:
      query in class ServerSession
      Throws:
      IOException
    • close

      public void close()
      Description copied from class: ServerSession
      Closes the session; closing twice does nothing.
      Specified by:
      close in class ServerSession
    • begin

      public void begin() throws IOException
      Opens a transaction. PostgreSQL prepares transaction control like any other statement, so the facade normally sends it through execute(String, Object[]) where it is traced; these exist so every engine answers the same calls.
      Specified by:
      begin in class ServerSession
      Throws:
      IOException
    • beginReadOnly

      public void beginReadOnly() throws IOException
      Description copied from class: ServerSession
      Begins a read-only transaction at the server's default isolation.
      Specified by:
      beginReadOnly in class ServerSession
      Throws:
      IOException
    • begin

      public void begin(boolean readOnly, TransactionIsolation isolation) throws IOException
      Opens a transaction at an isolation level. READ_UNCOMMITTED is refused: PostgreSQL accepts the words and runs READ COMMITTED.
      Specified by:
      begin in class ServerSession
      Throws:
      IOException
    • commit

      public void commit() throws IOException
      Description copied from class: ServerSession
      Commits the transaction.
      Specified by:
      commit in class ServerSession
      Throws:
      IOException
    • rollback

      public void rollback() throws IOException
      Description copied from class: ServerSession
      Rolls the transaction back.
      Specified by:
      rollback in class ServerSession
      Throws:
      IOException
    • savepoint

      public void savepoint(String verb, String name) throws IOException
      Savepoint control; the name must be a plain identifier.
      Specified by:
      savepoint in class ServerSession
      Throws:
      IOException
    • isClosed

      public boolean isClosed()
      Description copied from class: ServerSession
      True once the session is closed or its connection was lost.
      Specified by:
      isClosed in class ServerSession
    • lastInsertId

      public long lastInsertId()
      PostgreSQL reports generated keys through RETURNING rows, so there is no last insert id.
      Specified by:
      lastInsertId in class ServerSession