Skip to content

Instantly share code, notes, and snippets.

@kleontev
kleontev / script_statement_level_rollback.sql
Created February 1, 2022 17:35
PLSQL procedure statement level rollback
spool spool_statement_level_rollback.log
cl scr
set echo on
set time off
drop table slr_test purge;
create table slr_test(id int primary key);
@kleontev
kleontev / script_non_overlapping_range_constraint.sql
Last active April 25, 2022 04:05
enforce non-overlapping date range with unique constraint
-- задачка из чата https://t.me/oracle_dbd
-- https://t.me/c/1195394250/1821
spool spool_non_overlapping_range_constraint.log
drop table client_tariff purge;
create table client_tariff(
client_id number(38) not null,
effective_date date not null,
primary key(client_id, effective_date)
@kleontev
kleontev / script_snapshot_too_old_demo.sql
Created February 15, 2022 17:22
ORA-01555 snapshot_too_old quick demo
spool spool_snapshot_too_old_demo.log
set echo on time off timi on serverout on
drop table snapshot_too_old_demo purge;
create table snapshot_too_old_demo as select * from dual;
create undo tablespace tiny_undotbs2
datafile '/u01/app/oracle/oradata/ORCLCDB/orcl/tiny_undotbs2.dbf'
@kleontev
kleontev / ora_error.py
Created February 22, 2022 07:11
helper functions for cx_Oracle exceptions
import cx_Oracle
from functools import wraps
from typing import Optional, Any
def is_oracle_exception(e: Exception) -> bool:
return isinstance(e, cx_Oracle.Error)
def get_ora_error_attr(e: Exception, attr: str) -> Any:
@kleontev
kleontev / nested_dict.py
Created February 23, 2022 04:50
helper methods to access/modify nested dictionary structure in python
from typing import (
Any,
Dict,
Optional,
Union,
Tuple,
Hashable,
List
)
@kleontev
kleontev / as_completed.py
Created February 23, 2022 04:56
a version of concurrent.futures.as_completed that doesn't consume the whole iterable of futures before producing the first one.
from concurrent.futures import Future, wait, FIRST_COMPLETED
from typing import Iterable
def as_completed(fs: Iterable[Future], buffer: int = 5):
"""
Self-throttling version of concurrent.futures.as_completed.
Contrary to the standard library version, does not consume
Future iterable immediately.
"""
@kleontev
kleontev / use_display_cursor_to_identify_broken_where_clauses.sql
Created April 22, 2022 05:12
a simple approach to identify a broken predicate that makes a query return 0 rows
/*
Задача (из чата https://t.me/oracle_dbd):
Есть сложный запрос с большим количеством соединений и условий фильтрации.
При определенных условиях запрос возвращает существенно меньшее количество строк,
чем мы ожидаем, либо не возвращает вообще ни одной.
Нам нужно понять, какое из условий в запросе приводит к этому.
*/
set echo on lines 200 pages 0
@kleontev
kleontev / script_arraysize_lio_impact.sql
Last active April 22, 2022 06:01
ARRAYSIZE has a dramatic impact on LIO figures. reproducible on a simple "select * from t" as well
spool spool_arraysize_lio_impact.log
create table t1 (id, fill) as select rownum, lpad('x', 2000, 'y') from dual connect by rownum <= 1e5;
create table t2 as select * from t1;
exec dbms_stats.gather_table_stats(user, 't1');
exec dbms_stats.gather_table_stats(user, 't2');
@kleontev
kleontev / single_session_deadlock.log
Created May 18, 2022 06:17
deadlock demo: only one session is required (since it can have multiple transactions :)
SQL>
SQL> create table t(id, value) as select 1, 1 from dual;
Table created.
SQL>
SQL> update t set value = value + 1 where id = 1;
1 row updated.
@kleontev
kleontev / select_for_update_deadlock_one_session.log
Created May 18, 2022 07:16
deadlock, select for update - one session
SQL>
SQL> create table t (id, value) as select 1, 1 from dual
2 /
Table created.
Elapsed: 00:00:00.03
SQL>
SQL> var cur_1 refcursor
SQL> var cur_2 refcursor