Try enforcing closed LineStrings by adding their ST_StartPoint to the end of the them:
SELECT ST_MakePolygon( ST_MakeValid( ST_AddPoint( shape. ST_StartPoint( shape ) ) ) ) AS shape
WHERE waterway ='riverbank'
If you want to make sure you are not duplicating points for when a LineString is already closed, run:
CREATE TABLE schema.polytable AS
SELECT column1, column2, ST_AsText(ST_CONVEXHULL(ST_COLLECT(geomcolumn))) AS newgeomcolumn
GROUP BY column1, column2
As suggested by someone else, this works in this scenario.
You need to move the subquery outside of the function call:
SELECT ST_x(geom) As X, ST_y(geom) As Y, ST_z(geom) As Z
JOIN building ON surface_geometry.id = building.id
WHERE geometry IS NOT NULL
AND cityobject_id = 95