Repeated column values are no longer compressed for a cursor once an execution ended with an empty fetch

Aperta
#637 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
48/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python
Ambito
backend, databases

Direzione di ricerca

Start by reviewing the runnable reproducer in the issue and trace the Thin mode fetch path handling message type 21 and repeated column values; the payload does not name source files or tests. Reproduce cases A and B against Oracle Database, then compare the cursor behavior after the empty fetch. Done means the repeated values remain compressed on later executions, with a regression test covering that sequence.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Client Library or Database
  1. What versions are you using?
  • Oracle Database 23ai Free 23.26.3.0.0 and Oracle Database 19c Enterprise Edition 19.3.0.0.0 (container
    images): both behave the same way
  • python-oracledb 26.1.0, Thin mode
  • Python 3.14.10, Linux x86_64, sys.maxsize > 2**32: True
  1. Is it an error or a hang or a crash?

Neither: it's a performance problem. Rows come back up to 200 times larger than they need to.

  1. What error(s) or behavior you are seeing?

When a column value is equal to the one in the previous row, the database leaves it out, and the row's bit
vector (message type 21) says which values were left out. For a given cursor, this stops once one of its
executions has ended with a fetch that returned no rows. That happens whenever the number of rows is exactly
prefetchrows plus a multiple of arraysize: the last full batch makes the client ask for more, and the
answer is only the end of the data.

From then on, every execution of that cursor gets whole rows, whatever its bind values, prefetchrows and
arraysize are (cases A, E and F below). The execution with the empty fetch is still compressed; the ones
after it aren't. Compression only comes back when the statement is parsed again: other SQL text (case C), or
the statement leaving the statement cache. An execution that finds no rows at all doesn't cause it (case D).

With the statement cache, this means that a query whose row count once happened to be an exact multiple of the
fetch size receives uncompressed rows for the rest of the session. In the script below, that's 20.18 MB
instead of 0.10 MB for each later execution of the same 10,000 rows.

Output with 23ai (19c gives the same numbers, except 0.10 MB instead of 0.11 MB for the first executions):

python-oracledb 26.1.0, database 23.26.3.0.0
A: the first execution ends with an empty fetch
  execution 1: 'select * from dup_repro'  prefetchrows=1000 arraysize=1000: 10000 rows, 0.11 MB received
  execution 2: 'select * from dup_repro'  prefetchrows=1000 arraysize=1000: 10000 rows, 20.18 MB received
  execution 3: 'select * from dup_repro'  prefetchrows=1000 arraysize=1000: 10000 rows, 20.18 MB received
B: never an empty fetch
  execution 1: 'select * from dup_repro'  prefetchrows=999 arraysize=1000: 10000 rows, 0.11 MB received
  execution 2: 'select * from dup_repro'  prefetchrows=999 arraysize=1000: 10000 rows, 0.10 MB received
  execution 3: 'select * from dup_repro'  prefetchrows=999 arraysize=1000: 10000 rows, 0.10 MB received
C: as A, then other SQL text
  execution 1: 'select * from dup_repro'  prefetchrows=1000 arraysize=1000: 10000 rows, 0.11 MB received
  execution 2: 'select * from dup_repro'  prefetchrows=1000 arraysize=1000: 10000 rows, 20.18 MB received
  execution 3: 'select * from dup_repro where 1 = 1'  prefetchrows=1000 arraysize=1000: 10000 rows, 0.11 MB received
D: the first execution finds no rows
  execution 1: 'select * from dup_repro where id <= :n' {'n': 0} prefetchrows=999 arraysize=1000: 0 rows, 0.00 MB received
  execution 2: 'select * from dup_repro where id <= :n' {'n': 10000} prefetchrows=999 arraysize=1000: 10000 rows, 0.10 MB received
  execution 3: 'select * from dup_repro where id <= :n' {'n': 10000} prefetchrows=999 arraysize=1000: 10000 rows, 0.10 MB received
E: an empty fetch with n = 5000, then other bind values
  execution 1: 'select * from dup_repro where id <= :n' {'n': 5000} prefetchrows=1000 arraysize=1000: 5000 rows, 0.05 MB received
  execution 2: 'select * from dup_repro where id <= :n' {'n': 10000} prefetchrows=999 arraysize=1000: 10000 rows, 20.18 MB received
  execution 3: 'select * from dup_repro where id <= :n' {'n': 7777} prefetchrows=999 arraysize=1000: 7777 rows, 15.69 MB received
F: other bind values, then an empty fetch
  execution 1: 'select * from dup_repro where id <= :n' {'n': 10000} prefetchrows=999 arraysize=1000: 10000 rows, 0.11 MB received
  execution 2: 'select * from dup_repro where id <= :n' {'n': 7777} prefetchrows=999 arraysize=1000: 7777 rows, 0.08 MB received
  execution 3: 'select * from dup_repro where id <= :n' {'n': 3000} prefetchrows=1000 arraysize=1000: 3000 rows, 0.03 MB received
  execution 4: 'select * from dup_repro where id <= :n' {'n': 10000} prefetchrows=999 arraysize=1000: 10000 rows, 20.18 MB received
  execution 5: 'select * from dup_repro where id <= :n' {'n': 7777} prefetchrows=999 arraysize=1000: 7777 rows, 15.69 MB received

Expected: the executions after an empty fetch are compressed like those in case B (about 0.10 MB for
10,000 rows here).

The byte counts match what strace shows on the socket. It isn't specific to Thin mode: a C program using
OCI 23.26 shows the same pattern. Its query needs a where clause, because for a select * of whole table
rows OCI asks for a row format that is never compressed. So it seems to be the database's doing.

Is this expected? If it isn't, could it be passed on to the database team?

  1. Does your application call init_oracle_client()?

No (Thin mode).

  1. Include a runnable Python script that shows the problem.

Usage: python repro_compression.py user password host:port/service. It needs no privileges beyond creating
a table: it creates dup_repro (10,000 rows, every column value repeated) and drops it at the end. Bytes are
counted on the client socket.

"""
Repeated column values in fetched rows stop being compressed for a cursor once one of its executions ended
with an empty fetch. Self-contained: python-oracledb thin mode, bytes counted on the client socket.

Usage: python repro_compression.py user password host:port/service
Creates the table dup_repro (10,000 rows, every column value repeated) and drops it at the end.
"""

import socket
import sys

import oracledb

received = 0
_recv = socket.socket.recv


def counting_recv(self, *args, **kwargs):
    global received
    data = _recv(self, *args, **kwargs)
    received += len(data)
    return data


socket.socket.recv = counting_recv

USER, PASSWORD, DSN = sys.argv[1:4]
SQL = "select * from dup_repro"


def run(case, steps):
    """Runs each (sql, binds, prefetchrows, arraysize) step on one cursor of a new connection"""
    global received
    print(case)
    with oracledb.connect(user=USER, password=PASSWORD, dsn=DSN) as conn:
        cursor = conn.cursor()
        for n, (sql, binds, prefetch, fetch) in enumerate(steps, 1):
            cursor.prefetchrows = prefetch
            cursor.arraysize = fetch
            received = 0
            cursor.execute(sql, binds)
            rows = cursor.fetchall()
            print(f"  execution {n}: {sql!r} {binds or ''} prefetchrows={prefetch} arraysize={fetch}: "
                  f"{len(rows)} rows, {received / 1e6:.2f} MB received")


with oracledb.connect(user=USER, password=PASSWORD, dsn=DSN) as conn:
    print(f"python-oracledb {oracledb.__version__}, database {conn.version}")
    cursor = conn.cursor()
    cursor.execute("begin execute immediate 'drop table dup_repro purge'; exception when others then null; end;")
    columns = ", ".join(f"rpad('x', 200, 'x') c{i}" for i in range(1, 11))
    cursor.execute(f"create table dup_repro as select level id, {columns} from dual connect by level <= 10000")

# 1,000 rows with the execute, then 9 fetches of 1,000 and an empty one: executions 2 and 3 aren't compressed
run("A: the first execution ends with an empty fetch", [(SQL, None, 1000, 1000)] * 3)
# 999 rows with the execute, then 9 fetches of 1,000 and one of 1: all compressed
run("B: never an empty fetch", [(SQL, None, 999, 1000)] * 3)
# As A, then other SQL text in the same session: another cursor, compressed again
run("C: as A, then other SQL text", [(SQL, None, 1000, 1000)] * 2 + [(SQL + " where 1 = 1", None, 1000, 1000)])
# An execution that finds no rows at all, then executions that never end with an empty fetch
FILTERED = "select * from dup_repro where id <= :n"
run("D: the first execution finds no rows",
    [(FILTERED, {"n": 0}, 999, 1000)] + [(FILTERED, {"n": 10000}, 999, 1000)] * 2)
# Other bind values after an empty fetch, and fetch sizes that never end with an empty fetch
run("E: an empty fetch with n = 5000, then other bind values",
    [(FILTERED, {"n": 5000}, 1000, 1000), (FILTERED, {"n": 10000}, 999, 1000), (FILTERED, {"n": 7777}, 999, 1000)])
# The same with control executions first: compressed with both bind values until the empty fetch
run("F: other bind values, then an empty fetch",
    [(FILTERED, {"n": 10000}, 999, 1000), (FILTERED, {"n": 7777}, 999, 1000), (FILTERED, {"n": 3000}, 1000, 1000),
     (FILTERED, {"n": 10000}, 999, 1000), (FILTERED, {"n": 7777}, 999, 1000)])

with oracledb.connect(user=USER, password=PASSWORD, dsn=DSN) as conn:
    conn.cursor().execute("drop table dup_repro purge")
Lingua principale
Python
Stelle
458
Fork
123
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di oracle/python-oracledb

Tutte le issue di oracle/python-oracledb

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.