问题描述
这个问题几乎说明了一切。我正在尝试从具有联合主键的表中创建一个带有 TimescaleDB 的超表:
CREATE TABLE cars
(
id BIGINT NOT NULL GENERATED ALWAYS AS IDENTITY,time_bought TIMESTAMP NOT NULL,brand VARCHAR(100),);
ALTER TABLE cars ADD CONSTRAINT PK_id_time_bought PRIMARY KEY(id,time_bought);
SELECT create_hypertable('cars','time_bought');
当我尝试通过 Intellij 使用 Java 运行它时,出现此错误:
sql State : 42883
Error Code : 0
Message : ERROR: function create_hypertable(unkNown,unkNown) does not exist
Hint: No function matches the given name and argument types. You might need to add explicit type casts.
Position: 8
Location : db/migration/tenants/V1__init_schema.sql (C:\example\target\classes\db\migration\tenants\V1__init_schema.sql)
Line : 45
Statement : SELECT create_hypertable('cars','time_bought')
更新:我尝试在没有在表中放置任何主键的情况下运行迁移,但它仍然给出相同的错误。问题是 Flyway 根本不支持 TimescaleDB 功能吗?如果是这样,我该如何解决?
解决方法
根据 documentation of create_hypertable
对它的调用在我看来是正确的。所以很可能找不到 TimescaleDB 函数。常见的原因有:
- 未在数据库中创建 TimescaleDB 扩展。
- TimescaleDB 函数的架构与当前架构不同。
TimescaleDB 扩展是按数据库创建的。因此,如果它是在一个数据库中创建的,则在另一个数据库中将不可用。如果扩展已创建,可以使用 \dx
进行检查。例如
\dx
List of installed extensions
Name | Version | Schema | Description
---------+---------+------------+------------------------------
plpgsql | 1.0 | pg_catalog | PL/pgSQL procedural language
(1 row)
create extension timescaledb;
\dx
List of installed extensions
Name | Version | Schema | Description
-------------+-----------+------------+-------------------------------------------------------------------
plpgsql | 1.0 | pg_catalog | PL/pgSQL procedural language
timescaledb | 2.3.0-dev | public | Enables scalable inserts and complex queries for time-series data
(2 rows)
请注意,扩展是在架构 public
中创建的。
因此,如果会话不在同一架构中,例如 public
,则将找不到该函数。可以使用 SELECT current_schema;
检查当前架构。如果不是相同的模式,则应在函数调用中提供模式名称。例如:
SELECT current_schema;
current_schema
----------------
test_schema
(1 row)
SELECT create_hypertable('my_table','time_column');
ERROR: function create_hypertable(unknown,unknown) does not exist
LINE 1: SELECT create_hypertable('my_table','time_column');
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
SELECT public.create_hypertable('my_table','time_column');