private boolean hasRemainingCDReference(MColumnDescriptor oldCD) {
    assert oldCD != null;
    Query query = null;

    /**
     * In order to workaround oracle not supporting limit statement caused performance issue, HIVE-9447 makes
     * all the backend DB run select count(1) from SDS where SDS.CD_ID=? to check if the specific CD_ID is
     * referenced in SDS table before drop a partition. This select count(1) statement does not scale well in
     * Postgres, and there is no index for CD_ID column in SDS table.
     * For a SDS table with with 1.5 million rows, select count(1) has average 700ms without index, while in
     * 10-20ms with index. But the statement before
     * HIVE-9447( SELECT * FROM "SDS" "A0" WHERE "A0"."CD_ID" = $1 limit 1) uses less than 10ms .
     */
    try {
      // HIVE-21075: Fix Postgres performance regression caused by HIVE-9447
      LOG.debug("The dbType is {} ", dbType.getHiveSchemaPostfix());
      if (dbType.isPOSTGRES() || dbType.isMYSQL()) {
        query = pm.newQuery(MStorageDescriptor.class, "this.cd == inCD");
        query.declareParameters("MColumnDescriptor inCD");
        List<MStorageDescriptor> referencedSDs = null;
        LOG.debug("Executing listStorageDescriptorsWithCD");
        // User specified a row limit, set it on the Query
        query.setRange(0L, 1L);
        referencedSDs = (List<MStorageDescriptor>) query.execute(oldCD);
        LOG.debug("Done executing query for listStorageDescriptorsWithCD");
        pm.retrieveAll(referencedSDs);
        LOG.debug("Done retrieving all objects for listStorageDescriptorsWithCD");
        //if no other SD references this CD, we can throw it out.
        return referencedSDs != null && !referencedSDs.isEmpty();
      } else {
        query = pm.newQuery(
                "select count(1) from org.apache.hadoop.hive.metastore.model.MStorageDescriptor where (this.cd == inCD)");
        query.declareParameters("MColumnDescriptor inCD");
        long count = (Long) query.execute(oldCD);
        //if no other SD references this CD, we can throw it out.
        return count != 0;
      }
    } finally {
      if (query != null) {
        query.closeAll();
      }
    }
  }
Checks if a column descriptor has any remaining references by storage descriptors
in the db.
@param oldCD the column descriptor to check if it has references or not
@return true if has references
standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/ObjectStore.java