在我的Postgres扩展pg_sample_ext中创建一个简单的Custom操作符,但在数据库中实现它时遇到错误.下面提到了更新脚本和错误的代码.

Update script pg_sample_ext--1.0.1--1.0.2.sql:

-- Create necessary objects for version 1.0.2

-- Custom Operator: @*
-- Description: Custom operator that multiplies two values of type my_type

-- Create a new type to use in our operator.
CREATE TYPE my_type AS (value int);

-- Create a SQL function that defines the behaviour of the custom operator.
-- This function multiplies the values of two my_type operands.
CREATE FUNCTION multiply_values(left my_type, right my_type) RETURNS my_type AS $$
    SELECT ROW((left.value * right.value))::int;
$$ LANGUAGE SQL IMMUTABLE;

-- Create a custom operator that multiplies two values of type my_type.
-- The operator symbol is @*.
-- It takes two operands of type my_type and returns a value of the same type.
-- The behaviour is defined by the SQL function multiply_values.
CREATE OPERATOR @* (
    PROCEDURE = multiply_values,
    LEFTARG = my_type,
    RIGHTARG = my_type
);

错误:

spartacus=# SELECT ROW(2)::my_type @* ROW(3)::my_type AS result;
错误:  syntax error at or near "."
LINE 2:     SELECT ROW((left.value * right.value))::my_type;
                        ^
QUERY:
    SELECT ROW((left.value * right.value))::my_type;

CONTEXT:  SQL function "multiply_values" during inlining

推荐答案

主要问题是rightleft是保留的SQL关键字,因此不能在没有双引号的情况下使用它们.避免使用这样的标识符.

此外,您的函数还必须构造一个新的my_type作为结果:

CREATE FUNCTION multiply_values("left" my_type, "right" my_type) RETURNS my_type
   LANGUAGE sql
   IMMUTABLE AS
'SELECT ROW("left".value * "right".value)::my_type';

Postgresql相关问答推荐

docker-compose.yml用于使用postgres的spring-boot应用程序

在Postgre中的链接服务器上执行远程查询

在Postgres游标中从一行变量中减go 另一行变量

如何将 jackc/pgx 与连接池、上下文、准备好的语句等一起使用

使用 pgx 扫描范围类型

PostgreSQL中如何在同一行中存储多个与单个值相关的ID?

在连接字符串并将其附加到 PostgreSQL 中的 where 子句时出现语法错误.怎么修?

Docker compose read connection reset by peer error on pipeline

联合所有 postgresql Select 子句保留顺序

使用 Heroku CLI、Postgres 的 SQL 语法错误

是否可以在 postgresql 中添加表元数据?

postgresql 更新错误ERROR: invalid input syntax for type boolean:

获取 OperationalError: FATAL: sorry, too many clients already using psycopg2

如何通过我在 PostgreSQL 中的数据库获取列大小和类型

PostgreSQL:如何安装 plpythonu 扩展

错误:CASE types character varying and numeric cannot be matched

如何增加 max_locks_per_transaction

无法在 postgresql hibernate 中使用名为user的表

PostGIS - 将多面体转换为单面体

如何在 PostgreSQL 中获取数组的最后一个元素?