jaaql-middleware-python 5.3.19__py3-none-any.whl → 5.3.21__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
jaaql/constants.py CHANGED
@@ -18,6 +18,7 @@ KEY__database = "database"
18
18
  KEY__role = "role"
19
19
  KEY__read_only = "read_only"
20
20
  KEY__prevent_unused_parameters = "prevent_unused_parameters"
21
+ KEY__timelines = "timelines"
21
22
  KEY__install_key = "install_key"
22
23
  KEY__jaaql_password = "jaaql_password"
23
24
  KEY__super_db_password = "super_db_password"
@@ -63,6 +64,17 @@ CRON_dayOfWeek = "dayOfWeek"
63
64
  REGEX__dmbs_object_name = r'^[0-9a-zA-Z_]{1,63}$'
64
65
  REGEX__dmbs_procedure_name = r'^[0-9a-zA-Z_$-.\+]{1,63}$'
65
66
 
67
+ # The optional request key KEY__timelines ({"<timeline>": "<ISO-8601 moment>" | null}) sets, for the request's transaction
68
+ # only, the moment each timeline is viewed at. A moment becomes the transaction-local setting GUC__timeline_prefix + name,
69
+ # which the generated timeline views read as nullif(current_setting('timeline.<name>', true), ''); the prefix must match the
70
+ # DBMS package. Names are lower-cased before they are checked (setting names are case-insensitive). The patterns are for
71
+ # re.fullmatch
72
+ GUC__timeline_prefix = "timeline."
73
+ TIMELINES__max_count = 16
74
+ TIMELINE__max_moment_length = 64
75
+ REGEX__timeline_name = r'^[a-z_][a-z0-9_]{0,62}$'
76
+ REGEX__timeline_moment = r'^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])([T ]([01][0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9](\.[0-9]{1,6})?)?(Z|[+-](0[0-9]|1[0-5]):[0-5][0-9])?)?$'
77
+
66
78
  SEPARATOR__comma_space = ", "
67
79
  SEPARATOR__comma = ","
68
80
  SEPARATOR__space = " "
@@ -132,6 +144,12 @@ ERR__document_id_not_found = "Document id not found"
132
144
  ERR__unlock_code_expired = "The short unlock code has expired. Please use the long link found in your email"
133
145
  ERR__invalid_lock = "Either security event does not exist, has already been used, has expired"
134
146
  ERR__incorrect_lock_code = "Incorrect lock code"
147
+ ERR__timelines_malformed = "'timelines' must be an object mapping timeline names to moments"
148
+ ERR__timelines_too_many = "'timelines' names %d timelines, at most %d are allowed"
149
+ ERR__timeline_name_invalid = "Timeline name %s is invalid: expected a letter or underscore followed by at most 62 letters, digits or underscores"
150
+ ERR__timeline_name_repeated = "Timeline '%s' is named more than once in 'timelines' (timeline names are case-insensitive)"
151
+ ERR__timeline_moment_invalid = "Moment %s for timeline '%s' is invalid: expected null or an ISO-8601 date or date-time of at most %d characters, such as '2024-03-01', '2024-03-01T12:30:00' or '2024-03-01T12:30:00+01:00'"
152
+ ERR__timelines_with_autocommit = "'timelines' cannot be combined with 'autocommit': a moment holds for the request's transaction, which autocommit ends after every statement"
135
153
 
136
154
  PG_ENV__password = "POSTGRES_PASSWORD"
137
155
 
@@ -218,5 +236,5 @@ ROLE__dba = "dba"
218
236
 
219
237
  PROTOCOL__postgres = "postgresql://"
220
238
 
221
- VERSION = "5.3.19"
239
+ VERSION = "5.3.21"
222
240
 
jaaql/db/db_interface.py CHANGED
@@ -68,9 +68,19 @@ class DBInterface(ABC):
68
68
  def get_conn(self):
69
69
  pass
70
70
 
