Skip to content

Improve JDBC Postgres driver support #375

@lc-guy

Description

@lc-guy

The following prepared statement:

BEGIN; SET pgdog.sharding_key TO '10000000-0000-0000-0000-000000000000'; CREATE SCHEMA IF NOT EXISTS system;

returns an error (and seems to route it to shard All):

2025-08-29T11:10:29.042275Z TRACE routing ClientRequest {
    messages: [
        Parse(
            Parse {
                name: "__pgdog_1",
                query: "BEGIN",
                modified: true,
            },
        ),
        Bind(
            Bind {
                portal: b"\0",
                statement: b"__pgdog_1\0",
                codes: [],
                params: [],
                results: [],
                original: None,
            },
        ),
        Describe(
            Describe {
                payload: b"D\0\0\0\x06P\0",
                original: Some(
                    b"D\0\0\0\x06P\0",
                ),
            },
        ),
        Execute(
            Execute {
                portal: "",
            },
        ),
        Parse(
            Parse {
                name: "__pgdog_2",
                query: " SET pgdog.sharding_key TO '10000000-0000-0000-0000-000000000000'",
                modified: true,
            },
        ),
        Bind(
            Bind {
                portal: b"\0",
                statement: b"__pgdog_2\0",
                codes: [],
                params: [],
                results: [],
                original: None,
            },
        ),
        Describe(
            Describe {
                payload: b"D\0\0\0\x06P\0",
                original: Some(
                    b"D\0\0\0\x06P\0",
                ),
            },
        ),
        Execute(
            Execute {
                portal: "",
            },
        ),
        Parse(
            Parse {
                name: "__pgdog_3",
                query: " CREATE SCHEMA IF NOT EXISTS system",
                modified: true,
            },
        ),
        Bind(
            Bind {
                portal: b"\0",
                statement: b"__pgdog_3\0",
                codes: [],
                params: [],
                results: [],
                original: None,
            },
        ),
        Describe(
            Describe {
                payload: b"D\0\0\0\x06P\0",
                original: Some(
                    b"D\0\0\0\x06P\0",
                ),
            },
        ),
        Execute(
            Execute {
                portal: "",
            },
        ),
        Sync(
            Sync,
        ),
    ],
    route: Route {
        shard: All,
        read: false,
        order_by: [],
        aggregate: Aggregate {
            targets: [],
            group_by: [],
        },
        limit: Limit {
            limit: None,
            offset: None,
        },
        lock_session: false,
        distinct: None,
    },
} to Query(
    Route {
        shard: All,
        read: false,
        order_by: [],
        aggregate: Aggregate {
            targets: [],
            group_by: [],
        },
        limit: Limit {
            limit: None,
            offset: None,
        },
        lock_session: false,
        distinct: None,
    },
)
2025-08-29T11:10:29.042319Z TRACE [172.18.0.5:33632] <-- ErrorResponse {
    severity: "ERROR",
    code: "58000",
    message: "cross-shard queries are disabled",
    detail: Some(
        "query doesn't have a sharding key",
    ),
    context: None,
    file: None,
    routine: None,
}

With the following configuration:

[[databases]]
name = "example"
host = "postgres"
role = "primary"
user = "postgres"
shard = 0

[[databases]]
name = "example"
host = "postgres2"
role = "primary"
user = "postgres"
shard = 1

[[sharded_tables]]
database = "example"
column = "tenant_id"
data_type = "uuid"

[[sharded_mappings]]
database = "example"
column = "tenant_id"
kind = "list"
values = ["00000000-0000-0000-0000-000000000000"]
shard = 0

[[sharded_mappings]]
database = "example"
column = "tenant_id"
kind = "list"
values = ["10000000-0000-0000-0000-000000000000"]
shard = 1

The same thing happens when using /* pgdog_sharding_key: ... */ in a simple statement.

For reference; the tables in this database don't necessarily contain a tenant_id column, all sharding decisions are made purely using /* pgdog_sharding_key */ / SET pgdog.sharding_key. However, I see no other way to define the data_type for the sharding_key than include a sharded_tables entry. Discarding sharded_tables leads to the same behavior.

I assume there is no way of simply omitting the column name in order to match on all tables regardless, and force usage of the annotation?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No fields configured for Bug.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions