Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2018 April PostgreSQL: Use RAISE Statements to debug your Query and Function performance

PostgreSQL: Use RAISE Statements to debug your Query and Function performance

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

You can use the RAISE Statements for the report messages and raise errors.
Different level of RAISE statements are INFO, NOTICE, and EXCEPTION.

By default, NOTICE is always returning to the client only. We should use RAISE INFO for our internal query or function debugging.

We should break down our code into smaller parts and add RAISE statement with clock_timestamp().

We can compare the execution time difference between the code blocks and can find a slow running code block.
We can also add parameter and variable to the RAISE statement. We can print the current value of our parameter and variable.

Few examples:

1
2
3
4
RAISE INFO 'Hello World !';
RAISE NOTICE '%', variable_name;
RAISE NOTICE 'Current value of parameter (%)', my_var;
RAISE EXCEPTION '% cannot have null salary', EmpName;

One Practical Demonstration:

Create a table with Sample Data:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
CREATE TABLE Employee
(
EmpID SERIAL
,EmpName CHARACTER VARYING(50)
,Gender CHAR(1)
,AGE SMALLINT
);
INSERT INTO Employee
(
EmpName
,Gender
,AGE
)
VALUES
('Anvesh','M',27)
,('Mohan','M',30)
,('Roy','M',31)
,('Meera','F',27)
,('Richa','F',26)
,('Martin','M',35)
,('Mahesh','M',38)
,('Paresh','M',22)
,('Alina','F',21)
,('Alex','M',24);

Sample function for Custome Paging with the use of RAISE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
CREATE OR REPLACE FUNCTION fn_GetEmployeeData
(
Paging_PageSize INTEGER = NULL
,Paging_PageNumber INTEGER = NULL
)
RETURNS TABLE
(
outEmpID INTEGER
,outEmpName CHARACTER VARYING
,outGender CHAR(1)
,outAge SMALLINT
) AS
$BODY$
DECLARE PageNumber BIGINT;
DECLARE TempINT INTEGER;
BEGIN
/* ***************************************************************
Construct Custom paging parameter...
**************************************************************** */
IF (paging_pagesize IS NOT NULL AND paging_pagenumber IS NOT NULL) THEN
PageNumber := (Paging_PageSize * (Paging_PageNumber-1));
END IF;
 
RAISE INFO '%','Construction of Custom Paging Parameter - DONE '|| clock_timestamp();
/* ************************************************
Custome paging SQL Query construction.......
************************************************ */
TempINT := 10000;
WHILE (TempINT > 0)
LOOP
TempINT = TempINT - 1;
RAISE INFO '%','The current value of TempINT ' || TempINT;
END LOOP;
RETURN QUERY
SELECT
EmpID
,EmpName
,Gender
,Age
FROM public.Employee
ORDER BY EmpID
LIMIT Paging_PageSize
OFFSET PageNumber;
 
RAISE INFO '%','Final result set of main query - DONE ' || clock_timestamp();
EXCEPTION WHEN OTHERS THEN
RAISE;
END;
$BODY$
LANGUAGE 'plpgsql';

Execute this function and check the query message window:
You will find list of RAISE INFO messages that we mentioned in above function.

1
SELECT *FROM public.fn_GetEmployeeData(4,2);

Apr 14, 2018Anvesh Patel
PostgreSQL: VACUUM VERBOSE to find data pages and dead row versionsPostgreSQL: Get a full length column string in PgAdmin
Comments: 3
  1. Naren
    August 6, 2019 at 3:11 pm

    Can we append alerts or messages int o serverlog/errorlog of postgresql using raise ? please let me know how can we append alerts manually from SQL prompt into serverlog/errorlog.

    ReplyCancel
  2. Sushil Nagarale
    October 16, 2019 at 4:04 am

    There are different types of raise in postgres
    DEBUG, LOG, INFO, NOTICE, WARNING,EXCEPTION (default)

    You can add hierarchy
    and how to enable disable raise
    example if you set to warning only warning and exceciption will be printed

    (Please don’t spam my email)
    -Sushil Nagarale

    ReplyCancel
  3. Alicia
    April 15, 2020 at 11:56 pm

    Hi,
    Thank you for this very nice page.
    I’m trying to do something like you have here:
    RAISE INFO ‘%’,’Construction of Custom Paging Parameter – DONE ‘|| clock_timestamp();
    But I’m running this in Redshift PostgreSQL.
    No matter what I try, it won’t let me output a timestamp. I have also tried now, getdate, CURRENT_TIMESTAMP, etc.
    I’m not sure what I’m doing wrong.

    ReplyCancel

Leave a Reply to Sushil Nagarale Cancel reply

CAPTCHA
Refresh

*

Anvesh Patel
Anvesh Patel

Database Engineer

April 14, 2018 3 Comments PostgreSQLAnvesh Patel, database, database research and development, dbrnd, plpgsql, Postgres Query, postgresql, PostgreSQL Administrator, PostgreSQL Error, PostgreSQL Programming, PostgreSQL Tips and Tricks, Query Debugging, RAISE EXCEPTION, RAISE INFO, RAISE NOTICE
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....