src/main/java/com/conveyal/gtfs/loader/Table.java
private void applyAutoIncrementingSequence(Statement statement) throws SQLException {
        String selectMaxSql = String.format("SELECT MAX(id) + 1 FROM %s", name);

        int maxID = 0;
        LOG.info(selectMaxSql);
        statement.execute(selectMaxSql);
        ResultSet maxIdResult = statement.getResultSet();
        if (maxIdResult.next()) {
            maxID = maxIdResult.getInt(1);
        }
        // Set default max ID to 1 (the start value cannot be less than MINVALUE 1)
        // FIXME: Skip sequence creation if maxID = 1?
        if (maxID < 1) {
            maxID = 1;
        }

        String sequenceName = name + "_id_seq";
        String createSequenceSql = String.format("CREATE SEQUENCE %s START WITH %d", sequenceName, maxID);
        LOG.info(createSequenceSql);
        statement.execute(createSequenceSql);

        String alterColumnNextSql = String.format("ALTER TABLE %s ALTER COLUMN id SET DEFAULT nextval('%s')", name, sequenceName);
        LOG.info(alterColumnNextSql);
        statement.execute(alterColumnNextSql);
        String alterColumnNotNullSql = String.format("ALTER TABLE %s ALTER COLUMN id SET NOT NULL", name);
        LOG.info(alterColumnNotNullSql);
        statement.execute(alterColumnNotNullSql);
    }
private void applyAutoIncrementingSequence(Statement statement) throws SQLException {
        String selectMaxSql = String.format("SELECT MAX(id) + 1 FROM %s", name);

        int maxID = 0;
        LOG.info(selectMaxSql);
        statement.execute(selectMaxSql);
        ResultSet maxIdResult = statement.getResultSet();
        if (maxIdResult.next()) {
            maxID = maxIdResult.getInt(1);
        }
        // Set default max ID to 1 (the start value cannot be less than MINVALUE 1)
        // FIXME: Skip sequence creation if maxID = 1?
        if (maxID < 1) {
            maxID = 1;
        }

        String sequenceName = name + "_id_seq";
        String createSequenceSql = String.format("CREATE SEQUENCE %s START WITH %d", sequenceName, maxID);
        LOG.info(createSequenceSql);
        statement.execute(createSequenceSql);

        String alterColumnNextSql = String.format("ALTER TABLE %s ALTER COLUMN id SET DEFAULT nextval('%s')", name, sequenceName);
        LOG.info(alterColumnNextSql);
        statement.execute(alterColumnNextSql);
        String alterColumnNotNullSql = String.format("ALTER TABLE %s ALTER COLUMN id SET NOT NULL", name);
        LOG.info(alterColumnNotNullSql);
        statement.execute(alterColumnNotNullSql);
    }
Make id column serial and set the next value based on the current max value. This is intended to operate on
existing statement/connection and should not be applied to a table has been created with a serial (i.e., auto-
incrementing ID. This code is derived from https://stackoverflow.com/a/9490532/915811