title: Loop query on every server
body: <p>Let's say i have 20 servers in my DBeaver (Postgres 10) with exactly the same Database.</p>
  <p>I have 1 other Database that holds information about this 20 server.
  In this Database i have one table named server.</p>
  <p>with <code>select id,name from servers</code> i get id and name from the other 20.</p>
  <p>Now my goal is make a query that loops thruu this 20 servers and just gathering the amount of users on them.</p>
  <pre><code>select count (*) from users
  </code></pre>
  <p>I was thinking of making a long query with dblink function and just make an union all after each server like this.</p>
  <pre><code>Select * from dblink(
  'host=srv-01
  user=admin
  password=****
  dbname=srv-01',
  '
  select count (*) as total from users u '
  as (total integer)
  
  union all 
  
  Select * from dblink(
  'host=srv-02
  user=admin
  password=****
  dbname=srv-02',
  '
  select count (*) as total from users u '
  as (total integer)
  </code></pre>
  <p>The problem here is i also want to show the server name in my output but it's not possible like this.</p>
  <p>--&gt; Wished output</p>
  <pre><code>server   total
  srv-01    5
  srv-02    6
  </code></pre>
  <p>Since i have a list of the amount of servers and the names inside the server table i should be able to some kind of loop on the list of server from there or?</p>
  <p>Like looping thruu all 20 servers and then for each server execute the sql with union.</p>
  <p>Here is an example of my server table and of a users table
  <a href="https://dbfiddle.uk/?rdbms=postgres_10&amp;fiddle=f77a6fa6881ecd8b9e3e05b2624b7715" rel="nofollow noreferrer">example</a></p>
  <p>Problem is i don't know how? Can someone please help me?</p>
  <p>Thanks</p>