-
Notifications
You must be signed in to change notification settings - Fork 849
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create aggregate functions only once to avoid dependency issues #640
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
-- This file is meant to contain aggregate functions that need to be created only | ||
-- once and not recreated during updates. | ||
-- There is no CREATE OR REPLACE AGGREGATE which means that the only way to replace | ||
-- an aggregate is to DROP then CREATE which is problematic as it will fail | ||
-- if the previous version of the aggregate has dependencies. | ||
-- NOTE that WHEN CREATING NEW FUNCTIONS HERE you should also make sure they are | ||
-- created in an update script so that both new users and people updating from a | ||
-- previous version get the new function | ||
|
||
|
||
--This aggregate returns the "first" element of the first argument when ordered by the second argument. | ||
--Ex. first(temp, time) returns the temp value for the row with the lowest time | ||
CREATE AGGREGATE first(anyelement, "any") ( | ||
SFUNC = _timescaledb_internal.first_sfunc, | ||
STYPE = internal, | ||
COMBINEFUNC = _timescaledb_internal.first_combinefunc, | ||
SERIALFUNC = _timescaledb_internal.bookend_serializefunc, | ||
DESERIALFUNC = _timescaledb_internal.bookend_deserializefunc, | ||
PARALLEL = SAFE, | ||
FINALFUNC = _timescaledb_internal.bookend_finalfunc, | ||
FINALFUNC_EXTRA | ||
); | ||
|
||
--This aggregate returns the "last" element of the first argument when ordered by the second argument. | ||
--Ex. last(temp, time) returns the temp value for the row with highest time | ||
CREATE AGGREGATE last(anyelement, "any") ( | ||
SFUNC = _timescaledb_internal.last_sfunc, | ||
STYPE = internal, | ||
COMBINEFUNC = _timescaledb_internal.last_combinefunc, | ||
SERIALFUNC = _timescaledb_internal.bookend_serializefunc, | ||
DESERIALFUNC = _timescaledb_internal.bookend_deserializefunc, | ||
PARALLEL = SAFE, | ||
FINALFUNC = _timescaledb_internal.bookend_finalfunc, | ||
FINALFUNC_EXTRA | ||
); | ||
|
||
-- This aggregate partitions the dataset into a specified number of buckets (nbuckets) ranging | ||
-- from the inputted min to max values. | ||
CREATE AGGREGATE histogram (DOUBLE PRECISION, DOUBLE PRECISION, DOUBLE PRECISION, INTEGER) ( | ||
SFUNC = _timescaledb_internal.hist_sfunc, | ||
STYPE = INTERNAL, | ||
COMBINEFUNC = _timescaledb_internal.hist_combinefunc, | ||
SERIALFUNC = _timescaledb_internal.hist_serializefunc, | ||
DESERIALFUNC = _timescaledb_internal.hist_deserializefunc, | ||
PARALLEL = SAFE, | ||
FINALFUNC = _timescaledb_internal.hist_finalfunc, | ||
FINALFUNC_EXTRA | ||
); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -246,3 +246,42 @@ DROP FUNCTION IF EXISTS _timescaledb_internal.drop_trigger_on_all_chunks(INTEGER | |
DROP FUNCTION IF EXISTS _timescaledb_internal.get_general_trigger_definition(regclass); | ||
DROP FUNCTION IF EXISTS _timescaledb_internal.get_trigger_definition_for_table(INTEGER, text); | ||
DROP FUNCTION IF EXISTS _timescaledb_internal.need_chunk_trigger(int, oid); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you explain the update strategy a bit more? I'm a little confused why you have only changed the 0.4.2-->0.5.0 update script. Say I'm running version 0.8.0 (some arbitrary version in between 0.5.0 and 0.11.0), then these aggregates are already (incorrectly) installed. How am I going to get your fix? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, should have been clearer about that. Maybe I should add this as comment in the commit. Aggregates are not ever incorrectly installed, at least that is not what this fixes. I changed that specific update file because that is when histogram was introduced. So if you were to install a version prior to 0.5.0, you would not CREATE AGGREGATE histogram. It is a little ugly, but I also have to copy-paste code to create all the dependency functions. This is because update scripts are run before 0.5.0 changed sql scripts and so if I were to just do CREATE AGGREGATE, the state and transition functions inside the aggregate would not have been created and would not be recognized. I have not modified any update scripts for |
||
-- Adding this in the update script because aggregates.sql is not rerun in case of an update | ||
CREATE OR REPLACE FUNCTION _timescaledb_internal.hist_sfunc (state INTERNAL, val DOUBLE PRECISION, MIN DOUBLE PRECISION, MAX DOUBLE PRECISION, nbuckets INTEGER) | ||
RETURNS INTERNAL | ||
AS '@MODULE_PATHNAME@', 'hist_sfunc' | ||
LANGUAGE C IMMUTABLE PARALLEL SAFE; | ||
|
||
CREATE OR REPLACE FUNCTION _timescaledb_internal.hist_combinefunc(state1 INTERNAL, state2 INTERNAL) | ||
RETURNS INTERNAL | ||
AS '@MODULE_PATHNAME@', 'hist_combinefunc' | ||
LANGUAGE C IMMUTABLE PARALLEL SAFE; | ||
|
||
CREATE OR REPLACE FUNCTION _timescaledb_internal.hist_serializefunc(INTERNAL) | ||
RETURNS bytea | ||
AS '@MODULE_PATHNAME@', 'hist_serializefunc' | ||
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE; | ||
|
||
CREATE OR REPLACE FUNCTION _timescaledb_internal.hist_deserializefunc(bytea, INTERNAL) | ||
RETURNS INTERNAL | ||
AS '@MODULE_PATHNAME@', 'hist_deserializefunc' | ||
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE; | ||
|
||
CREATE OR REPLACE FUNCTION _timescaledb_internal.hist_finalfunc(state INTERNAL, val DOUBLE PRECISION, MIN DOUBLE PRECISION, MAX DOUBLE PRECISION, nbuckets INTEGER) | ||
RETURNS INTEGER[] | ||
AS '@MODULE_PATHNAME@', 'hist_finalfunc' | ||
LANGUAGE C IMMUTABLE PARALLEL SAFE; | ||
|
||
-- This aggregate partitions the dataset into a specified number of buckets (nbuckets) ranging | ||
-- from the inputted min to max values. | ||
CREATE AGGREGATE histogram (DOUBLE PRECISION, DOUBLE PRECISION, DOUBLE PRECISION, INTEGER) ( | ||
SFUNC = _timescaledb_internal.hist_sfunc, | ||
STYPE = INTERNAL, | ||
COMBINEFUNC = _timescaledb_internal.hist_combinefunc, | ||
SERIALFUNC = _timescaledb_internal.hist_serializefunc, | ||
DESERIALFUNC = _timescaledb_internal.hist_deserializefunc, | ||
PARALLEL = SAFE, | ||
FINALFUNC = _timescaledb_internal.hist_finalfunc, | ||
FINALFUNC_EXTRA | ||
); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,7 +28,7 @@ to consider. | |
commands that change or remove objects. In some cases, | ||
modifications of metadata are also necessary. | ||
2. `DROP FUNCTION` needs to be idempotent. In most cases that means | ||
commands should have an `IF NOT EXISTS` clause. The reason is that | ||
commands should have an `IF EXISTS` clause. The reason is that | ||
some modfiles might try to, e.g., `DROP` functions that aren't | ||
present because they only exist in an intermediate version of the | ||
database, which is skipped over. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add a 5th bullet to this list with the same note that you have in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated and rebased |
||
|
@@ -39,6 +39,11 @@ to consider. | |
4. The creation of new metadata tables need to be part of modfiles, | ||
similar to `ALTER`s of such tables. Otherwise, later modfiles | ||
cannot rely on those tables being present. | ||
5. When creating a new aggregate, the `CREATE` statement should be | ||
added to both aggregate.sql AND an update file. aggregate.sql is | ||
run once when TimescaleDB is installed so adding a definition in | ||
an update file is the only way to ensure that upgrading users get | ||
the new function. | ||
|
||
Note that modfiles that contain no changes need not exist as a | ||
file. Transition modfiles must, however, be listed in the | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a weird extra return right here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The line was getting too long that is why I split this to multiple lines.
My thought was that if we later add more parts to the build pipeline, we can concat the string form the next line