Postgres

Postgres Endpoint Documentation

OverviewCopied!

PostgreSQL endpoints provide a powerful interface for interacting with PostgreSQL databases. These endpoints support a wide range of operations from simple queries to complex transactions, making it suitable for applications requiring advanced database functionality.

Key FeaturesCopied!

  • Complete Query Support: Support for all PostgreSQL query capabilities

  • Transaction Management: Robust handling of database transactions

  • Advanced Data Types: Support for PostgreSQL's rich set of data types

  • Prepared Statements: Optimized query execution with parameter binding

  • Batch Operations: Ability to execute multiple statements efficiently

API Request StructureCopied!

PostgreSQL API requests follow this general structure:

{
  "kind": "postgres",
  "type": "[operation_type]",
  "[additional_parameters]": "[values]"
}

OperationsCopied!

Simple Query

Executes a simple SQL statement without parameters.

{
  "kind": "postgres",
  "type": "simple",
  "query": "SELECT * FROM users LIMIT 10"
}

Parameterized Query

Executes a SQL query with bound parameters.

{
  "kind": "postgres",
  "type": "query",
  "query": "SELECT * FROM users WHERE age > $1 AND status = $2",
  "params": [
    {"Int4": 18},
    {"Text": "active"}
  ]
}

Query One

Retrieves a single row from the result set.

{
  "kind": "postgres",
  "type": "query_one",
  "query": "SELECT * FROM users WHERE id = $1",
  "params": [
    {"Int4": 42}
  ]
}

Query Optional

Retrieves a single row from the result set, or null if no rows match.

{
  "kind": "postgres",
  "type": "query_opt"
}

Execute

Executes a statement that doesn't return rows (like INSERT, UPDATE, DELETE).

{
  "kind": "postgres",
  "type": "execute",
  "query": "UPDATE users SET last_login = NOW() WHERE id = $1",
  "params": [
    {"Int4": 42}
  ]
}

Batch Execute

Executes multiple SQL statements in a single batch.

{
  "kind": "postgres",
  "type": "batch_execute",
  "query": "CREATE TEMP TABLE temp_users(id INT, name TEXT); INSERT INTO temp_users VALUES(1, 'Alice'), (2, 'Bob');"
}

Typed Query

Executes a query with explicitly typed parameters.

{
  "kind": "postgres",
  "type": "query_typed",
  "query": "SELECT * FROM users WHERE joined_date > $1::TIMESTAMP",
  "params": [
    [{"type": "TIMESTAMP"}, {"value": "2023-01-01T00:00:00Z"}]
  ]
}

Raw Queries

Execute queries with raw binary format results.

{
  "kind": "postgres",
  "type": "query_raw"
}
{
  "kind": "postgres",
  "type": "query_typed_raw"
}

COPY Operations

Supports PostgreSQL's high-performance COPY command.

{
  "kind": "postgres",
  "type": "copy_in",
  "query": "COPY users(id, name, email) FROM STDIN"
}
{
  "kind": "postgres",
  "type": "copy_out",
  "query": "COPY users TO STDOUT"
}

Connection Status

Check connection status and notifications.

{
  "kind": "postgres",
  "type": "is_valid"
}
{
  "kind": "postgres",
  "type": "is_closed"
}
{
  "kind": "postgres",
  "type": "notifications"
}

Prepared Statements

Prepare statements for repeated execution.

{
  "kind": "postgres",
  "type": "prepare"
}
{
  "kind": "postgres",
  "type": "prepare_typed"
}

Cancel Operation

Cancel an ongoing query operation.

{
  "kind": "postgres",
  "type": "cancel_token",
  "query": "query_id_to_cancel"
}

Type Cache Management

Clear the PostgreSQL type cache.

{
  "kind": "postgres",
  "type": "clear_type_cache"
}

Parameter TypesCopied!

PostgreSQL API supports a wide range of parameter types:

JSON Representation

PostgreSQL Type

"Null"

NULL value

{"Bool": true/false}

BOOLEAN

{"Int2": value}

SMALLINT (16-bit integer)

{"Int4": value}

INTEGER (32-bit integer)

{"Int8": value}

BIGINT (64-bit integer)

{"Float4": value}

REAL (32-bit floating point)

{"Float8": value}

DOUBLE PRECISION (64-bit floating point)

{"Text": "value"}

TEXT or VARCHAR

{"Bytes": [values]}

BYTEA (binary data)

{"Json": object}

JSON or JSONB

Best PracticesCopied!

Performance Optimization

  1. Index Usage:

    • Create appropriate indexes for query patterns

    • Use EXPLAIN ANALYZE to verify query plans

    • Consider partial and expression indexes for specific queries

  2. Query Efficiency:

    • Use prepared statements for repeated queries

    • Leverage LIMIT and OFFSET for pagination

    • Use batch operations for multiple similar statements

  3. Connection Management:

    • Use connection pooling

    • Check connection status before use

    • Properly handle connection errors

Security

  1. Authentication:

    • Use strong authentication methods

    • Apply the principle of least privilege to database users

    • Utilize SSL/TLS for connection encryption

  2. Data Validation:

    • Always use parameterized queries to prevent SQL injection

    • Validate input before sending to the database

    • Implement row-level security for multi-tenant applications

Error Handling

  1. Robust Error Management:

    • Implement appropriate retry logic for transient errors

    • Log database errors with context information

    • Use explicit transactions for operations requiring atomicity

  2. Transaction Management:

    • Explicitly manage transactions for related operations

    • Set appropriate isolation levels

    • Implement proper error handling within transactions

Common Use CasesCopied!

  • Web application backends

  • Geospatial applications (using PostGIS)

  • Data warehousing and business intelligence

  • Multi-tenant SaaS applications

  • Full-text search applications