if you do data processing of source material in postgres, and haven't yet gotten into Window functions, you are missing out. You can offload entire workloads and code pipelines to the database, which can probably figure out how to handle what you're doing more efficiently than you can; work in some Common Table Expressions (WITH) and you have a grammar for doing all sorts of things via queries that you'd normally start with procedural code for
https://www.postgresql.org/docs/current/tutorial-window.html
https://www.postgresql.org/docs/current/queries-with.html