-
Notifications
You must be signed in to change notification settings - Fork 63
feat: side-effect SELECT #158
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
denysvitali
wants to merge
6
commits into
pgdogdev:main
Choose a base branch
from
denysvitali:feature/pg-advisory-lock
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b9a0098
feat: side-effect SELECT
denysvitali f61a44d
fix: support more nodes
denysvitali 422ebba
fix: pick one shard
denysvitali c21bc8f
style: fmt
denysvitali 3b45570
test: Shard::Direct
denysvitali fbeb81f
fix: attempt to send to all shards
denysvitali File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
feat: side-effect SELECT
- Loading branch information
commit b9a0098a727860533e6396e36633c3509f1d68b0
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,24 @@ use pg_query::{ | |
use regex::Regex; | ||
use tracing::{debug, trace}; | ||
|
||
static SIDE_EFFECT_FUNCTIONS: Lazy<HashSet<String>> = Lazy::new(|| { | ||
let mut set = HashSet::new(); | ||
// Advisory lock functions | ||
set.insert("pg_advisory_lock".to_lowercase()); | ||
set.insert("pg_advisory_xact_lock".to_lowercase()); | ||
set.insert("pg_advisory_lock_shared".to_lowercase()); | ||
set.insert("pg_advisory_xact_lock_shared".to_lowercase()); | ||
set.insert("pg_try_advisory_lock".to_lowercase()); | ||
set.insert("pg_try_advisory_xact_lock".to_lowercase()); | ||
set.insert("pg_try_advisory_lock_shared".to_lowercase()); | ||
set.insert("pg_try_advisory_xact_lock_shared".to_lowercase()); | ||
// Sequence functions | ||
set.insert("nextval".to_lowercase()); | ||
set.insert("setval".to_lowercase()); | ||
// Add other functions with side-effects here | ||
set | ||
}); | ||
|
||
static REPLICATION_REGEX: Lazy<Regex> = Lazy::new(|| { | ||
Regex::new( | ||
"(CREATE_REPLICATION_SLOT|IDENTIFY_SYSTEM|DROP_REPLICATION_SLOT|READ_REPLICATION_SLOT|ALTER_REPLICATION_SLOT|TIMELINE_HISTORY).*", | ||
|
@@ -226,6 +244,13 @@ impl QueryParser { | |
let mut command = match root.node { | ||
// SELECT statements. | ||
Some(NodeEnum::SelectStmt(ref stmt)) => { | ||
// Check for side-effect functions in the target list | ||
if Self::has_side_effect_function(stmt) { | ||
debug!("Query contains side-effect function, routing to primary."); | ||
return Ok(Command::Query(Route::write(None))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This makes it a cross-shard query. I don't think we want that, necessarily, especially for advisory locks. You'd probably want to pick a shard that handles it, since advisory locks on different shards won't actually mutually exclude queries. |
||
} | ||
|
||
// If no side-effect functions found, proceed with normal SELECT routing | ||
if matches!(shard, Shard::Direct(_)) { | ||
return Ok(Command::Query(Route::read(shard))); | ||
} | ||
|
@@ -701,6 +726,30 @@ impl QueryParser { | |
fn delete(_stmt: &DeleteStmt) -> Result<Command, Error> { | ||
Ok(Command::Query(Route::write(None))) | ||
} | ||
|
||
/// Check if a SELECT statement contains any functions with side effects. | ||
fn has_side_effect_function(stmt: &SelectStmt) -> bool { | ||
for target_item_wrapper_node in &stmt.target_list { | ||
if let Some(NodeEnum::ResTarget(res_target)) = target_item_wrapper_node.node.as_ref() { | ||
denysvitali marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if let Some(val_node_wrapper) = &res_target.val { | ||
if let Some(NodeEnum::FuncCall(func_call)) = val_node_wrapper.node.as_ref() { | ||
if let Some(name_part_node) = func_call.funcname.last() { | ||
if let Some(NodeEnum::String(func_name_node_val)) = | ||
name_part_node.node.as_ref() | ||
{ | ||
if SIDE_EFFECT_FUNCTIONS | ||
.contains(&func_name_node_val.sval.to_lowercase()) | ||
{ | ||
return true; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
false | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.