Skip to content
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

Special behavior invocation via comment instead of a custom SQL #168

Open
ankurcha opened this issue Sep 19, 2022 · 6 comments
Open

Special behavior invocation via comment instead of a custom SQL #168

ankurcha opened this issue Sep 19, 2022 · 6 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@ankurcha
Copy link

Is your feature request related to a problem? Please describe.
The custom SQL introduced for selecting shard and replicas is functional but it makes transition to a sharded environment and testing less transparent. It would be better if we could follow https://github.com/ossc-db/pg_hint_plan kind of approach where the SET SHARDING KEY TO ... and SET SERVER ROLE TO '(PRIMARY|REPLICA|ANY|AUTO|DEFAULT)' is into comments like:

/*+ SET SERVER ROLE TO '(PRIMARY|REPLICA|ANY|AUTO|DEFAULT)' +*/

to set the right behaior.

Describe the solution you'd like
Use comment based way of enabling shard / replica selection.

Describe alternatives you've considered
separate pools and application logic.

Additional context

@ankurcha
Copy link
Author

Refers to: #165

@levkk
Copy link
Contributor

levkk commented Sep 19, 2022

Agreed! One small issue I ran into so far is ORMs, e.g. ActiveRecord, will start a transaction automatically if you create/save a model, but it won't put the comment into the BEGIN statement; PgCat won't know which shard to talk to until it's too late. rails/rails#46032

For others, e.g. Django, this is not an issue because transactions are not started automatically.

Any ideas are welcome! We will probably just modify ActiveRecord on our side and run with the comment approach, but we'd like to make this easy to use for everyone.

@ankurcha , what ORM, if any, are you using for your apps?

@ankurcha
Copy link
Author

I am using golang without any ORM. For ORMs like activerecord we would likely need to do some monkey patching to prepend the sql. In my experience, adding comments to annotate sql is probably going to be difficult to consistently added without some custom implementation for each one.

You could also think about wrapping the sql driver for each supported language.

@levkk levkk added enhancement New feature or request help wanted Extra attention is needed labels Sep 22, 2022
@jmeagher
Copy link
Contributor

There's a lot of overlap between this and what I'm working on in #293 to get around the Rails comment limitation. We solved that on the Rails side with a custom DB adapter. I'll see what I can do to merge the ideas from this and #165 into one solution that works in general.

@DeoLeung
Copy link

wondering how to use shard comment in sqlalchemy transaction
https://stackoverflow.com/questions/76563760/how-to-add-comment-before-begin-statement-in-sqlalchemy-for-postgres

@n-oden
Copy link

n-oden commented Apr 23, 2024

Just adding my voice to the chorus here: implementing this would make transitioning to pgcat from pgpool-II substantially simpler. PGPool uses comments to allow clients to pick whether they will be normally load-balanced or sent directly to the primary instance: https://www.pgpool.net/docs/42/en/html/runtime-config-load-balancing.html -- adding another comment on top of that one is trivial, but using a sql-ish statement that can only be parsed by pgcat makes the cutover process a lot trickier.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

5 participants