Skip to content

Add 'Fail' mode when no shard selected #234

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft

Conversation

Copilot
Copy link

@Copilot Copilot AI commented Jun 27, 2025

This implements a "Fail" mode when no shard is selected, equivalent to pgcat's MR #859. When enabled, applications must explicitly select a shard for every connection, preventing data compromise in multi-tenant scenarios.

Problem

In multi-tenant applications using shards as tenants, it's critical that every query explicitly selects the correct shard. Without this requirement, queries may inadvertently access data from all shards, leading to potential data compromise and cross-tenant data leakage.

Solution

Added a new configuration option require_shard_selection that enforces explicit shard selection:

[general]
require_shard_selection = true

When enabled, queries without explicit shard selection will fail with a ShardSelectionRequired error.

Valid shard selection methods

  • Comments: /* pgdog_shard: 1 */ SELECT * FROM users
  • SET statements: SET pgdog.shard TO 1; SELECT * FROM users
  • Sharding keys: /* pgdog_sharding_key: user_123 */ SELECT * FROM users

Implementation details

  • Added require_shard_selection: bool configuration option to [general] section
  • Added ShardSelectionRequired error variant to the parser error enum
  • Modified query parser to check for explicit shard selection and fail if required but not provided
  • Added comprehensive test coverage for the new functionality
  • Ensured backward compatibility (default is false)

Example

# pgdog.toml
[general]
require_shard_selection = true

[[databases]]
name = "myapp"
shard = 0
host = "127.0.0.1"
port = 5432

[[databases]]  
name = "myapp"
shard = 1
host = "127.0.0.1"
port = 5433

With this configuration:

  • SELECT * FROM users → fails with ShardSelectionRequired
  • /* pgdog_shard: 1 */ SELECT * FROM users → routes to shard 1
  • SET pgdog.shard TO 0; SELECT * FROM users → routes to shard 0

Testing

All existing tests pass and new tests verify the functionality works correctly with various query types and shard selection methods.

Fixes #221.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: christoudias <2363711+christoudias@users.noreply.github.com>
@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

Co-authored-by: christoudias <2363711+christoudias@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] Add 'Fail' mode when no shard selected Add 'Fail' mode when no shard selected Jun 27, 2025
@Copilot Copilot AI requested a review from christoudias June 27, 2025 19:36
Copilot finished work on behalf of christoudias June 27, 2025 19:36
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.

Add 'Fail' mode when no shard selected
3 participants