MIN/MAX functions for a record - Mailing list pgsql-hackers

From Viliam Ďurina
Subject MIN/MAX functions for a record
Date
Msg-id CAO=iB8L4WYSNxCJ8GURRjQsrXEQ2-zn3FiCsh2LMqvWq2WcONg@mail.gmail.com
Whole thread Raw
Responses Re: MIN/MAX functions for a record
List pgsql-hackers
In my queries I often need to do MIN/MAX for tuples, for example:

  SELECT MAX(row(year, month)) 
  FROM (VALUES(2025, 1), (2024,2)) x(year, month);

This query throws:

    ERROR: function max(record) does not exist

In this case you can replace it with `MAX((year||'-'||month||'-1')::date)`. However in my case I have an event table with `event_time` and `text` columns, I'm grouping that table by some key and want to have the text for the newest event. I would do `MAX(ROW(event_time, text)).text`. Workarounds for this are clumsy, e.g. with a subquery with LIMIT 1.

The lack of this feature is kind of unexpected, because the `>` operator or `GREATEST` function are defined for records:

    SELECT 
        GREATEST((2025, 1), (2024, 2)), 
        (2025, 1) > (2024, 2)

Was this ever discussed or is there something preventing the implementation?

Viliam

pgsql-hackers by date:

Previous
From: Ashutosh Bapat
Date:
Subject: Re: Memory consumed by child SpecialJoinInfo in partitionwise join planning
Next
From: Sergey Prokhorenko
Date:
Subject: Re: UUID v7