Thread: creating and accessing temp table data inside a non-committed transaction
Plpgsql experts: I've got a situation and am wondering if temp tables are the accepted solution. (setting the temp parameter on create table) I've never worked with them and a colleague just brought them to my attention as a way to solve a problem I've encountered. He's not sure about this, so I am hoping you guys can point me in the right direction. The problem is that I've got function that takes some parameters, for example: set constraints all deferred; select my_func( x, y, z ); commit; The problem is that the values x, y and z are inserted into a permanent table Q early in the processing of my_func. Then, several calls down in deep_func(), still inside my_func, I need to access the value of x. I tried to do a select on Q to get the value, but I came up NULL! Guessing that is because the transaction hasn't committed yet since I am still inside my_func. Right? So how do I get the value of parameter x? Possible solutions: 1) just pass x all the way down the calling path so that deep_func( x ) can just access it as a parameter value. Pain in the butt because none of the intermediate calls need x. 2) insert the value of a into a temp table and the do a select on that within deep_func(). But will I stull come up NULL, or do temp tables work differently? 3) some other technique if I am thinking about this totally the wrong way! As always, help clearing my confusion muchly appreciated! (obviously I can just do a bunch of experiments on my own, but expert insight is always nice to have) - Leon
Re: creating and accessing temp table data inside a non-committed transaction
From
"Kevin Grittner"
Date:
Leon Starr <leon_starr@modelint.com> wrote: > The problem is that the values x, y and z are inserted into a > permanent table Q early in the processing of my_func. Then, > several calls down in deep_func(), still inside my_func, I need to > access the value of x. I tried to do a select on Q to get the > value, but I came up NULL! Guessing that is because the > transaction hasn't committed yet since I am still inside my_func. > Right? I don't think so. Any database changes made by a transaction should be visible to that transaction. When you say "several calls down in deep_func()", you don't mean that you are in a BEFORE trigger function, or a function called from a BEFORE trigger, do you? Perhaps you could boil the issue down to a small self-contained example of the issue? -Kevin
Ah! That's what I had originally thought. So, if what you are saying is true, then I just have a bug of some sort. I am NOT using any triggers. It's just a simple function with deeper function calls nested about 5 levels down. To slightly rephrase: Any database changes (inserts/updates) made by a transaction are visible inside that transaction. So if I do an insert inside a transaction and then later, within the same transaction, do a select on the inserted row, I should select the data that I inserted. Correct? I will do my diagnostics with that in mind. Thanks! - Leon On Jan 31, 2011, at 7:42 AM, Kevin Grittner wrote: > Leon Starr <leon_starr@modelint.com> wrote: > >> The problem is that the values x, y and z are inserted into a >> permanent table Q early in the processing of my_func. Then, >> several calls down in deep_func(), still inside my_func, I need to >> access the value of x. I tried to do a select on Q to get the >> value, but I came up NULL! Guessing that is because the >> transaction hasn't committed yet since I am still inside my_func. >> Right? > > I don't think so. Any database changes made by a transaction should > be visible to that transaction. When you say "several calls down in > deep_func()", you don't mean that you are in a BEFORE trigger > function, or a function called from a BEFORE trigger, do you? > > Perhaps you could boil the issue down to a small self-contained > example of the issue? > > -Kevin
Oops. Just noticed that I accidentally posted to the wrong group. I meant to put this in Novice! But thanks anyway! - Leon On Jan 31, 2011, at 7:42 AM, Kevin Grittner wrote: > Leon Starr <leon_starr@modelint.com> wrote: > >> The problem is that the values x, y and z are inserted into a >> permanent table Q early in the processing of my_func. Then, >> several calls down in deep_func(), still inside my_func, I need to >> access the value of x. I tried to do a select on Q to get the >> value, but I came up NULL! Guessing that is because the >> transaction hasn't committed yet since I am still inside my_func. >> Right? > > I don't think so. Any database changes made by a transaction should > be visible to that transaction. When you say "several calls down in > deep_func()", you don't mean that you are in a BEFORE trigger > function, or a function called from a BEFORE trigger, do you? > > Perhaps you could boil the issue down to a small self-contained > example of the issue? > > -Kevin