49 Commits

Author SHA1 Message Date
Keyur Panchal
cc6dc85dd2
Add hint when hypertable creation fails (#7565)
When creating a hypertable, we create default indexes on the partition
column, which requires that the column be a part of the table's primary
or composite key. The error message shown to the user in this scenario
did not indicate this correctly. This adds a hint to make this clearer.

Closes #2907.
2025-01-08 19:13:58 +00:00
Sven Klemm
98668b8dea Sync FK behaviour when creating hypertable
We now allow FKs to hypertables but the initial check when creating
the hypertable was not adjusted so tables with pre-existing FKs
would not be allowed to be changed into hypertable while creating
the FK constraint afterwards succeeded.
2024-09-30 10:49:12 +02:00
Fabrízio de Royes Mello
e9e19ca6a8 Fix create default indexes on chunks
When creating a hypertable with default indexes and migrating data by
setting `migrate_data=>true` is not creating the default indexes on
chunks.

Fixed it by changing the internal order or operations by first create
the default indexes and after it migrate the data.

Fixes #7190
2024-08-12 14:02:46 -03:00
Alejandro Do Nascimento Mora
85b27b4f34 Fix create_hypertable referenced by fk succeeds
Foreign keys pointing to hypertables are not supported. Creating a
hypertable from a table referenced by foreign key succeeds, but it
leaves the referencing (child) table in a broken state, failing on every
insert with a `violates foreign key constraint` error.

To prevent this scenario, if a foreign key reference to the table exists
before converting it to a hypertable, the following error will be
raised:

```
cannot have FOREIGN KEY contraints to hypertable "<table_name>"
HINT: Remove all FOREIGN KEY constraints to table "<table_name>"
  before making it a hypertable.
```

Fixes #6452
2024-01-30 15:01:12 -03:00
Nikhil Sontakke
844807a374 Change show_chunks/drop_chunks using creation time
- Updated show_chunks, drop_chunks APIs to get the affected
chunks using chunk creation time metadata based on the
"date/time/interval" like boundary specified for the INTEGER
columns.

- We honor "integer_now" function if it's specified so as to keep
backwards compatibility with the existing behavior

Co-authored-by: Dipesh Pandit <dipesh@timescale.com>
2023-11-02 18:37:09 +05:30
Dipesh Pandit
0b87a069e7 Add metadata for chunk creation time
- Added creation_time attribute to timescaledb catalog table "chunk".
  Also, updated corresponding view timescaledb_information.chunks to
  include chunk_creation_time attribute.
- A newly created chunk is assigned the creation time during chunk
  creation to handle new partition range for give dimension (Time/
  SERIAL/BIGSERIAL/INT/...).
- In case of an already existing chunk, the creation time is updated as
  part of running upgrade script. The current timestamp (now()) at the
  time of upgrade has been assigned as chunk creation time.
- Similarly, downgrade script is updated to drop the attribute
  creation_time from catalog table "chunk".
- All relevant queries/views/test output have been updated accordingly.

Co-authored-by: Nikhil Sontakke <nikhil@timescale.com>
2023-10-04 14:49:05 +05:30
Lakshmi Narayanan Sreethar
44e41c12ab Fix segfault in set_integer_now_func
When an invalid function oid is passed to set_integer_now_func, it finds
out that the function oid is invalid but before throwing the error, it
calls ReleaseSysCache on an invalid tuple causing a segfault. Fixed that
by removing the invalid call to ReleaseSysCache.

Fixes #6037
2023-09-06 15:11:07 +05:30
Sven Klemm
a9751ccd5e Move partitioning functions to _timescaledb_functions schema
To increase schema security we do not want to mix our own internal
objects with user objects. Since chunks are created in the
_timescaledb_internal schema our internal functions should live in
a different dedicated schema. This patch make the necessary
adjustments for the following functions:

- get_partition_for_key(val anyelement)
- get_partition_hash(val anyelement)
2023-08-29 12:55:22 +02:00
Sven Klemm
0a66bdb8d3 Move functions to _timescaledb_functions schema
To increase schema security we do not want to mix our own internal
objects with user objects. Since chunks are created in the
_timescaledb_internal schema our internal functions should live in
a different dedicated schema. This patch make the necessary
adjustments for the following functions:

- to_unix_microseconds(timestamptz)
- to_timestamp(bigint)
- to_timestamp_without_timezone(bigint)
- to_date(bigint)
- to_interval(bigint)
- interval_to_usec(interval)
- time_to_internal(anyelement)
- subtract_integer_from_now(regclass, bigint)
2023-08-21 15:01:35 +02:00
Sven Klemm
0dd06e919f Move get_create_command into _timescaledb_functions schema
To increase schema security we do not want to mix our own internal
objects with user objects. Since chunks are created in the
_timescaledb_internal schema our internal functions should live in
a different dedicated schema. This patch make the necessary
adjustments for the get_create_command function.
2023-08-21 11:29:37 +02:00
Bharathy
9a2cbe30a1 Fix ChunkAppend, ConstraintAwareAppend child subplan
When TidRangeScan is child of ChunkAppend or ConstraintAwareAppend node, an
error is reported as "invalid child of chunk append: Node (26)". This patch
fixes the issue by recognising TidRangeScan as a valid child.

Fixes: #4872
2023-01-18 18:06:30 +05:30
Bharathy
f6dd55a191 Hypertable FK reference to partitioned table
Consider a hypertable which has a foreign key constraint on a
referenced table, which is a parititioned table. In such case, foreign
key constraint is duplicated for each of the partitioned table. When
we insert into a hypertable, we end up checking the foreign key
constraint multiple times, which obviously leads to foreign key
constraint violation. Instead we only check foreign key constraint of
the parent table of the partitioned table.

Fixes #4684
2022-09-27 21:09:05 +05:30
Bharathy
b869f91e25 Show warnings during create_hypertable().
The schema of base table on which hypertables are created, should define
columns with proper data types. As per postgres best practices Wiki
(https://wiki.postgresql.org/wiki/Don't_Do_This), one should not define
columns with CHAR, VARCHAR, VARCHAR(N), instead use TEXT data type.
Similarly instead of using timestamp, one should use timestamptz.
This patch reports a WARNING to end user when creating hypertables,
if underlying parent table, has columns of above mentioned data types.

Fixes #4335
2022-09-12 18:47:47 +05:30
Dmitry Simonenko
65b5dc900f Support add_dimension() with existing data
This change allows to create new dimensions even with
existing chunks.

It does not modify any existing data or do migration,
instead it creates full-range (-inf/inf) dimension slice for
existing chunks in order to be compatible with newly created
dimension.

All new chunks created after this will follow logic of the new
dimension and its partitioning.

Fix: #2818
2022-08-01 10:52:03 +03:00
gayyappan
2702140fa3 Cannot add dimension if table has empty chunks
add_dimension should fail when table has no
data but still has empty chunks.
Fixes #1623
2020-02-10 10:47:23 -05:00
Matvey Arye
01f2bbaf5a Add better errors for no permission in callbacks
Have better permission errors when setting
the integer now func and the partitioning func.

Also move tests from tsl to apache2 for the now func.
2019-10-11 13:00:55 -04:00
Sven Klemm
f89fd07c5b Remove year from SQL file license text
This changes the license text for SQL files to be identical
with the license text for C files.
2019-01-13 23:30:22 +01:00
Sven Klemm
787cc0470e Replace hardcoded database name from regression tests
Replace hardcoded database name from regression tests with :TEST_DBNAME
Remove creation of database single_2 from test runner and add it to
bgw_launcher and loader test since no other tests used those
use SQL comments in test scripts
2018-12-28 19:26:27 +01:00
Sven Klemm
31e9c5b5cc Fix time column handling in get_create_command
Fixes get_create_command to produce a valid create_hypertable
call when the column name is a keyword
Add test.execute_sql helper function to test support functions
2018-12-28 18:15:57 +01:00
Erik Nordström
e4a4f8e2f8 Add support for functions on open (time) dimensions
TimescaleDB has always supported functions on closed (space)
dimension, i.e., for hash partitioning. However, functions have not
been supported on open (time) dimensions, instead requiring columns to
have a supported time type (e.g, integer or timestamp). This restricts
the tables that can be time partitioned. Tables with custom "time"
types, which can be transformed by a function expression into a
supported time type, are not supported.

This change generalizes partitioning so that both open and closed
dimensions can have an associated partitioning function that
calculates a dimensional value. Fortunately, since we already support
functions on closed dimensions, the changes necessary to support this
on any dimension are minimal. Thus, open dimensions now support an
(optional) partitioning function that transforms the input type to a
supported time type (e.g., integer or timestamp type). Any indexes on
such dimensional columns become expression indexes.

Tests have been added for chunk expansion and the hashagg and sort
transform optimizations on tables that are using a time partitioning
function.

Currently, not all of these optimizations are well supported, but this
could potentially be fixed in the future.
2018-12-12 10:14:31 +01:00
Sven Klemm
46d3479757 Add tests for calling functions with NULL arguments 2018-11-23 20:54:27 +01:00
Joshua Lockerman
20ec6914c0 Add license headers to SQL files and test code 2018-10-29 13:28:19 -04:00
Amy Tai
c71960c048 Fix pg_dump test and make insert block trigger a non-internal trigger
Previously, the pg_dump test was broken because it is not possible to reference psql variables
from inside bash commands run through psql. This is fixed by hardcoding the username passed to
the bash commands inside the test.

Also, we changed the insert block trigger preventing inserts into hypertable to a non-internal
trigger, because internal triggers are not dumped by pg_dump. We need to dump the trigger so that
it is already in place after a pg_restore, to prevent users from accidentally inserting rows into
a hypertable while timescaledb_restoring=on.
2018-09-04 18:34:57 -04:00
Erik Nordström
37142e9094 Block INSERTs on a hypertable's root table
A hypertable's root table should never have any tuples, but it can
acquire tuples by accident if the TimescaleDB extension is not
preloaded or `timescaledb.restoring` is set to ON.

To avoid the above issue, a hypertable's root table now has a
(internal) trigger that generates an error when tuples are
inserted. This preserves the integrity of the hypertable even when
restoring or the extension is not preloaded.

An internal trigger has the advantage of being mostly transparent to
users (e.g., it doesn't show up with \d) and it is not inherited by
chunks, so it needs no special handling to avoid adding it to chunks.

The blocking trigger is added in the update script and if it is
detected that the hypertable's root table has data in the process, the
update will fail with an error and instructions on how to fix.
2018-08-07 14:22:41 +02:00
Matvey Arye
657a12f6f2 Add tests for table OF type
This makes sure that tables created OF type work as hypertables.
It also makes sure that ALTER table command related to OF work
correctly.
2018-08-06 15:44:36 -04:00
Erik Nordström
77b00356c4 Enforce IMMUTABLE partitioning functions
This adds a simple check to enforce that partitioning functions
are IMMUTABLE. This is a requirement since a partitioning function
must always return the same value given the same input.
2018-08-02 20:45:10 +02:00
Erik Nordström
fc36699a43 Do not fail add_dimension() on non-empty table with 'if_not_exists'
This fixes an issue with the `if_not_exists` option to add_dimension()
that caused the command to fail on a non-empty table. When this option
is set, the command should only fail if the dimension does not exists
and the table is non-empty.
2018-03-08 15:14:36 +01:00
Erik Nordström
ef744916a8 Migrate table data when creating a hypertable
Tables can now hold existing data, which is optionally migrated from
the main table to chunks when create_hypertable() is called.

The data migration is similar to the COPY path, with the single
difference that the inserted/copied tuples come from an existing table
instead of being read from a file. After the data has been migrated,
the main table is truncated.

One potential downside of this approach is that all of this happens in
a single transaction, which means that the table is blocked while
migration is ongoing, preventing inserts by other transactions.
2018-02-15 23:30:54 +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
Matvey Arye
264956f0c1 Block DROP NOT NULL on time-partitioned columns.
Time-partitioning cannot handle NULL values.
2018-01-11 09:17:59 -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
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
e44e47ed88 Update add_dimension to take INTERVAL times
The user should be able to add time dimensions using INTERVAL when
the column type is TIMESTAMP/TIMESTAMPTZ/DATE, so this change adds
that support.

Additionally it adds some additional tests and checks for
add_dimension, e.g., a nice error when the table is not a
hypertable.
2017-12-07 12:09:35 -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
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
Olof Rensfelt
201a948452 Check that time dimensions are set as NOT NULL.
Add check that time dimensions are set as NOT NULL in the
main table that a hypertable is created from. If it is not
set, the constraint will be added.
2017-11-02 09:12:15 +01:00
Matvey Arye
543a75f865 Refactor regression tests
This commit moves a lot of test setup logic to runner.sh. Also
passes the right commands to the regression infrastructure to create
appropriate users and run tests as a regular user.
2017-10-05 15:56:33 -04: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
Rob Kiefer
997556af40 Add backup/restore scripts for single hypertables
Using pg_dump/pg_restore for a database with hypertables will back
up all internal tables too, which is sometimes not desired. These
scripts allow one to backup regular PostgreSQL tables separately
and then pick and choose which hypertables to backup and restore.
This also provides a forward-compatible way of backing up and
restoring hypertables, since data is dumped to CSV and restored
by re-creating and re-inserting the data.
2017-09-06 11:24:45 -04:00
Michael J. Freedman
f2d5c3fc5d Error if add_dimension given both partition number and interval length 2017-08-18 10:41:14 -04:00
Rob Kiefer
f3df02d50c Improve handling of non-TIMESTAMP/TZ timestamps
Previously the default chunk time in microseconds was too large
for a SMALLINT or INTEGER field. Now, we only assign a default
value if the type is TIMESTAMP or TIMESTAMPTZ. Integer timestamps,
such as SMALLINT, INTEGER, and BIGINT, need to be explicitly set
since only the user knows what units the numbers represent.

Further, we check to make sure the chunk time interval is not too
large for SMALLINT and INTEGER so as to avoid confusing problems
later when the user goes to insert.
2017-08-17 12:13:00 -04:00
Erik Nordström
696cc4c4f1 Provide API for adding hypertable dimensions
A new public-facing API `add_dimension(table, column, ...)`
makes it possible to add additional dimensions (partitioning
columns) to a hypertable.

Currently, new dimension can only be added to empty tables.

The code has also been refactored with a corresponding
internal function that is called by both `add_dimension()`
and `create_hypertable()`.
2017-06-27 19:39:14 +03: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
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
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
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
Olof Rensfelt
406dbb7bd6 Remove clustered tests. 2017-02-22 12:16:48 +01:00
Erik Nordström
036c40886c Simplify cluster setup
Setting up single node is now:

```
CREATE EXTENSION IF NOT EXISTS iobeamdb CASCADE;
select setup_single_node();
```

To setup a cluster do (on meta node):
```
CREATE EXTENSION IF NOT EXISTS iobeamdb CASCADE;
select set_meta();
```

on data node:
```
CREATE EXTENSION IF NOT EXISTS iobeamdb CASCADE;
select join_cluster('metadb', 'metahost');
```

This assumes that the commands are issued by the same user on both the
meta node and the data node. Otherwise the data node also has to
specify the user name to use when connecting to the meta node.
2017-02-07 12:22:36 +01:00
Erik Nordström
7b94c573ba Refactor directory structure and tests
- Directory structure now matches common practices
- Regression tests now run with pg_regress via the PGXS infrastructure.
- Unit tests do not integrate well with pg_regress and have to be run
  separately.
- Docker functionality is separate from main Makefile. Run with
  `make -f docker.mk` to build and `make -f docker.mk run` to run
  the database in a container.
2017-01-31 20:14:19 +01:00