Add NEWER_92_METRICS to the default set of COMMON_METRICS when server
        version is 9.2 or later.

        Store the list of metrics in the check instance to avoid rebuilding it at
        every collection cycle.

        In case we have multiple instances pointing to the same postgres server
        monitoring different databases, we want to collect server metrics
        only once. See https://github.com/DataDog/dd-agent/issues/1211

def _get_instance_metrics(self, key, db, database_size_metrics, collect_default_db):
        """
        Add NEWER_92_METRICS to the default set of COMMON_METRICS when server
        version is 9.2 or later.

        Store the list of metrics in the check instance to avoid rebuilding it at
        every collection cycle.

        In case we have multiple instances pointing to the same postgres server
        monitoring different databases, we want to collect server metrics
        only once. See https://github.com/DataDog/dd-agent/issues/1211
        """
        metrics = self.instance_metrics.get(key)

        if metrics is None:
            host, port, dbname = key
            # check whether we already collected server-wide metrics for this
            # postgres instance
            if self._server_known(host, port):
                # explicitly set instance metrics for this key to an empty list
                # so we don't get here more than once
                self.instance_metrics[key] = []
                self.log.debug(
                    "Not collecting instance metrics for key: {} as "
                    "they are already collected by another instance".format(key)
                )
                return None
            self._set_server_known(host, port)

            # select the right set of metrics to collect depending on postgres version
            if self._is_9_2_or_above(key, db):
                self.instance_metrics[key] = dict(self.COMMON_METRICS, **self.NEWER_92_METRICS)
            else:
                self.instance_metrics[key] = dict(self.COMMON_METRICS)

            # add size metrics if needed
            if database_size_metrics:
                self.instance_metrics[key].update(self.DATABASE_SIZE_METRICS)

            metrics = self.instance_metrics.get(key)

        # this will happen when the current key contains a postgres server that
        # we already know, let's avoid to collect duplicates
        if not metrics:
            return None

        res = {
            'descriptors': [('psd.datname', 'db')],
            'metrics': metrics,
            'query': "SELECT psd.datname, %s "
            "FROM pg_stat_database psd "
            "JOIN pg_database pd ON psd.datname = pd.datname "
            "WHERE psd.datname not ilike 'template%%' "
            "  AND psd.datname not ilike 'rdsadmin' "
            "  AND psd.datname not ilike 'azure_maintenance' ",
            'relation': False,
        }

        if not collect_default_db:
            res["query"] += "  AND psd.datname not ilike 'postgres'"

        return res