Skip to content

feat: partial sharding support for PREPARE and EXECUTE - #1368

Open
levkk wants to merge 19 commits into
mainfrom
levkk-prepared-simple-routing
Open

feat: partial sharding support for PREPARE and EXECUTE#1368
levkk wants to merge 19 commits into
mainfrom
levkk-prepared-simple-routing

Conversation

@levkk

@levkk levkk commented Aug 16, 2026

Copy link
Copy Markdown
Collaborator

Prepared statements

Correctly store statements sent with PREPARE in the global cache. We use the whole query string with data type specifications as key, while anonymizing the statement name. This is equivalent to using Parse as cache key.

PREPARE __stmt_1(int) AS SELECT $1;

becomes

PREPARE __pgdog_template_name(int) AS SELECT $1;

It's then dynamically rewritten to

PREPARE __pgdog_1(int) AS SELECT $1

at execution time.

This is because it's kinda hard to parse the data types out of the string and we need a unique key identifying the prepared statement in the cache.

Sharding

Handle EXECUTE by extracting parameters and passing them to our query router. This works with I think most of our sharding features, i.e., direct-to-shard and cross-shard queries.

TODOs

  1. Comment-based routing isn't supported. This is because we parse and deparse the original statement to put it in the prepared statement cache, which erases the comment.
  2. Query rewrites, e.g., insert split, don't work yet.

Notes for reviewers

  1. Most of the diff in client/prepared_statements module is from moving stuff around. There is some diff for handling PREPARE, that's where I would focus the review.
  2. Simulated messages sent by Server now get processed through the server state machine; this was necessary for correct state management for handling PREPARE statements; seems to have no bad side effects.

@levkk
levkk requested a review from meskill August 16, 2026 20:02
@levkk
levkk marked this pull request as ready for review August 16, 2026 20:03
@levkk levkk changed the title feat: sharding support for PREPARE and EXECUTE feat: partial sharding support for PREPARE and EXECUTE Aug 17, 2026
@levkk
levkk force-pushed the levkk-prepared-simple-routing branch from e2e9044 to ebcc0c7 Compare August 17, 2026 18:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant