53 Commits

Author SHA1 Message Date
Sven Klemm
65562f02e8 Support unique constraints on compressed chunks
This patch allows unique constraints on compressed chunks. When
trying to INSERT into compressed chunks with unique constraints
any potentially conflicting compressed batches will be decompressed
to let postgres do constraint checking on the INSERT.
With this patch only INSERT ON CONFLICT DO NOTHING will be supported.
For decompression only segment by information is considered to
determine conflicting batches. This will be enhanced in a follow-up
patch to also include orderby metadata to require decompressing
less batches.
2023-03-13 12:04:38 +01:00
Fabrízio de Royes Mello
a4356f342f Remove trailing whitespaces from test code 2022-11-18 16:31:47 -03:00
Bharathy
d00a55772c error compressing wide table
Consider a compressed hypertable has many columns (like more than 600 columns).
In call to compress_chunk(), the compressed tuple size exceeds, 8K which causes
error as "row is too big: size 10856, maximum size 8160."

This patch estimates the tuple size of compressed hypertable and reports a
warning when compression is enabled on hypertable. Thus user gets aware of
this warning before calling compress_chunk().

Fixes #4398
2022-09-17 11:24:23 +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
Bharathy
ed212b4442 GROUP BY error when setting compress_segmentby with an enum column
When using a custom ENUM data type for compressed hypertable on the GROUP BY
clause raises an error.

Fixed it by generating scan paths for the query by checking if the SEGMENT BY
column is a custom ENUM type and then report a valid error message.

Fixes #3481
2022-09-01 07:31:07 +05:30
Konstantina Skovola
f28131cff5 Don't ask for orderby column if default already set
When enabling compression on a hypertable, the orderby option can be
omitted, which will set the default value of "time DESC".
However previously, when executing the same command twice not setting
orderby, the second time we would get an error that orderby was
previously set and must be specified.
For example when executing
`alter table set (timescaledb.compress, timescaledb.segmentby = '..')`

The reason for that error was that it's unclear if no orderby means
leave as is, or if it means set the default value.
But in the case where orderby is already set to the default value,
there is no ambiguity and both cases are equivalent, so the default
value can be reset without giving an error.

Fixes #4331
2022-06-02 11:18:11 +03:00
Josh Soref
68aec9593c Fix various misspellings
This patch fixes various misspellings of committed, constraint and
insufficient in code, comments and documentation.
2022-04-22 11:06:52 +02:00
Konstantina Skovola
a154ae56e9 Fix ADD COLUMN IF NOT EXISTS error on compressed hypertable
Stop throwing exception with message "column of relation already exists"
when running the command ALTER TABLE ... ADD COLUMN IF NOT EXISTS ...
on compressed hypertables.

Fix #4087
2022-04-01 00:44:07 -07:00
Alexander Kuzmenkov
37190e8a8a Cache chunk data when performing chunk exclusion
We cache the Chunk structs in RelOptInfo private data. They are later
used to estimate the chunk sizes, check which data nodes they belong
to, et cetera. Looking up the chunks is expensive, so this change
speeds up the planning.
2022-02-21 13:35:27 +03:00
Sven Klemm
6dddfaa54e Lock down search_path in install scripts
This patch locks down search_path in extension install and update
scripts to only contain pg_catalog, this requires that any reference
in those scripts is fully qualified. Additionally we add explicit
create commands to all update scripts for objects added to the
public schema. This change will make update scripts fail if a
function with identical signature already exists when installing
or upgrading instead reusing the existing object.
2022-02-09 17:53:20 +01:00
gayyappan
c0050a1315 Improve error message for compress_chunk
Fix the error message for compress chunks so that it
specifies the cagg name instead of the materialized
hypertable name.
2022-01-28 10:37:08 -05:00
Fabrízio de Royes Mello
7e3e771d9f Fix compression policy on tables using INTEGER
Commit fffd6c2350f5b3237486f3d49d7167105e72a55b fixes problem related
to PortalContext using PL/pgSQL procedure to execute the policy.
Unfortunately this new implementation introduced a problem when we use
INTEGER and not BIGINT for the time dimension.

Fixed it by dealing correclty with the integer types: SMALLINT, INTEGER
and BIGINT.

Also refatored the policy compression procedure replacing the two
procedures `policy_compression_{interval|integer}` by a simple
`policy_compression_execute` casting dimension type dynamically.

Fixes #3773
2021-11-05 14:55:23 -03:00
Sven Klemm
151637db89 Allow ALTER TABLE DROP COLUMN on compressed hypertable
This patch allows dropping columns from hypertables with compression
enabled when the dropped column is neither an orderby nor a
segmentby column.

Fixes #3683
2021-11-04 17:47:35 +01:00
Sven Klemm
93ffec7c10 Allow ALTER TABLE ADD COLUMN with DEFAULT on compressed hypertable
When adding a new column with a default postgres doesnt materialize
the default expression but just stores it in catalog with a flag
signaling that it is missing from the physical row.
Any expressions used as default that do not require materialization
can be allowed on compressed hypertables as well.

The following statements will work on compressed hypertables with
this patch:
ALTER TABLE t ADD COLUMN c1 int DEFAULT 42;
ALTER TABLE t ADD COLUMN c2 text NOT NULL DEFAULT 'abc';
2021-11-03 11:10:54 +01:00
gayyappan
2e6b05dfc0 Move test for error reporting to compression_errors.sql
compression_insert.sql has tests related to inserts
The test that is being moved verifies the error message.
2021-10-21 11:44:11 -04:00
gayyappan
fffd6c2350 Use plpgsql procedure for executing compression policy
This PR removes the C code that executes the compression
policy. Instead we use a PL/pgSQL procedure to execute
the policy.

PG13.4 and PG12.8 introduced some changes
that require PortalContexts while executing transactions.
The compression policy procedure compresses chunks in
multiple transactions. We have seen some issues with snapshots
and portal management in the policy code (due to the
PG13.4 code changes). SPI API has transaction-portal management
code. However, the compression policy code does not use SPI
interfaces. But it is fairly easy to just convert this into
a PL/pgSQL procedure (which calls SPI) rather than replicating
portal managment code in C to manage multiple txns in the
compression policy.

This PR also disallows decompress_chunk, compress_chunk and
recompress_chunk in txn read only mode.

Fixes #3656
2021-10-13 09:11:59 -04:00
Markos Fountoulakis
bc740a32fb Add distributed hypertable compression policies
Add support for compression policies on Access Nodes. Extend the
compress_chunk() function to maintain compression state per chunk
on the Access Node.
2021-05-07 16:50:12 +03:00
gayyappan
f649736f2f Support ADD COLUMN for compressed hypertables
Support ALTER TABLE .. ADD COLUMN <colname> <typname>
for hypertables with compressed chunks.
2021-01-14 09:32:50 -05:00
gayyappan
05d8ac2a59 Do not allow unique index on compressed hypertables.
CREATE UNIQUE INDEX is not supported on a hypertable
that has compression enabled.
2020-12-10 11:28:32 -05:00
Erik Nordström
3cf9c857c4 Make errors and messages conform to style guide
Errors and messages are overhauled to conform to the official
PostgreSQL style guide. In particular, the following things from the
guide has been given special attention:

* Correct capitalization of first letter: capitalize only for hints,
  and detail messages.
* Correct handling of periods at the end of messages (should be elided
  for primary message, but not detail and hint messages).
* The primary message should be short, factual, and avoid reference to
  implementation details such as specific function names.

Some messages have also been reworded for clarity and to better
conform with the last bullet above (short primary message). In other
cases, messages have been updated to fix references to, e.g., function
parameters that used the wrong parameter name.

Closes #2364
2020-10-20 16:49:32 +02:00
Mats Kindahl
02ad8b4e7e Turn debug messages into DEBUG1
We have some debug messages that are printed as notices, but are more
suitable to have at `DEBUG1` level. This commit removes a notice about
indexes being added and turns it into a `DEBUG1` notice.
2020-09-29 11:04:07 +02:00
Sven Klemm
bb891cf4d2 Refactor retention policy
This patch changes the retention policy to store its configuration
in the bgw_job table and removes the bgw_policy_drop_chunks table.
2020-08-03 22:33:54 +02:00
gayyappan
9f13fb9906 Add functions for compression stats
Add chunk_compression_stats and hypertable_compression_stats
functions to get before/after compression sizes
2020-08-03 10:19:55 -04:00
Brian Rowe
68aee5144c Rename add_drop_chunks_policy
This change replaces the add_drop_chunks_policy function with
add_retention_policy.  This also renames the older_than parameter
of that function as retention_window.  Likewise, the
remove_drop_chunks_policy is also being renamed
remove_retention_policy.

Fixes #2119
2020-07-30 09:53:21 -07:00
Ruslan Fomkin
16897d2238 Drop FK constraints on chunk compression
Drop Foreign Key constraints from uncompressed chunks during the
compression. This allows to cascade data deletion in FK-referenced
tables to compressed chunks. The foreign key constrains are restored
during decompression.
2020-04-14 23:12:15 +02:00
Ruslan Fomkin
dfcb6afb39 Move out WITH OIDS test to be run on PG12_LT
WITH OIDS option doesn't exist in PG12. This fix moves the test of
blocking compression for tables WITH OIDS into a separate file, which
is run only for PG version before 12.
2020-04-14 23:12:15 +02:00
Erik Nordström
afb4c7ba51 Refactor planner hooks
This change refactors our main planner hooks in `planner.c` with the
intention of providing a consistent way to classify planned relations
across hooks. In our hooks, we'd like to know whether a planned
relation (`RelOptInfo`) is one of the following:

* Hypertable
* Hypertable child (a hypertable can appear as a child of itself)
* Chunk as a child of hypertable (from expansion)
* Chunk as standalone (operation directly on chunk)
* Any other relation

Previously, there was no way to consistently know which of these one
was dealing with. Instead, a mix of various functions was used without
"remembering" the classification for reuse in later sections of the
code.

When classifying relations according to the above categories, the only
source of truth about a relation is our catalog metadata. In case of
hypertables, this is cached in the hypertable cache. However, this
cache is read-through, so, in case of a cache miss, the metadata will
always be scanned to resolve a new entry. To avoid unnecessary
metadata scans, this change introduces a way to do cache-only
queries. This requires maintaining a single warmed cache throughout
planning and is enabled by using a planner-global cache object. The
pre-planning query processing warms the cache by populating it with
all hypertables in the to-be-planned query.
2020-04-14 23:12:15 +02:00
Erik Nordström
a4fb0cec3f Cleanup compression-related errors
This change fixes a number of typos and issues with inconsistent
formatting for compression-related code. A couple of other fixes for
variable names, etc. have also been applied.
2020-03-11 13:27:16 +01:00
gayyappan
91fe723d3a Drop chunks from materialized hypertables
Add support for dropping chunks from materialized
hypertables. drop_chunks_policy can now be set up
for materialized hypertables.
2020-02-26 11:50:58 -05:00
Erik Nordström
9da50cc686 Move enterprise features to community
As of this change, a number of enterprise features are accessible to
community users. These features include:

* reorder
* policies around reorder and drop chunks

The move chunks feature is still covered by enterprise. Gapfill no
longer warns about expired enterprise license.

Tests have been updated to reflect these changes.
2020-02-20 17:08:03 +01:00
gayyappan
565cca795a Support disabling compression when foreign keys are present
Fix failure with disabling compression when no
compressed chunks are present and the table has foreign
key constraints
2020-02-14 08:54:58 -05:00
gayyappan
b1b840f00e Use timescaledb prefix for compression errors
Modify compression parameter related error messages
to make them consistent.
2020-01-28 09:08:58 -05:00
Matvey Arye
6122e08fcb Fix error in compression constraint check
The constraint check previously assumed that the col_meta
offset for a column was equal to that columns attribute
offset. This is incorrect in the presence of dropped columns.

Fixed to match on column names.

Fixes #1590
2020-01-02 13:56:46 -05:00
Matvey Arye
85d30e404d Add ability to turn off compression
Since enabling compression creates limits on the hypertable
(e.g. types of constraints allowed) even if there are no
compressed chunks, we add the ability to turn off compression.
This is only possible if there are no compressed chunks.
2019-10-29 19:02:58 -04:00
Matvey Arye
2fe51d2735 Improve (de)compress_chunk API
This commit improves the API of compress_chunk and decompress_chunk:

- have it return the chunk regclass processed (or NULL in the
  idempotent case);
- mark it as STRICT
- add if_not_compressed/if_compressed options for idempotency
2019-10-29 19:02:58 -04:00
Matvey Arye
92aa77247a Improve minor UIUX
Some small improvements:

- allow alter table with empty segment by if the original definition
  had an empty segment by. Improve error msgs.
- block compression on tables with OIDs
- block compression on tables with RLS
2019-10-29 19:02:58 -04:00
Matvey Arye
7380efa0fe Add tests that constraint adding is blocked
Adding constraints to tables that have compression enabled should
be blocked for now.
2019-10-29 19:02:58 -04:00
Matvey Arye
a399a57af9 Block most DDL on hypertable with compression
Block most DDL commands on hypertables with compression enabled.
This restriction will be relaxed over time.

The only alter table commands currently allowed are:
Add/Remove index, Set storage options, clustering index,
set statistics and set tablespace.

We also disallow reseting compression options.
2019-10-29 19:02:58 -04:00
gayyappan
72588a2382 Restrict constraints on compressed hypertables.
Primary and unqiue constraints are limited to segment_by and order_by
columns and foreign key constraints are limited to segment_by columns
when creating a compressed hypertable. There are no restrictions on
check constraints.
2019-10-29 19:02:58 -04:00
Matvey Arye
0f3e74215a Split segment meta min_max into two columns
This simplifies the code and the access to the min/max
metadata. Before we used a custom type, but now the min/max
are just the same type as the underlying column and stored as two
columns.

This also removes the custom type that was used before.
2019-10-29 19:02:58 -04:00
Joshua Lockerman
64f56d5088 Create indexes on segmentby columns
This commit creates indexes on all segmentby columns of the compressed
hypertable.
2019-10-29 19:02:58 -04:00
Matvey Arye
8250714a29 Add fixes for Windows
- Fix declaration of functions wrt TSDLLEXPORT consistency
- Empty structs need to be created with '{ 0 }' syntax.
- Alignment sentinels have to use uint64 instead of a struct
  with a 0-size member
- Add some more ORDER BY clauses in the tests to constrain
  the order of results
- Add ANALYZE after running compression in
  transparent-decompression test
2019-10-29 19:02:58 -04:00
Matvey Arye
be946c436d Block add_drop_policy on internal compressed table
Add drop policy can only be added to the public uncompressed hypertable.
This blocks a call when a policy is added to the internal
hypertable with compressed data.
2019-10-29 19:02:58 -04:00
Matvey Arye
0db50e7ffc Handle drops of compressed chunks/hypertables
This commit add handling for dropping of chunks and hypertables
in the presence of associated compressed objects. If the uncompressed
chunk/hypertable is dropped than drop the associated compressed object
using DROP_RESTRICT unless cascading is explicitly enabled.

Also add a compressed_chunk_id index on compressed tables for
figuring out whether a chunk is compressed or not.

Change a bunch of APIs to use DropBehavior instead of a cascade bool
to be more explicit.

Also test the drop chunks policy.
2019-10-29 19:02:58 -04:00
gayyappan
6e60d2614c Add compress chunks policy support
Add and drop compress chunks policy using bgw
infrastructure.
2019-10-29 19:02:58 -04:00
Matvey Arye
ea7d2c7e60 Enforce license checks for compression
Enforce enterprise license check for compression. Note: these
checks are now outdated as compression is now a community,
not enterprise feature.
2019-10-29 19:02:58 -04:00
Matvey Arye
bce292a64f Fix locking when altering compression options
Take an exclusive lock when taking compression options as it is
safer.
2019-10-29 19:02:58 -04:00
Matvey Arye
cdf6fcb69a Allow altering compression options
We now allow changing the compression options on a hypertable
as long as there are no existing compressed chunks.
2019-10-29 19:02:58 -04:00
Matvey Arye
eba612ea2e Add time column to compressed order by list
Add the column to the order by list if it's not already there.
This is never wrong and might improve performance. This
also guarantees that we have at least one ordering column
during compression and therefore can always use tuplesort
(o/w we'd need a non-tuplesort method of getting tuples).
2019-10-29 19:02:58 -04:00
Matvey Arye
6f22a7a68c Improve parsing of segment by and order by lists
Replace custom parsing of order by and segment by lists
with the postgres parser. The segment by list is now
parsed in the same way as the GROUP BY clause and the
order by list in the same way as the ORDER BY clause.

Also fix default for nulls first/last to follow the PG
convention: LAST for ASC, FIRST for DESC.
2019-10-29 19:02:58 -04:00