Is there an SQL construct to get it?
pgsql-general by date:
Соглашаюсь с условиями обработки персональных данных