Skip to content

Instantly share code, notes, and snippets.

@bitifet
Last active October 6, 2016 13:11
Show Gist options
  • Save bitifet/bb02bb0aae4d7cab2c1d to your computer and use it in GitHub Desktop.
Save bitifet/bb02bb0aae4d7cab2c1d to your computer and use it in GitHub Desktop.
Allow per-schema plv8_init functions.
/* public.plv8_startup()
*
* Allow to have multiple per-schema plv8_init functions.
*
* USAGE:
*
* 1. Add the following line to the end of your postgresql.conf file:
*
* plv8.start_proc = 'public.plv8_startup'
*
* 2. Then define functions named "plv8_init" in each schema you want.
* They will be automaically called on plv8 startup as if it were
* simple plv8_init function. For more details see:
*
* http://pgxn.org/dist/plv8/doc/plv8.html#Start-up.procedure
*/
create or replace function public.plv8_startup()
returns void as
$$
// Get non-postgres schema list:
var schemas = plv8.execute(
"SELECT n.nspname AS \"name\"\n"
+ "FROM pg_catalog.pg_namespace n\n"
+ "WHERE n.nspname !~ '^pg_' AND n.nspname <> 'information_schema'\n"
);
// Check for plv8_init function in each one and execute it:
for (var i in schemas) {
var fName = schemas[i].name+".plv8_init";
try {
plv8.find_function(fName).apply(this, arguments);
} catch (e) {};
};
$$
language 'plv8'
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment