INSTALL POSTGRESQL IN LINUX

Check the linux distribution for the postgresql to be installed

[root@DBZX21 ~]# uname -a
Linux DBZX21 5.4.17-2136.304.4.1.el8uek.x86_64 #2 SMP Tue Feb 8 11:54:24 PST 2022 x86_64 x86_64 x86_64 GNU/Linux

[root@DBZX21 ~]# cat /etc/redhat-release
Red Hat Enterprise Linux release 8.5 (Ootpa)

Based on the distribution type, use the package installation utility to install postgresql

[root@DBZX21 kish]# yum install postgresql* -y

Initialize the postgre libraries using postgresql-setup

[postgres@DBZX21 ~]$ /usr/bin/postgresql-setup --initdb
 * Initializing database in '/var/lib/pgsql/data'
 * Initialized, logs are in /var/lib/pgsql/initdb_postgresql.log

Enable the postgresql service and start the service using systemctl utility

[postgres@DBZX21 ~]$ systemctl enable postgresql
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-unit-files ====
Authentication is required to manage system service or unit files.
Authenticating as: kishan
Password:
==== AUTHENTICATION COMPLETE ====
Created symlink /etc/systemd/system/multi-user.target.wants/postgresql.service → /usr/lib/systemd/system/postgresql.service.
==== AUTHENTICATING FOR org.freedesktop.systemd1.reload-daemon ====
Authentication is required to reload the systemd state.
Authenticating as: kishan
Password:
==== AUTHENTICATION COMPLETE ====
[postgres@DBZX21 ~]$ systemctl start postgresql
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ====
Authentication is required to start 'postgresql.service'.
Authenticating as: kishan
Password:
==== AUTHENTICATION COMPLETE ====

[postgres@DBZX21 ~]$ systemctl status postgresql
● postgresql.service - PostgreSQL database server
   Loaded: loaded (/usr/lib/systemd/system/postgresql.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2022-11-08 21:24:36 IST; 35s ago
  Process: 70150 ExecStartPre=/usr/libexec/postgresql-check-db-dir postgresql (code=exited, status=0/SUCCESS)
 Main PID: 70152 (postmaster)
    Tasks: 8 (limit: 35835)
   Memory: 15.9M
   CGroup: /system.slice/postgresql.service
           ├─70152 /usr/bin/postmaster -D /var/lib/pgsql/data
           ├─70154 postgres: logger process
           ├─70156 postgres: checkpointer process
           ├─70157 postgres: writer process
           ├─70158 postgres: wal writer process
           ├─70159 postgres: autovacuum launcher process
           ├─70160 postgres: stats collector process
           └─70161 postgres: bgworker: logical replication launcher

Check the process related to postgre which are up to validate the status

[postgres@DBZX21 ~]$ ps -ef|grep postgre|grep -Ev 'su|grep|ps|bash'
postgres   70063   70062  0 21:24 pts/0    00:00:00 /usr/libexec/pk-command-not-found [postgres@DBZX21 ~]$ /usr/bin/postgresql-setup --initdb
postgres   70152       1  0 21:24 ?        00:00:00 /usr/bin/postmaster -D /var/lib/pgsql/data
postgres   70154   70152  0 21:24 ?        00:00:00 postgres: logger process
postgres   70156   70152  0 21:24 ?        00:00:00 postgres: checkpointer process
postgres   70157   70152  0 21:24 ?        00:00:00 postgres: writer process
postgres   70158   70152  0 21:24 ?        00:00:00 postgres: wal writer process
postgres   70159   70152  0 21:24 ?        00:00:00 postgres: autovacuum launcher process
postgres   70160   70152  0 21:24 ?        00:00:00 postgres: stats collector process
postgres   70161   70152  0 21:24 ?        00:00:00 postgres: bgworker: logical replication launcher

Use psql to login to the command line

List all the current databases

[postgres@DBZX21 ~]$ psql
postgres=# \l+
                                                                    List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   |  Size   | Tablespace |                Description

-----------+----------+----------+-------------+-------------+-----------------------+---------+------------+-------------------------------------
-------
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |                       | 7319 kB | pg_default | default administrative connection da
tabase
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +| 7185 kB | pg_default | unmodifiable empty database
           |          |          |             |             | postgres=CTc/postgres |         |            |
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +| 7185 kB | pg_default | default template for new databases
           |          |          |             |             | postgres=CTc/postgres |         |            |
(3 rows)

Use help to print all the DDL commands that can be used

postgres=# \h
Available help:
  ABORT                            CLUSTER                          CREATE VIEW                      DROP USER MAPPING
  ALTER AGGREGATE                  COMMENT                          DEALLOCATE                       DROP VIEW
  ALTER COLLATION                  COMMIT                           DECLARE                          END
  ALTER CONVERSION                 COMMIT PREPARED                  DELETE

That is it, postgresql is installed successfully!

QUERIES AND COMMANDS THAT CAN BE USED IN PSQL

Postgresql provides attractive way to check all the queries and keywords that can be used with a backslash ‘\’

Below command can be used to check all the backslash commands in psql utility

postgres=# \?
General
  \copyright             show PostgreSQL usage and distribution terms
  \crosstabview [COLUMNS] execute query and display results in crosstab
  \errverbose            show most recent error message at maximum verbosity
  \g [FILE] or ;         execute query (and send results to file or |pipe)
  \gexec                 execute query, then execute each value in its result
  \gset [PREFIX]         execute query and store results in psql variables
  \gx [FILE]             as \g, but forces expanded output mode
  \q                     quit psql
  \watch [SEC]           execute query every SEC seconds

Help
  \? [commands]          show help on backslash commands
  \? options             show help on psql command-line options
  \? variables           show help on special variables
  \h [NAME]              help on syntax of SQL commands, * for all commands

Query Buffer
  \e [FILE] [LINE]       edit the query buffer (or file) with external editor
  \ef [FUNCNAME [LINE]]  edit function definition with external editor
  \ev [VIEWNAME [LINE]]  edit view definition with external editor
  \p                     show the contents of the query buffer
  \r                     reset (clear) the query buffer
  \s [FILE]              display history or save it to file
  \w FILE                write query buffer to file

Input/Output
  \copy ...              perform SQL COPY with data stream to the client host
  \echo [STRING]         write string to standard output
  \i FILE                execute commands from file
  \ir FILE               as \i, but relative to location of current script
  \o [FILE]              send all query results to file or |pipe
  \qecho [STRING]        write string to query output stream (see \o)

Conditional
  \if EXPR               begin conditional block
  \elif EXPR             alternative within current conditional block
  \else                  final alternative within current conditional block
  \endif                 end conditional block

Informational
  (options: S = show system objects, + = additional detail)
  \d[S+]                 list tables, views, and sequences
  \d[S+]  NAME           describe table, view, sequence, or index
  \da[S]  [PATTERN]      list aggregates
  \dA[+]  [PATTERN]      list access methods
  \db[+]  [PATTERN]      list tablespaces
  \dc[S+] [PATTERN]      list conversions
  \dC[+]  [PATTERN]      list casts
  \dd[S]  [PATTERN]      show object descriptions not displayed elsewhere
  \dD[S+] [PATTERN]      list domains
  \ddp    [PATTERN]      list default privileges
  \dE[S+] [PATTERN]      list foreign tables
  \det[+] [PATTERN]      list foreign tables
  \des[+] [PATTERN]      list foreign servers
  \deu[+] [PATTERN]      list user mappings
  \dew[+] [PATTERN]      list foreign-data wrappers
  \df[antw][S+] [PATRN]  list [only agg/normal/trigger/window] functions
  \dF[+]  [PATTERN]      list text search configurations
  \dFd[+] [PATTERN]      list text search dictionaries
  \dFp[+] [PATTERN]      list text search parsers
  \dFt[+] [PATTERN]      list text search templates
  \dg[S+] [PATTERN]      list roles
  \di[S+] [PATTERN]      list indexes
  \dl                    list large objects, same as \lo_list
  \dL[S+] [PATTERN]      list procedural languages
  \dm[S+] [PATTERN]      list materialized views
  \dn[S+] [PATTERN]      list schemas
  \do[S+] [PATTERN]      list operators
  \dO[S+] [PATTERN]      list collations
  \dp     [PATTERN]      list table, view, and sequence access privileges
  \drds [PATRN1 [PATRN2]] list per-database role settings
  \dRp[+] [PATTERN]      list replication publications
  \dRs[+] [PATTERN]      list replication subscriptions
  \ds[S+] [PATTERN]      list sequences
  \dt[S+] [PATTERN]      list tables
  \dT[S+] [PATTERN]      list data types
  \du[S+] [PATTERN]      list roles
  \dv[S+] [PATTERN]      list views
  \dx[+]  [PATTERN]      list extensions
  \dy[+]  [PATTERN]      list event triggers
  \l[+]   [PATTERN]      list databases
  \sf[+]  FUNCNAME       show a function's definition
  \sv[+]  VIEWNAME       show a view's definition
  \z      [PATTERN]      same as \dp

Formatting
  \a                     toggle between unaligned and aligned output mode
  \C [STRING]            set table title, or unset if none
  \f [STRING]            show or set field separator for unaligned query output
  \H                     toggle HTML output mode (currently off)
  \pset [NAME [VALUE]]   set table output option
                         (NAME := {border|columns|expanded|fieldsep|fieldsep_zero|
                         footer|format|linestyle|null|numericlocale|pager|
                         pager_min_lines|recordsep|recordsep_zero|tableattr|title|
                         tuples_only|unicode_border_linestyle|
                         unicode_column_linestyle|unicode_header_linestyle})
  \t [on|off]            show only rows (currently off)
  \T [STRING]            set HTML <table> tag attributes, or unset if none
  \x [on|off|auto]       toggle expanded output (currently off)

Connection
  \c[onnect] {[DBNAME|- USER|- HOST|- PORT|-] | conninfo}
                         connect to new database (currently "postgres")
  \conninfo              display information about current connection
  \encoding [ENCODING]   show or set client encoding
  \password [USERNAME]   securely change the password for a user

Operating System
  \cd [DIR]              change the current working directory
  \setenv NAME [VALUE]   set or unset environment variable
  \timing [on|off]       toggle timing of commands (currently off)
  \! [COMMAND]           execute command in shell or start interactive shell

Variables
  \prompt [TEXT] NAME    prompt user to set internal variable
  \set [NAME [VALUE]]    set internal variable, or list all if no parameters
  \unset NAME            unset (delete) internal variable

Large Objects
  \lo_export LOBOID FILE
  \lo_import FILE [COMMENT]
  \lo_list
  \lo_unlink LOBOID      large object operations

All DDL commands that can be used are printed using help \h

postgres=# \h
Available help:
  ABORT                            CLUSTER                          CREATE VIEW                      DROP USER MAPPING
  ALTER AGGREGATE                  COMMENT                          DEALLOCATE                       DROP VIEW
  ALTER COLLATION                  COMMIT                           DECLARE                          END
  ALTER CONVERSION                 COMMIT PREPARED                  DELETE                           EXECUTE
  ALTER DATABASE                   COPY                             DISCARD                          EXPLAIN
  ALTER DEFAULT PRIVILEGES         CREATE ACCESS METHOD             DO                               FETCH
  ALTER DOMAIN                     CREATE AGGREGATE                 DROP ACCESS METHOD               GRANT
  ALTER EVENT TRIGGER              CREATE CAST                      DROP AGGREGATE                   IMPORT FOREIGN SCHEMA
  ALTER EXTENSION                  CREATE COLLATION                 DROP CAST                        INSERT
  ALTER FOREIGN DATA WRAPPER       CREATE CONVERSION                DROP COLLATION                   LISTEN
  ALTER FOREIGN TABLE              CREATE DATABASE                  DROP CONVERSION                  LOAD
  ALTER FUNCTION                   CREATE DOMAIN                    DROP DATABASE                    LOCK
  ALTER GROUP                      CREATE EVENT TRIGGER             DROP DOMAIN                      MOVE
  ALTER INDEX                      CREATE EXTENSION                 DROP EVENT TRIGGER               NOTIFY
  ALTER LANGUAGE                   CREATE FOREIGN DATA WRAPPER      DROP EXTENSION                   PREPARE
  ALTER LARGE OBJECT               CREATE FOREIGN TABLE             DROP FOREIGN DATA WRAPPER        PREPARE TRANSACTION
  ALTER MATERIALIZED VIEW          CREATE FUNCTION                  DROP FOREIGN TABLE               REASSIGN OWNED
  ALTER OPERATOR                   CREATE GROUP                     DROP FUNCTION                    REFRESH MATERIALIZED VIEW
  ALTER OPERATOR CLASS             CREATE INDEX                     DROP GROUP                       REINDEX
  ALTER OPERATOR FAMILY            CREATE LANGUAGE                  DROP INDEX                       RELEASE SAVEPOINT
  ALTER POLICY                     CREATE MATERIALIZED VIEW         DROP LANGUAGE                    RESET
  ALTER PUBLICATION                CREATE OPERATOR                  DROP MATERIALIZED VIEW           REVOKE
  ALTER ROLE                       CREATE OPERATOR CLASS            DROP OPERATOR                    ROLLBACK
  ALTER RULE                       CREATE OPERATOR FAMILY           DROP OPERATOR CLASS              ROLLBACK PREPARED
  ALTER SCHEMA                     CREATE POLICY                    DROP OPERATOR FAMILY             ROLLBACK TO SAVEPOINT
  ALTER SEQUENCE                   CREATE PUBLICATION               DROP OWNED                       SAVEPOINT
  ALTER SERVER                     CREATE ROLE                      DROP POLICY                      SECURITY LABEL
  ALTER STATISTICS                 CREATE RULE                      DROP PUBLICATION                 SELECT
  ALTER SUBSCRIPTION               CREATE SCHEMA                    DROP ROLE                        SELECT INTO
  ALTER SYSTEM                     CREATE SEQUENCE                  DROP RULE                        SET
  ALTER TABLE                      CREATE SERVER                    DROP SCHEMA                      SET CONSTRAINTS
  ALTER TABLESPACE                 CREATE STATISTICS                DROP SEQUENCE                    SET ROLE
  ALTER TEXT SEARCH CONFIGURATION  CREATE SUBSCRIPTION              DROP SERVER                      SET SESSION AUTHORIZATION
  ALTER TEXT SEARCH DICTIONARY     CREATE TABLE                     DROP STATISTICS                  SET TRANSACTION
  ALTER TEXT SEARCH PARSER         CREATE TABLE AS                  DROP SUBSCRIPTION                SHOW
  ALTER TEXT SEARCH TEMPLATE       CREATE TABLESPACE                DROP TABLE                       START TRANSACTION
  ALTER TRIGGER                    CREATE TEXT SEARCH CONFIGURATION DROP TABLESPACE                  TABLE
  ALTER TYPE                       CREATE TEXT SEARCH DICTIONARY    DROP TEXT SEARCH CONFIGURATION   TRUNCATE
  ALTER USER                       CREATE TEXT SEARCH PARSER        DROP TEXT SEARCH DICTIONARY      UNLISTEN
  ALTER USER MAPPING               CREATE TEXT SEARCH TEMPLATE      DROP TEXT SEARCH PARSER          UPDATE
  ALTER VIEW                       CREATE TRANSFORM                 DROP TEXT SEARCH TEMPLATE        VACUUM
  ANALYZE                          CREATE TRIGGER                   DROP TRANSFORM                   VALUES
  BEGIN                            CREATE TYPE                      DROP TRIGGER                     WITH
  CHECKPOINT                       CREATE USER                      DROP TYPE
  CLOSE                            CREATE USER MAPPING              DROP USER
HOW TO CREATE A DATABASE IN POSTGRESQL

Creating a database in postgres is pretty simple in postgres

Create a basic role first for the database

[postgres@DBZX21 ~]$ psql
psql (10.21)
Type "help" for help.

postgres=# CREATE ROLE prole
postgres-# SUPERUSER
postgres-# LOGIN
postgres-# PASSWORD 'prole';
CREATE ROLE

Create a directory for datafiles to be physically stored and give permissions

[oracle@DBZX21 ~]$ mkdir -p /u01/postgre/datafile
[root@DBZX21 u01]# chmod -R 775 postgre/
[root@DBZX21 u01]# chown -R postgres:postgres postgre/

[root@DBZX21 u01]# ls -lrt postgre/
total 0
drwx------ 3 postgres postgres 29 Nov  9 15:26 datafile

Create a tablespace to be used by default for application data

postgres=# CREATE TABLESPACE ptab
postgres-# OWNER prole
postgres-# LOCATION '/u01/postgre/datafile';
CREATE TABLESPACE

Create a database with all the above specified values for parameters

postgres=# CREATE DATABASE pgre
postgres-# WITH
postgres-#     OWNER = prole
postgres-#     ENCODING = 'UTF8'
postgres-#     TABLESPACE = ptab
postgres-#     CONNECTION LIMIT = 30;
CREATE DATABASE
SIMPLE PROGRAM FOR ATM TRANSACTION IN PYTHON

A simple program to simulate an ATM transaction as per real time scenario.

#ATM transaction
#Declare the variables

denomination = [3000,6000,9000]
password = 'eXhydra@369'
amount = 100000

#Initialize the value of i

i = 0

#Ask the user to enter the password or pin for ATM 

while i < 3:
    usrpwd = input("Please enter the password : ")
    #If the password is correct, then ask the amount to be withdrawn otherwise prompt to reenter the password
    if usrpwd == password:
        withdraw = int(input("Enter transaction amount to be withdrawn : "))
        #Withdraw the correct amount as per the balance available
        if withdraw > 100000 or withdraw < 1:
            print("Enter an amount 'greater than 0' or 'less than or equal to balance'",amount)
            continue
        else:
            #Print the withdraw message and ask for repeat of transaction
            balance = amount - withdraw
            print("Transaction is successfully completed, account balance is :",balance)
            #If another transaction need to be performed, then enter the amount again
            rewithdraw = input("Do you need to perform another transaction? Yes or No: ")
            if rewithdraw == 'Yes' or rewithdraw == 'YES' or rewithdraw == 'yes':
                amount = balance
                continue
            #If the transaction need to be closed, print the thanks message
            elif rewithdraw == 'no' or rewithdraw == 'No' or rewithdraw == 'NO':
                print("Thanks for using XYZ bank. Please visit us next time")
                break
            #If any invalid input is provided, then break out of the loop
            else:
                print('*xxxxxxx* Invalid response! Transaction cancelled *xxxxxxx*')
                break
    #Three attempts are allowed for wrong password and post that transaction will be cancelled for security reasons
    else:
        print("Wrong password provided, enter the correct password again!")
        i += 1
        if i > 2:
            print('*' * 40)
            print("Password attempts exceeded closing the transaction...")
            print('*' * 40)
        
        
DEFAULT OR RESERVED KEYWORDS IN PYTHON

Keywords in python are the reserved words which cannot be customized for individual variables.

Python Interpreter throws “SyntaxError: invalid syntax” error during parse phase if a variable is declared with a reserved keyword. Each keyword has its own purpose in python which are system related internal code.

help(keyword.kwlist)

Code:

#import keywords
import keyword

#Print them
print("Reserved keywords are: ")
for resrvd_words in keyword.kwlist:
    print(resrvd_words)

Output:

False
None
True
__peg_parser__
and
as
assert
async
await
break
class
continue
def
del
elif
else
except
finally
for
from
global
if
import
in
is
lambda
nonlocal
not
or
pass
raise
return
try
while
with
yield

A simple function to check reserved keywords. keyword has a function iskeyword() which takes one argument for word to check if the entered string is keyword or not.

#Library for keyword 
import keyword
#w = ['abc','efg','True']

#Function which checks if the entered string is keyword or not
def _CRKW_(w):
    if keyword.iskeyword(w):
        return "Reserved keywords"
    else:
        return "Non reserved words"
            
_CRKW_('T')

ARITHMETIC OPERATORS IN PYTHON

What is Operator?

Operators are characters which denotes a special symbol to accomplish mathematical operations or logical or comparisons etc.

Operands are the objects used in the expression which is connected with operators to provide valuable results post calculation

Below are the types of operators used in python

  • Arithmetic
  • Assignment
  • Comparison
  • Logical
  • Identity
  • Membership
  • Bitwise

In this article, arithmetic operators in python are going to be discussed.

ARITHMETIC OPERATORS:

BODMAS (brackets, division, multiplication, addition, subtraction) is the rules which is used to calculate the arithmetic operations in an order. Always the operators are used from left to right in the expression for equal precedence of same symbols.

BEDMAS(Brackets, exponential, division, multiplication, addition, subtraction) is similar to BODMAS rules which is used to calculate the arithmetic operations in an order. But there is slight difference in the operators used like exponential and modulus. Always the operators are used from left to right in the expression for equal precedence of same symbols.

Below are the arithmetic operators which are calculated in pythonic way which has two positive integers

#Declare variable
 x = 3
 y = 6
 
#print the operations done by arithmetic operators for each value
print('Addition : {0}\
      \nSubtraction : {1}\
      \nProduct : {2}\
      \nFloat Division : {3}\
      \nFloor Division : {4}\
      \nModulo : {5}\
      \nPower : {6}'.format(x+y,\
                          x-y,\
                          x*y,\
                          x/y,\
                          x//y,\
                          x%y,\
                          x**y))

Output:

Addition : 9      
Subtraction : -3      
Product : 18      
Float Division : 0.5      
Floor Division : 0      
Modulo : 3      
Power : 729

Below are the arithmetic operators which are calculated in pythonic way which has one decimal and one negative integer

#Declare variable

 x1 = 0.3
 y1 = -6

#print the operations done by arithmetic operators for each value

print('Addition : {0}\
      \nSubtraction : {1}\
      \nProduct : {2}\
      \nFloat Division : {3}\
      \nFloor Division : {4}\
      \nModulo : {5}\
      \nPower : {6}'.format(x1 + y1,\
                          x1 - y1,\
                          x1 * y1,\
                          x1 / y1,\
                          x1 // y1,\
                          x1 % y1,\
                          x1 ** y1))

Output:

Addition : -5.7      
Subtraction : 6.3      
Product : -1.7999999999999998      
Float Division : -0.049999999999999996      
Floor Division : -1.0      
Modulo : -5.7      
Power : 1371.7421124828536

If there is a floating point value or operand in an expression in any one of the position, then the result will always be floating point.

Below are the arithmetic operators which are calculated in pythonic way which has one negative decimal and one negative integer

#Declare variable

 x2 = -0.6
 y2 = -9

#print the operations done by arithmetic operators for each value

print('Addition : {0}\
      \nSubtraction : {1}\
      \nProduct : {2}\
      \nFloat Division : {3}\
      \nFloor Division : {4}\
      \nModulo : {5}\
      \nPower : {6}'.format(x2 + y2,\
                          x2 - y2,\
                          x2 * y2,\
                          x2 / y2,\
                          x2 // y2,\
                          x2 % y2,\
                          x2 ** y2))

Output:

Addition : -9.6      
Subtraction : 8.4      
Product : 5.3999999999999995      
Float Division : 0.06666666666666667      
Floor Division : 0.0      
Modulo : -0.6      
Power : -99.22903012752124

Expression with multiple operators with multiple operands

#Declare variable

x3 = -0.6
y3 = -9
z3 = 3

#print the operations done by arithmetic operators for each value

print(x3 + y3 - z3 * x3 / y3 // z3 % x3 ** y3)

Output:

-9.6

Real World Use Cases:

Code:

#How much total mark is scored by a student xyz in exam
#Addition operator
outof = 400
maths = 80
science = 50
computer = 90
biology = 30

total_marks = maths + science + computer + biology
print('Student xyz scored ',total_marks,'out of ',outof)

Output:

Student xyz scored  250 out of  400

Code:

#Product Use case
#Check the validity of the internet pack by converting it from day to hours, minutes and seconds

internet_validity_days = 30
hours = 24 * internet_validity_days
minutes = 1440 * hours 
seconds = 86400 * minutes

print("There are {0} hours, {1} minutes and {2} seconds validity for internet pack to expire".format(hours, minutes, seconds))

Output:

There are 720 hours, 1036800 minutes and 89579520000 seconds validity for internet pack to expire

Code:

#Floor Division
#Check the number of people who need to share money for a new year party

people = 10
party_charge = 100000
individual_share = party_charge // people
print("Each individual should contribute ${}".format(individual_share))

Output:

Each individual should contribute $10000

Summary:

In real world, there are lot of applications which depend on simple arithmetic operators which acts as a solution in many ways like

  • basic amenities which are purchased daily
  • time or distance taken to reach a destination
  • money withdrawal from atm

Every thing that happens in a day to day real world, are based on numbers and numbers are from mathematics.

ORA-00932: inconsistent datatypes: expected NUMBER got DATE

kIsH@STAX<>CREATE INDEX rp_gbl_ind ON kish.rp(R_ID)
GLOBAL PARTITION BY RANGE(R_ID)
(PARTITION ind_date_p1 VALUES LESS THAN (TO_DATE(' 2022-01-01 00:00:00', 'YYYY-MM-DD HH24:MI:SS')),
 PARTITION ind_date_p2 VALUES LESS THAN (TO_DATE(' 2022-02-01 00:00:00', 'YYYY-MM-DD HH24:MI:SS' )),
 PARTITION ind_date_p3 VALUES LESS THAN (TO_DATE(' 2022-03-01 00:00:00', 'YYYY-MM-DD HH24:MI:SS')),
 PARTITION ind_date_p4 VALUES LESS THAN (MAXVALUE));
  2    3    4    5    6  (PARTITION ind_date_p1 VALUES LESS THAN (TO_DATE(' 2022-01-01 00:00:00', 'YYYY-MM-DD HH24:MI:SS')),
                                         *
ERROR at line 3:
ORA-00932: inconsistent datatypes: expected NUMBER got DATE

kIsH@STAX<>oerr ora 00932
SP2-0734: unknown command beginning “oerr ora 0…” – rest of line ignored.
kIsH@STAX<>!oerr ora 00932
00932, 00000, “inconsistent datatypes: expected %s got %s”
// *Cause: One of the following:
// – An attempt was made to perform an operation on incompatible
// datatypes. For example, adding a character field to a date
// field (dates may only be added to numeric fields) or
// concatenating a character field with a long field.
// – An attempt was made to perform an operation on a database
// object (such as a table or view) that was not intended for
// normal use. For example, system tables cannot be modified by
// a user. Note that on rare occasions this error occurs
// because a misspelled object name matched a restricted
// object’s name.
// – An attempt was made to use an undocumented view.
// *Action: If the cause is
// – different datatypes, then use consistent datatypes. For
// example, convert the character field to a numeric field with
// the TO_NUMBER function before adding it to the date field.
// Functions may not be used with long fields.
// – an object not intended for normal use, then do not access
// the restricted object.

ORA-40216: feature not supported

Is auto index supported in non-exadata ?

No, it is not supported

SQL> begin
   dbms_auto_index.configure ('AUTO_INDEX_MODE','REPORT ONLY');
end;
/  2    3    4
begin
*
ERROR at line 1:
ORA-40216: feature not supported
ORA-06512: at "SYS.DBMS_SYS_ERROR", line 79
ORA-06512: at "SYS.DBMS_AUTO_INDEX_INTERNAL", line 10364
ORA-06512: at "SYS.DBMS_AUTO_INDEX", line 301
ORA-06512: at line 2
oerr ora 40216
40216, 00000, "feature not supported"
// *Cause:  The feature was not supported in the API.
// *Action: Modify the code to avoid usage of the feature.
WARNING: too many parse errors – Error= 4044

Error:

WARNING: too many parse errors, count=20934 SQL hash=0x20fs2579
PARSE ERROR: ospid=23444, error=4044 for statement:
SELECT /*+ FULL(P) +*/ * FROM "PROCEDURE" P <================

Cause:

ORA-04044
Cause: An attempt was made to specify a procedure, function, or package in an inappropriate place in a statement.
Action: Refer to Oracle7 Server SQL Reference for the correct placement of procedures, functions, and packages in statements.

Solution:

Inform application team to correct the sql statement which contains a function or package and should not be used in the place of query.

HOW TO SET CUSTOMIZED THRESHOLD TO INCLUDE SQL IN AWR REPORT

AWR helps to identify the problematic SQL which are reported as per the given snapshots. But, how does AWR determine the SQL to be printed ?

Not all the SQL are considered to be the nominees to be included in the AWR or ASH or ADDM report. Sometimes, even if a SQL has consumed lot of elapsed time, they are nowhere in the performance report. Often DBA’s have this misconception that if AWR does not include Top SQL for a PDB or ADDM doesnot include any recommendations or advise for any SQL or ADDM report shows this message ‘There was no significant database activity to run the ADDM. ‘, then this is not mostly a bug or incorrect report but expected behavior.

Activity During the Analysis Period
-----------------------------------
Total database time was 99 seconds.<============= DB time is only 99 sec which is not significant
The average number of active sessions was .01.  <============= number of average active session is .01 which is too low 


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

There are no findings to report.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

          Additional Information
          ----------------------

Miscellaneous Information
-------------------------
There was no significant database activity to run the ADDM.

The database's maintenance windows were active during 99% of the analysis
period.

There is an option to influence these parameters with dbms_workload_repository.awr_set_report_thresholds

Below are the parameters and its values

Parameters

top n events - the number of the most important wait events that will be considered

number of the most active files to be included, top n files

Number of the most active segments to be featured, top n segments

number of the most active services to be featured, top n services

top n sql is the number of the most important SQL statements that will be used.

Top n sql max is the maximum number of SQL statements that will be included if their activity exceeds the top sql pct threshold.

Between top n sql and top n max sql, top sql pct is the significance threshold for SQL statements.

shared memory low threshold, shmem threshold

versions threshold - low threshold for plan version count

A simple test case to demonstrate the behavior. This test case provides a sample

Enable AWR snapshots in PDB

SQL> startup;
ORACLE instance started.

Total System Global Area 3221222296 bytes
Fixed Size                  9143192 bytes
Variable Size             654311424 bytes
Database Buffers         2550136832 bytes
Redo Buffers                7630848 bytes
Database mounted.
Database opened.


SQL> alter system set awr_pdb_autoflush_enabled=true;

System altered.

SQL>  show parameter offset

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
awr_snapshot_time_offset             integer     0
SQL> alter system set awr_snapshot_time_offset=1000000;

System altered.

Connect to the PDB

SQL> sho pdbs

    CON_ID CON_NAME                       OPEN MODE  RESTRICTED
---------- ------------------------------ ---------- ----------
         2 PDB$SEED                       READ ONLY  NO
         3 PDB2                              MOUNTED
SQL> alter pluggable database PDB2 open;

Pluggable database altered.

SQL> alter session set container=PDB2;

Session altered.

SQL> show con_name

CON_NAME
------------------------------
PDB2

Specify the threshold for a parameter with sql threshold which contribute to x% of database performance, and x value is specified as 1% for easy simulation

exec dbms_workload_repository.awr_set_report_thresholds(top_sql_pct => 1); -- here i specified to include SQL which consume 1 % of the total database activity. Similarly, as per your database requirements, this percentage value can be modified.

Create a snapshot at beginning and end.

Execute a sample query with high parallelism and check if the SQL is included in the report


SQL> exec DBMS_WORKLOAD_REPOSITORY.CREATE_SNAPSHOT();
PL/SQL procedure successfully completed.

SQL> select /*+parallel(32) */ count(*) from dba_objects do, dba_source ds where do.owner = ds.owner;

  COUNT(*)
----------
1.0262E+10

SQL> exec DBMS_WORKLOAD_REPOSITORY.CREATE_SNAPSHOT();

PL/SQL procedure successfully completed.

SQL> @?/rdbms/admin/addmrpt

Current Instance
~~~~~~~~~~~~~~~~

   DB Id    DB Name      Inst Num Instance
----------- ------------ -------- ------------
 1769171242 eXhydra              1 eXhydra


Instances in this Workload Repository schema
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  DB Id      Inst Num   DB Name      Instance     Host
------------ ---------- ---------    ----------   ------
  1769171242     1      TARKM2       TARKM2       celclnx42.us

Using 1769171242 for database Id
Using          1 for instance number


Specify the number of days of snapshots to choose from
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Entering the number of days (n) will result in the most recent
(n) days of snapshots being listed.  Pressing <return> without
specifying a number lists all completed snapshots.



Listing the last 3 days of Completed Snapshots
Instance     DB Name      Snap Id       Snap Started    Snap Level
------------ ------------ ---------- ------------------ ----------

eXhydra       eXhydra               1  31 Oct 2022 12:13    1
                                  2  31 Oct 2022 12:14    1
                                  3  31 Oct 2022 12:15    1
                                  4  31 Oct 2022 13:31    1
                                  5  31 Oct 2022 13:40    1
                                  6  31 Oct 2022 13:43    1
                                  7  31 Oct 2022 13:53    1


Specify the Begin and End Snapshot Ids
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Enter value for begin_snap: 1
Begin Snapshot Id specified: 1

Enter value for end_snap: 7
End   Snapshot Id specified: 7



Specify the Report Name
~~~~~~~~~~~~~~~~~~~~~~~
The default report file name is addmrpt_1_1_7.txt.  To use this name,
press <return> to continue, otherwise enter an alternative.

Enter value for report_name:

Using the report name addmrpt_1_1_7.txt


Running the ADDM analysis on the specified pair of snapshots ...


Generating the ADDM report for this analysis ...


          ADDM Report for Task 'TASK_28'
          ------------------------------

Analysis Period
---------------
AWR snapshot range from 1 to 7.
Time period starts at 31-OCT-22 12.13.52 PM
Time period ends at 31-OCT-22 01.53.16 PM

Analysis Target
---------------
Database 'Localhost' with DB ID 1769171242.
Database version 19.0.0.0.0.
ADDM performed an analysis of instance Localhost, numbered 1 and hosted at
celclnx42.us.oracle.com.
ADDM detected that the system is a PDB.

Activity During the Analysis Period
-----------------------------------
Total database time was 498 seconds.
The average number of active sessions was .08.

Summary of Findings
-------------------
   Description         Active Sessions      Recommendations
                       Percent of Activity
   ------------------  -------------------  ---------------
1  Top SQL Statements  .08 | 95.56          1


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


          Findings and Recommendations
          ----------------------------

Finding 1: Top SQL Statements
Impact is .08 active sessions, 95.56% of total activity.
--------------------------------------------------------
SQL statements consuming significant database time were found. These
statements offer a good opportunity for performance improvement.

   Recommendation 1: SQL Tuning
   Estimated benefit is .08 active sessions, 95.56% of total activity.
   -------------------------------------------------------------------
   Action
      Run SQL Tuning Advisor on the SELECT statement with SQL_ID
      "1tf1karxnuxvz".
      Related Object
         SQL statement with SQL_ID 1tf1karxnuxvz.
         select /*+parallel(32) */ count(*) from dba_objects do, dba_source ds
         where do.owner = ds.owner
   Rationale
      The SQL spent 100% of its database time on CPU, I/O and Cluster waits.
      This part of database time may be improved by the SQL Tuning Advisor.
   Rationale
      Database time for this SQL was divided as follows: 100% for SQL
      execution, 0% for parsing, 0% for PL/SQL execution and 0% for Java
      execution.
   Rationale
      SQL statement with SQL_ID "1tf1karxnuxvz" was executed 1 times and had
      an average elapsed time of 454 seconds.
   Rationale
      At least one execution of the statement ran in parallel.



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

          Additional Information
          ----------------------

Miscellaneous Information
-------------------------
Wait class "Application" was not consuming significant database time.
Wait class "Commit" was not consuming significant database time.
Wait class "Concurrency" was not consuming significant database time.
Wait class "Configuration" was not consuming significant database time.
CPU was not a bottleneck for the DB on this instance.
Wait class "Network" was not consuming significant database time.
Wait class "User I/O" was not consuming significant database time.
Session connect and disconnect calls were not consuming significant database
time.
Hard parsing of SQL statements was not consuming significant database time.


End of Report