37 Commits

Author SHA1 Message Date
Matvey Arye
26965826f4 Move index and constraints drop handling to event trigger
This Fixes at least two bugs:

1) A drop of a referenced table used to drop the associated
FK constraint but not the metadata associated with the constraint.
Fixes #43.

2) A drop of a column removed any indexes associated with the column
but not the metadata associated with the index.
2018-02-09 10:15:07 -05:00
Erik Nordström
d6baccb9d7 Improve tablespace handling, including blocks for DROP and REVOKE
This change improves the handling of tablespaces as follows:

- Add if_not_attached / if_attached options to attach_tablespace() and
  detach_tablespace(), respectively
- Block DROP tablespace if it is still attached to a table
- Block REVOKE if it means the table owner no longer has CREATE
  permissions on an attached tablespace
- Make error messages follow the PostgreSQL style guide
2018-02-05 23:16:20 +01:00
Erik Nordström
b9a6f890a1 Handle DROP SCHEMA for hypertable and chunk schemas
Dropping a schema that a hypertable depends on should clean up
dependent metadata. There are two schemas that matter for hypertables:
the hypertable's schema and the associated schema where chunks are
stored.

This change deals with the above as follows:

- If the hypertable schema is dropped, the hypertable and all chunks
should be deleted as well, including metadata.
- If an associated schema is dropped, the hypertables that use that
associated schema will have their associated schemas reset to the
internal schema.
- Even if no hypertable currently uses the dropped schema as their
associated schema, there might be chunks that reside in the dropped
schema (e.g., if the associated schema was changed for their
hypertables), so those chunks should have the metadata deleted.
2018-02-05 23:07:55 +01:00
Rob Kiefer
7fa6a0416e Fix several Windows compile errors and warnings
Previously stdint.h was not included on Windows so INT16_MAX and
friends were not defined. Additionally, having tablespace_attach
with PG_FUNCTION_ARGS in the header file caused issues during
linking, so a direct call version of the function is now exported
for others to use instead of the PG_FUNCTION_ARGS version.

Two minor warnings regarding not having a return in all cases are
also addressed.
2018-01-30 13:05:45 -05:00
Erik Nordström
fa19a54a88 Handle deletes on metadata objects via native catalog API
Deletes on metadata in the TimescaleDB catalog has so far been a mix
of native deletes using the C-based catalog API and SQL-based DELETE
statements that CASCADEs.

This mixed environment is confusing, and SQL-based DELETEs do not
consistently clean up objects that are related to the deleted
metadata.

This change moves towards A C-based API for deletes that consistently
deletes also the dependent objects (such as indexes, tables and
constraints).  Ideally, we should prohobit direct manipulation of
catalog tables using SQL statements to avoid ending up in a bad state.

Once all catalog manipulations happend via the native API, we can also
remove the cache invalidation triggers on the catalog tables.
2018-01-26 21:39:12 +01:00
Erik Nordström
6e011d12fb Refactor hypertable-related API functions
This is a continuation of prior efforts to refactor API functions in C
to:

- improve usage of proper error codes
- use error messages that better conform with the PostgreSQL standard.
- improve security by avoiding that lots of code run under SECURITY DEFINER
- move towards doing all metadata updates using a consistent catalog API

Most importantly, `create_hypertable()` has been refactored in C,
which simplifies a lot of code that previously required
upcalls/downcalls between C code and plpgsql code, or duplicated
functionality between the two environments.
2018-01-26 18:42:20 +01:00
Erik Nordström
71962b86ec Refactor dimension-related API functions
The functions for adding and updating dimensions have been refactored
in C to:

- improve usage of proper error codes
- make messages that better conform with the PostgreSQL standard.
- improve security by avoiding that lots of code run under SECURITY DEFINER

A new if_not_exists option has also been added to add_dimension() and
a the number of partitions can now be set using the new
set_number_partitions() function.

A bug in the validation of smallint time intervals has been fixed. The
previous code didn't check for intervals > 0 and smallint intervals
accepted values up to UINT16_MAX instead of INT16_MAX.
2018-01-25 19:02:34 +01:00
Erik Nordström
d135256ed7 Spread chunk indexes across tablespaces like chunks
Currently, chunk indexes are always created in the tablespace of the
index on the main table (which could be none/default one), even if the
chunks themselves are created in different tablespaces. This is
problematic in a multi-disk setting where each disk is a separate
tablespace where chunks are placed. The chunk indexes might exhaust
the space on the common (often default tablespace) which might not
have a lot of disk space. This also prohibits the database, including
index storage to grow by adding new tablespaces.

Instead, chunk indexes are now created in the "next" tablespace after
that of their chunks to both spread indexes across tablespaces and
avoid colocating indexes with their chunks (for I/O throughput
reasons). To optionally avoid this spreading, one can pin chunk
indexes to a specific tablespace by setting an explicit tablespace on
a main table index.
2018-01-19 10:48:45 +01:00
Erik Nordström
b6e2780460 Apply new indentation (pgindent) used in PostgreSQL 10
Source code indentation has been updated in PostgreSQL 10 to fix a
number of issues. This update applies this new indentation to the
entire code base.

The new indentation requires a new version of pg_bsd_indent, which can
be found here:

https://git.postgresql.org/git/pg_bsd_indent.git
2018-01-18 15:19:23 +01:00
Matvey Arye
ad7d361418 Better accounting for number of items stored in a subspace
We add better accounting for number of items stored in a subspace
to allow better pruning. Instead of pruning based on the number of
dimension_slices in subsequent dimensions we now track number of total
items in the subspace store and prune based on that.

We add two GUC variables:
1) max_open_chunks_per_insert (default work_mem in bytes / 512. This
assumes an entry is 512 bytes)
2) max_cached_chunks_per_hypertable (default 100). Maximum cached chunks per
hypertable.
2018-01-10 13:50:36 -05:00
Matvey Arye
12f92ea1fa Improve speed of out-of-order inserts
Previously, the cache in chunk_dispatch was limited to only hold
the chunk_insert_state for the last time dimension as a consequence
of logic in subspace_store. This has now been relaxed so that a
chunk_dispatch holds the cache for any chunk_insert_states that it
encounters. Logic for the hypertable chunk cache has not been changed.

The rule that we should follow is to limit the subspace store size for
caches that survive across commands. But caches within commands can be
allowed to grow.
2018-01-10 13:50:36 -05:00
Erik Nordström
4df8f287a6 Add proper permissions handling for associated (chunk) schemas
A hypertable's associated schema is used to create and store internal
data tables (chunks). A hypertable creates tables in that schema,
typically with full superuser permissions, regardless of whether the
hypertable's owner or the current user have permissions for the schema.
If the schema doesn't exist, the hypertable will create it when
creating the first chunk, even though the user or table owner does
not have permissions to create schemas in the database.

This change adds proper permissions checks to create_hypertable() so
that users cannot create hypertables with a custom associated schema
unless they have the proper permissions on the schema or the database.

Chunks are also no longer created with internal schema permissions if
the associated schema is something different from the internal schema.
2017-12-28 11:24:29 +01:00
Erik Nordström
21efcce95c Refactor chunk table creation and unify constraint handling
This change is part of an effort to create a consistent way
of dealing with metadata catalog updates, which is currently
a mix of C API and INSERT/UPDATE/DELETE statements from SQL
code. This mix makes catalog handling unnecessarily complex as
there are multiple ways to update metadata, increasing the risk
of security issues with publically exposed SQL functions. It also
complicates things like cache invalidation, requiring different
mechanisms for C and SQL code. Catalog updates from SQL code
require triggers on metadata tables for cache invalidation that
do not work with native catalog updates.

The creation of chunks has been particularly messy in this regard,
making the code hard to follow. Especially the handling of a chunk's
constraints, where dimensional and other constraints were handled
differently. With this change, constraint handling is now consistent
across constraint types with a single API for updating metadata.

Reduce memory usage for out-of-order inserts

The chunk_result_relation_info should be put on the chunk memory
context. This will cause the rri constraint expr to also go onto
that context and be correctly freed when the chunk insert state
is destroyed.
2017-12-28 11:24:29 +01:00
Erik Nordström
0e76b5fa05 Do not add tablespaces to hypertable objects
A hypertable's tablespaces are now always retrieved from
the tablespace metadata table instead of being cached
with the hypertable. This avoids having to do cache invalidation
when updating the tablespace table.
2017-12-09 18:27:50 +01:00
Erik Nordström
6e92383592 Add function to detach tablespaces from hypertables
Tablespaces can now be detached from hypertables using
`tablespace_detach()`. This function can either detach
a tablespace from all tables or only a specific table.