71
- def is_connection_error(self, ex) -> bool:
72
- # Overridden by the concrete interface. True when a failure means the connection/backend went
73
- # away (so nothing persisted and the operation is safe to retry on a fresh connection).
71
+ def is_connection_closed(self, conn) -> bool:
72
+ # Overridden by the concrete interface. True when the connection/backend went away, taking any
73
+ # uncommitted transaction with it (so that transaction persisted nothing)
74
+ return False
75
+
76
+ def statement_may_end_transaction(self, query: str) -> bool:
77
+ # Overridden by the concrete interface. True when running the text may end the transaction it runs
78
+ # in (a COMMIT of the request's own), so what ran before it may persist whatever happens after
79
+ return True
80
+
81
+ def has_ended_transaction(self, conn) -> bool:
82
+ # Overridden by the concrete interface. True when a transactional connection is out of its
83
+ # transaction after a statement succeeded: that statement ended it
74
84
  return False
75
85
 
76
86
  def log_warning(self, exc):
@@ -81,19 +91,25 @@ class DBInterface(ABC):
81
91
  if self.logging:
82
92
  logging.warning(exc, exc_info=False)
83
93
 
84
- def __attempt_commit_rollback(self, conn, err):
94
+ def __attempt_commit_rollback(self, conn, err, commit: bool = True):
85
95
  # Returns the commit failure rather than swallowing it. A failed commit means the transaction
86
96
  # did NOT persist, so the caller must not report success. Silently swallowing it turned lost
87
97
  # writes into a misleading downstream error - e.g. create_account's CREATE ROLE lost on a
88
98
  # connection killed by a \wipe dbms reboot, then "GRANT registered TO <role>" failing with
89
99
  # "role does not exist" on the very next statement.
100
+ # commit False (a read_only request): nothing it did may persist, so it is rolled back like a
101
+ # failed request, never left open for whoever handles the connection next to commit
90
102
  try:
91
- if err is None:
103
+ if err is None and commit:
104
+ if self.is_connection_closed(conn):
105
+ # The COMMIT cannot reach the server, so nothing persisted: a caller that owns the
106
+ # request may re-run it on a fresh connection
107
+ return ConnectionLostError("the connection was closed before COMMIT")
92
108
  self.commit(conn)
93
109
  else:
94
110
  self.rollback(conn)
95
111
  except Exception as ex:
96
- if err is None:
112
+ if err is None and commit:
97
113
  self.log_warning(ex) # commit failed - the transaction did not persist
98
114
  return ex
99
115
  else:
@@ -113,43 +129,53 @@ class DBInterface(ABC):
113
129
  self.log_warning(ex) # Serious error, connection failure to db or similar
114
130
  raise ex
115
131
 
116
- def __raise_commit_failure(self, commit_err):
117
- # A failed commit persisted nothing; surface it (instead of the old silent swallow) so the
118
- # caller cannot treat lost writes as success and hit a misleading error further on. A
119
- # connection-loss failure is raised as a retriable marker so a self-contained caller can
120
- # re-run on a fresh connection; any other commit failure is a genuine error.
121
- if self.is_connection_error(commit_err):
122
- raise ConnectionLostError(str(commit_err))
123
- translated = self.translate_commit_error(commit_err)
132
+ def __commit_failure(self, conn, commit_err, commit_error_set):
133
+ # A failed commit persisted nothing (or, lost in flight, possibly did); surface it (instead of the
134
+ # old silent swallow) so the caller cannot treat lost writes as success and hit a misleading error
135
+ # further on. A COMMIT that never reached the server is the retriable marker, so a self-contained
136
+ # caller can re-run on a fresh connection; any other commit failure is a genuine error.
137
+ if isinstance(commit_err, ConnectionLostError):
138
+ return commit_err
139
+ try:
140
+ translated = self.translate_commit_error(conn, commit_err, commit_error_set)
141
+ except Exception as ex:
142
+ # Translating reads what the database raised. Failing to must not cost the answer, nor (put_conn_handle_error) the
143
+ # connection, so the failure is answered untranslated
144
+ self.log_warning(ex)
145
+ translated = None
124
146
  if translated is not None:
125
- raise translated
126
- raise HttpStatusException("Commit failed, transaction not persisted: " + str(commit_err),
127
- HTTPStatus.INTERNAL_SERVER_ERROR)
128
-
129
- def translate_commit_error(self, commit_err):
130
- # A deferred constraint trigger raises at COMMIT rather than during its statement. When what it
131
- # raises is an error the statement path would have handed back as a handled error, a subclass
132
- # returns that error here so the caller sees the same response either way; None keeps the 500
147
+ return translated
148
+ return HttpStatusException("Commit failed, transaction not persisted: " + str(commit_err),
149
+ HTTPStatus.INTERNAL_SERVER_ERROR)
150
+
151
+ def translate_commit_error(self, conn, commit_err, error_set: str = None):
152
+ # A deferred constraint raises at COMMIT rather than during its statement. A subclass returns the
153
+ # error the statement path would have handed back for it, attributed to error_set (the request's
154
+ # query set, when it has only one), so the caller sees the same response either way; None keeps the 500
133
155
  return None
134
156
 
135
- def handle_error(self, conn, err, echo=ECHO__none):
157
+ def handle_error(self, conn, err, echo=ECHO__none, commit_error_set: str = None):
136
158
  commit_err = self.__attempt_commit_rollback(conn, err)
137
159
  if err is None and commit_err is not None:
138
- self.__raise_commit_failure(commit_err)
160
+ raise self.__commit_failure(conn, commit_err, commit_error_set)
139
161
  self.__err_to_exception(err, echo)
140
162
 
141
- def put_conn_handle_error(self, conn, err, echo=ECHO__none, skip_rollback_commit: bool = False):
142
- commit_err = None
143
- if not skip_rollback_commit:
144
- commit_err = self.__attempt_commit_rollback(conn, err)
145
-
163
+ def put_conn_handle_error(self, conn, err, echo=ECHO__none, skip_commit: bool = False, commit_error_set: str = None):
164
+ commit_failure = None
146
165
  try:
147
- self.put_conn(conn)
148
- except Exception as ex:
149
- self.log_warning(ex)
150
-
151
- if err is None and commit_err is not None:
152
- self.__raise_commit_failure(commit_err)
166
+ commit_err = self.__attempt_commit_rollback(conn, err, commit=not skip_commit)
167
+ # Decided before the connection goes back: from then on the putback thread is working on it
168
+ if err is None and commit_err is not None:
169
+ commit_failure = self.__commit_failure(conn, commit_err, commit_error_set)
170
+ finally:
171
+ # Whatever ending the transaction or deciding the answer raised, the connection goes back to the pool
172
+ try:
173
+ self.put_conn(conn)
174
+ except Exception as ex:
175
+ self.log_warning(ex)
176
+
177
+ if commit_failure is not None:
178
+ raise commit_failure
153
179
 
154
180
  self.__err_to_exception(err, echo)
155
181
 
@@ -1,7 +1,9 @@
1
+ import re
1
2
  import uuid
2
3
 
3
4
  import psycopg
4
5
  from psycopg import OperationalError
6
+ from psycopg.pq import TransactionStatus
5
7
  from psycopg_pool import ConnectionPool, PoolClosed
6
8
  import queue
7
9
  from psycopg.errors import ProgrammingError, InvalidParameterValue, UndefinedFunction, InternalError
@@ -12,10 +14,12 @@ from jaaql.constants import ERR__invalid_token
12
14
  from jaaql.db.db_interface import DBInterface, ECHO__none, CHAR__newline
13
15
  from jaaql.exceptions.http_status_exception import *
14
16
  from jaaql.exceptions.custom_http_status import CustomHTTPStatus
15
- from jaaql.exceptions.jaaql_interpretable_handled_errors import UserUnauthorized, handled_procedure_error_from_raise
17
+ from jaaql.exceptions.jaaql_interpretable_handled_errors import UserUnauthorized, DatabaseOperationalError, handled_error_from_database_error, \
18
+ database_error_descriptor
16
19
  from jaaql.constants import KEY__database
17
20
 
18
21
  ERR__connect_db = "Could not create connection to database!"
22
+ ERR__commit_outcome_unknown = "The connection was lost during COMMIT, so whether the transaction was persisted is unknown: "
19
23
 
20
24
  PGCONN__min_conns = 5
21
25
  PGCONN__max_conns = 10
@@ -39,6 +43,9 @@ ERR__must_use_canned_query = "Must use canned query as you are not an admin!"
39
43
 
40
44
  QUERY__dba_query = "SELECT pg_has_role(datdba::regrole, 'MEMBER') FROM pg_database WHERE datname = %(database)s;"
41
45
  QUERY__dba_query_external = "SELECT pg_has_role(datdba::regrole, 'MEMBER') FROM pg_database WHERE datname = current_database();"
