Skip to content

feat(lb): label replicas - #2075

Open
levkk wants to merge 8 commits into
mainfrom
levkk-label-replicas
Open

levkk wants to merge 8 commits into
mainfrom
levkk-label-replicas

Conversation

@levkk

@levkk levkk commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Fix #2051

Support comment-based routing to specific set of replicas. This allows the caller to specify which replicas each query should use, based on a pre-configured label.

Example

# pgdog.toml
[[databases]]
host = "10.0.0.1"
name = "prod"
labels = ["analytics"]

[[databases]]
host = "10.0.01"
name = "prod"
labels = ["slow_queries", "analytics"]

The caller can select which database(s) each query uses as follows:

-- this will run on the "analytics" replicas, using `load_balancing_algorithm` to even out
/* pgdog_role: analytics */ SELECT * FROM users GROUP BY login_count;

-- this will run only on "slow_queries"
/* pgdog_role: slow_queries */ SELECT * FROM users WHERE not_indexed_field = $1;

Implementation details

Labeled queries will ignore host bans if the number of replicas it can send the query to is 0. For example, if you have two "analytics" replicas, and one of them is banned, the other will serve all queries, but if both are, the bans will be ignored and the query will attempt to connect to one of them using load_balancing_algorithm.

We are re-using pgdog_role because the routing decision conflicts with primary/replica and the caller expects the label to respected over a read/write preference.

Mandatory labels

By default, a query without a label will be routed between all replicas. This may not be desirable, e.g., you'd want to exclude some underpowered or special replicas from regular traffic.

You can do this with a config:

[[databases]]
host = "10.0.0.1"
name = "prod"
labels = ["analytics"]
label_required = true

Queries without a label will not be routed to this replica.

Specifying labels

Labels can be specified in the query as follows:

  1. Query comment: /* pgdog_role: analytics */
  2. Session parameter, e.g., SET pgdog.role TO 'analytics' or via connection parameter, e.g., -c pgdog.role=analytics

Reviewer notes

Request was split up into LbRequest and Request, to avoid passing information to the Pool it doesn't need.

@codecov

codecov Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 99.24433% with 3 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
pgdog/src/backend/schema/mod.rs 0.00% 1 Missing ⚠️
...ery_engine/test/regression_bind_transaction_end.rs 0.00% 1 Missing ⚠️
pgdog/src/frontend/router/comment.rs 0.00% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@github-actions

github-actions Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Metal bench (arm64) failed. 155 of 520 measures outside bounds.

