CREATE TABLE test_mapalgebra(
	rid serial PRIMARY KEY,
	rast raster
);
CREATE OR REPLACE FUNCTION make_test_raster()
	RETURNS void
	AS $$
	DECLARE
		width int := 2;
		height int := 2;
		x int;
		y int;
		rast raster;
	BEGIN
		rast := ST_MakeEmptyRaster(width, height, 0, 0, 1, -1, 0, 0);
		rast := ST_AddBand(rast, 1, '8BUI', 0, 0);

--hard coding value 1 and 10 in the 2 cells
				rast := ST_SetValue(rast, 1, 1, 1, 10);
				rast := ST_SetValue(rast, 1, 2, 1, 10);
		INSERT INTO test_mapalgebra (rast) VALUES (rast);

		rast := ST_MakeEmptyRaster(width, height, 0, 0, 1, -1, 0, 0);
		rast := ST_AddBand(rast, 1, '8BUI', 0, 0);
--hard coding 2 and 7 in the 2 cells
				rast := ST_SetValue(rast, 1, 1, 1, 2);
				rast := ST_SetValue(rast, 1, 2, 1, 7);
		INSERT INTO test_mapalgebra (rast) VALUES (rast);		
		RETURN;
	END;
	$$ LANGUAGE 'plpgsql';
SELECT make_test_raster();
DROP FUNCTION make_test_raster();

-- First, take a look at the answer from the substraction:

WITH foo AS (
	SELECT
		ST_MapAlgebraExpr(a.rast, b.rast, '(([rast1]-[rast2]))::int', '8BUI') AS rast
	FROM test_mapalgebra a
	CROSS JOIN test_mapalgebra b
	WHERE a.rid = 1
		AND b.rid = 2
)
SELECT
	(ST_BandMetadata(rast)).*,
	(ST_SummaryStats(rast)).*
FROM foo;

/* I get:
 pixeltype | hasnodata | nodatavalue | isoutdb | path | count | sum | mean | stddev | min | max 
-----------+-----------+-------------+---------+------+-------+-----+------+--------+-----+-----
 8BUI      | t         |           0 | f       |      |     2 |  11 |  5.5 |    2.5 |   3 |   8


just as expected*/

-- then we test the conditional version with boolean output:



WITH foo AS (
	SELECT
		ST_MapAlgebraExpr(a.rast, b.rast, '(([rast1]-[rast2]) > 5)::int', '1BB') AS rast
	FROM test_mapalgebra a
	CROSS JOIN test_mapalgebra b
	WHERE a.rid = 1
		AND b.rid = 2
)
SELECT
	(ST_BandMetadata(rast)).*,
	(ST_SummaryStats(rast)).*
FROM foo;



/* Here I get:
 pixeltype | hasnodata | nodatavalue | isoutdb | path | count | sum | mean | stddev | min | max 
-----------+-----------+-------------+---------+------+-------+-----+------+--------+-----+-----
 1BB       | t         |           0 | f       |      |     1 |   1 |    1 |      0 |   1 |   1
(1 row)


So both pixels returns true as I understand it.

The very strange thing is that if I play with the expression and changes 5 to 9 then both values become false (or 0), which is actually the expected result.
 But at once I get under 8, instead of giving one false and one true I get both true.

 I get the same result if I change pixeltype to 8BUI so I think it seems to be the expression itself, not the casting to boolean.
*/
 