This is a bit tricky, because your polygons aren't normalized in terms of orientation or starting point: some are oriented counter-clockwise, starting in the lower-left, while other are oriented clockwise, starting in the upper-left. This means that you can't use simple index-based calls to ST_PointN to pull out the the edge representing a specific edge of a polygon.
How to fix this? I'd run a simple
UPDATE composer SET geom = ST_Envelope(geom);
Now all of your polygons will be oriented clockwise, starting in the lower-left. With these standardized polygons, we know that:
- the western edge is defined by points 0 and 1
- the northern edge is defined by points 1 and 2
- the eastern edge is defined by points 2 and 3
- the southern edge is defined by points 3 and 4
Using this, you can use ST_PointN to extract the two endpoints that define each of the edges. A couple "gotchas": ST_PointN uses indexes that start at 1, not 0, and it only runs on LineStrings, not polygons. So, we use ST_Boundary to get the line representing our polygon's exterior ring. Now, we can pull out the endpoints for each edge and join the table back to itself:
UPDATE composer c
SET page_west =
(SELECT page_number FROM composer west
WHERE west.gid != c.gid
AND ST_Intersects(west.geom, ST_PointN(ST_Boundary(c.geom), 1))
AND ST_Intersects(west.geom, ST_PointN(ST_Boundary(c.geom), 2))),
page_north =
(SELECT page_number FROM composer north
WHERE north.gid != c.gid
AND ST_Intersects(north.geom, ST_PointN(ST_Boundary(c.geom), 2))
AND ST_Intersects(north.geom, ST_PointN(ST_Boundary(c.geom), 3))),
page_east =
(SELECT page_number FROM composer east
WHERE east.gid != c.gid
AND ST_Intersects(east.geom, ST_PointN(ST_Boundary(c.geom), 3))
AND ST_Intersects(east.geom, ST_PointN(ST_Boundary(c.geom), 4))),
page_south =
(SELECT page_number FROM composer south
WHERE south.gid != c.gid
AND ST_Intersects(south.geom, ST_PointN(ST_Boundary(c.geom), 4))
AND ST_Intersects(south.geom, ST_PointN(ST_Boundary(c.geom), 5)));