Skip to content

Instantly share code, notes, and snippets.

View dincosman's full-sized avatar

Osman DINC dincosman

View GitHub Profile
@dincosman
dincosman / stress-cpu-Oracle.sql
Last active December 17, 2024 16:33
CPU-intensive script to test CPU performance in Oracle database
SET SERVEROUTPUT ON;
DECLARE
job_name VARCHAR2(30);
cpu_count NUMBER;
loop_iterations NUMBER := 500000000;
start_snap_id NUMBER;
end_snap_id NUMBER;
job_running NUMBER;
@dincosman
dincosman / get_row_counts_postgres.sql
Last active December 5, 2024 20:58
Get Row Counts for Each Table in PostgreSQL Database
CREATE FUNCTION pg_temp.get_rows(p_schemaname text, p_tablename text)
RETURNS bigint AS $$
DECLARE
result_rowcount bigint;
BEGIN
EXECUTE format('SELECT COUNT(*) FROM "%I"."%I"', p_schemaname, p_tablename)
INTO result_rowcount;
RETURN result_rowcount;
END;
$$ LANGUAGE plpgsql;
@dincosman
dincosman / get_row_count_xml.sql
Last active December 18, 2024 12:54
Get Row Counts for Each Table in Oracle Database by using XMLQUERY
SQL> set autotrace traceonly statistics
SQL>select
owner,
table_name,
XMLCAST(
XMLQUERY(
'/ROWSET/ROW/NUMROWS' PASSING DBMS_XMLGEN.GETXMLTYPE(
'select count(*) NUMROWS
from ' || '"' || OWNER || '". "' || TABLE_NAME || '"'
@dincosman
dincosman / get_row_counts.sql
Last active December 18, 2024 12:54
Get Row Counts for Each Table in Oracle Database
SQL> set autotrace traceonly statistics
SQL> WITH function get_rows(
p_owner in varchar2, p_tablename in varchar2
) return number as result_rowcount number default NULL;
begin execute immediate 'select count(*)
from ' || '"' || p_owner || '"."' || p_tablename || '"' INTO result_rowcount;
return result_rowcount;
end;
select
@dincosman
dincosman / email_listener.py
Created September 30, 2024 13:21
a Python script that listens for notifications on the new_email channel and sends emails
import psycopg2
import select
from datetime import datetime
import subprocess
import logging
import time
#Configure logging
logging.basicConfig(
filename='/var/log/postgresql_email_listener/postgresql_email_listener.log',
@dincosman
dincosman / lock_waiting_sessions.sql
Last active August 16, 2024 10:31
Detected sessions waiting for lock and the session holding the lock.
SQL> SELECT gvs.inst_id,DECODE (request, 0, 'Holder: ', 'waiter:') || gvl.sid sess,
status,
id1,
id2,
lmode,
request,
gvl.TYPE
FROM gv$lock gvl, gv$session gvs
WHERE (id1, id2, gvl.TYPE) IN (SELECT id1, id2, TYPE
FROM gv$lock
@dincosman
dincosman / most_invalidated_sql.sql
Created August 12, 2024 20:31
Most Invalidated Sql statements
SQL> SELECT sql_id,
sum(invalidations)
FROM gv$sql
WHERE invalidations > 0
GROUP BY sql_id
ORDER BY sum(invalidations) desc
FETCH first 20 ROWS only ;
@dincosman
dincosman / database_audit_spooler.sh
Created July 28, 2024 13:59
Database Audit Records spooler shell script
#!/bin/bash
# Set Oracle environment variables
source /home/oracle/.bashrc
export ORAENV_ASK=NO
export ORACLE_SID=bltdb1
. oraenv bltdb1
#export ORACLE_PDB_SID=testpdb
# Execute SQL query and spool output to file
@dincosman
dincosman / audit_record_spooler.sql
Last active December 18, 2024 13:19
Using JSON to share audit records with SIEM tools
#SQL query to generate JSON output
SPOOL audit.log APPEND
SELECT '{ "oracle_audit": ' ||
json_object(
'AUDIT_TYPE' VALUE AUDIT_TYPE,
'SESSIONID' VALUE SESSIONID,
'OS_USERNAME' VALUE OS_USERNAME,
'USERHOST' VALUE USERHOST,
'TERMINAL' VALUE TERMINAL,
@dincosman
dincosman / compare_oracle_passsword.sql
Last active July 26, 2024 20:13
Compare Oracle hashed password with plain text
SQL> SELECT
username,
guessed_password,
stored_hashed_pwd,
CASE
WHEN stored_hashed_pwd = computed_hashed_pwd THEN 'Y'
ELSE 'N'
END AS result
FROM ( -- This SELECT concatenates the salt with the guessed password and hashes it.
SELECT