move more code to postgres