When you do something extremely carefully, you do it painstakingly. Your grandmother is going to love that quilt you worked on so painstakingly, sewing each individual stitch by hand.
If you work so hard and diligently at something that it practically hurts, you do it painstakingly. Take this word apart and it's easy to see it's all about taking pains to do things the right way, so anything that causes you a bit of agony to get just right can be described this way. If you painstakingly iced the names of your party guests on individual cupcakes, it'll be a little painful to watch them be gobbled up in three minutes.