Last active
August 29, 2015 14:08
-
-
Save zpl/9d076cf8badc106616d2 to your computer and use it in GitHub Desktop.
Postgres in-query debug messages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
example: | |
# | |
WITH RECURSIVE t(n) AS ( | |
VALUES (debug('init: ' || 1,1)) | |
UNION ALL | |
SELECT debug('recursion: ' || n+1 ,n+1) | |
FROM t | |
WHERE n < 100) | |
SELECT sum(n), | |
debug('final query') | |
FROM t; | |
NOTICE: Debug: init: 1 | |
NOTICE: Debug: recursion: 2 | |
NOTICE: Debug: recursion: 3 | |
NOTICE: Debug: recursion: 4 | |
NOTICE: Debug: recursion: 5 | |
NOTICE: Debug: recursion: 6 | |
NOTICE: Debug: recursion: 7 | |
NOTICE: Debug: recursion: 8 | |
NOTICE: Debug: recursion: 9 | |
NOTICE: Debug: recursion: 10 | |
NOTICE: Debug: final query | |
sum | debug | |
-----+------- | |
55 | | |
(1 row) | |
*/ | |
CREATE FUNCTION debug(msg TEXT) RETURNS void AS $$ | |
BEGIN | |
RAISE NOTICE 'Debug: %', msg; | |
RETURN; | |
END; | |
$$ LANGUAGE plpgsql; | |
CREATE FUNCTION debug(msg TEXT, value ANYELEMENT) RETURNS ANYELEMENT AS $$ | |
BEGIN | |
RAISE NOTICE 'Debug: %', msg; | |
RETURN value; | |
END; | |
$$ LANGUAGE plpgsql; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment