55 Commits

Author SHA1 Message Date
Matvey Arye
0584c47c7a Created chunk_get_or_create in sql with an SPI connector in C 2017-06-22 20:15:38 +02:00
Olof Rensfelt
2a01ebca61 Ensure that chunks are aligned.
Previously, chunks could end up unaligned betweeen partitions if the chunk_time_interval
was updated between chunk creations. Now there is a check when a chunk is created whether there is
a chunk matching the time point in another partition and if there is, the new chunk is created
with the same interval.
2017-06-15 13:19:29 -04:00
Matvey Arye
73622bf1eb Fix default index creation duplication of indexes
Previously there was a bug causing indexes for partition, time to
be created twice.  This fixes #77.
2017-06-08 14:25:32 -04:00
Matvey Arye
c8872fec47 Fix command-tag for COPY and INSERT
Previously the count returned by insert and copy was wrong because
the count was reset on every execute. But, often there is an execute
in the middle of an insert (i.e. to create a chunk). This fixes the
logic to reset the count only at the start of the top-level statement.
Fixes #64
2017-06-08 14:03:14 -04:00
Matvey Arye
bfe58b61f7 Refactor towards supporting version upgrades
Clean up the table schema to get rid of legacy tables and functionality
that makes it more difficult to provide an upgrade path.

Notable changes:
* Get rid of legacy tables and code
* Simplify directory structure for SQL code
* Simplify table hierarchy: remove root table and make chunk tables
* inherit directly from main table
* Change chunk table suffix from _data to _chunk
* Simplify schema usage: _timescaledb_internal for internal functions.
* _timescaledb_catalog for metadata tables.
* Remove postgres_fdw dependency
* Improve code comments in sql code
2017-06-08 13:55:05 -04:00
Matvey Arye
db01c84c2d Make time-bucket function parallel safe
There was no reason for it not to be parallel safe before.
2017-06-02 16:15:51 -04:00
Erik Nordström
18db11c7cf Fix timestamp test
The timestamp test broke with Postgres 9.6.3, because that version
contained an accuracy fix for the `to_timestamp()` function, giving
slightly different results for some timestamp values. This fixes the
timestamp test to expect the correct `to_timestamp()` output as of
version 9.6.3 of Postgres.
2017-06-02 18:07:13 +02:00
Matvey Arye
97bbb5979e Make constraint exclusion work with non-text partition keys
Fixes #66
2017-06-02 10:19:16 -04:00
Matvey Arye
f2b42ebe0e Fix problems with partitioning logic for padded fields
Padded fields such as character(20) did not work correctly before
because of differences between type output and coercion/casting.
2017-06-02 10:12:38 -04:00
Olof Rensfelt
997029a86d if_not_exist flag to create_hypertable now works on hypertables with data as well 2017-06-01 16:51:37 +02:00
Erik Nordström
347a8bdacf Reference the correct column when scanning partition epochs
This fixes a bug that caused the wrong column to be referenced when
scanning the partition_epoch table. The bug did not usually manifest
itself because the referenced (hypertable) ID was typically
incremented in unison with the desired (partition epoch) ID.
2017-05-31 14:55:54 +02:00
Olof Rensfelt
8ccc8cc156 Add if_not_exists flag to create_hypertable()
Previously create_hypertable() would throw an error when called on
an already existing hypertable. This can now be skipped by setting
if_not_exists argument to true.
2017-05-24 18:37:24 +02:00
Matvey Arye
4638688e5b Improve GUC handling
This PR improves GUC handling to be more inline with the Postgres GUC
system.
2017-05-22 17:04:52 -04:00
Matvey Arye
cedcafc959 Remove setup_timescaledb() and fix pg_dump/pg_restore.
This PR removes the need to run setup_timescaledb. It also fixes
pg_dump and pg_restore. Previously, the database would restore in
a broken state because trigger functions were never attached to
meta tables (since setup_timescaledb() was not run). However, attaching
those triggers at extension creation also causes problems since the data
copy happens after extension creation but we don't want triggers fired
on the data restored (that could cause duplicate rows, for example).

