55 Commits

Author SHA1 Message Date
Matvey Arye
c373d6b359 Remove executor which had logic for adjusting es_processed
Previously, the executor code adjusted es_processed to set the
number of tuples that have been processed in the completion tag.
That was necessary since we had triggers that re-routed tuples to chunks
and cancelled the inserts on the base table (thus those tuples were not
counted in es_processed). We no longer use triggers and thus this logic
was no longer used. In fact, this was mostly dead code because
`additional_tuples` was always 0.
2018-01-05 13:34:51 -05:00
Matvey Arye
da8cc797a4 Add support for multiple extension version in one pg instance
This PR adds the ability to have multiple different versions of the timescaledb
extension be used by different databases in the same PostgreSQL
instance (server).

This is accomplished by splitting this extension into two .so files.
1) timescaledb.so -- stuff under loader/. Really not a lot of code.
    This code MUST be backwards compatible in the future.
2) timescaledb-version.so (most of our code). Need
   not be backwards compatible.

Timescaledb.so becomes a small stub which is preloaded and whose main
reason for existing is to dynamically load the right
timescaledb-version.so when the time comes.

This change allows either of the above .so to be loaded in
shared_preload_libraries. But timescaledb.so allows for multiple
versions used on different databases in the same instance along
with smoother upgrades. Using timescaledb-version.so allows for
finer-grained control and lock-in and is appropriate in only a few
production environments.

This PR also adds version checking so that a clear failure message
will be displayed if the .so version does not match the SQL extension
version.

To support multi-version functionality we changed the way SQL update
scripts are generated. Previously, the system used a bunch of
intermediate upgrade scripts.  So with 3 versions, you would have an
update script of 1--2, 2--3.  But, this PR changes things so that we
produce direct "shortcut" update files: 1--3, 2--3.
This is done for 2 reasons:
 1) Each of the update files should point to
    $libdir/timescaledb-current_version. Since you cannot guarantee that
    Previous .so for each intermediate version has been installed.
 2) You don't want intermediate version updates installed without the
    .so. For example, if you have versions 1,2,3
    and you are installing version 3, you want the upgrade files 1--3,
    2--3 but not 1--2 because if you have 1--2
    then a user could do ALTER EXTENSION timescaledb UPDATE TO 2. But
    the .so for version 2 may not be installed.

In order to test this functionality, we add a mock extension version .so
that we can test extension loading inside the regression framework.
2018-01-05 12:15:54 -05:00
Erik Nordström
68faddca24 Make chunks inherit reloptions set on the hypertable
When tables are created with storage parameters (reloptions),
these should be inherited by chunks so that one can set options
such as fillfactor and autovacuum settings on the hypertable.
This change makes chunks inherit the reloptions set on a hypertable
and allows altering options via the ALTER TABLE command.
2017-12-28 17:15:55 +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
Matvey Arye
2fe447ba14 Make TimescaleDB work with pg_upgrade
Compatibility with pg_upgrade required 2 changes:
1) search_path on functions cannot be blank for pg_upgrade.
2) The timescaledb.restoring GUC had to apply to more code (now moved to
   higher-level check)

`pg_upgrade` must be passed the following option: `-O "-c timescaledb.restoring='on'"`
2017-12-19 11:47:49 -05:00
jwdeitch
6cfdd7904b Prevent native partitioning attachment of hypertables
- Raise an exception upon an attach partition event on a hypertable to a native postgres partition
2017-12-18 13:01:16 +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
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
Erik Nordström
5d0cbc12fd Recurse CLUSTER command to chunks
Clustering a table means reordering it according to an index.
This operation requires an exclusive lock and extensive
processing time. On large hypertables with many chunks, CLUSTER
risks blocking a lot of operations by holding locks for a long time.
This is alleviated by processing each chunk in a new transaction,
ensuring locks are only held on one chunk at a time.
2017-11-20 20:15:43 +01:00
Erik Nordström
ca0968aaa0 Make all partitioning functions take anyelement argument
All partitioning functions now has the signature `int func(anyelement)`.
This cleans up some special handling that was necessary to support
the legacy partitioning function that expected text input.
2017-11-17 17:59:16 +01: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
4a0a0d8253 Fix column type change on plain tables
A recent change blocked changing types of space-partitioned hypertable
columns. However, this blocking should not apply to regular tables,
which otherwise causes a crash. This change fixes this issue by
properly checking that the the table is a hypertable.
2017-10-30 14:54:37 +01:00
Erik Nordström
cf009cc584 Avoid string conversion in hash partitioning
Hash partitioning previously relied on coercing (casting) values to
strings before calculating a hash value, including creating CHECK
constraints with casts. This approach is fairly suboptimal from a
performance perspective and might have issues related to different
character encodings depending on system.

Hash partitioning now instead uses a partitioning function that takes
an anyelement type that calls type-dependent hash functions internal
to PostgreSQL. This should provide more efficient hashing both by
avoiding unnecessary string conversions and by using more optimal
type-specific hash functions.

Support for the previous hash partitioning function is preserved for
backwards compatibility. Hypertables created with the previous
function will continue to use to old hashing strategy, while new
tables will default to the updated hash partitioning.

For safety, this change also blocks changing types on hash-partitioned
columns, since it seems hard to guarantee the same hash result between
different types.
2017-10-25 15:30:56 +02:00
Matvey Arye
c420c11e44 Create a catalog entry for constraint-backed indexes
This change makes our catalog more in-line with the pg catalog.
It also will simplify a lot of other code.
2017-10-24 11:26:04 -04:00
Erik Nordström
114fa8d537 Refactor functions used to recurse DDL commands to chunks
When recursing DDL operations to chunks, you often want
access to the hypertable in the chunk handler function. This
change gives the handler access to the hypertable data structure
rather then just the hypertable OID, obviating the need to do
a separate lookup that has likely already been done previously.
2017-10-16 21:36:23 +02:00
Matvey Arye
1d73fb81c1 Fix bug with extension starting too early.
Extension_new_state() was sometimes using the pg cache before it
was fully initialized. This commit adds a check to make sure that
does not happen.
2017-10-11 11:07:44 -04:00
Erik Nordström
742acf5dc2 Fix mixed declaration and code warning and indentation
A previous commit mixed declarations and code, which causes
warnings on some compilers. This change quenches the warning
and fixes some indentation/formatting issues.
2017-10-10 17:25:10 +02:00
Olof Rensfelt
430ed8a113 Fix bug with collected commands in index statement.
We previously blocked valid plain postgres statements. The blocking code
is now moved to only affect hypertables.
2017-10-09 16:59:45 +02:00
Matvey Arye
614c2b7821 Fix permissions bugs and run tests as normal user
All regression tests will now use a non-superuser unless superuser is
necessary. This PR is a meant to prevent things like issue #226.
This PR also fixes some more permission bugs found during this testing.
2017-10-05 15:56:33 -04:00
Erik Nordström
4c451e0f0f Fix rename and reindex bugs when objects are not relations
When issuing rename or reindex commands that do not involve
relations, the relation rangevar in the utility statement
will be NULL.

This change adds extra checks to avoid calling, e.g.,
RangeVarGetRelid() when a relation's rangevar is NULL, as
that function does not accept NULL input values.
2017-10-05 19:34:30 +02:00
Matvey Arye
c3ebc676e3 Fix permission problems with dropping hypertables and chunks
This change fixes permissions with dropping hypertables and chunks.
Fixes #226.
2017-10-05 12:06:09 -04: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
a2bad2b5f7 Fix constraint validation on regular tables
Constraints on regular PostgreSQL tables need to be validatated so
that they don't have a foreign key constraint that references a
hypertable, as this is not yet supported. This change fixes a bug in
this validation caused by not checking for the proper node types when
parsing a CreateStmt node. The CreateStmt is now also parsed in the
DDL end hook, instead of the processUtility hook, which ensures that
the CreateStmt node has been run through parse analysis. The parse
analysis changes the contents of the node to be more consistent across
different CREATE statements. For instance, a CREATE LIKE statement
will look more similar to a regular CREATE statement after parse
analysis.
2017-10-03 10:06:42 +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
Erik Nordström
4d3e2c0523 Fix error message about creating constraints using exiting index 2017-09-20 18:01:47 +02:00
Erik Nordström
2cafb2446c Add another missing break statement 2017-09-19 18:35:09 +02:00
Erik Nordström
aa904fa5d0 Block adding constraints without a constraint name
The current approach of handling alter table commands does not allow
getting the result of the command; for instance, the object address of
a created constraint on a hypertable. Thus there is no way to get the
auto-generated name of a constraint, which is needed when the
corresponding constraints are created on the hypertable's chunks.

Therefore, this change blocks the ability to create constraints
without an explicit name. When a better approach to handling alter
table is deviced, it is possible to remove this restriction.
2017-09-19 09:09:16 +02:00
Erik Nordström
1d54ccea45 Add missing break statement in process utility hook 2017-09-18 14:10:06 -04:00
Erik Nordström
a13039fd58 Fix dump and restore for tables with triggers and constraints
During a database restore, the creation of triggers and constraints on
hypertables should not recurse to chunks since the chunks will be
restored with existing triggers and constraints. Failing to block
recursing during restore risks duplicating these objects on chunks.

Further, operations on chunks (e.g., ALTER TABLE) should be allowed
during the restore process since those operations are needed to
recreate the constraints and triggers.

