ST_MapAlgebra (expression version) — Expression version - Returns a one-band raster given one or two input rasters, band indexes and one or more user-specified SQL expressions.
raster ST_MapAlgebra(raster  rast, integer  nband, text  pixeltype, text  expression, double precision  nodataval=NULL);
raster ST_MapAlgebra(raster  rast, text  pixeltype, text  expression, double precision  nodataval=NULL);
raster ST_MapAlgebra(raster  rast1, integer  nband1, raster  rast2, integer  nband2, text  expression, text  pixeltype=NULL, text  extenttype=INTERSECTION, text  nodata1expr=NULL, text  nodata2expr=NULL, double precision  nodatanodataval=NULL);
raster ST_MapAlgebra(raster  rast1, raster  rast2, text  expression, text  pixeltype=NULL, text  extenttype=INTERSECTION, text  nodata1expr=NULL, text  nodata2expr=NULL, double precision  nodatanodataval=NULL);
Expression version - Returns a one-band raster given one or two input rasters, band indexes and one or more user-specified SQL expressions.
Availability: 2.1.0
                        Creates a new one band raster formed by applying a valid PostgreSQL algebraic operation defined by the expression on the input raster (rast). If nband is not provided, band 1 is assumed. The new raster will have the same georeference, width, and height as the original raster but will only have one band.
                    
                        If pixeltype is passed in, then the new raster will have a band of that pixeltype.  If pixeltype is passed NULL, then the new raster band will have the same pixeltype as the input rast band.
                    
Keywords permitted for expression
[rast] - Pixel value of the pixel of interest
[rast.val] - Pixel value of the pixel of interest
[rast.x] - 1-based pixel column of the pixel of interest
[rast.y] - 1-based pixel row of the pixel of interest
                        Creates a new one band raster formed by applying a valid PostgreSQL algebraic operation to the two bands defined by the expression on the two input raster bands rast1, (rast2). If no band1,  band2 is specified band 1 is assumed. The resulting raster will be aligned (scale, skew and pixel corners) on the grid defined by the first raster. The resulting raster will have the extent defined by the extenttype parameter.
                    
A PostgreSQL algebraic expression involving the two rasters and PostgreSQL defined functions/operators that will define the pixel value when pixels intersect. e.g. (([rast1] + [rast2])/2.0)::integer
The resulting pixel type of the output raster. Must be one listed in ST_BandPixelType, left out or set to NULL. If not passed in or set to NULL, will default to the pixeltype of the first raster.
Controls the extent of resulting raster
                                            INTERSECTION - The extent of the new raster is the intersection of the two rasters. This is the default.
                                        
                                            UNION - The extent of the new raster is the union of the two rasters.
                                        
                                            FIRST - The extent of the new raster is the  same as the one of the first raster.
                                        
                                            SECOND - The extent of the new raster is the  same as the one of the second raster.
                                        
                                    An algebraic expression involving only rast2 or a constant that defines what to return when pixels of rast1 are nodata values and spatially corresponding rast2 pixels have values.
                                
                                    An algebraic expression involving only rast1 or a constant that defines what to return when pixels of rast2 are nodata values and spatially corresponding rast1 pixels have values.
                                
A numeric constant to return when spatially corresponding rast1 and rast2 pixels are both nodata values.
Keywords permitted in expression, nodata1expr and nodata2expr
[rast1] - Pixel value of the pixel of interest from rast1
[rast1.val] - Pixel value of the pixel of interest from rast1
[rast1.x] - 1-based pixel column of the pixel of interest from rast1
[rast1.y] - 1-based pixel row of the pixel of interest from rast1
[rast2] - Pixel value of the pixel of interest from rast2
[rast2.val] - Pixel value of the pixel of interest from rast2
[rast2.x] - 1-based pixel column of the pixel of interest from rast2
[rast2.y] - 1-based pixel row of the pixel of interest from rast2
WITH foo AS (
    SELECT ST_AddBand(ST_MakeEmptyRaster(10, 10, 0, 0, 1, 1, 0, 0, 0), '32BF'::text, 1, -1) AS rast
)
SELECT
    ST_MapAlgebra(rast, 1, NULL, 'ceil([rast]*[rast.x]/[rast.y]+[rast.val])')
FROM foo;
                    
WITH foo AS (
    SELECT 1 AS rid, ST_AddBand(ST_AddBand(ST_AddBand(ST_MakeEmptyRaster(2, 2, 0, 0, 1, -1, 0, 0, 0), 1, '16BUI', 1, 0), 2, '8BUI', 10, 0), 3, '32BUI'::text, 100, 0) AS rast UNION ALL
    SELECT 2 AS rid, ST_AddBand(ST_AddBand(ST_AddBand(ST_MakeEmptyRaster(2, 2, 0, 1, 1, -1, 0, 0, 0), 1, '16BUI', 2, 0), 2, '8BUI', 20, 0), 3, '32BUI'::text, 300, 0) AS rast
)
SELECT
    ST_MapAlgebra(
        t1.rast, 2,
        t2.rast, 1,
        '([rast2] + [rast1.val]) / 2'
    ) AS rast
FROM foo t1
CROSS JOIN foo t2
WHERE t1.rid = 1
    AND t2.rid = 2;