The solution to this chicken-and-egg problem in this PR is to have
a special configuration (GUC) variable `timescaledb.restoring` that,
if 'on', would prevent the extension from attaching triggers at
extension creation. Then, after restoration, you'd call
restore_timescaledb() to attach the triggers and unset the GUC above.
This procedure is documented in the README as part of this PR.
2017-05-22 16:12:21 -04:00
Matvey Arye
fc4ddd65b5 Fix bug with dropping chunks on tables with indexes 2017-05-20 15:54:46 -04:00
Matvey Arye
32215ff6c5 Add default indexes for hypertables
Changed create_hypertable to add time DESC and partitioning_column, time DESC
indexes by default (the latter only if partitioning column not null).
Indexes only created if there is no index on time and
partitioning_column, time respectively. Index creation can be turned off
with the create_default_indexes parameter to create_hypertable (default true).
2017-05-20 15:54:46 -04:00
Matvey Arye
b2900f9f85 Disable query optimization on regular tables (non-hypertables)
This PR disables query optimizations on regular tables by default.
The option timescaledb.optimize_plain_tables = 'on' enables them
again. timescaledb.disable_optimizations = 'on' disables all
optimizations (note the change from 'true' to 'on').
2017-05-20 15:31:47 -04:00
Matvey Arye
f227db4b52 Fixes command tag return for COPY on hypertables.
Previously, COPY on hypertables always returned 'COPY 0' in the
command tag. Now, returns the correct count. Fixes #40.
2017-05-16 16:43:47 -04:00
Matvey Arye
662be94d69 Add the first(value, time),last(value, time) aggregates
Add the first and last aggregate functions, which allows for getting
the first and last value, by time, per group. Addresses issue #30.
2017-05-16 09:46:49 -04:00
Matvey Arye
384a8fb0d1 Add regression tests for deleted unit tests 2017-05-16 09:04:13 -04:00
Olof Rensfelt
13d3acbcfa Fix bug with alter table add/drop column if exists
Previously, an ALTER TABLE ADD COLUMN IF NOT EXISTS on a hypertable
caused an error if the column already did exist. The reversed problem
with DROP COLUMN is also fixed. Resolves issue #42.
2017-05-15 21:21:33 +02:00
Matvey Arye
f960c24cd4 Fix bug with querying a row as a composite type
Previously a query like `SELECT "1dim" FROM "1dim"` on a hypertable would
fail since the change_table_walker did not properly change the types on
whole row vars. That is now fixed.
2017-05-12 13:39:39 -04:00
Matvey Arye
c3f930f6c8 Add time_bucket functions
Adds functions to bucket timestamps. These functions are more
powerful versions of date_trunc, since they can take arbitrary
intervals.
2017-05-03 15:40:49 -04:00
Matvey Arye
b128ac2a87 Fix bug with INSERT INTO...SELECT
Previously, INSERT INTO...SELECT FROM hypertable was broken. This was
caused by incorrectly tracking the commandType in change_name_walker.
Fixes #24.
2017-05-03 13:37:23 -04:00
Matvey Arye
e20edf890c Add better error checking for index creation.
This PR adds more regression tests for index creation and tests for more
user-errors. Significantly, it checks for the presence of both the time
and spaced-partition columns in unique indexes. This is needed because
Timescale cannot guarantee uniqueness if colliding rows don't land in the
same chunk. Fixes #29.
2017-05-03 13:21:32 -04:00
Robin Thomas
72f754a576 use PostgreSQL's own hash_any function as default partfunc;
remove all murmur3-related source code. Alter regression tests
to reflect new hash values for inputs, and a slightly different
set of input data to ensure that sufficient chunks and partitions
are tested. Some changes to .sh scripts in sql/setup that seem
to be used only to power the "unit tests", which I cannot
yet run successfully.
2017-04-29 18:31:44 -04:00
Olof Rensfelt
d070514f2a Fixed time chunk ranges.
To allow better caching and reduce the need for locking, chunks now
have fixed start and stop times set when the chunk is created.
2017-04-23 19:45:46 -04:00
Matvey Arye
b134cdffd8 Add test for COPY TO 2017-04-12 12:53:58 -04:00
Matvey Arye
a78f1570ba Remove dblink dependency.
The dblink extension is blacklisted by some cloud-hosting providers and
is an unnecessary dependency for single-node operation. Since we don't plan
to use dblink to implement clustering this PR removes the dependency.
2017-04-12 11:14:54 -04:00
Matvey Arye
83346079d7 Make optimization of date_trunc more general.
Handle case where there is an additional constraint on time.
This makes the pathkeys for the IndexPath incompatible with
upper level ordering. Since we know the appropriate transform
for those pathkeys already, apply it to those paths too.
2017-04-05 16:35:14 -04:00
Matvey Arye
ea0c97d590 Optimize queries with date_trunc in ORDER BY and a LIMIT.
Common time-series rollups are of the form SELECT date_trunc(const,
time) as t, aggregates... GROUP BY t ORDER BY t DESC LIMIT const2.
Previously, Postgres would not optimize such queries because it
could not match date_trunc(const, time) to any indexes on time, since
it has no way to know that a sort on time is always a valid sort on
date_trunc(const, time).

