Pages

Monday, August 26, 2024

ALTER INDEX REBUILD ONLINE in Partitioned Index

ORA-14086: a partitioned index may not be rebuilt as a whole

SET LINESIZE 140
COL sql_cmd FOR A120
SET PAGESIZE 0
SET HEADING OFF
SET FEEDBACK OFF
SET VERIFY OFF

spool rebuild_ix.sql
SELECT 'ALTER INDEX '||index_owner||'.'||index_name||' REBUILD PARTITION '||PARTITION_NAME||' ONLINE;' AS sql_cmd
  FROM DBA_IND_PARTITIONS
 WHERE index_name = 'MY_INDEX';
spool off

--Same for a user
SELECT 'ALTER INDEX '||index_name||' REBUILD PARTITION '||PARTITION_NAME||' ONLINE;' AS sql_cmd
  FROM USER_IND_PARTITIONS
 WHERE index_name = '
MY_INDEX';

SQL_CMD
------------------------------------------------------------------
ALTER INDEX MY_INDEX REBUILD PARTITION SYS_P392 ONLINE;
ALTER INDEX MY_INDEX REBUILD PARTITION SYS_P393 ONLINE;
ALTER INDEX MY_INDEX REBUILD PARTITION SYS_P394 ONLINE;
ALTER INDEX MY_INDEX REBUILD PARTITION SYS_P395 ONLINE;
ALTER INDEX MY_INDEX REBUILD PARTITION SYS_P396 ONLINE;
ALTER INDEX MY_INDEX REBUILD PARTITION SYS_P397 ONLINE;
ALTER INDEX MY_INDEX REBUILD PARTITION SYS_P398 ONLINE;
ALTER INDEX MY_INDEX REBUILD PARTITION SYS_P399 ONLINE;

Before rebuild:
SEGMENT_NAME                   USED_MB
------------------------------ --------------
MY_INDEX                                 3567

After rebuild
SEGMENT_NAME                   USED_MB
------------------------------ --------------
MY_INDEX                                  2301     

Thursday, August 22, 2024

Rebuild Tables and Indexes

Rebuild Tables and Indexes from crontab

CREATE DBA USER
CREATE USER SH_USER IDENTIFIED BY XXXXXX;
GRANT CONNECT, RESOURCE TO SH_USER;
GRANT DBA TO SH_USER;
ALTER USER SH_USER DEFAULT TABLESPACE IGT_TABLE;

crontab
20 7 * * * /software/oracle/oracle/scripts/space/rebuild_tables_and_indexes.sh

rebuild_tables_and_indexes.sh
#!/bin/bash
. ~/.bash_profile
. /etc/sh/orash/oracle_login.sh igt
ORACLE_SID=igt
ORACLE_HOME=/software/oracle/1910
WORK_DIR=/software/oracle/oracle/scripts/space/rebuild_tables_and_indexes
RUN_DATE=`date +"%Y%m%d"_"%H%M%S"`

cd ${WORK_DIR}
echo "Start Rebuild Indexes at $RUN_DATE" >> rebuild_tables_and_indexes.log
sqlplus SH_USER/XXXXXXXX@orainst @rebuild_tables_and_indexes.sql
sqlplus SH_USER/XXXXXXXX@orainst @log_connection.sql


rebuild_tables_and_indexes.sql
----------------------
--USER_A
----------------------
ALTER TABLE USER_A.GA_W_COUNTERS_HISTORY ENABLE ROW MOVEMENT;
ALTER TABLE USER_A.GA_W_COUNTERS_HISTORY SHRINK SPACE;
ALTER TABLE USER_A.GA_W_COUNTERS_HISTORY DEALLOCATE UNUSED;

DROP INDEX USER_A.GWCH_PROCESSED_STATIC_IDX;
CREATE INDEX USER_A.GWCH_PROCESSED_STATIC_IDX ON USER_A.GA_W_COUNTERS_HISTORY (is_processed, static_id) TABLESPACE IGT_INDEX;

DROP INDEX USER_A.GWCH_TS_LAST_MODIFIED_IDX;
CREATE INDEX USER_A.GWCH_TS_LAST_MODIFIED_IDX ON USER_A.GA_W_COUNTERS_HISTORY (ts_last_modified) TABLESPACE IGT_INDEX;

----------------------
--USER_B
----------------------
ALTER TABLE USER_B.GA_W_COUNTERS_HISTORY ENABLE ROW MOVEMENT;
ALTER TABLE USER_B.GA_W_COUNTERS_HISTORY SHRINK SPACE;
ALTER TABLE USER_B.GA_W_COUNTERS_HISTORY DEALLOCATE UNUSED;

