Eselente ya me funciono y asi quedo:
Código SQL:
Ver originalCREATE OR REPLACE FUNCTION p_poblaingas_geoindex(CHARACTER VARYING)
RETURNS VOID AS
$BODY$
DECLARE
rec refcursor;
SQL VARCHAR;
city ALIAS FOR $1;
year_mi INTEGER;
year_ma INTEGER;
i INTEGER;
BEGIN
SQL := 'SELECT MIN(periodo) AS min,MAX(periodo) AS max
FROM servcon_poblaingas_'||city;
OPEN rec FOR EXECUTE SQL;
FETCH rec INTO year_mi,year_ma;
i := year_mi;
LOOP
EXECUTE 'SELECT AddGeometryColumn ('||QUOTE_LITERAL('servcon_poblaingas_'||city||'_'||i)||','||QUOTE_LITERAL('geom')||',4326,'||QUOTE_LITERAL('POINT')||',2)';
EXECUTE 'UPDATE servcon_poblaingas_'||city||'_'||i||' set geom = GeometryFromText(''POINT(''||CX||'' ''||CY||'')'',4326)';
EXECUTE 'CREATE INDEX idx_poblaingas_'||city||'_'||i||' ON servcon_poblaingas_'||city||'_'||i||' USING GIST (geom)';
i := i + 1;
EXIT WHEN i > year_ma;
END LOOP;
RETURN;
END;
$BODY$
LANGUAGE 'plpgsql' VOLATILE;