suite throughput cpu/txn user cycles/txn user instr/txn kernel cycles/txn kernel instr/txn ctx/txn
pooler 19.15 (+0.8% ± 0.9%) 18,223 (+0.4% ± 0.7%) 31,478 (+0.3% ± 0.03%) 30,705 (+1.3% ± 1.4%) 50,992 (-0.0% ± 0.2%)
lb 19.84 (+1.2% ± 1.1%) 19,004 (+0.4% ± 0.6%) 32,739 (+0.7% ± 0.08%), bound +0.5% 31,555 (+1.6% ± 2.0%) 52,120 (-0.3% ± 0.2%)
sharding-w2 32.9 (+1.1% ± 0.5%) 44,685 (+0.6% ± 0.9%) 95,323 (+0.2% ± 0.01%) 40,797 (+1.5% ± 2.0%) 53,786 (+0.0% ± 0.7%) 1.548 (-0.0% ± 0.4%)
sharding-w8 240,290 (-2.1% ± 1.1%) 29.75 (+2.5% ± 1.2%) 45,356 (+1.0% ± 1.9%) 50,899 (+0.5% ± 0.04%) 36,841 (+4.5% ± 1.9%) 42,451 (-0.0% ± 0.1%) 0.1667 (-2.5% ± 3.7%)
Counters per txn
suite frontend stall l1i refill backend stall
pooler 8,128 (+0.7% ± 1.5%) 2,726 (+1.0% ± 0.5%)
lb 8,426 (-0.6% ± 0.9%) 2,775 (+0.8% ± 0.4%)
sharding-w2 19,258 (-0.0% ± 1.9%) 4,380 (+0.5% ± 1.5%)
sharding-w8 21,744 (+1.2% ± 5.8%) 4,416 (+1.8% ± 3.8%)
benchmark measure result baseline change bound
extended_cached::aggregate alloc_bytes 917 893 +2.69% ± 0.00% +1%
extended_cached::aggregate instructions 17,682 17,432 +1.43% ± 0.00% +1%
extended_cached::begin alloc_bytes 344 320 +7.50% ± 0.00% +1%
extended_cached::begin instructions 6,789 6,717 +1.07% ± 0.00% +1%
extended_cached::cte alloc_bytes 1,288 1,264 +1.90% ± 0.00% +1%
extended_cached::delete_all_shards alloc_bytes 408 384 +6.25% ± 0.00% +1%
extended_cached::delete_by_key alloc_bytes 612 588 +4.08% ± 0.00% +1%
extended_cached::distinct alloc_bytes 600 576 +4.17% ± 0.00% +1%
extended_cached::group_by alloc_bytes 968 944 +2.54% ± 0.00% +1%
extended_cached::having alloc_bytes 1,104 1,080 +2.22% ± 0.00% +1%
extended_cached::having_distinct_limit alloc_bytes 1,530 1,506 +1.59% ± 0.00% +1%
extended_cached::insert alloc_bytes 536 512 +4.69% ± 0.00% +1%
extended_cached::insert_returning alloc_bytes 536 512 +4.69% ± 0.00% +1%
extended_cached::insert_split alloc_bytes 7,386 7,026 +5.12% ± 0.00% +1%
extended_cached::insert_split instructions 41,496 41,019 +1.16% ± 0.00% +1%
extended_cached::join alloc_bytes 1,596 1,572 +1.53% ± 0.00% +1%
extended_cached::lookup alloc_bytes 912 888 +2.70% ± 0.00% +1%
extended_cached::min_max_sum alloc_bytes 880 856 +2.80% ± 0.00% +1%
extended_cached::order_limit_offset alloc_bytes 907 883 +2.72% ± 0.00% +1%
extended_cached::select_all_shards alloc_bytes 600 576 +4.17% ± 0.00% +1%
extended_cached::select_by_key alloc_bytes 912 888 +2.70% ± 0.00% +1%
extended_cached::select_in_list alloc_bytes 1,120 1,096 +2.19% ± 0.00% +1%
extended_cached::select_omni alloc_bytes 408 384 +6.25% ± 0.00% +1%
extended_cached::set alloc_bytes 401 377 +6.37% ± 0.00% +1%
extended_cached::subquery alloc_bytes 792 768 +3.12% ± 0.00% +1%
extended_cached::update_all_shards alloc_bytes 408 384 +6.25% ± 0.00% +1%
extended_cached::update_by_key alloc_bytes 612 588 +4.08% ± 0.00% +1%
extended_cold::aggregate alloc_bytes 1,990 1,966 +1.22% ± 0.00% +1%
extended_cold::begin alloc_bytes 782 758 +3.17% ± 0.00% +1%
extended_cold::cte alloc_bytes 1,839 1,815 +1.32% ± 0.00% +1%
extended_cold::delete_all_shards alloc_bytes 876 852 +2.82% ± 0.00% +1%
extended_cold::delete_by_key alloc_bytes 1,101 1,077 +2.23% ± 0.00% +1%
extended_cold::distinct alloc_bytes 1,099 1,075 +2.23% ± 0.00% +1%
extended_cold::group_by alloc_bytes 1,819 1,795 +1.34% ± 0.00% +1%
extended_cold::insert alloc_bytes 1,487 1,463 +1.64% ± 0.00% +1%
extended_cold::insert_returning alloc_bytes 1,529 1,505 +1.59% ± 0.00% +1%
extended_cold::insert_split alloc_bytes 9,722 9,362 +3.85% ± 0.00% +1%
extended_cold::join alloc_bytes 2,174 2,150 +1.12% ± 0.00% +1%
extended_cold::min_max_sum alloc_bytes 1,558 1,534 +1.56% ± 0.00% +1%
extended_cold::order_limit_offset alloc_bytes 1,523 1,499 +1.60% ± 0.00% +1%

Microbench: 353 of 507 measures within bounds. See verdict.json for the rest.

Microbench latency: 18 benches changed more than 3%
benchmark latency baseline change instructions change
extended_cached::insert 1,846 ns 1,760 ns +4.92% ± 0.43% +0.57% ± 0.00%
extended_evicting::begin 1,928 ns 1,841 ns +4.68% ± 0.66% +0.60% ± 0.00%
full_simple::order_limit_offset 27,798 ns 29,096 ns -4.46% ± 0.84% +0.14% ± 0.00%
full_named_cached::order_limit_offset 28,216 ns 29,456 ns -4.19% ± 2.32% -0.04% ± 0.00%
full_extended_cached::order_limit_offset 29,006 ns 30,198 ns -3.95% ± 0.70% +0.33% ± 0.00%
extended_cached::update_all_shards 1,609 ns 1,548 ns +3.94% ± 0.37% +0.67% ± 0.00%
extended_cached::having_distinct_limit 2,809 ns 2,703 ns +3.93% ± 0.36% +0.33% ± 0.00%
extended_cold::delete_all_shards 2,432 ns 2,341 ns +3.88% ± 0.28% +0.44% ± 0.00%
extended_cached::select_in_list 2,649 ns 2,551 ns +3.84% ± 1.18% +0.34% ± 0.00%
extended_cached::begin 1,217 ns 1,172 ns +3.82% ± 0.41% +1.07% ± 0.00%
extended_cached::order_limit_offset 2,216 ns 2,135 ns +3.78% ± 0.88% +0.47% ± 0.00%
extended_cached::insert_returning 1,912 ns 1,844 ns +3.68% ± 0.50% +0.53% ± 0.00%
extended_cached::delete_all_shards 1,490 ns 1,441 ns +3.44% ± 0.58% +0.78% ± 0.00%
extended_cached::select_omni 1,741 ns 1,685 ns +3.29% ± 1.05% +0.65% ± 0.00%
extended_cached::distinct 1,983 ns 1,921 ns +3.26% ± 0.82% +0.56% ± 0.00%
extended_cached::select_all_shards 1,964 ns 1,903 ns +3.20% ± 0.84% +0.58% ± 0.00%
extended_cached::min_max_sum 2,573 ns 2,494 ns +3.16% ± 0.21% +0.37% ± 0.00%
extended_cached::having 2,468 ns 2,395 ns +3.07% ± 0.37% +0.40% ± 0.00%
Hot code per txn
path base head change
pooler 113.2 KB 113.7 KB +0.4 KB
sharding-w2 167.4 KB 169.4 KB +1.9 KB

pooler:

crate base head change
pgdog::frontend::client::query_engine 20.1 KB 20.8 KB +0.7 KB
tokio 18.4 KB 18.6 KB +0.2 KB
pgdog::backend::pool 17.4 KB 17.4 KB +0.1 KB
aws-lc (TLS crypto) 10.4 KB 9.9 KB -0.5 KB
rustls (TLS) 8.3 KB 8.4 KB +0.1 KB
pgdog::frontend (other) 7.8 KB 7.9 KB +0.1 KB
pgdog::backend::server 7.7 KB 7.8 KB +0.1 KB
other 6.2 KB 5.8 KB -0.4 KB
everything else 17.0 KB 17.2 KB +0.2 KB
function head change
pgdog::frontend::client::Client::run::{{closure}} 5.6 KB +0.1 KB
pgdog::frontend::client::query_engine::query::<impl pgdog::frontend::client::query_engine: 5.6 KB +0.2 KB
pgdog::frontend::client::query_engine::QueryEngine::handle::{{closure}} 4.4 KB +0.1 KB
pgdog::frontend::client::query_engine::query::<impl pgdog::frontend::client::query_engine: 3.4 KB +0.1 KB
tokio::runtime::task::raw::poll 3.2 KB +0.1 KB

sharding-w2:

crate base head change
pgdog::frontend::router 29.1 KB 30.1 KB +0.9 KB
pgdog::frontend::client::query_engine 21.7 KB 21.7 KB +0.0 KB
pgdog::backend::pool 18.3 KB 18.9 KB +0.6 KB
tokio 18.7 KB 18.6 KB -0.1 KB
aws-lc (TLS crypto) 13.0 KB 13.1 KB +0.1 KB
other 10.1 KB 9.9 KB -0.1 KB
pgdog::frontend (other) 8.8 KB 8.7 KB -0.1 KB
pgdog::backend::server 8.1 KB 8.1 KB +0.1 KB
everything else 39.7 KB 40.2 KB +0.6 KB
function head change
pgdog::frontend::client::Client::run::{{closure}} 6.2 KB +0.0 KB
pgdog::frontend::client::query_engine::query::<impl pgdog::frontend::client::query_engine: 5.4 KB +0.1 KB
pgdog::frontend::client::query_engine::QueryEngine::handle::{{closure}} 4.2 KB +0.1 KB
pgdog::frontend::router::parser::query::select::<impl pgdog::frontend::router::parser::que 3.8 KB -0.1 KB
tokio::runtime::task::raw::poll 3.4 KB +0.1 KB
lb flamegraph vs dbeed6a

lb

Open interactive for hover, zoom and search.

lb removed

Open interactive.

microbench flamegraph vs dbeed6a

microbench

Open interactive for hover, zoom and search.

microbench removed

Open interactive.

pooler flamegraph vs dbeed6a

pooler

Open interactive for hover, zoom and search.

pooler removed

Open interactive.

sharding-w2 flamegraph vs dbeed6a

sharding-w2

Open interactive for hover, zoom and search.

sharding-w2 removed

Open interactive.

sharding-w8 flamegraph vs dbeed6a

sharding-w8

Open interactive for hover, zoom and search.

sharding-w8 removed

Open interactive.

Logs

lb, microbench, pooler, sharding-w2, sharding-w8, verdict.json

Compared 56897d2 with dbeed6a on m8g.metal-24xl (spot).

@github-actions

github-actions Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Metal bench (x86) failed. 152 of 520 measures outside bounds.

suite throughput cpu/txn user cycles/txn user instr/txn kernel cycles/txn kernel instr/txn ctx/txn
pooler 34.75 (+0.1% ± 0.2%) 22,960 (-0.1% ± 0.8%) 32,795 (+0.1% ± 0.02%) 57,808 (+0.2% ± 0.2%) 38,582 (-0.0% ± 0.2%)
lb 35.86 (+0.1% ± 0.1%) 24,536 (+0.9% ± 0.8%) 34,106 (+0.5% ± 0.03%) 58,576 (-0.3% ± 0.3%) 39,194 (-0.1% ± 0.10%)
sharding-w2 56.59 (+0.6% ± 0.7%) 53,543 (+0.9% ± 1.2%) 98,957 (+0.2% ± 0.00%) 72,750 (+0.6% ± 1.1%) 43,096 (-0.0% ± 0.2%) 1.39 (-0.2% ± 0.3%)
sharding-w8 147,459 (-1.3% ± 2.2%) 48.43 (+0.7% ± 0.7%) 58,829 (+1.7% ± 1.2%) 54,419 (+0.3% ± 0.07%) 64,149 (-0.0% ± 1.0%) 34,204 (-0.0% ± 0.1%) 0.1675 (-1.6% ± 2.8%)
Counters per txn
suite frontend stall l1i refill backend stall
pooler 73,943 (-0.3% ± 0.8%) 3,635 (+0.2% ± 0.2%)
lb 79,778 (+2.3% ± 0.7%) 3,819 (+1.1% ± 0.3%)
sharding-w2 163,694 (+1.8% ± 3.1%) 6,134 (-0.3% ± 0.6%)
sharding-w8 122,468 (-0.2% ± 4.5%) 5,014 (+0.2% ± 0.6%)
benchmark measure result baseline change bound
extended_cached::aggregate alloc_bytes 917 893 +2.69% ± 0.00% +1%
extended_cached::begin alloc_bytes 344 320 +7.50% ± 0.00% +1%
extended_cached::cte alloc_bytes 1,320 1,296 +1.85% ± 0.00% +1%
extended_cached::delete_all_shards alloc_bytes 408 384 +6.25% ± 0.00% +1%
extended_cached::delete_by_key alloc_bytes 620 596 +4.03% ± 0.00% +1%
extended_cached::distinct alloc_bytes 600 576 +4.17% ± 0.00% +1%
extended_cached::group_by alloc_bytes 968 944 +2.54% ± 0.00% +1%
extended_cached::having alloc_bytes 1,104 1,080 +2.22% ± 0.00% +1%
extended_cached::having_distinct_limit alloc_bytes 1,530 1,506 +1.59% ± 0.00% +1%
extended_cached::insert alloc_bytes 536 512 +4.69% ± 0.00% +1%
extended_cached::insert_returning alloc_bytes 536 512 +4.69% ± 0.00% +1%
extended_cached::insert_split alloc_bytes 7,386 7,026 +5.12% ± 0.00% +1%
extended_cached::join alloc_bytes 1,620 1,596 +1.50% ± 0.00% +1%
extended_cached::lookup alloc_bytes 928 904 +2.65% ± 0.00% +1%
extended_cached::min_max_sum alloc_bytes 880 856 +2.80% ± 0.00% +1%
extended_cached::order_limit_offset alloc_bytes 907 883 +2.72% ± 0.00% +1%
extended_cached::select_all_shards alloc_bytes 600 576 +4.17% ± 0.00% +1%
extended_cached::select_by_key alloc_bytes 928 904 +2.65% ± 0.00% +1%
extended_cached::select_in_list alloc_bytes 1,136 1,112 +2.16% ± 0.00% +1%
extended_cached::select_omni alloc_bytes 408 384 +6.25% ± 0.00% +1%
extended_cached::set alloc_bytes 401 377 +6.37% ± 0.00% +1%
extended_cached::subquery alloc_bytes 792 768 +3.12% ± 0.00% +1%
extended_cached::update_all_shards alloc_bytes 408 384 +6.25% ± 0.00% +1%
extended_cached::update_by_key alloc_bytes 620 596 +4.03% ± 0.00% +1%
extended_cold::aggregate alloc_bytes 1,990 1,966 +1.22% ± 0.00% +1%
extended_cold::begin alloc_bytes 782 758 +3.17% ± 0.00% +1%
extended_cold::cte alloc_bytes 1,871 1,847 +1.30% ± 0.00% +1%
extended_cold::delete_all_shards alloc_bytes 876 852 +2.82% ± 0.00% +1%
extended_cold::delete_by_key alloc_bytes 1,109 1,085 +2.21% ± 0.00% +1%
extended_cold::distinct alloc_bytes 1,099 1,075 +2.23% ± 0.00% +1%
extended_cold::group_by alloc_bytes 1,819 1,795 +1.34% ± 0.00% +1%
extended_cold::insert alloc_bytes 1,487 1,463 +1.64% ± 0.00% +1%
extended_cold::insert_returning alloc_bytes 1,529 1,505 +1.59% ± 0.00% +1%
extended_cold::insert_split alloc_bytes 9,722 9,362 +3.85% ± 0.00% +1%
extended_cold::join alloc_bytes 2,198 2,174 +1.10% ± 0.00% +1%
extended_cold::min_max_sum alloc_bytes 1,558 1,534 +1.56% ± 0.00% +1%
extended_cold::order_limit_offset alloc_bytes 1,523 1,499 +1.60% ± 0.00% +1%
extended_cold::select_all_shards alloc_bytes 1,070 1,046 +2.29% ± 0.00% +1%
extended_cold::select_by_key alloc_bytes 1,427 1,403 +1.71% ± 0.00% +1%
extended_cold::select_in_list alloc_bytes 1,652 1,628 +1.47% ± 0.00% +1%

Microbench: 355 of 507 measures within bounds. See verdict.json for the rest.

Microbench latency: 21 benches changed more than 3%
benchmark latency baseline change instructions change
extended_cold::select_all_shards 3,930 ns 4,128 ns -4.79% ± 0.66% +0.22% ± 0.00%
extended_cold::update_by_key 4,376 ns 4,592 ns -4.69% ± 2.18% +0.15% ± 0.00%
extended_cold::distinct 4,188 ns 4,384 ns -4.47% ± 0.68% +0.18% ± 0.00%
simple_cold::begin 1,879 ns 1,966 ns -4.43% ± 1.35% +0.45% ± 0.00%
extended_cold::delete_by_key 3,812 ns 3,984 ns -4.31% ± 0.70% +0.18% ± 0.00%
extended_cold::begin 1,981 ns 2,069 ns -4.25% ± 0.86% +0.40% ± 0.00%
simple_cold::set 2,406 ns 2,506 ns -3.99% ± 1.24% -1.71% ± 0.00%
extended_evicting::set 2,662 ns 2,768 ns -3.83% ± 0.84% -1.51% ± 0.00%
simple_cold::update_by_key 4,260 ns 4,428 ns -3.79% ± 1.08% +0.16% ± 0.00%
extended_cold::select_by_key 4,811 ns 4,993 ns -3.64% ± 1.09% +0.15% ± 0.00%
simple_cold::select_all_shards 3,807 ns 3,951 ns -3.64% ± 1.10% +0.23% ± 0.00%
simple_cold::update_all_shards 3,381 ns 3,507 ns -3.59% ± 0.63% +0.21% ± 0.00%
extended_cold::delete_all_shards 2,859 ns 2,963 ns -3.51% ± 0.37% +0.26% ± 0.00%
extended_evicting::select_all_shards 4,137 ns 4,285 ns -3.45% ± 1.00% +0.19% ± 0.00%
simple_cold::delete_by_key 3,696 ns 3,828 ns -3.44% ± 1.48% +0.19% ± 0.00%
extended_evicting::update_all_shards 3,647 ns 3,777 ns -3.44% ± 0.69% +0.19% ± 0.00%
extended_evicting::begin 2,145 ns 2,221 ns -3.42% ± 1.31% +0.38% ± 0.00%
extended_cold::update_all_shards 3,533 ns 3,655 ns -3.33% ± 1.06% +0.20% ± 0.00%
extended_cold::select_omni 3,573 ns 3,689 ns -3.14% ± 1.10% +0.25% ± 0.00%
simple_cold::distinct 4,109 ns 4,239 ns -3.07% ± 0.61% +0.19% ± 0.00%
extended_evicting::distinct 4,381 ns 4,519 ns -3.05% ± 0.87% +0.17% ± 0.00%
Hot code per txn
path base head change
pooler 110.8 KB 112.0 KB +1.2 KB
sharding-w2 170.4 KB 172.1 KB +1.8 KB

pooler:

crate base head change
pgdog::frontend::client::query_engine 21.2 KB 21.5 KB +0.2 KB
tokio 17.7 KB 17.8 KB +0.1 KB
pgdog::backend::pool 16.6 KB 16.8 KB +0.2 KB
pgdog::frontend (other) 9.2 KB 8.9 KB -0.2 KB
rustls (TLS) 7.0 KB 7.4 KB +0.4 KB
pgdog::backend::server 7.2 KB 7.4 KB +0.2 KB
aws-lc (TLS crypto) 7.4 KB 7.3 KB -0.1 KB
jemalloc 6.8 KB 6.4 KB -0.3 KB
everything else 17.7 KB 18.3 KB +0.6 KB
function head change
pgdog::frontend::client::Client::run::{{closure}} 6.9 KB -0.2 KB
pgdog::frontend::client::query_engine::query::<impl pgdog::frontend::client::query_engine: 6.3 KB +0.2 KB
pgdog::frontend::client::query_engine::QueryEngine::handle::{{closure}} 4.6 KB +0.2 KB
pgdog::frontend::client::query_engine::query::<impl pgdog::frontend::client::query_engine: 3.8 KB +0.1 KB
tokio::runtime::task::raw::poll 3.6 KB +0.1 KB

sharding-w2:

crate base head change
pgdog::frontend::router 27.2 KB 28.2 KB +0.9 KB
pgdog::frontend::client::query_engine 23.4 KB 23.9 KB +0.4 KB
pgdog::backend::pool 17.9 KB 18.9 KB +1.1 KB
tokio 18.1 KB 18.1 KB -0.1 KB
other 11.4 KB 11.2 KB -0.2 KB
jemalloc 10.1 KB 10.8 KB +0.7 KB
pgdog::frontend (other) 10.8 KB 10.6 KB -0.2 KB
aws-lc (TLS crypto) 10.8 KB 10.3 KB -0.4 KB
everything else 40.6 KB 40.2 KB -0.4 KB
function head change
pgdog::frontend::client::Client::run::{{closure}} 7.9 KB -0.2 KB
pgdog::frontend::client::query_engine::query::<impl pgdog::frontend::client::query_engine: 6.2 KB -0.1 KB
pgdog::frontend::client::query_engine::QueryEngine::handle::{{closure}} 4.8 KB +0.2 KB
pgdog::frontend::router::parser::query::select::<impl pgdog::frontend::router::parser::que 4.8 KB +0.1 KB
tokio::runtime::task::raw::poll 3.8 KB +0.0 KB
lb flamegraph vs dbeed6a

