Storing postID in session variable to query database when visitor on custom template page

If your only need is convert a a permalink structure in a get variable use an endpoint.

<?php
/**
 * Plugin Name: Question 112350 Plugin
 * Author: G.M.
*/

/**
* Add a new role cloning capabilities from editor and flush rewrite rules
*/
function install_q112350_plugin() {
    plugin_q112350_endpoint();
    flush_rewrite_rules();
}

/**
* Remove the role and flush rewrite rules
*/
function unistall_q112350_plugin() {
    flush_rewrite_rules();
}

/**
* Add the endpoint
*/
function plugin_q112350_endpoint() {
    add_rewrite_endpoint( 'myvar', EP_ALL );
}

add_action( 'init', 'plugin_q112350_endpoint' );
register_activation_hook( __FILE__, 'install_q112350_plugin' );
register_deactivation_hook( __FILE__, 'unistall_q112350_plugin' );

Now the url http://example.com/myvar/125 will set the variable myvar = 125.

You can use get_query_var('myvar'); to retrieve this value in any template file or in pre_get_posts action and in any other later hook.