Skip to content

Commit

Permalink
create string_agg in slony1_base.sql not slony1_funcs.sql
Browse files Browse the repository at this point in the history
If the CREATE AGGREGATE is in slony1_funcs.sql it will fail on an update functions
since the aggregate already exists.  As part of the upgrade we will check
to see if we need to create the aggregate.
  • Loading branch information
ssinger committed Aug 9, 2013
1 parent 8e63eea commit d7a19d0
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 12 deletions.
33 changes: 33 additions & 0 deletions src/backend/slony1_base.sql
Expand Up @@ -723,6 +723,39 @@ comment on column @NAMESPACE@.sl_components.co_eventtype is 'what kind of event
comment on column @NAMESPACE@.sl_components.co_event is 'which event have I started processing?';



--
-- we create a function + aggregate for string_agg to aggregate strings
-- some versions of PG (ie prior to 9.0) don't support this
CREATE OR replace function @NAMESPACE@.agg_text_sum(txt_before TEXT, txt_new TEXT) RETURNS TEXT AS
$BODY$
DECLARE
c_delim text;
BEGIN
c_delim = ',';
IF (txt_before IS NULL or txt_before='') THEN
RETURN txt_new;
END IF;
RETURN txt_before || c_delim || txt_new;
END;
$BODY$
LANGUAGE plpgsql;
comment on function @NAMESPACE@.agg_text_sum(text,text) is
'An accumulator function used by the slony string_agg function to
aggregate rows into a string';
--
-- create a string_agg function in the slony schema.
-- PG 8.3 does not have this function so we make our own
-- when slony stops supporting PG 8.3 we can switch to
-- the PG 9.0+ provided version of string_agg
--
CREATE AGGREGATE @NAMESPACE@.string_agg(text) (
SFUNC=@NAMESPACE@.agg_text_sum,
STYPE=text,
INITCOND=''
);


-- ----------------------------------------------------------------------
-- Last but not least grant USAGE to the replication schema objects.
-- ----------------------------------------------------------------------
Expand Down
19 changes: 7 additions & 12 deletions src/backend/slony1_funcs.sql
Expand Up @@ -5487,7 +5487,13 @@ create table @NAMESPACE@.sl_components (
alter table @NAMESPACE@.sl_log_2
enable replica trigger apply_trigger;
end if;

if not exists (select 1 from information_schema.routines where routine_schema = '_@CLUSTERNAME@' and routine_name = 'string_agg') then
CREATE AGGREGATE @NAMESPACE@.string_agg(text) (
SFUNC=@NAMESPACE@.agg_text_sum,
STYPE=text,
INITCOND=''
);
end if;
return p_old;
end;
$$ language plpgsql;
Expand Down Expand Up @@ -6262,14 +6268,3 @@ LANGUAGE plpgsql;
comment on function @NAMESPACE@.agg_text_sum(text,text) is
'An accumulator function used by the slony string_agg function to
aggregate rows into a string';
--
-- create a string_agg function in the slony schema.
-- PG 8.3 does not have this function so we make our own
-- when slony stops supporting PG 8.3 we can switch to
-- the PG 9.0+ provided version of string_agg
--
CREATE AGGREGATE @NAMESPACE@.string_agg(text) (
SFUNC=@NAMESPACE@.agg_text_sum,
STYPE=text,
INITCOND=''
);

0 comments on commit d7a19d0

Please sign in to comment.