Skip to content

Instantly share code, notes, and snippets.

@belden
Last active August 29, 2015 14:17
Show Gist options
  • Select an option

  • Save belden/7d395d7bc23bf19717e1 to your computer and use it in GitHub Desktop.

Select an option

Save belden/7d395d7bc23bf19717e1 to your computer and use it in GitHub Desktop.
A postgreql function that drops itself

Playing around on the train this morning:

drop function if exists belden_redef();
create function belden_redef()
returns bool
as
$$
begin
  raise notice 'now you see me...';
  drop function if exists belden_redef();
  raise notice '...now you don''t';
  return true;
end;
$$
language plpgsql volatile;
select belden_redef();
select belden_redef();

In other words:

  1. Try to drop a function
  2. Create a function that drops itself halfway through execution
  3. Call the function
  4. Call the function again

Output:

NOTICE:  function belden_redef() does not exist, skipping
DROP FUNCTION
CREATE FUNCTION
NOTICE:  now you see me...
NOTICE:  ...now you don't
 belden_redef
--------------
 t
(1 row)

ERROR:  function belden_redef() does not exist
LINE 1: select belden_redef();
               ^
HINT:   No function matches the given name and argument types.
        You might need to add explicit type casts.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment