Conversation
ganesh-krishnan
previously approved these changes
Jan 31, 2020
lore/io/connection.py
Outdated
| def _select(self, sql, bindings): | ||
| return self.__execute(sql, bindings).fetchall() | ||
| if self._use_psycopg2: | ||
| try: |
Contributor
There was a problem hiding this comment.
Wonder if there is value in falling back to SQL alchemy as a final try before giving up completely.
Contributor
There was a problem hiding this comment.
Not a blocker though. This looks good!
ilystsov
suggested changes
Oct 30, 2023
ilystsov
left a comment
There was a problem hiding this comment.
If self._use_psycopg2 is False, we immediately return the result for the alternative execution.
Merged the two with statements into one line.
if the error isn't about "no results to fetch", the original exception will be automatically re-raised without needing to explicitly call raise e.
Comment on lines
+441
to
+453
| def _connection_execute(self, sql, bindings): | ||
| if self._use_psycopg2: | ||
| with self._connection.engine.raw_connection().connection as conn: | ||
| with conn.cursor() as cursor: | ||
| cursor.execute(sql, bindings) | ||
| try: | ||
| return ResultWrapper(cursor.fetchall()) | ||
| except psycopg2.ProgrammingError as e: | ||
| if 'no results to fetch' in str(e): | ||
| return None | ||
| raise e | ||
| else: | ||
| return self._connection.execute(sql, bindings) |
There was a problem hiding this comment.
Suggested change
| def _connection_execute(self, sql, bindings): | |
| if self._use_psycopg2: | |
| with self._connection.engine.raw_connection().connection as conn: | |
| with conn.cursor() as cursor: | |
| cursor.execute(sql, bindings) | |
| try: | |
| return ResultWrapper(cursor.fetchall()) | |
| except psycopg2.ProgrammingError as e: | |
| if 'no results to fetch' in str(e): | |
| return None | |
| raise e | |
| else: | |
| return self._connection.execute(sql, bindings) | |
| def _connection_execute(self, sql, bindings): | |
| if not self._use_psycopg2: | |
| return self._connection.execute(sql, bindings) | |
| try: | |
| with self._connection.engine.raw_connection().connection as conn, conn.cursor() as cursor: | |
| cursor.execute(sql, bindings) | |
| return ResultWrapper(cursor.fetchall()) | |
| except psycopg2.ProgrammingError as e: | |
| if 'no results to fetch' not in str(e): | |
| raise | |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What
Use Psycopg2 directly for PG selects in order to improve performance.
Why
sqlalchemy ads ~1ms of additional latency for basic selects and more for queries which return large result sets.
Without sqlalchemy
With sqlalchemy
sql_test.py