Database Management PostgreSQL Subjective
Sep 25, 2025

What are PostgreSQL custom operators and how to create them?

Detailed Explanation

Custom operators allow defining new symbols for operations on data types.

Example:

-- Create function first
CREATE FUNCTION add_percent(numeric, numeric)
RETURNS numeric AS $$
BEGIN
RETURN $1 + ($1 * $2 / 100);
END;
$$ LANGUAGE plpgsql;

-- Create operator
CREATE OPERATOR +% (
LEFTARG = numeric,
RIGHTARG = numeric,
FUNCTION = add_percent
);

-- Usage
SELECT 100 +% 15; -- Returns 115

Benefits:
• Domain-specific syntax
• Improved readability
• Custom data type operations

Discussion (0)

No comments yet. Be the first to share your thoughts!

Share Your Thoughts
Feedback