You are in the right place.
Lower octane fuels ignite more easily, so they ignite earlier and burn faster. In higher compression engines this can be a problem because they can ignite before the crank reaches the correct position, pressure being one of the factors in the ignition point. This produces the characteristic "knocking" or "pinking". It is pre-detonation and it is harmful as well as power-sapping.
So they add chemicals that delay the ignition. They raise the octane of the gas so that it detonates at the correct time, but they also have less energy per gallon ... Not much less.
In a lean running motor, the engine runs hotter by default, and that can cause pre-ignition if the octane rating is too low, because heat is one of the components that determines exactly when the gas will ignite.
Heat, pressure, spark timing, mixture ratio, octane rating ... the factors that influence the detonation point.