DROP INDEX USER_B.GWCH_PROCESSED_STATIC_IDX;
CREATE INDEX USER_B.GWCH_PROCESSED_STATIC_IDX ON USER_B.GA_W_COUNTERS_HISTORY (is_processed, static_id) TABLESPACE IGT_INDEX;

DROP INDEX USER_B.GWCH_TS_LAST_MODIFIED_IDX;
CREATE INDEX USER_B.GWCH_TS_LAST_MODIFIED_IDX ON USER_B.GA_W_COUNTERS_HISTORY (ts_last_modified) TABLESPACE IGT_INDEX;


log_connection.sql
SET LINESIZE 140
SET PAGESIZE 0
SET HEADING OFF
SET FEEDBACK OFF
SET VERIFY OFF
SET ECHO OFF
SET NEWPAGE NONE
SET SHOW OFF
SET TRIMSPOOL ON

spool log_connection.log APPEND
SELECT '=======================' FROM DUAL;
SELECT 'Start Rebuild Tables and Indexes at '||TO_CHAR(SYSDATE,'YYYYMMDD hh24:mi:ss') FROM DUAL;
SELECT '    ' FROM DUAL;
spool off
exit;

Sunday, August 18, 2024

See objects in RECYCLEBIN and Purge RECYCLEBIN

See Recyclebin
COL owner FOR A30
COL original_name FOR A30
COL object_name FOR A30
SELECT owner, original_name, object_name, type, droptime
FROM DBA_RECYCLEBIN;

Purge a specific table PURGE TABLE RB$$33750$TABLE$0; 

Purge schema Recyclebin
PURGE RECYCLEBIN;



Purge all Recyclebin
PURGE DBA_RECYCLEBIN;

Monday, August 12, 2024

How To Configure Data Encryption and Integrity

How To Configure Data Encryption and Integrity



Network encryption is of prime importance to you if your databases is on the cloud.

Oracle Advanced Security native Oracle Net Services encryption and integrity.

Encryption and integrity parameters are defined by modifying a sqlnet.ora file on the clients and the servers on the network.

The profile on client and server systems using data encryption and integrity must contain some or all of the parameters listed below.


About Activating Encryption and Integrity
In any network connection, it is possible for both the client and server to support more than one encryption algorithm and more than one integrity algorithm. When a connection is made, the server selects which algorithm to use, if any, from those algorithms specified in the sqlnet.ora files.

The server searches for a match between the algorithms available on both the client and the server, and picks the first algorithm in its own list that also appears in the client list.
If one side of the connection does not specify an algorithm list, all the algorithms installed on that side are acceptable.
The connection fails with error message
ORA-12650 if either side specifies an algorithm that is not installed.


You can choose to configure any or all of the available Oracle Advanced Security encryption algorithms (Table 9-3), and the available integrity algorithm (SHA-1).
Only one encryption algorithm and one integrity algorithm are used for each connect session.


About Negotiating Encryption and Integrity
To negotiate whether to turn on encryption or integrity, you can specify four possible values for the Oracle Advanced Security encryption and integrity configuration parameters.
The four values are listed in the order of increasing security.

REJECTED provides the minimum amount of security between client and server communications,

ACCEPTED - Default. - Accepts, if possible, whatever encryption the other side is sending.

REQUESTED - "Preferred" method of Encryption, but not mandatory.

REQUIRED  - Mandatory method of Encryption. Provides the maximum amount of network security:



Oracle Database servers and clients are set to ACCEPT encrypted connections out of the box. This means that you can enable the desired encryption and integrity settings for a connection pair by configuring just one side of the connection, server-side or client-side.

So, for example, if there are many Oracle clients connecting to an Oracle database, you can configure the required encryption and integrity settings for all these connections by making the appropriate sqlnet.ora changes at the server end.
You do not need to implement configuration changes for each client separately.

REJECTED
Select this value if you do not elect to enable the security service, even if required by the other side.

In this scenario, this side of the connection specifies that the security service is not permitted. If the other side is set to REQUIRED, the connection terminates with error message ORA-12650. If the other side is set to REQUESTED, ACCEPTED, or REJECTED, the connection continues without error and without the security service enabled.

ACCEPTED
Select this value to enable the security service if required or requested by the other side.

