Tax on traditional IRA distributions

I have a traditional IRA account and have contributed to this account off-and-on for the past twenty or so years. In some years, the contributions were tax deductible, while some other year they were not [due to income restrictions].


My understanding [this could very well be wrong] is that when I took distributions from non-deductible IRA, I'll have to pay taxes only on the gains and not on the contributions which were already taxed.

 

Does that mean that I have to keep tracks myself how much was from non-deductible bucket and how much was from deductible bucket?

 

That's a lot of digging into twent years or so history!