title: How to index subgroups in table in Postgres?
body: <p>Supposing I have a table like this:</p>
  
  <pre><code>select country_id, city_id, person_id from mytable
  
  
  country_id,city_id,person_id
  123,45,100334
  123,45,3460456
  123,45,943875
  123,121,4362
  123,121,124747
  146,87,3457320
  146,89,3495879
  146,89,34703924
  </code></pre>
  
  <p>I want to index the subgroups of country_id and city_id to get such result:</p>
  
  <pre><code>select country_id, city_id, person_id, ???, ??? from mytable
  
  country_id,city_id,person_id,country_num,city_num
  123,45,100334,1,1
  123,45,3460456,1,1
  123,45,943875,1,1
  123,121,4362,1,2
  123,121,124747,1,2
  146,87,3457320,2,1
  146,89,3495879,2,2
  146,89,34703924,2,2
  </code></pre>
  
  <p>In other words, I want to numerate all countries in the sequence with integer numbers from 1, and also I want to mark cities the same way within each country separately. Is there an elegant way to do it in Postgres?</p>