Conditional CREATE Index for Postgresql and Oracle
In order to create an index conditionally (e.g. only, if it does not exist) in postgresql or in oracle, one can use the following two idioms
Postgres
create or replace function inline_0() returns integer as '
declare v_exists integer;
begin
select into v_exists count(*) from pg_class where relname = ''acs_permissions_object_id_idx'';
if v_exists = 0 then
create index acs_permissions_object_id_idx on acs_permissions(object_id);
end if;
return null;
end;' language 'plpgsql'
select inline_0();
drop function inline_0();
Oracle
declare v_exists integer;
begin
select count(*) into v_exists from user_indexes where lower(index_name)='acs_permissions_object_id_idx';
if v_exists = 0 then
execute immediate 'create index acs_permissions_object_id_idx on acs_permissions(object_id)';
end if;
end;
/
show errors
These examples were posted by Dave Bauer OpenACS Development forum and can be used in a similar style for conditionally creating tables, etc.