46
+ # Applies an interface's session_settings in one statement however many there are, names and values bound as parameters.
47
+ # is_local = true: they end with the request's transaction (COMMIT or ROLLBACK), so they never reach a later checkout
48
+ QUERY__apply_session_settings = "SELECT set_config(s.n, s.v, true) FROM unnest(%s::text[], %s::text[]) AS s(n, v)"
42
49
 
43
50
  try:
44
51
  # Pipeline mode needs libpq >= 14. create_app refuses to boot when this is False so a server
@@ -49,11 +56,19 @@ except Exception:
49
56
  PIPELINE_SUPPORTED = False
50
57
 
51
58
 
59
+ def _execute_pending_statement(cursor, statement):
60
+ # A pending statement is SQL text, or a (SQL, parameters) pair when it carries client-supplied values
61
+ if isinstance(statement, tuple):
62
+ cursor.execute(statement[0], statement[1])
63
+ else:
64
+ cursor.execute(statement)
65
+
66
+
52
67
  class JaaqlPGConnection(psycopg.Connection):
53
68
  """
54
69
  Connection whose per-checkout session-authorization statements (jaaql__set_session_authorization
55
- / SET ROLE) are deferred at checkout and sent pipelined with the first real query, saving a
56
- network round trip per checkout.
70
+ / SET ROLE, then the transaction-local session settings) are deferred at checkout and sent
71
+ pipelined with the first real query, saving a network round trip per checkout.
57
72
 
58
73
  Safety property: authorization must never be skippable. Any cursor obtained through the normal
59
74
  cursor() call while statements are still pending executes them eagerly first, so raw connection
@@ -87,7 +102,7 @@ class JaaqlPGConnection(psycopg.Connection):
87
102
  pending = self.jaaql_take_pending_auth()
88
103
  with super().cursor() as flush_cursor:
89
104
  for statement in pending:
90
- flush_cursor.execute(statement)
105
+ _execute_pending_statement(flush_cursor, statement)
91
106
  finally:
92
107
  self._jaaql_flushing = False
93
108
  return super().cursor(*args, **kwargs)
@@ -100,6 +115,48 @@ def _statement_is_preparable(query: str) -> bool:
100
115
  return ";" not in query.rstrip().rstrip(";")
101
116
 
102
117
 
118
+ REGEX__word = re.compile(r"[A-Za-z_][A-Za-z0-9_$]*")
119
+ STATEMENTS__ending_transaction = {"COMMIT", "END", "ROLLBACK", "ABORT"}
120
+
121
+
122
+ def _leading_word(query: str, i: int = 0) -> (str, int):
123
+ # The first word at or after i, upper-cased, and where it ends. Whitespace, -- comments and /* */ comments, which nest, are
124
+ # skipped as Postgres skips them
125
+ n = len(query)
126
+ while i < n:
127
+ if query[i].isspace():
128
+ i += 1
129
+ elif query.startswith("--", i):
130
+ while i < n and query[i] not in "\r\n":
131
+ i += 1
132
+ elif query.startswith("/*", i):
133
+ depth = 0
134
+ while i < n:
135
+ if query.startswith("/*", i):
136
+ depth += 1
137
+ i += 2
138
+ elif query.startswith("*/", i):
139
+ depth -= 1
140
+ i += 2
141
+ if depth == 0:
142
+ break
143
+ else:
144
+ i += 1
145
+ else:
146
+ break
147
+ match = REGEX__word.match(query, i)
148
+ return (match.group(0).upper(), match.end()) if match is not None else ("", i)
149
+
150
+
151
+ def _statement_may_end_transaction(query: str) -> bool:
152
+ # True when running the text may end the transaction it runs in, persisting what ran before it: several statements, any of
153
+ # which may (a ';' in a literal counts too, erring towards True), or one COMMIT, END, ROLLBACK, ABORT or PREPARE TRANSACTION
154
+ if not _statement_is_preparable(query):
155
+ return True
156
+ word, end = _leading_word(query)
157
+ return word in STATEMENTS__ending_transaction or (word == "PREPARE" and _leading_word(query, end)[0] == "TRANSACTION")
158
+
159
+
103
160
  def _escape_unescaped_percent(query: str) -> str:
104
161
  # psycopg3 scans every '%' in the SQL when parameters are supplied and
105
162
  # rejects anything that isn't a placeholder or '%%'. JAAQL uses named
@@ -171,34 +228,45 @@ class DBPGInterface(DBInterface):
171
228
  conn.jaaql_take_pending_auth()
172
229
  do_reset = False
173
230
  if do_reset:
174
- with conn.cursor() as cursor:
175
- cursor.execute("RESET ROLE;")
176
- did_close = False
177
- if hasattr(conn, "jaaql_reset_key"):
178
- try:
231
+ try:
232
+ if conn.info.transaction_status != TransactionStatus.IDLE:
233
+ # Whatever the request left open is not the putback's to persist (a read_only request, a caller that
234
+ # returned a connection mid-transaction): the commit below is for the reset statements alone
235
+ conn.rollback()
236
+ with conn.cursor() as cursor:
237
+ cursor.execute("RESET ROLE;")
238
+ if hasattr(conn, "jaaql_reset_key"):
179
239
  cursor.execute("SELECT jaaql_extension.jaaql__reset_session_authorization('" + str(conn.jaaql_reset_key) + "');")
180
- except:
181
- did_close = True
182
- conn.close()
183
- if not did_close:
184
240
  cursor.execute("RESET ALL;")
185
- conn.commit()
241
+ conn.commit()
242
+ except Exception:
243
+ # A connection that could not be reset must never serve another request, and must not be kept from the pool
244
+ # either: closed, putconn discards it and the pool opens a replacement
245
+ conn.close()
186
246
  DBPGInterface.HOST_POOLS[username][db_name].putconn(conn)
187
247
 
188
248
  @staticmethod
189
249
  def put_conn_threaded(username: str, db_name: str, the_queue: queue.Queue):
190
250
  while True:
251
+ conn, do_reset = the_queue.get()
191
252
  try:
192
- conn, do_reset = the_queue.get()
193
253
  DBPGInterface._process_returned_conn(username, db_name, conn, do_reset)
194
254
  except Exception:
195
- pass # Ignore, pool has likely been wiped
255
+ # The pool has likely been wiped or replaced. Close the connection so it cannot linger outside any pool
256
+ try:
257
+ conn.close()
258
+ except Exception:
259
+ pass
196
260
 
197
- def __init__(self, config, host: str, port: int, db_name: str, username: str, role: str = None, password: str = None, sub_role: str = None):
261
+ def __init__(self, config, host: str, port: int, db_name: str, username: str, role: str = None, password: str = None, sub_role: str = None,
262
+ session_settings: dict = None):
198
263
  super().__init__(config, host, username)
199
264
 
200
265
  self.role = role
201
266
  self.sub_role = sub_role
267
+ # {setting name: value}, applied transaction-locally after the authorization statements. Built server side (see
268
+ # pop_timeline_settings), never named by the client
269
+ self.session_settings = session_settings
202
270
  if sub_role is not None:
203
271
  if len([ch for ch in sub_role if not ch.isalnum() and ch not in ['_', '-']]) != 0:
204
272
  raise HttpStatusException(ERR__invalid_role, HTTPStatus.UNAUTHORIZED)
@@ -255,17 +323,28 @@ class DBPGInterface(DBInterface):
255
323
  # without executing (defensive: every return path should already have cleared it).
256
324
  # Without this a later checkout could execute a previous user's authorization
257
325
  conn.jaaql_take_pending_auth()
258
- if self.role is not None or self.sub_role is not None:
326
+ if conn.autocommit:
327
+ # A request that asked for autocommit hands its connection back still in autocommit and
328
+ # psycopg_pool does not reset it, so the next request to land on it would run statement by
329
+ # statement: a multi-query submit no longer atomic, a transaction-local setting gone before
330
+ # the query it was meant for. transform() applies autocommit again for requests that ask.
331
+ # The pool only hands out idle connections, so this cannot fail on an open transaction
332
+ conn.autocommit = False
333
+ if self.role is not None or self.sub_role is not None or self.session_settings:
259
334
  # Deferred: sent pipelined with the first query by execute_query, or flushed eagerly by
260
335
  # JaaqlPGConnection.cursor() if the connection is used rawly. Errors these statements
261
- # raise are translated by _translate_session_auth_error at execution time; dead pool
262
- # connections, previously detected here, are recycled by execute_query's
263
- # OperationalError retry loop
336
+ # raise are translated by _translate_session_auth_error at execution time; a dead pool
337
+ # connection, previously detected here, surfaces at that first query, and the request
338
+ # is re-run on a fresh connection (InterpretJAAQL.transform, ConnectionLostError)
264
339
  pending = []
265
340
  if self.role is not None:
266
341
  pending.append("SELECT jaaql_extension.jaaql__set_session_authorization('" + self.role + "', '" + conn.jaaql_reset_key + "');")
267
342
  if self.sub_role is not None:
268
343
  pending.append("SET ROLE \"" + self.sub_role + "\"")
344
+ if self.session_settings:
345
+ # After the authorization statements, so the settings are applied as the user and only once the
346
+ # parallel verifier has accepted the request
347
+ pending.append((QUERY__apply_session_settings, (list(self.session_settings.keys()), list(self.session_settings.values()))))
269
348
  conn.jaaql_set_pending_auth(pending)
270
349
  return conn
271
350
 
@@ -329,107 +408,102 @@ class DBPGInterface(DBInterface):
329
408
 
330
409
  def execute_query(self, conn, query, parameters=None, wait_hook: queue.Queue = None, prepare: bool = False,
331
410
  capture_provenance: list = None):
332
- x = 0
333
- err = None
334
- while x < PGCONN__max_conns:
335
- x += 1
336
- try:
337
- cursor_factory = conn.jaaql_raw_cursor if isinstance(conn, JaaqlPGConnection) else conn.cursor
338
- with cursor_factory() as cursor:
339
- do_prepare = prepare and _statement_is_preparable(query)
411
+ try:
412
+ cursor_factory = conn.jaaql_raw_cursor if isinstance(conn, JaaqlPGConnection) else conn.cursor
413
+ with cursor_factory() as cursor:
414
+ do_prepare = prepare and _statement_is_preparable(query)
340
415
 
341
- if wait_hook:
342
- try:
343
- res, err, code = wait_hook.get(timeout=WAIT_HOOK__timeout)
344
- except queue.Empty:
345
- # The parallel verifier never delivered a verdict (its single serial thread
346
- # wedged, e.g. on a connection killed by a DB reboot). Fail closed and let
347
- # the worker unwind - without this bound the worker blocks forever holding
348
- # an open transaction, and enough of them starve the pool into nginx 504s.
349
- raise Exception(ERR__verification_timed_out)
350
- if not res:
351
- if code == 500:
352
- raise Exception(err)
353
- raise UserUnauthorized()
354
-
355
- def execute_main_query():
356
- if parameters is None or len(parameters.keys()) == 0:
357
- cursor.execute(query, prepare=do_prepare)
358
- else:
359
- cursor.execute(_escape_unescaped_percent(query), parameters, prepare=do_prepare)
416
+ if wait_hook:
417
+ try:
418
+ verdict = wait_hook.get(timeout=WAIT_HOOK__timeout)
419
+ except queue.Empty:
420
+ # The parallel verifier never delivered a verdict (its single serial thread
421
+ # wedged, e.g. on a connection killed by a DB reboot). Fail closed and let
422
+ # the worker unwind - without this bound the worker blocks forever holding
423
+ # an open transaction, and enough of them starve the pool into nginx 504s.
424
+ raise Exception(ERR__verification_timed_out)
425
+ # The verdict belongs to the request, not to this attempt at it: left on the hook, a
426
+ # request re-run on a fresh connection after losing its first one reads it again
427
+ # instead of waiting out WAIT_HOOK__timeout for a second verdict that never comes
428
+ wait_hook.put(verdict)
429
+ res, err, code = verdict
430
+ if not res:
431
+ if code == 500:
432
+ raise Exception(err)
433
+ raise UserUnauthorized()
434
+
435
+ def execute_main_query():
436
+ if parameters is None or len(parameters.keys()) == 0:
437
+ cursor.execute(query, prepare=do_prepare)
438
+ else:
439
+ cursor.execute(_escape_unescaped_percent(query), parameters, prepare=do_prepare)
360
440
 
361
- # Claimed only now, after the wait_hook: if verification rejects the request,
362
- # the statements remain pending and the putback thread discards them unsent
363
- pending_auth = conn.jaaql_take_pending_auth() if isinstance(conn, JaaqlPGConnection) else None
441
+ # Claimed only now, after the wait_hook: if verification rejects the request,
442
+ # the statements remain pending and the putback thread discards them unsent
443
+ pending_auth = conn.jaaql_take_pending_auth() if isinstance(conn, JaaqlPGConnection) else None
364
444
 
365
- if pending_auth:
366
- auth_cursor = conn.jaaql_raw_cursor()
367
- try:
368
- # Pipeline mode is unsafe for two kinds of statement, so restrict it to
369
- # single-command queries on transactional (non-autocommit) connections;
370
- # everything else falls back to sequential execution:
371
- # - it forces the extended protocol on every execute, which rejects a
372
- # multi-command string ("cannot insert multiple commands into a
373
- # prepared statement"); the sequential fallback runs the main query
374
- # under the simple protocol that permits several commands.
375
- # - it wraps statements in an implicit transaction, which statements
376
- # that cannot run in a transaction block (CREATE DATABASE, VACUUM,
377
- # CREATE INDEX CONCURRENTLY, ...) reject. JAAQL runs those with
378
- # autocommit=True, so skipping the pipeline there keeps them standalone.
379
- if PIPELINE_SUPPORTED and _statement_is_preparable(query) and not conn.autocommit:
380
- with conn.pipeline():
381
- for statement in pending_auth:
382
- auth_cursor.execute(statement)
383
- execute_main_query()
384
- else:
445
+ if pending_auth:
446
+ auth_cursor = conn.jaaql_raw_cursor()
447
+ try:
448
+ # Pipeline mode is unsafe for two kinds of statement, so restrict it to
449
+ # single-command queries on transactional (non-autocommit) connections;
450
+ # everything else falls back to sequential execution:
451
+ # - it forces the extended protocol on every execute, which rejects a
452
+ # multi-command string ("cannot insert multiple commands into a
453
+ # prepared statement"); the sequential fallback runs the main query
454
+ # under the simple protocol that permits several commands.
455
+ # - it wraps statements in an implicit transaction, which statements
456
+ # that cannot run in a transaction block (CREATE DATABASE, VACUUM,
457
+ # CREATE INDEX CONCURRENTLY, ...) reject. JAAQL runs those with
458
+ # autocommit=True, so skipping the pipeline there keeps them standalone.
459
+ if PIPELINE_SUPPORTED and _statement_is_preparable(query) and not conn.autocommit:
460
+ with conn.pipeline():
385
461
  for statement in pending_auth:
386
- auth_cursor.execute(statement)
462
+ _execute_pending_statement(auth_cursor, statement)
387
463
  execute_main_query()
388
- except (InternalError, UndefinedFunction, InvalidParameterValue) as ex:
389
- translated = self._translate_session_auth_error(ex)
390
- if translated is not None:
391
- raise translated
392
- if isinstance(ex, InternalError):
393
- traceback.print_exc()
394
- raise ex
395
- finally:
396
- try:
397
- auth_cursor.close()
398
- except Exception:
399
- pass
400
- else:
401
- execute_main_query()
402
-
403
- if cursor.description is None:
404
- return [], [], []
405
- else:
406
- if capture_provenance is not None:
407
- capture_provenance.clear()
408
- pgresult = cursor.pgresult
409
- capture_provenance.extend(
410
- (pgresult.ftype(idx), pgresult.ftable(idx), pgresult.ftablecol(idx))
411
- for idx in range(pgresult.nfields))
412
- return [desc[0] for desc in cursor.description], [desc.type_code for desc in cursor.description], cursor.fetchall()
413
- except OperationalError as ex:
414
- if ex.sqlstate is None or ex.sqlstate.startswith("08") or ex.sqlstate.startswith("57"):
415
- if isinstance(conn, JaaqlPGConnection):
416
- # This putconn bypasses the reset queue, so make sure no unsent
417
- # authorization statements ride back into the pool where check() or a
418
- # later checkout could flush them as the wrong user
419
- conn.jaaql_take_pending_auth()
420
- DBPGInterface.HOST_POOLS[self.username][self.db_name].putconn(conn)
421
- DBPGInterface.HOST_POOLS[self.username][self.db_name].check()
422
- conn = self.get_conn()
423
- err = ex
464
+ else:
465
+ for statement in pending_auth:
466
+ _execute_pending_statement(auth_cursor, statement)
467
+ execute_main_query()
468
+ except (InternalError, UndefinedFunction, InvalidParameterValue) as ex:
469
+ translated = self._translate_session_auth_error(ex)
470
+ if translated is not None:
471
+ raise translated
472
+ if isinstance(ex, InternalError):
473
+ traceback.print_exc()
474
+ raise ex
475
+ finally:
476
+ try:
477
+ auth_cursor.close()
478
+ except Exception:
479
+ pass
424
480
  else:
425
- raise ex
426
- except Exception as ex:
427
- if self.output_query_exceptions:
428
- traceback.print_exc()
429
- raise ex
481
+ execute_main_query()
430
482
 
431
- if err:
432
- raise err
483
+ if cursor.description is None:
484
+ return [], [], []
485
+ else:
486
+ if capture_provenance is not None:
487
+ capture_provenance.clear()
488
+ pgresult = cursor.pgresult
489
+ capture_provenance.extend(
490
+ (pgresult.ftype(idx), pgresult.ftable(idx), pgresult.ftablecol(idx))
491
+ for idx in range(pgresult.nfields))
492
+ return [desc[0] for desc in cursor.description], [desc.type_code for desc in cursor.description], cursor.fetchall()
493
+ except Exception as ex:
494
+ if isinstance(ex, OperationalError) and conn.closed:
495
+ # The connection is gone and the transaction on it with it. The query is not retried here on
496
+ # another connection: the caller holds this one and would go on to commit and return it, so
497
+ # the request is re-run as a whole by the owner of its connection (InterpretJAAQL.transform,
498
+ # ConnectionLostError). A database restart leaves every idle pooled connection dead, so have
499
+ # the pool replace those before that re-run checks one out
500
+ try:
501
+ DBPGInterface.HOST_POOLS[self.username][self.db_name].check()
502
+ except Exception:
503
+ pass
504
+ if self.output_query_exceptions:
505
+ traceback.print_exc()
506
+ raise ex
433
507
 
434
508
  def commit(self, conn):
435
509
  conn.commit()
@@ -437,16 +511,28 @@ class DBPGInterface(DBInterface):
437
511
  def rollback(self, conn):
438
512
  conn.rollback()
439
513
 
440
- def is_connection_error(self, ex) -> bool:
441
- # psycopg raises OperationalError when the backend/connection went away (e.g. terminated by
442
- # \wipe dbms). The same class the execute retry loop keys off; a commit that fails this way
443
- # persisted nothing, so the operation is safe to retry on a fresh connection.
444
- return isinstance(ex, OperationalError)
514
+ def is_connection_closed(self, conn) -> bool:
515
+ # True once psycopg has seen the connection fail (backend terminated by \wipe dbms or a restart, network
516
+ # loss) or it was closed. Whatever transaction was open on it can no longer commit
517
+ return conn.closed
518
+
519
+ def statement_may_end_transaction(self, query: str) -> bool:
520
+ return _statement_may_end_transaction(query)
521
+
522
+ def has_ended_transaction(self, conn) -> bool:
523
+ return not conn.autocommit and conn.info.transaction_status == TransactionStatus.IDLE
445
524
 
446
- def translate_commit_error(self, commit_err):
525
+ def translate_commit_error(self, conn, commit_err, error_set: str = None):
447
526
  if not isinstance(commit_err, psycopg.Error):
448
527
  return None
449
- return handled_procedure_error_from_raise(commit_err)
528
+ if conn.closed:
529
+ # Lost while the COMMIT was in flight: the server may have committed before the connection went (a crash
530
+ # after the commit record was flushed, a network loss before the reply). The outcome is unknown, so this
531
+ # is reported as it stands and never re-run, which could apply the request twice
532
+ return DatabaseOperationalError(message=ERR__commit_outcome_unknown + str(commit_err), descriptor=database_error_descriptor(commit_err))
533
+ # The server refused the COMMIT (a deferred constraint, a serialization failure, ...), so the transaction
534
+ # was rolled back: the error maps exactly as it does when a statement raises it
535
+ return handled_error_from_database_error(commit_err, error_set)
450
536
 
451
537
  def handle_db_error(self, err, echo):
452
538
  if isinstance(err, ProgrammingError) and hasattr(err, 'pgresult'):