This commit implements this optimization for date_trunc but it could
apply to a wider range of functions, to be implemented later.
2017-04-05 16:35:14 -04:00
Erik Nordström
9023de61db Cleanup tests and make errors less verbose
Previously, each test set their own (although mostly the same)
configuration for log output and error verbosity. This is now set
globally in the test runner so that tests only need to set these
configuration parameters if they need to override the defaults.  The
log verbosity is also reduced so that errors aren't generated with the
line number of the source file that output the error. Line numbers in
the output can break tests when upgrading to a new PostgreSQL version
that outputs a different line number.
2017-03-23 22:56:03 +01:00
Erik Nordström
c60b08e83a Fix DROP EXTENSION
DROP EXTENSION didn't properly reset caches and other saved state
causing various errors related to bad state when the extension was
dropped and/or recreated later.

This patch adds functionality to track the state of the extension and
also signals DROP EXTENSION to other backends that might be running,
allowing them to reset their internal extension state.
2017-03-22 19:43:40 +01:00
Erik Nordström
473d743927 Cleanup insert state on error
If an error is generated in any of the insert triggers, the insert
state kept during a batch insert might be left in an undefined state,
breaking the next insert. This patch makes sure errors are captured in
the insert triggers so that the state can be cleaned up.
2017-03-22 15:20:45 +01:00
Rob Kiefer
cb90eef350 Rename setup_db() and fix port for local connections
If a user attempts to setup a database while not connecting using
the network, port is NULL and thus fails constraint checks. Instead,
we now use the default Postgres port of 5432 when this happens.

Also, setup_db() is renamed to setup_timescaledb() for clarity in
the presence of other extensions.
2017-03-22 09:26:03 -04:00
Matvey Arye
e0d63e915c Add query tests for ORDER BY time DESC queries LIMIT queries
These tests show that non-aggregated queries that have an ORDER BY time
DESC LIMIT x structure perform well. Postgres processes such queries by
using a time DESC index and then performs a MergeAppend. Some further
optimization to avoid touching unnecessary tables due to constraints
can be added but they are not a priority. Much worse is that these
optimizations do not work when grouping by time (either through
integer division or using date_trunc).

In order to use the non-aggregated query optimizations we had
to create an index with time as the leading field and no
WHERE clause on the index.
2017-03-21 11:36:47 -04:00
Matvey Arye
012fe87499 Forbid users from creating custom triggers on hypertables 2017-03-14 09:27:59 -04:00
Matvey Arye
1f13354bf9 Make the planner use metadata cache
Previously, the planner used a direct query via the SPI interface to
retrieve metadata info needed for query planner functions like query
rewriting. This commit updates the planner to use our caching system.
This is a performance improvement for pretty much all operations,
both data modifications and queries.

For hypertables, this added a cache keyed by the main table OID and
added negative entries (because the planner often needs to know if a
table is /not/ a hypertable).
2017-03-13 14:18:33 -04:00
Matvey Arye
fbdcab186f Allow non-superusers to work with the db (but also mess up the catalog)
Previous to this commit non-superusers could not do anything inside
a database with the timescale extension loaded. Now, non-superuser
can create their own hypertables and work inside the db. There are
two big caveats:
      1) All users have read/write permissions to the timescaledb
      catalog.
      2) Permission changes applied to the main tables are not
      propagated to the associated tables.
2017-03-13 10:11:37 -04:00
Erik Nordström
3c1e9cc4ce Refactor insert path to use triggers instead of temp copy table.
This patch refactors the insert path to use insert triggers
instead of a temporary copy table. The copy table previously
served as an optimization for big batches where the cost of
inserting tuple-by-tuple into chunks was amortized by inserting
all tuples for a specific chunk in one insert. However, to avoid
deadlocks the tuples also had to inserted in a specific chunk
order, requiring adding an index to the copy table.

With trigger insertion, tuples are instead collected over a batch
into a sorting state, which is sorted in an "after" trigger. This
removes the overhead of the copy table and index. It also provides
a fast-path for single-tuple batches that avoids doing sorting
altogether.
2017-03-10 19:46:43 +01:00
Olof Rensfelt
de3370deb8 Add tests for rename and drop hypertables 2017-03-10 14:21:51 +01:00
Rob Kiefer
3760993519 Renames setup_single_node() to setup_db() 2017-03-06 15:36:29 -05:00
Erik Nordström
11938ebb08 Default to asking for number of partitions when partitioning column set.
Since create_hypertable() allows you to optionally specify a
partitioning column, it makes sense to default to one partition when
no column is specified and asking for the number of partitions when a
column is specified and the number of partitions is not (instead of
defaulting to one).

This patch also changes the order and type of partitioning-related
input arguments to create_hypertable() so that the number of
partitions can easily be specified alongside the partitioning column
and without type casting.
2017-03-06 20:26:02 +01:00
Rob Kiefer
ee3ad4678d Rename iobeamdb to TimescaleDB 2017-03-06 11:06:49 -05:00
Rob Kiefer
2093b0ac6f Fix verbosity level from normal to default 2017-03-02 13:50:23 -05:00
Rob Kiefer
5478231f3e Reduce verbosity to remove line nums from output. 2017-03-02 13:09:33 -05:00
enordstr NA
1787793b64 Merged in enordstr/backend-database/enordstr/close-chunks-at-end-of-insert (pull request #89)
Fix chunk-related deadlocks.

Approved-by: ci-vast
Approved-by: Matvey Arye
Approved-by: RobAtticus NA
2017-02-23 20:52:44 +00:00
Erik Nordström
e3fabf993a Fix chunk-related deadlocks.
This patch fixes two deadlock cases.

The first case occurred as a result of taking partition and chunk
locks in inconsistent orders. When creating the first chunk C1
in a table, concurrent INSERT workers would race to create
that chunk. The result would be that the transactions queue up on
the partition lock P, effectively serializing these transactions.
This would lead to these concurrent transactions to insert
at very different offsets in time, one at a time. At some point
in the future, some n'th transaction Tn queued up on P would get
that lock as the preceeding inserters T1-(n-1) finish their inserts
and move on to their next batches. When Tn finally holds P, one of
the preceeding workers starts a new transaction that finds that it
needs to close C1, grabbing a lock on C1 and then on P. However,
it will block on P since Tn already holds P. Tn will also believe
it needs to close C1, thus trying to grab a lock on C1, but will
block, causing a deadlock.

The second case can occur on multi-partition hypertables. With
multiple partitions there are more than one open-ended chunk
at a time (one for each partition). This leads to a deadlock case
when two processes try to close (and thus lock) the chunks in
different order. For instance process P1 closes chunk C1 and then
C2, while process P2 locks in order C2 and C1.

The fix for the first case is to remove the partition lock
altogether. As it turns out, this lock is not needed.
Instead, transactions can race to create new chunks, thus causing
conflicts. A conflict in creating a new chunk can safely be
ignored and it also avoids taking unecessary locks. Removing the
partition lock also avoids the transaction serialization that
happens around this lock, which is especially bad for long-running
transactions (e.g., big INSERT batches).

The fix for the second multi-partition deadlock case is to always
close chunks in chunk ID order. This requires closing chunks at
the end of a transaction, once a transaction knows all the chunks
it needs to close. This also has the added benefit of reducing the
time a transaction holds exclusive locks on chunks, potentially
improving insert performance.
2017-02-23 15:41:57 +01:00
Olof Rensfelt
549b69ea81 Block renaming of hypertables 2017-02-23 13:37:45 +01:00
Olof Rensfelt
220e938930 Make all tests use create_single_db.sql 2017-02-22 15:18:56 +01:00