Having the ability to detach tablespace allows more
advanced storage management, for instance, one can detach
tablespaces that are running low on diskspace while attaching
new ones to replace the old ones.
2017-12-09 18:27:50 +01:00
Erik Nordström
e593876cb0 Refactor tablespace handling
Attaching tablespaces to hypertables is now handled
in native code, with improved permissions checking and
caching of tablespaces in the Hypertable data object.
2017-12-09 18:27:50 +01:00
Erik Nordström
c4a46ac8a1 Add hypertable cache lookup on ID/pkey
Hypertables can now be looked up through the cache on
ID/pkey in addition to OID.
2017-12-09 18:27:50 +01:00
Rob Kiefer
66396fb81e Add build support for Windows
Windows 64-bit binaries should now be buildable using the cmake
build system either from the command line or from Visual Studio.

Previous issues regarding unresolved symbols have been resolved
with compatibility header files to properly export symbols or
getting GUCs via normal APIs.
2017-11-27 12:04:44 -05:00
Erik Nordström
1e947da456 Permission fixes and allow SET ROLE
This change reduces the usage of SECURITY DEFINER on SQL
functions and fixes related permissions issues. It also
properly checks hypertable permissions relative the current_user
instead of the session_user, which otherwise breaks SET ROLE,
among other things.
2017-11-27 15:55:26 +01:00
Matvey Arye
13e1cb5343 Add reindex function
reindex allows you to reindex the indexes of only certain chunks,
filtering by time. This is a common use case because a user may
want to reindex chunks after they are no longer getting new data once.

reindex also has a recreate option which will not use REINDEX
but will rather CREATE INDEX a new index and then
DROP INDEX / RENAME new_index to old_name. This approach has advantages
in terms of blocking reads for a much shorter period of time. However,
it does more work and will use more disk space during the operation.
2017-11-21 14:08:57 -05:00
Erik Nordström
500563ffe5 Add support for PostgreSQL 10
The extension now works with PostgreSQL 10, while
retaining compatibility with version 9.6.

PostgreSQL 10 has numerous internal changes to functions and
APIs, which necessitates various glue code and compatibility
wrappers to seamlessly retain backwards compatiblity with older
versions.

Test output might also differ between versions. In particular,
the psql client generates version-specific output with `\d` and
EXPLAINs might differ due to new query optimizations. The test
suite has been modified as follows to handle these issues. First,
tests now use version-independent functions to query system
catalogs instead of using `\d`. Second, changes have been made to
the test suite to be able to verify some test outputs against
version-dependent reference files.
2017-11-10 09:44:20 +01:00
Erik Nordström
bc595c1826 Use per-chunk memory context for cached chunks
The chunk cache needs to free chunk memory as
it evicts chunks from the cache. This was previously
done by pfree:ing the chunk memory, but this didn't
account for sub-allocated objects, like the chunk's
hypercube. This lead to some chunk objects remaining
in the cache's memory context, thus inflating memory
usage, although the objects were no longer associated
with a chunk.

This change adds a per-chunk memory context in the cache
that allows all chunk memory to be easily freed when
the cache entry is evicted or when the chunk cache
is destroyed.
2017-11-06 14:38:12 +01:00
Erik Nordström
097db3d589 Refactor chunk index handling
This change refactors the chunk index handling to make better use
of standard PostgreSQL catalog information, while removing the
hypertable_index metadata table and associated triggers, including
those on the chunk_index table. The chunk_index table itself is
also simplified.

A benefit of this refactoring is that indexes are no longer
created using string mangling to construct the CREATE INDEX command
for a chunk, based on the string definition of the hypertable
index. Instead, indexes are created in C using proper index-related
internal data structures.

Chunk indexes can now also be renamed and are added in the parent
index tablespace. Changing tablespace on a hypertable index also
recurses to chunks, as expected. Default indexes that are added when
creating a hypertable use the hypertable's tablespace.

