Skip to content

Instantly share code, notes, and snippets.

@edoardoc
Created December 10, 2021 16:53
Show Gist options
  • Save edoardoc/2d481a3e54cff41a478ac21b799e3355 to your computer and use it in GitHub Desktop.
Save edoardoc/2d481a3e54cff41a478ac21b799e3355 to your computer and use it in GitHub Desktop.
PL/pgSQL Template to run inline a complicated function and get results in console
-- PL/pgSQL Template to run inline a complicated function and get results in console
DO $$
DECLARE
-- ADD QUERY DECLARATION HERE:
filteringIdOfSomeKind UUID;
-- ADD QUERY DECLARATION HERE -- END!
t timestamptz;
target record;
BEGIN
t := clock_timestamp();
filteringIdOfSomeKind := '8cae6ff8-b532-4749-94b4-ffc51c933b86';
RAISE NOTICE 'HELLO THERE filteringIdOfSomeKind = %', filteringIdOfSomeKind;
RAISE NOTICE '----------------------';
FOR target IN
-- ADD CORE PART OF QUERY
SELECT random()*100 as theId, * FROM generate_series(1,5)
-- ADD CORE PART OF QUERY -- END! (REMOVE LAST ;)
LOOP
-- the only way to address the output is to fetch single fields as this:
-- RAISE NOTICE 'fieldNameNo1= %', 'target.fieldNameNo1';
-- RAISE NOTICE 'fieldNameNo2= %', 'target.fieldNameNo2';
-- ...
-- or just print the whole thing TODO: add some formatting
RAISE NOTICE '%', target;
END LOOP;
RAISE NOTICE '----------------------';
RAISE NOTICE 'END BLOCK time spent=%', clock_timestamp() - t;
END; $$;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment