public long getNextCSIdForMPartitionColumnStatistics(long numStats) throws MetaException {
    long maxCsId = 0;
    Transaction tx = pm.currentTransaction();
    try {
      dbType.lockInternal();
      tx.begin();
      JDOConnection jdoConn = null;
      try {
        jdoConn = pm.getDataStoreConnection();
        Connection dbConn = (Connection) jdoConn.getNativeConnection();

        setAnsiQuotes(dbConn);

        // This loop will be iterated at max twice. If there is no records, it will first insert and then do a select.
        // We are not using any upsert operations as select for update and then update is required to make sure that
        // the caller gets a reserved range for CSId not used by any other thread.
        boolean insertDone = false;
        while (maxCsId == 0) {
          String query = sqlGenerator.addForUpdateClause(
              "SELECT \"NEXT_VAL\" FROM \"SEQUENCE_TABLE\" " + "WHERE \"SEQUENCE_NAME\"= " + quoteString(
                  "org.apache.hadoop.hive.metastore.model.MPartitionColumnStatistics"));
          LOG.debug("Execute query: " + query);
          try (Statement statement = dbConn.createStatement(); ResultSet rs = statement.executeQuery(query)) {
            if (rs.next()) {
              maxCsId = rs.getLong(1);
            } else if (insertDone) {
              throw new MetaException("Invalid state of SEQUENCE_TABLE for MPartitionColumnStatistics");
            } else {
              insertDone = true;
              query = "INSERT INTO \"SEQUENCE_TABLE\" (\"SEQUENCE_NAME\", \"NEXT_VAL\")  VALUES ( " + quoteString(
                  "org.apache.hadoop.hive.metastore.model.MPartitionColumnStatistics") + "," + 1 + ")";
              try {
                statement.executeUpdate(query);
              } catch (SQLException e) {
                // If the record is already inserted by some other thread continue to select.
                if (dbType.isDuplicateKeyError(e)) {
                  continue;
                }
                LOG.error("Unable to insert into SEQUENCE_TABLE for MPartitionColumnStatistics.", e);
                throw e;
              }
            }
          }
        }

        long nextMaxCsId = maxCsId + numStats + 1;
        String query = "UPDATE \"SEQUENCE_TABLE\" SET \"NEXT_VAL\" = " + nextMaxCsId + " WHERE \"SEQUENCE_NAME\" = " + quoteString(
            "org.apache.hadoop.hive.metastore.model.MPartitionColumnStatistics");

        try (Statement statement = dbConn.createStatement()) {
          statement.executeUpdate(query);
        }
      } finally {
        closeDbConn(jdoConn);
      }
      tx.commit();
      return maxCsId;
    } catch (Exception e) {
      LOG.error("Unable to getNextCSIdForMPartitionColumnStatistics", e);
      throw new MetaException("Unable to getNextCSIdForMPartitionColumnStatistics  "
              + " due to: " + e.getMessage());
    } finally {
      if (tx.isActive()) {
        tx.rollback();
      }
      dbType.unlockInternal();
    }
  }
Gets the next CS id from sequence MPartitionColumnStatistics and increment the CS id by numStats.
@return The CD id before update.
standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/DirectSqlUpdatePart.java