Creating Hypertable indexes with the CONCURRENTLY modifier is
currently blocked, due to unclear semantics regarding concurrent
creation over many tables, including how to deal with snapshots.
2017-10-03 10:51:32 +02:00
Erik Nordström
04d01ce6ca Split DDL processing into start and end hooks
The ProcessUtility hook doesn't give any information on applied DDL
commands, which makes it hard to implement DDL processing that
requires the result of a DDL command on a hypertable (for instance,
adding a constraint or index without an explicit name).

This change splits the DDL processing over start and end hooks,
handling DDL commands before and after regular PostgreSQL processing,
respectively.

The start DDL hook is still based on the ProcessUtility hook, while
the end DDL hook is based on an event trigger that allows getting
information on the created/dropped/altered objects.
2017-09-22 12:54:22 +02:00
Matvey Arye
51821b3709 Move trigger handling from PLPGSQL to C
Applying triggers to chunks requires taking the definition
of a trigger on a hypertable and executing it on a chunk. Previously
this was done with string replacement in the trigger definition.
This was not especially safe, and thus we moved the logic to C
where we can do proper parsing/deparsing and replacement of the table
name. Another positive aspect is that we got rid of some DDL triggers.
2017-09-14 13:01:46 -04:00
Erik Nordström
c2f686dbba Refactor chunk creation to handle chunk collisions and alignment
When new chunks are created, the calculated chunk hypercube might
collide or not align with existing chunks when partitioning has
changed in one or more dimensions. In such cases, the chunk should be
cut to fit the alignment criteria and any collisions should be
resolved. Unfortunately, alignment and collision detection wasn't
properly handled.

This refactoring adds proper axis-aligned bounding box collision
detection generalized to N dimensions. It also correctly handles
dimension alignment.
2017-09-06 15:07:13 +02:00
Erik Nordström
6a5a7eb398 Reduce memory usage on long-running COPY operations
This change ensures that the per-tuple exprcontext (on which per-tuple
state is allocated), is reset for every new tuple processed in a
long-running COPY transaction.
2017-08-17 17:29:44 +02:00
Erik Nordström
953346c18b Make VACUUM and REINDEX recurse to chunks
Previously, when issued on hypertable, database maintenance
commands, like VACUUM and REINDEX, only affected the main
table and did not recurse to chunks.

This change fixes that issue, allowing database maintainers
to issue single commands on hypertables that affect all
the data stored in the hypertable.

These commands (VACUUM, REINDEX) only work at the table level
for hypertables. If issued at other levels, e.g., schema, or
database, the behavior is the same as in standard PostgreSQL
as all tables are covered by default.

REINDEX commands that specify a hypertable index do not
recurse as that requires mapping the hypertable
index to the corresponding index on the chunk. This might
be fixed in a future update.
2017-08-15 17:26:52 +02:00
Erik Nordström
a6309dac48 Fix a number of comments and cleanup unused code 2017-06-22 20:15:38 +02:00
Matvey Arye
ce3d630b6d Run pgindent on code 2017-06-22 20:15:38 +02:00
Matvey Arye
5452dc56d9 Fix partiton functions; bug fixes (including memory) 2017-06-22 20:15:38 +02:00
Erik Nordström
e75cd7e66b Finer grained memory management
Also fix a number of memory allocation bugs
and properly initialize chunks that are allocated
during a scan for chunks.
2017-06-22 20:15:38 +02:00
Matvey Arye
3c460f02b4 Fix partitioning, memory, and tests 2017-06-22 20:15:38 +02:00
Erik Nordström
fe51d8d7fc Add native scan for the chunk table
- The chunk table can now be scanned in the C code
- Rename DimensionAxis to DimensionVec
2017-06-22 20:15:38 +02:00
Matvey Arye
fc68baa8cc Separate out subspace_store and add it to the hypertable object as well 2017-06-22 20:15:38 +02:00
Erik Nordström
700c9c8a79 Refactor insert path in C.
Also in this commit:

- Rename time/space to open/closed for more generality.
- Create a Point data type for mapping a tuple to an
  N-dimensional space.
- Numerous fixes and cleanups.
2017-06-22 20:15:38 +02:00
Erik Nordström
7b8de0c592 Refactor catalog for new schema and add native data types
This is the first stab at updating the table and data type
definitions in the catalog module in the C code. This also
adds functions for natively scanning the dimension and
dimension_slice tables.
2017-06-22 20:15:38 +02:00