SELECT DISTINCT 
  -- document
    t0.id, t0.createdat, t0.modifiedat, t0.validuntil, t0.envelope_id
  , t0.firstprint_id, t0.followsdocument_id, t0.iscomplete
  , t0.isdispatched, t0.ispublishedindb, t0.isreconstructed
  , t0.location_id, t0.numberofpages, t0.numberofsheets
  , t0.parentdocument_id, t0.reviewer_id, t0.signature, t0.bequest_id
  , t0.city_id, t0.documentcategory, t0.documentcontentteibody
  , t0.datetype, t0.enddatestatus, t0.endday, t0.endmonth, t0.endyear
  , t0.startdatestatus, t0.startday, t0.startmonth, t0.startyear
  , t0.documentdelivery_id, t0.documentid, t0.documentstatus
  -- historical person
  , t4.id, t4.createdat, t4.modifiedat, t4.validuntil, t4.firstname
  , t4.surname, t4.title, t4.birthdatetype, t4.birthstartdatestatus
  , t4.birthstartday, t4.birthstartmonth, t4.birthstartyear
  , t4.birthendstatus, t4.birthendday, t4.birthendmonth, t4.birthendyear
  , t4.deathdatetype, t4.deathstartdatestatus, t4.deathstartday
  , t4.deathstartmonth, t4.deathstartyear, t4.deathendstatus
  , t4.deathendday, t4.deathendmonth, t4.deathendyear, t4.dnbref
  , t4.gender, t4.isinstitution, t4.personid, t4.pseudonym, t4.wikilink
  -- extended biography
  , t5.id, t5.createdat, t5.modifiedat, t5.validuntil
  -- sitecity birth
  , t6.id, t6.createdat, t6.modifiedat, t6.validuntil
  , t6.extendedbiography_id, t6.city, t6.country, t6.dnbref, t6.region
  , t6.sitecityid, t6.wikilink, t6.zipcode
  -- sitecity death
  , t7.id, t7.createdat, t7.modifiedat, t7.validuntil
  , t7.extendedbiography_id, t7.city, t7.country, t7.dnbref, t7.region
  , t7.sitecityid, t7.wikilink, t7.zipcode
  -- sitecity
  , t8.id, t8.createdat, t8.modifiedat, t8.validuntil
  , t8.extendedbiography_id, t8.city, t8.country, t8.dnbref, t8.region
  , t8.sitecityid, t8.wikilink, t8.zipcode
  -- appuser
  , t9.id, t9.createdat, t9.modifiedat, t9.validuntil, t9.activated
  , t9.emailaddress, t9.firstname, t9.institution, t9.lastlogindate
  , t9.loggedin, t9.loggedinsince, t9.loginname, t9.password
  , t9.registrationdate, t9.salt, t9.surname, t9.title
  -- appuserrole
  , t10.id, t10.createdat, t10.modifiedat, t10.validuntil
  , t10.description, t10.userrole
FROM   public.Document t0 
  LEFT OUTER JOIN public.DocumentCoAuthorPerson t1 ON t0.id = t1.document_id
  LEFT OUTER JOIN public.DocumentAddresseePerson t2 ON t0.id = t2.document_id
  LEFT OUTER JOIN public.historicalperson t3 ON t0.authorperson_id = t3.id
  LEFT OUTER JOIN public.historicalperson t4 ON t0.authorperson_id = t4.id
  LEFT OUTER JOIN public.sitecity t8 ON t0.city_id = t8.id
  LEFT OUTER JOIN public.appuser t9 ON t0.editor_id = t9.id
  LEFT OUTER JOIN public.extendedbiography t5 ON t4.extendedbiography_id = t5.id
  LEFT OUTER JOIN public.sitecity t6 ON t4.sitecity_birth_id = t6.id
  LEFT OUTER JOIN public.sitecity t7 ON t4.sitecity_death_id = t7.id
  LEFT OUTER JOIN public.appuserrole t10 ON t9.appuserrole_id = t10.id
WHERE  (t0.validuntil > NOW()
    AND t0.ispublishedindb = true
    AND (t1.validuntil > NOW() OR t1.id IS NULL)
    AND (t2.validuntil > NOW() OR t2.id IS NULL)
    AND 1 = 1
       )
ORDER  BY t0.startyear DESC, t0.startmonth DESC, t0.startday DESC
LIMIT 400