Skip to content

Instantly share code, notes, and snippets.

@danielleevandenbosch
Created November 1, 2023 13:53
Show Gist options
  • Save danielleevandenbosch/709a9f2c216a56034118ebeba10c4eb8 to your computer and use it in GitHub Desktop.
Save danielleevandenbosch/709a9f2c216a56034118ebeba10c4eb8 to your computer and use it in GitHub Desktop.
Live Template for PostgreSQL. This allows you to type func and it gives you a really good template for a function in postgres using datagrip
CREATE OR REPLACE FUNCTION $funcname$ () RETURNS VOID LANGUAGE plpgsql VOLATILE AS
$$$
/*
==================================================
Author: $user$
Created At: $date$
Where Used: $var$
==================================================
*/
DECLARE
--===========try catch vars===========
_returned_sqlstate TEXT;
_message_text TEXT;
_pg_exception_detail TEXT;
_pg_exception_hint TEXT;
_pg_exception_context TEXT;
--====================================
--============== DECLARE VARIABLES ===============
--============== END VARIABLES ===================
BEGIN
--============= SET VARIABLE VALUES ==============
--============= END VARIABLE VALUES ==============
--/////////////////// BODY ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
--try code goes here
--/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
--===========CATCH CODE FOR ERRORS=============
EXCEPTION WHEN others THEN
GET STACKED DIAGNOSTICS
_returned_sqlstate = RETURNED_SQLSTATE
, _message_text = MESSAGE_TEXT
, _pg_exception_detail = PG_EXCEPTION_DETAIL
, _pg_exception_hint = PG_EXCEPTION_HINT
, _pg_exception_context = PG_EXCEPTION_CONTEXT;
_pg_exception_hint = _pg_exception_hint; --TODO add your parameters here.
PERFORM it.pg_func_exceptions_insert (
_returned_sqlstate
, _message_text
, _pg_exception_detail
, _pg_exception_hint
, _pg_exception_context
, current_query()
, FALSE --false -> email will be sent to DBA upon function failure
);
RAISE EXCEPTION E'Got exception:
state : %
message: %
detail : %
hint : %
context: %', _returned_sqlstate, _message_text, _pg_exception_detail, _pg_exception_hint, _pg_exception_context;
--============END CATCH CODE=========================
END;
$$$;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment