code: def collect(self):
          
          if psycopg2 is None:
              self.log.error('Unable to import module psycopg2')
              return {}
  
          # Get list of databases
          dbs = self._get_db_names()
          if len(dbs) == 0:
              self.log.error("I have 0 databases!")
              return {}
  
          if self.config['metrics']:
              metrics = self.config['metrics']
          elif str_to_bool(self.config['extended']):
              metrics = registry['extended']
              if str_to_bool(self.config['has_admin']) \
                      and 'WalSegmentStats' not in metrics:
                  metrics.append('WalSegmentStats')
  
          else:
              metrics = registry['basic']
  
          # Iterate every QueryStats class
          for metric_name in set(metrics):
              if metric_name not in metrics_registry:
                  self.log.error(
                      'metric_name %s not found in metric registry' % metric_name)
                  continue
  
              for dbase in dbs:
                  conn = self._connect(database=dbase)
                  try:
                      klass = metrics_registry[metric_name]
                      stat = klass(dbase, conn,
                                   underscore=self.config['underscore'])
                      stat.fetch(self.config['pg_version'])
                      for metric, value in stat:
                          if value is not None:
                              self.publish(metric, value)
  
                      # Setting multi_db to True will run this query on all known
                      # databases. This is bad for queries that hit views like
                      # pg_database, which are shared across databases.
                      #
                      # If multi_db is False, bail early after the first query
                      # iteration. Otherwise, continue to remaining databases.
                      if stat.multi_db is False:
                          break
                  finally:
                      conn.close()
docs: Do pre-flight checks, get list of db names, collect metrics, publish