Let's improve the documentation of upper()

From: Pietro Pugni <pietro(dot)pugni(at)gmail(dot)com>
To: pgsql-docs(at)postgresql(dot)org
Subject: Let's improve the documentation of upper()
Date: 2015-12-15 02:19:08
Message-ID: EBB489A4-94D2-45C9-AD03-DC492392EAB8@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-docs

Hi all,
I suggest to improve the documentation related to the upper() function for discrete sets.
It should be useful to report some examples using discrete type ranges like datarange or int8range.

I found this discussion on the bug mailing list: http://www.postgresql.org/message-id/20150116152713.2582.10294@wrigleys.postgresql.org <http://www.postgresql.org/message-id/20150116152713.2582.10294@wrigleys.postgresql.org>
It states that the mathematical definition used for upper bound is actually wrong for discrete sets when explicitly defining ranges as closed sets and I agree.
I was working a lot with dates when I first found this “bug”.

Users as me expect that upper(‘[1,2]’::int8range) returns 2 but it doesn’t. From the documentation it’s hard to find why PostgreSQL returns 3 instead of 2, so an example with a brief and mathematically clear explanation should be provided (the explanation provided is not clear at all).

Also, from the answers given in the link above I suspect that this function will be never modified to work as expected but I would like to ask if it could be done in the near future.

As for now the only way to return the actual mathematical upper bound of a discrete range is to change if from being right closed to right opened. This will return the expected value:
SELECT upper(‘[1,2)’::int8range)
but this is more a hack than a real solution because that range should contain only the number 1. Given integer numbers, the interval [1,2) translates to {1} but what the user really wants is {1,2}. Again, the upper bound of {1} is 1 while the upper bound of {1,2} is 2.

The definition described in the documentation also contrasts with the lower() function. Users like me expect a similar behavior for this function, but it doesn’t and instead is mathematically correct.

Regards
Pietro Pugni

Browse pgsql-docs by date

  From Date Subject
Next Message Adrien Nayrat 2015-12-24 13:51:23 Fix for pg_replication_origin_session_is_setup
Previous Message Alvaro Herrera 2015-12-11 17:34:56 Re: [HACKERS] max_worker_processes on the standby