Hi Pietr,
I'm sorry but I don't agree with this type of approach at all.
An SQL function should do a single, clearly identifiable thing,
avoiding any form of automagic or silver bullets as much as
possible.
SQL functions (and SQL queries too) should be seen as successive
stages of a pipeline, keeping each step isolated and clearly
identifiable.
Even more so when processing large datasets that require long
processing times.
In these cases the correct approach is to automate the processes
using more or less complex SQL scripts so to always allow the
identification of all possible anomalies which will then have
to be remedied subsequently (probably by applying some manual
correction).
At least, this is the lesson I learned when in past years I
worked side by side with the cartographers of the Tuscany
Region with their huge 20 million polygons topological
datasets.
The best solution was always to develop a SQL script
(possibly of hundreds of queries) that worked exactly
like a pipeline, and which at the end presented a list
of (few) problematic features to check and possibly fix.
Let's go back to the use case you hypothesized:
1) following a professional approach ST_IsValid() and
eventually MakeValid() should always be executed in
the very early stages of the pipeline, because it's
well known that very often the input datasets can
be dirty.
it makes no sense to automagically delegate this
absolutely critical preliminary step to the SQL
functions that will be called later.
2) even when ST_ReducePrecision() returns invalid
geometries (NULL), it's not at all difficult for
the Query of the next step of the pipeline to
recover the original values.
but at least this way a clear trace will be kept
of all the critical issues encountered, which can
then be verified case by case trying to identify
(and possibly correct) the offending causes.
If instead we start introducing silver bullets everywhere
we'll eventually lose any form of atomic control over
operations.
Perhaps such an approach would be appreciated by less savvy
users who are in a hurry, but would deeply disappoint
more professional users.
bye Sandro