In this scenario, this side of the connection does not require the security service, but it is enabled if the other side is set to REQUIRED or REQUESTED.
If the other side is set to REQUIRED or REQUESTED, and an encryption or integrity algorithm match is found, the connection continues without error and with the security service enabled.
If the other side is set to REQUIRED and no algorithm match is found, the connection terminates with error message ORA-12650.

If the other side is set to REQUESTED and no algorithm match is found, or if the other side is set to ACCEPTED or REJECTED, the connection continues without error and without the security service enabled.

REQUESTED
Select this value to enable the security service if the other side permits it.

In this scenario, this side of the connection specifies that the security service is desired but not required.
The security service is enabled if the other side specifies ACCEPTED, REQUESTED, or REQUIRED.
There must be a matching algorithm available on the other side, otherwise the service is not enabled.
If the other side specifies REQUIRED and there is no matching algorithm, the connection fails.

REQUIRED
Select this value to enable the security service or preclude the connection.

In this scenario, this side of the connection specifies that the security service must be enabled. The connection fails if the other side specifies REJECTED or if there is no compatible algorithm on the other side.

Table 9-2 shows whether the security service is enabled, based on a combination of client and server configuration parameters.
If either the server or client has specified REQUIRED, the lack of a common algorithm causes the connection to fail.
Otherwise, if the service is enabled, lack of a common service algorithm results in the service being disabled.

Configuration
Configuration is done in sqlnet.ora.
Since server can also make client call - on client side need specify both

On server:
SQLNET.ENCRYPTION_SERVER = [accepted | rejected | requested | required]
SQLNET.ENCRYPTION_TYPES_SERVER = (valid_encryption_algorithm [,valid_encryption_algorithm])

On client:
SQLNET.ENCRYPTION_CLIENT = [accepted | rejected | requested | required]
SQLNET.ENCRYPTION_TYPES_CLIENT = (valid_encryption_algorithm [,valid_encryption_algorithm])


For Example:

Client side "sqlnet.ora" file.
To force encryption from a client, while not affecting any other connections to the server, we would add the following to the client "sqlnet.ora" file.
The server does not need to be altered as the default settings (ACCEPTED and no named encryption algorithm) will allow it to successfully negotiate a connection.

SQLNET.ENCRYPTION_CLIENT=REQUIRED
SQLNET.ENCRYPTION_TYPES_CLIENT=(AES256)

Server side "sqlnet.ora" file.
If we would prefer clients to use encrypted connections to the server, but will accept non-encrypted connections, we would add the following to the server side "sqlnet.ora".

SQLNET.ENCRYPTION_SERVER=REQUESTED
SQLNET.ENCRYPTION_TYPES_SERVER=(AES256)

Other options:
SQLNET.ENCRYPTION_CLIENT = REQUESTED
SQLNET.ENCRYPTION_TYPES_CLIENT= (AES256, AES192, AES128)

SQLNET.ENCRYPTION_SERVER = REQUESTED
SQLNET.ENCRYPTION_TYPES_SERVER= (AES256, AES192, AES128)

In this example:
REQUESTED : The client or server will request encrypted traffic if it is possible, but will accept non-encrypted traffic if encryption is not possible.


Data Integrity
The advanced security data integrity functionality is separate to network encryption, but it is often discussed in the same context and in the same sections of the manuals.
The configuration is similar to that of network encryption, using the following parameters in the server and/or client "sqlnet.ora" files.

# Server
SQLNET.CRYPTO_CHECKSUM_SERVER
SQLNET.CRYPTO_CHECKSUM_TYPES_SERVER

# Client
SQLNET.CRYPTO_CHECKSUM_CLIENT
SQLNET.CRYPTO_CHECKSUM_TYPES_CLIENT

The SQLNET.CRYPTO_CHECKSUM_[SERVER|CLIENT] parameters have the same allowed values as the SQLNET.ENCRYPTION_[SERVER|CLIENT] parameters, with the same style of negotiations.

The SQLNET.CRYPTO_CHECKSUM_TYPES_[SERVER|CLIENT] parameters only accepts the SHA1 value prior to 12c. 
From 12c onward they also accept MD5, SHA1, SHA256, SHA384 and SHA512, with SHA256 being the default.

Sunday, August 11, 2024

cleanup PERFSTAT tables

How to cleanup PERFSTAT tables

PERFSTAT tables grow in size over time

OWNER           TABLESPACE_NAME  SEGMENT_NAME             USED_MB
--------------- ---------------- ------------------------ -------PERFSTAT        WORKAREA         STATS$SQL_SUMMARY           1664
PERFSTAT        WORKAREA         STATS$SYSSTAT_PK             888
PERFSTAT        WORKAREA         STATS$SQL_SUMMARY_PK         664
PERFSTAT        WORKAREA         STATS$SYSSTAT                608
PERFSTAT        WORKAREA         STATS$EVENT_HISTOGRAM_PK     416
PERFSTAT        WORKAREA         STATS$LATCH                  360
PERFSTAT        WORKAREA         STATS$LATCH_PK               328
PERFSTAT        WORKAREA         STATS$EVENT_HISTOGRAM        272
PERFSTAT        WORKAREA         STATS$PARAMETER_PK           184
PERFSTAT        WORKAREA         STATS$SQLTEXT                176


sqlplus perfstat/perfstat@igt

To make the perfstat job to run every hour:
$ORACLE_HOME/rdbms/admin/spauto.sql


To query current status:
SELECT MIN(SNAP_TIME) FROM stats$snapshot;

MIN(SNAP_TIME)
------------------
11-JAN-24

SELECT SYSDATE FROM DUAL;

SYSDATE
------------------
11-AUG-24


To manually truncate perfstat tables
$ORACLE_HOME/rdbms/admin/sptrunc.sql

DESCRIPTION:    Truncates data in Statspack tables
SQL> @$ORACLE_HOME/rdbms/admin/sptrunc.sql
Warning
~~~~~~~
Running sptrunc.sql removes ALL data from Statspack tables.  You may
wish to export the data before continuing.

About to Truncate Statspack Tables
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If would like to exit WITHOUT truncating the tables, enter any text at the
begin_or_exit prompt (e.g. 'exit'), otherwise if you would like to begin
the truncate operation, press <return>
Enter value for begin_or_exit:

Table truncated.
Table truncated.
5108 rows deleted.
1 row deleted.
Commit complete.
Package altered.
... Truncate operation complete
SQL>


Per Oracle Technote How To Automate Purging of Statspack Snapshots (Doc ID 464214.1)

CREATE OR REPLACE PACKAGE SPPURPKG
IS
  PROCEDURE purge(in_days_older_than IN INTEGER);
END SPPURPKG;
/

CREATE OR REPLACE PACKAGE BODY SPPURPKG
IS
  PROCEDURE purge(in_days_older_than IN INTEGER)IS
    CURSOR get_snaps(in_days IN INTEGER) IS
      SELECT s.rowid,
s.snap_id,
s.dbid,
s.instance_number
    FROM stats$snapshot s,
sys.v_$database d,
sys.v_$instance i
       WHERE s.dbid = d.dbid
         AND s.instance_number = i.instance_number
         AND s.snap_time < TRUNC(SYSDATE) - in_days;
 
errcontext VARCHAR2(100);
errmsg VARCHAR2(1000);
save_module VARCHAR2(48);
save_action VARCHAR2(32);
  BEGIN
    errcontext := 'save settings of DBMS_APPLICATION_INFO';
dbms_application_info.read_module(save_module, save_action);
dbms_application_info.set_module('SPPURPKG.PURGE', 'begin');
errcontext := 'open/fetch get_snaps';
dbms_application_info.set_action(errcontext);
FOR x IN get_snaps(in_days_older_than) LOOP
    errcontext := 'delete (cascade) STATS$SNAPSHOT';
  dbms_application_info.set_action(errcontext);
  DELETE FROM stats$snapshot
       WHERE ROWID = x.rowid;
      errcontext := 'delete "dangling" STATS$SQLTEXT rows';
      dbms_application_info.set_action(errcontext);
  
      DELETE FROM stats$sqltext
       WHERE (old_hash_value, text_subset) not in
               (SELECT /*+ hash_aj (ss) */ old_hash_value, text_subset
                  FROM stats$sql_summary ss
               );
      errcontext := 'delete "dangling" STATS$DATABASE_INSTANCE rows';
      dbms_application_info.set_action(errcontext);

  DELETE FROM stats$database_instance i
       WHERE i.instance_number = x.instance_number
         AND i.dbid = x.dbid
         AND NOT EXISTS
                    (SELECT 1
                       FROM stats$snapshot s
                      WHERE s.dbid = i.dbid
                        AND s.instance_number = i.instance_number
                        AND s.startup_time = i.startup_time
                    );
      errcontext := 'delete "dangling" STATS$STATSPACK_PARAMETER rows';
      dbms_application_info.set_action(errcontext);
  
      DELETE FROM stats$statspack_parameter p
       WHERE p.instance_number = x.instance_number
         AND p.dbid = x.dbid
         AND NOT EXISTS
                    (SELECT 1
                       FROM stats$snapshot s
                      WHERE s.dbid = p.dbid
                        AND s.instance_number = p.instance_number
                    );
      errcontext := 'fetch/close get_snaps';
      dbms_application_info.set_action(errcontext);
    END LOOP;
commit;
    errcontext := 'restore saved settings of DBMS_APPLICATION_INFO';
    dbms_application_info.set_module(save_module, save_action);
  EXCEPTION
    WHEN OTHERS THEN
  rollback;
      errmsg := sqlerrm;
      dbms_application_info.set_module(save_module, save_action);
      raise_application_error(-20000, errcontext || ': ' || errmsg);
  END purge;
END SPPURPKG;
/

--Create a job to delete data older than 10 days
BEGIN
 DBMS_JOB.submit(v_job_number, 
    WHAT => 'SPPURPKG.purge(10);',
    NEXT_DATE => TRUNC(SYSDATE) +1 + 0/24 + 15/1440,
    INTERVAL  => 'TRUNC(SYSDATE) +1 + 0/24 + 15/1440'); 
 COMMIT;
END;
/

--Execute the job
BEGIN
  SPPURPKG.purge(10);
  commit;
END;
/

--See jobs under perfstat user
COL WHAT FOR A30
COL INTERVAL FOR A40
SELECT what, interval FROM USER_JOBS;

 WHAT                           INTERVAL
------------------------------ ----------------------------------
SPPURPKG.purge(10);            TRUNC(SYSDATE) +1 + 0/24 + 15/1440
statspack.snap;                trunc(SYSDATE+1/24,'HH')

Thursday, August 8, 2024

grant change notification permissions

=======================
Oracle server Permissions
=======================
What to check

SQL> show parameter statistics
NAME                                 TYPE        VALUE
------------------------------------ ----------- --------------------
client_statistics_level              string      TYPICAL
optimizer_adaptive_statistics        boolean     FALSE
optimizer_real_time_statistics       boolean     FALSE
optimizer_use_pending_statistics     boolean     FALSE
statistics_level                     string      BASIC
timed_os_statistics                  integer     0
timed_statistics                     boolean     FALSE 

statistics_level should be set to TYPICAL
timed_statistics should be set to TRUE

statistics_level
TYPICAL  - Is the default 
BASIC  - disables the collection of many of the important statistics
Setting the STATISTICS_LEVEL parameter to BASIC disables the collection of many of the important statistics required by Oracle Database features and functionality, including:

timed_statistics
Specifies whether statistics related to time are collected.


=======================
Schema Permissions
=======================
What to check

SELECT *
  FROM DBA_SYS_PRIVS
 WHERE privilege = 'CHANGE NOTIFICATION' AND grantee = 'XXX';

sqlplus / as sysdba
grant change notification to vipcreator with admin option;
exit;

sqlplus vipcreator/xxxxxxx@orainst
SELECT 'grant change notification to '||USERNAME||';'
FROM ALL_USERS
WHERE ORACLE_MAINTAINED = 'N';

Execute the sqls in output 
GRANT CHANGE NOTIFICATION TO USERNAME;
GRANT EXECUTE ON DBMS_CHANGE_NOTIFICATION TO 
GRANT EXECUTE ON DBMS_CHANGE_NOTIFICATION TO USERNAME;

Check Change Notification Permission:
SELECT *
  FROM DBA_SYS_PRIVS
 WHERE privilege = 'CHANGE NOTIFICATION' 
   AND grantee = 'USERNAME';


Once there is notification, data will go into table DBA_CHANGE_NOTIFICATION_REGS

Table Level
See if monitoring is enabled
SELECT monitoring, count(1) 
  FROM USER_TABLES 
 GROUP BY MONITORING;

See if monitoring is logged to USER_TAB_MODIFICATIONS 
SELECT table_name, timestamp 
  FROM USER_TAB_MODIFICATIONS 
 WHERE table_name = 'SGA_W_LOG';

Flash writes to USER_TAB_MODIFICATIONS 
BEGIN
 DBMS_STATS.flush_database_monitoring_info;
END;
/

After flush, data should be in USER_TAB_MODIFICATIONS 
SELECT table_name, timestamp 
  FROM USER_TAB_MODIFICATIONS 
 WHERE table_name = 'SGA_W_LOG';