The announcement that a Houston-area Republican congressman is retiring has set up what could be one of the most closely contested House races in the country and provided a glimpse of how much more competitive parts of Texas have become.

Representative Pete Olson, first elected in 2008, announced on Thursday that he would not seek a seventh term in his increasingly diverse district. Democrats had viewed the seat as one that could come open because it has been increasingly difficult for Republicans to defend.

Mr. Olson cited family reasons for stepping down at the end of this session, saying in a statement that his time in Congress had come “at great personal sacrifice to my family.” His statement added, “As someone who has long advocated for policies that put our families first, it’s time for me to take my own advice and be a more consistent presence to help our family.”

His office said he was not available for an interview on Friday.

The district — Texas’ 22nd, in the southern suburbs of Houston — has been represented by a Republican for all but two of the past 40 years; the former House majority leader Tom DeLay held it for two decades. But the 2018 election there was unusually close: Mr. Olson won by five percentage points, compared with 19 points in 2016. At the presidential level, the district went Republican by eight points in 2016, down from 25 points four years earlier.