How to get PIDs of processes with most network usage in descending order

I’m using my phone’s hotspot for using internet on my laptop with Ubuntu 18.04 installed. But even if I’m not doing anything, my laptop is still using the data and consumed my whole data pack within 20 minutes.

This is happening from last 3 days and I’m looking for a solution. I want to know what exactly is using this much data?

On Windows, network usage is directly visible in Task Manager. So, I guess there is an equivalent way to do the same on Linux.

I tried using ps command but I don’t think it gives details about network usage (Correct me if I’m wrong).

Also, I tried searching on stackoverflow and came across tools like iftop and many others. But I’m not able to find any details about the cause of the issue from any of the tools.

I’m not even able to install those tools mentioned on the articles found on the web/stackoverflow.

So, I want to know if there is any command that will sort the processes according to the network usage and that too without using any tools that need to be installed.

Is there any way to do this?

What caused queries to execute repeatedly with different pids?

Recently I have noticed some of the queries are repeatedly being executed with new pids, this results in 100% CPU utilization.

So I queried my postgres to see what’s going on, with:

SELECT pid, xact_start, datname, usename, query FROM pg_stat_activity ORDER BY xact_start ASC;

pid   |        xact_start       | datname    | usename     | query  4095  |  2017-06-27 with time.. | db_name    | role_name   | SELECT .... 4096  |  2017-06-27 with time.. | db_name    | role_name   | SELECT .... 4098  |  2017-06-27 with time.. | db_name    | role_name   | SELECT .... 4099  |  2017-06-27 with time.. | db_name    | role_name   | SELECT .... 4100  |  2017-06-27 with time.. | db_name    | role_name   | SELECT .... 4120  |  2017-06-27 with time.. | db_name    | role_name   | SELECT .... 4125  |  2017-06-27 with time.. | db_name    | role_name   | SELECT .... 

Above queries are identical that are being executed at different time. there is a huge list.

Also checked what time it took for execution:

SELECT max(now() - xact_start) FROM pg_stat_activity WHERE state IN ('idle in transaction', 'active');

Which recorded more than 20 seconds.

        max  -----------------  00:00:20.428998  (1 row) 

Resolution:

What I have done to resolve this is make a db dump and re-import it to a new db. This should have cleared some thing from the database, which I’m trying to get an understanding of.

Possibilities but unknown:

  1. pg_dump should have cleared some thing from database i.e; cached tables or cleared locked queries.
  2. While restored it should have cleared something.

But What Happened? can someone help me understand what changed when I created a new db and imported the old db into it? because now my CPU load is at 10% from 100%.