How to run a function once on every post in database?

Yes, this is possible. Something like this should affect all posts that are currently published. Literally run this once and then disable it by commenting out the add_action or you will have a bunch of unwanted queries to the database.

Running once: With this code placed in your functions.php, the function will run when the front end of your site is loaded in a browser. Load the home page of your site in a browser then disable the function. Confirm that your post format was applied to existing posts.

     add_action( 'init', 'my_function', 10);

     function my_function() {
        $args = array( 'post_type' => 'post', 'post_status' => 'publish');
        $pages = get_pages($args);
        foreach ( $pages as $page ) {
            // Do something here. Call your formatting function
            }
    }

Edit: If it absolutely must run not more than once, have your function look for an SQL table with a boolean value of true or false. If false or table does not exist, run the function, create the table and set it to true.