lb

Open interactive for hover, zoom and search.

lb removed

Open interactive.

microbench flamegraph vs dbeed6a

microbench

Open interactive for hover, zoom and search.

microbench removed

Open interactive.

pooler flamegraph vs dbeed6a

pooler

Open interactive for hover, zoom and search.

pooler removed

Open interactive.

sharding-w2 flamegraph vs dbeed6a

sharding-w2

Open interactive for hover, zoom and search.

sharding-w2 removed

Open interactive.

sharding-w8 flamegraph vs dbeed6a

sharding-w8

Open interactive for hover, zoom and search.

sharding-w8 removed

Open interactive.

Logs

lb, microbench, pooler, sharding-w2, sharding-w8, verdict.json

Compared 56897d2 with dbeed6a on c7a.metal-48xl (spot).

@levkk

levkk commented Nov 19, 2026

Copy link
Copy Markdown
Collaborator Author

Going to wait on #2077 before merging.

@levkk levkk changed the title feat: label replicas feat(lb): label replicas Nov 19, 2026
@levkk
levkk force-pushed the levkk-label-replicas branch from 2a49b0c to 56897d2 Compare November 19, 2026 22:53
@levkk
levkk marked this pull request as ready for review November 19, 2026 22:59
@levkk
levkk requested a review from meskill November 19, 2026 22:59
Comment on lines 195 to 197
other.database_number = self.database_number;
other.passwords = self.passwords.clone();
self == &other

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we add new fields to this check to avoid reconnections?

Comment on lines 17 to 19
/// Minimum replica LSN the client wants the replica to have
/// before allowing reads on it, in its 64-bit representation.
pub(crate) lsn: Option<u64>,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we move lsn also to the LbRequest?

) -> Self {
Self {
request: PoolRequest::new(id, route, created_at),
label: route.label.clone(),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wonder if we can drop the clone here and make the LbRequest with lifetime instead.

@@ -447,12 +457,14 @@
return Err(Error::AllReplicasDown);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we found no candidates for the label we'll get this error, which is not quite right and we probably need a separate error

Comment on lines +11 to +17
let replicas = LoadBalancer::new(
&None,
&[reporting, analytics, backup],
LoadBalancingStrategy::RoundRobin,
ReadWriteSplit::ExcludePrimary,
Default::default(),
);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I've added helper super::test_lb that maybe helpful

Comment on lines +121 to +123
let mut route = Route::read(ShardWithPriority::default());
route.label = Some("reporting".into());
let request = LbRequest::new(Default::default(), &route, false, Instant::now());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this probably could be put to function to reuse in tests instead of duplication

// make sure we don't use the banned targets calculations,
// since it can skew the distribution when we have banned instances
// Let labeled requests use banned targets when no unbanned replica remains.
let ignore_ban = request.label.is_some() && !has_unbanned_replica(&candidates);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, this will probably won't work well if the primary has the label and all replicas with label are banned, we'll ignore the bans instead routing to primary in this case

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

anyway, I'm worried about this approach of doing the fallback here, since any label misspell or config misconfiguration could show working queries instead of signaling of issues.

I faced the same controversy for the pgdog.role refactor - it sounds like the real cases may want different outcome (fail or use fallback) and not strict rule. I don't like very much that the routing can stop working as expected without visual signs

Comment on lines 535 to 537
candidates
.iter()
.for_each(|target| target.ban.unban(true, UnbanReason::AllTargetsBanned));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, that may backfire with labels - if a single query with label doesn't find it's target, we'll unban the whole cluster here.

This branch has not been deployed

No deployments
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.

[LB] Label-based replica selection

2 participants