It’s good to be David and Julia Koch.

New York’s richest couple just bought an Upper East Side mansion on a whim — for $40 million in cash, sources told The Post.

They came upon the 15,000-square-foot home when they visited a ritzy decorator show house earlier this summer.

The Kochs liked the stately mansion on East 76th Street so much, they plunked down $40.25 million.

In some circles, that could be considered a bargain, since the home had been listed for $51 million.

The seller was real-estate mogul Joseph Chetrit, who bought the property as part of six 19th-century row houses in 2007 for a combined $26 million and turned them into three double-wide mansions — gutting the interiors but keeping the facades intact.

The one the Kochs bought was the largest of the three.

“They just fell in love with it and felt like they had to have it,” a source said.

David and Julia met on a blind date and have been married since 1996. They have an estimated net worth of $47.5 billion.