To fix this, the "restoring" GUC is used to bypass custom handling of
events in the ProcessUtility hook.
2017-09-18 11:35:28 +02:00
Erik Nordström
6660348ab6 Cleanup process utility for consistency
- Move DDL upcalls to function macros for clarity and readability
- Rename some functions for consistency and remove unused parameters
- Refactor some code that did unecessary work
2017-09-16 09:06:30 +02:00
Erik Nordström
27675488f6 Block adding constraints using an existing index
Adding a constraint using an existing index changes the existing
index's name as a side effect (PostgreSQL wants the index name to
match the constraint name). This creates a mismatch between the
index's name and our metadata in the chunk_index table. Further, since
the name change happens as a result of an internal command (and not
via DDL command that we can capture) there is currently no way to sync
up this information.
2017-09-15 21:42:07 +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
Matvey Arye
238003316e Block ALTER TABLE and handle DROP TABLE on chunks
This PR adds logic to block alter table and handle drop operations on chunks.
2017-09-13 18:42:45 -04:00
Matvey Arye
72d668150e Move security checks for ALTER TABLE ALTER COLUMN to C 2017-09-13 18:28:24 -04:00
Matvey Arye
19d3d8981b Handle changing the type of dimension columns correctly.
Update the type in the dimension metadata table and recreate
the check constraints.
2017-09-13 18:28:24 -04:00
Matvey Arye
17c4ba9ec3 Handle ALTER TABLE rename column
Update the column names stored in the dimension metadata table.
2017-09-13 18:28:24 -04:00
Matvey Arye
66932cf8d5 Forbid relocating extension after install.
Unfortunately, Postgres forbids relocating the "public api" schema
of extensions that use multiple schemas after initial installation.
This PR marks the extension as not relocatable and tests relocation
during initial install.
2017-09-12 11:38:52 -04:00
Matvey Arye
48e0a61131 Remove triggers from chunk and chunk_constraint
Streamline code and remove triggers from chunk and
chunk_constraint. Lots of additional cleanup. Also removes need to CASCADE
hypertable drops (fixes #88).
2017-09-07 11:31:48 -04:00
Matvey Arye
4dcbe6114d Add support for hypertable constraints
This PR add support for primary-key, foreign-key, unique, and exclusion constraints.
Previously supported are CHECK and NOT NULL constraints. Now, foreign key
constraints where a hypertable references a plain table is support
(while vice versa, with a plain table references a hypertable, is still not).
2017-09-07 11:31:48 -04: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
a1a28ecf6f Refactor process utility
This breaks up the handling of process utility statements into a clean
and efficient switch statement with separate functions for each
statement.
2017-08-09 13:49:17 +02:00
Erik Nordström
2200010ddb Fix EXPLAIN and PREPAREd statements on INSERTs
This fixes some issues that caused EXPLAIN and PREPAREd statements to
fail on INSERTs. The plan modification on INSERTs passed on a plan
node to the execution stage. However, that plan node pointer becomes
invalid when the plan is copied, as happens with prepared
statements. The fix required another modification to the plan by
wrapping also the ModifyTable plan node. This wrapping is necessary to
access the plan state node (ModifyTableState) on execution. Having
access to the ModifyTableState node is necessary to set the correct
arbiter index list for ON CONFLICT statements.

This change also fixes the output of EXPLAIN to properly show the plan
tree, and performs some cleanups.
2017-07-31 16:18:40 +02:00
Erik Nordström
1f3dcd814f Make INSERTs use a custom plan instead of triggers
With this change, hypertables no longer rely on an INSERT trigger to
dispatch tuples to chunks. While an INSERT trigger worked well for
both INSERTs and COPYs, it caused issues with supporting some regular
triggers on hypertables, and didn't support RETURNING statements and
upserts (ON CONFLICT DO UPDATE).

INSERTs are now handled by modifying the plan for INSERT statements. A
custom plan node is inserted as a subplan to a ModifyTable plan node,
taking care of dispatching tuples to chunks by setting the result
table for every tuple scanned.

COPYs are handled by modifying the regular copy code. Unfortunately,
this required copying a significant amount of regular PostgreSQL
source code since there are no hooks to add modifications. However,
since the modifications are small it should be fairly easy to keep the
code in sync with upstream changes.
2017-07-27 15:49:01 +02:00
Olof Rensfelt
f23bf58285 Remove empty chunks on TRUNCATE hypertable. 2017-07-26 21:17:44 +02:00
Olof Rensfelt
4d2a65daa6 Add infrastructure to build update script files.
SQL code is now split into setup, functions, and init files to
allow a subset to be run when the extension is updated. During
build, an update script is now also generated.
2017-07-12 13:00:29 -04:00
Olof Rensfelt
a5725d9875 Add support to rename and change schema on hypertable. 2017-07-12 16:53:02 +02:00
Erik Nordström
142f58c2cc Cleanup planner and process utility hooks
Make sure parameter names are in lowercase-underscore format. Also,
rename the query tree walker change_table_name_walker() to
hypertable_query_walker() to reflect that it is no long renaming
anything (only setting up state in case of a query on a hypertable).
2017-07-10 10:43:11 +02: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