Document SQLAlchemy scaling evidence and archive completed changes
This commit is contained in:
@@ -0,0 +1,168 @@
|
||||
date: 2026-11-05T21:55:44.776362+00:00 commit: 07965a2fb67b57f6031fed70a9a3ae963571269f
|
||||
$ docker compose exec -T api python <<'PY'
|
||||
import asyncio
|
||||
import sqlalchemy
|
||||
from sqlalchemy import make_url, select, text, tuple_
|
||||
from sqlalchemy.ext.asyncio import create_async_engine
|
||||
from messenger.messaging.infrastructure.postgres import ChatRow, MessageRow
|
||||
from messenger.shared.config import PlatformSettings
|
||||
|
||||
async def main():
|
||||
engine = create_async_engine(make_url(PlatformSettings().database_url.get_secret_value()).set(drivername="postgresql+asyncpg"), hide_parameters=True)
|
||||
try:
|
||||
async with engine.connect() as connection:
|
||||
print("SQLAlchemy", sqlalchemy.__version__)
|
||||
print(await connection.scalar(select(sqlalchemy.func.version())))
|
||||
chat = await connection.scalar(select(ChatRow.id).where(ChatRow.title.startswith("k6 ")).order_by(ChatRow.created_at.desc()).limit(1))
|
||||
if chat is None:
|
||||
raise RuntimeError("run track1 first")
|
||||
first = select(MessageRow.id, MessageRow.chat_id, MessageRow.sender_id, MessageRow.text, MessageRow.created_at).where(MessageRow.chat_id == chat).order_by(MessageRow.created_at, MessageRow.id).limit(51)
|
||||
cursor = (await connection.execute(select(MessageRow.created_at, MessageRow.id).where(MessageRow.chat_id == chat).order_by(MessageRow.created_at, MessageRow.id).offset(1000).limit(1))).first()
|
||||
queries = [("first page", first)]
|
||||
if cursor is not None:
|
||||
queries += [("keyset after 1001", first.where(tuple_(MessageRow.created_at, MessageRow.id) > tuple_(*cursor))), ("offset 1001 (comparison only)", first.offset(1001))]
|
||||
for label, statement in queries:
|
||||
compiled = str(statement.compile(dialect=engine.dialect, compile_kwargs={"literal_binds": True}))
|
||||
print(label, compiled, sep="\n")
|
||||
for row in await connection.execute(text("EXPLAIN (ANALYZE, BUFFERS) " + compiled)):
|
||||
print(row[0])
|
||||
finally:
|
||||
await engine.dispose()
|
||||
|
||||
asyncio.run(main())
|
||||
PY
|
||||
|
||||
SQLAlchemy 2.0.45
|
||||
PostgreSQL 17.11 on aarch64-unknown-linux-musl, compiled by gcc (Alpine 15.2.0) 15.2.0, 64-bit
|
||||
first page
|
||||
SELECT messages.id, messages.chat_id, messages.sender_id, messages.text, messages.created_at
|
||||
FROM messages
|
||||
WHERE messages.chat_id = '01a1adb3-a03e-7172-85b9-f7c54b7d443c' ORDER BY messages.created_at, messages.id
|
||||
LIMIT 51
|
||||
Limit (cost=0.56..91.85 rows=51 width=81) (actual time=0.005..0.016 rows=51 loops=1)
|
||||
Buffers: shared hit=56
|
||||
-> Index Scan using messages_history_idx on messages (cost=0.56..11104.46 rows=6203 width=81) (actual time=0.005..0.014 rows=51 loops=1)
|
||||
Index Cond: (chat_id = '01a1adb3-a03e-7172-85b9-f7c54b7d443c'::uuid)
|
||||
Buffers: shared hit=56
|
||||
Planning:
|
||||
Buffers: shared hit=6 read=2
|
||||
I/O Timings: shared read=0.005
|
||||
Planning Time: 0.058 ms
|
||||
Execution Time: 0.022 ms
|
||||
Traceback (most recent call last):
|
||||
File "<stdin>", line 30, in <module>
|
||||
File "/usr/local/lib/python3.14/asyncio/runners.py", line 205, in run
|
||||
return runner.run(main)
|
||||
~~~~~~~~~~^^^^^^
|
||||
File "/usr/local/lib/python3.14/asyncio/runners.py", line 128, in run
|
||||
return self._loop.run_until_complete(task)
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
|
||||
File "/usr/local/lib/python3.14/asyncio/base_events.py", line 720, in run_until_complete
|
||||
return future.result()
|
||||
~~~~~~~~~~~~~^^
|
||||
File "<stdin>", line 23, in main
|
||||
File "/opt/venv/lib/python3.14/site-packages/sqlalchemy/sql/elements.py", line 312, in compile
|
||||
return self._compiler(dialect, **kw)
|
||||
~~~~~~~~~~~~~~^^^^^^^^^^^^^^^
|
||||
File "/opt/venv/lib/python3.14/site-packages/sqlalchemy/sql/elements.py", line 324, in _compiler
|
||||
return dialect.statement_compiler(dialect, self, **kw)
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^
|
||||
File "/opt/venv/lib/python3.14/site-packages/sqlalchemy/sql/compiler.py", line 1447, in __init__
|
||||
Compiled.__init__(self, dialect, statement, **kwargs)
|
||||
~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
File "/opt/venv/lib/python3.14/site-packages/sqlalchemy/sql/compiler.py", line 887, in __init__
|
||||
self.string = self.process(self.statement, **compile_kwargs)
|
||||
~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
File "/opt/venv/lib/python3.14/site-packages/sqlalchemy/sql/compiler.py", line 933, in process
|
||||
return obj._compiler_dispatch(self, **kwargs)
|
||||
~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^
|
||||
File "/opt/venv/lib/python3.14/site-packages/sqlalchemy/sql/visitors.py", line 138, in _compiler_dispatch
|
||||
return meth(self, **kw) # type: ignore # noqa: E501
|
||||
File "/opt/venv/lib/python3.14/site-packages/sqlalchemy/sql/compiler.py", line 4927, in visit_select
|
||||
text = self._compose_select_body(
|
||||
text,
|
||||
...<6 lines>...
|
||||
kwargs,
|
||||
)
|
||||
File "/opt/venv/lib/python3.14/site-packages/sqlalchemy/sql/compiler.py", line 5089, in _compose_select_body
|
||||
t = self._generate_delimited_and_list(
|
||||
select._where_criteria, from_linter=from_linter, **kwargs
|
||||
)
|
||||
File "/opt/venv/lib/python3.14/site-packages/sqlalchemy/sql/compiler.py", line 2785, in _generate_delimited_and_list
|
||||
return separator.join(
|
||||
~~~~~~~~~~~~~~^
|
||||
s
|
||||
^
|
||||
for s in (c._compiler_dispatch(self, **kw) for c in clauses)
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
if s
|
||||
^^^^
|
||||
)
|
||||
^
|
||||
File "/opt/venv/lib/python3.14/site-packages/sqlalchemy/sql/compiler.py", line 2787, in <genexpr>
|
||||
for s in (c._compiler_dispatch(self, **kw) for c in clauses)
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
File "/opt/venv/lib/python3.14/site-packages/sqlalchemy/sql/compiler.py", line 2787, in <genexpr>
|
||||
for s in (c._compiler_dispatch(self, **kw) for c in clauses)
|
||||
~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^
|
||||
File "/opt/venv/lib/python3.14/site-packages/sqlalchemy/sql/visitors.py", line 138, in _compiler_dispatch
|
||||
return meth(self, **kw) # type: ignore # noqa: E501
|
||||
File "/opt/venv/lib/python3.14/site-packages/sqlalchemy/sql/compiler.py", line 3427, in visit_binary
|
||||
return self._generate_generic_binary(
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
|
||||
binary,
|
||||
^^^^^^^
|
||||
...<3 lines>...
|
||||
**kw,
|
||||
^^^^^
|
||||
)
|
||||
^
|
||||
File "/opt/venv/lib/python3.14/site-packages/sqlalchemy/sql/compiler.py", line 3486, in _generate_generic_binary
|
||||
+ binary.right._compiler_dispatch(
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
|
||||
self, eager_grouping=eager_grouping, **kw
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
)
|
||||
^
|
||||
File "/opt/venv/lib/python3.14/site-packages/sqlalchemy/sql/visitors.py", line 138, in _compiler_dispatch
|
||||
return meth(self, **kw) # type: ignore # noqa: E501
|
||||
File "/opt/venv/lib/python3.14/site-packages/sqlalchemy/sql/compiler.py", line 2792, in visit_tuple
|
||||
return "(%s)" % self.visit_clauselist(clauselist, **kw)
|
||||
~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^
|
||||
File "/opt/venv/lib/python3.14/site-packages/sqlalchemy/sql/compiler.py", line 2801, in visit_clauselist
|
||||
return self._generate_delimited_list(clauselist.clauses, sep, **kw)
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
File "/opt/venv/lib/python3.14/site-packages/sqlalchemy/sql/compiler.py", line 2768, in _generate_delimited_list
|
||||
return separator.join(
|
||||
~~~~~~~~~~~~~~^
|
||||
s
|
||||
^
|
||||
for s in (c._compiler_dispatch(self, **kw) for c in elements)
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
if s
|
||||
^^^^
|
||||
)
|
||||
^
|
||||
File "/opt/venv/lib/python3.14/site-packages/sqlalchemy/sql/compiler.py", line 2770, in <genexpr>
|
||||
for s in (c._compiler_dispatch(self, **kw) for c in elements)
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
File "/opt/venv/lib/python3.14/site-packages/sqlalchemy/sql/compiler.py", line 2770, in <genexpr>
|
||||
for s in (c._compiler_dispatch(self, **kw) for c in elements)
|
||||
~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^
|
||||
File "/opt/venv/lib/python3.14/site-packages/sqlalchemy/sql/visitors.py", line 138, in _compiler_dispatch
|
||||
return meth(self, **kw) # type: ignore # noqa: E501
|
||||
File "/opt/venv/lib/python3.14/site-packages/sqlalchemy/sql/compiler.py", line 3729, in visit_bindparam
|
||||
ret = self.render_literal_bindparam(
|
||||
bindparam, within_columns_clause=True, **kwargs
|
||||
)
|
||||
File "/opt/venv/lib/python3.14/site-packages/sqlalchemy/sql/compiler.py", line 3868, in render_literal_bindparam
|
||||
return self.render_literal_value(value, bindparam.type)
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^
|
||||
File "/opt/venv/lib/python3.14/site-packages/sqlalchemy/dialects/postgresql/base.py", line 2085, in render_literal_value
|
||||
value = super().render_literal_value(value, type_)
|
||||
File "/opt/venv/lib/python3.14/site-packages/sqlalchemy/sql/compiler.py", line 3903, in render_literal_value
|
||||
raise exc.CompileError(
|
||||
...<3 lines>...
|
||||
)
|
||||
sqlalchemy.exc.CompileError: No literal value renderer is available for literal value "UUID('01a1adb3-b8bf-7142-8926-0a5f9c9125d5')" with datatype NULL
|
||||
exit: 1
|
||||
Reference in New Issue
Block a user