pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedDownsides of liberally using CREATE TEMP TABLE ... ON COMMIT DROP
7+ messages / 4 participants
[nested] [flat]
* Downsides of liberally using CREATE TEMP TABLE ... ON COMMIT DROP
@ 2018-01-28 13:58 Ryan Murphy <ryanfmurphy@gmail.com>
0 siblings, 1 reply; 7+ messages in thread
From: Ryan Murphy @ 2018-01-28 13:58 UTC (permalink / raw)
To: pgsql-general
Hello,
I heard at a PostgreSQL talk that you should not liberally create temp
tables in the course of frequently-used functions etc, because (roughly)
you're using up some of the same resources that you for your regular tables.
Is this true? Is there an important reason not to have e.g. a plpgsql
function that uses a temp table? What are the specific problems if I do
this? Is the problem ameliorated if I add ON COMMIT DROP?
Best,
Ryan
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Downsides of liberally using CREATE TEMP TABLE ... ON COMMIT DROP
@ 2018-01-28 14:40 David G. Johnston <david.g.johnston@gmail.com>
parent: Ryan Murphy <ryanfmurphy@gmail.com>
0 siblings, 1 reply; 7+ messages in thread
From: David G. Johnston @ 2018-01-28 14:40 UTC (permalink / raw)
To: Ryan Murphy <ryanfmurphy@gmail.com>; +Cc: pgsql-general
On Sunday, January 28, 2018, Ryan Murphy <ryanfmurphy@gmail.com> wrote:
> Hello,
>
> I heard at a PostgreSQL talk that you should not liberally create temp
> tables in the course of frequently-used functions etc, because (roughly)
> you're using up some of the same resources that you for your regular tables.
>
> Is this true? Is there an important reason not to have e.g. a plpgsql
> function that uses a temp table? What are the specific problems if I do
> this? Is the problem ameliorated if I add ON COMMIT DROP?
>
I believe the main, and maybe only, concern is the bloating of the system
catalog tables since you are constantly adding and removing records. Yes,
they will be vacuumed but vacuuming and bloat on catalog tables slows every
single query down to some, degree since every query has to lookup its
objects is those catalogs. Though caching probably alleviates some of that.
The way most temp tables are used on commit drop likely has little impact
on this, but the specific usage pattern matters a great deal in answering
the question.
David J.
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Downsides of liberally using CREATE TEMP TABLE ... ON COMMIT DROP
@ 2018-01-28 14:46 Ryan Murphy <ryanfmurphy@gmail.com>
parent: David G. Johnston <david.g.johnston@gmail.com>
0 siblings, 1 reply; 7+ messages in thread
From: Ryan Murphy @ 2018-01-28 14:46 UTC (permalink / raw)
To: David G. Johnston <david.g.johnston@gmail.com>; +Cc: pgsql-general
>
> I believe the main, and maybe only, concern is the bloating of the system
> catalog tables since you are constantly adding and removing records. Yes,
> they will be vacuumed but vacuuming and bloat on catalog tables slows every
> single query down to some, degree since every query has to lookup its
> objects is those catalogs. Though caching probably alleviates some of that
>
Yes, that's exactly the concern I heard, thanks for reminding me.
If I want to e.g. temporarily store a "setof records" or a "table" result
in a variable as part of a calculation in a plpgsql function, do I have any
other option than CREATE TEMPORARY TABLE? It didn't seem to work when I
DECLAREd a variable of type "setof table_name" or "setof
table_name%rowtype", and then SELECT INTO it.
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Downsides of liberally using CREATE TEMP TABLE ... ON COMMIT DROP
@ 2018-01-28 15:53 Andy Colson <andy@squeakycode.net>
parent: Ryan Murphy <ryanfmurphy@gmail.com>
0 siblings, 1 reply; 7+ messages in thread
From: Andy Colson @ 2018-01-28 15:53 UTC (permalink / raw)
To: Ryan Murphy <ryanfmurphy@gmail.com>; David G. Johnston <david.g.johnston@gmail.com>; +Cc: pgsql-general
On 01/28/2018 08:46 AM, Ryan Murphy wrote:
> I believe the main, and maybe only, concern is the bloating of the system catalog tables since you are constantly adding and removing records. Yes, they will be vacuumed but vacuuming and bloat on catalog tables slows every single query down to some, degree since every query has to lookup its objects is those catalogs. Though caching probably alleviates some of that
>
>
> Yes, that's exactly the concern I heard, thanks for reminding me.
>
> If I want to e.g. temporarily store a "setof records" or a "table" result in a variable as part of a calculation in a plpgsql function, do I have any other option than CREATE TEMPORARY TABLE? It didn't seem to work when I DECLAREd a variable of type "setof table_name" or "setof table_name%rowtype", and then SELECT INTO it.
>
You may not need temp tables at all. You can use subselects, derived tables, and cte's:
select sum(a+b) as total
from (
select a, b+1
from detail
) as tmpx;
This does the same thing as a temp table, with no temp table.
-Andy
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Downsides of liberally using CREATE TEMP TABLE ... ON COMMIT DROP
@ 2018-01-28 19:22 Rob Sargent <robjsargent@gmail.com>
parent: Andy Colson <andy@squeakycode.net>
0 siblings, 1 reply; 7+ messages in thread
From: Rob Sargent @ 2018-01-28 19:22 UTC (permalink / raw)
To: Andy Colson <andy@squeakycode.net>; +Cc: Ryan Murphy <ryanfmurphy@gmail.com>; David G. Johnston <david.g.johnston@gmail.com>; pgsql-general
“On commit drop” is generally good practice for several reasons: if your function is called in a loop, or the create statement is in a loop or you want to call your function more than once in a given session (with out explicitly dropping your temp table).
The commit in question is the function btw.
> On Jan 28, 2018, at 8:53 AM, Andy Colson <andy@squeakycode.net> wrote:
>
>> On 01/28/2018 08:46 AM, Ryan Murphy wrote:
>> I believe the main, and maybe only, concern is the bloating of the system catalog tables since you are constantly adding and removing records. Yes, they will be vacuumed but vacuuming and bloat on catalog tables slows every single query down to some, degree since every query has to lookup its objects is those catalogs. Though caching probably alleviates some of that
>> Yes, that's exactly the concern I heard, thanks for reminding me.
>> If I want to e.g. temporarily store a "setof records" or a "table" result in a variable as part of a calculation in a plpgsql function, do I have any other option than CREATE TEMPORARY TABLE? It didn't seem to work when I DECLAREd a variable of type "setof table_name" or "setof table_name%rowtype", and then SELECT INTO it.
>
> You may not need temp tables at all. You can use subselects, derived tables, and cte's:
>
> select sum(a+b) as total
> from (
> select a, b+1
> from detail
> ) as tmpx;
>
>
> This does the same thing as a temp table, with no temp table.
>
> -Andy
>
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Downsides of liberally using CREATE TEMP TABLE ... ON COMMIT DROP
@ 2018-01-28 21:45 David G. Johnston <david.g.johnston@gmail.com>
parent: Rob Sargent <robjsargent@gmail.com>
0 siblings, 1 reply; 7+ messages in thread
From: David G. Johnston @ 2018-01-28 21:45 UTC (permalink / raw)
To: Rob Sargent <robjsargent@gmail.com>; +Cc: Andy Colson <andy@squeakycode.net>; Ryan Murphy <ryanfmurphy@gmail.com>; pgsql-general
On Sunday, January 28, 2018, Rob Sargent <robjsargent@gmail.com> wrote:
>
> The commit in question is the function btw.
>
That doesn't make sense - functions can't commit.
David J.
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Downsides of liberally using CREATE TEMP TABLE ... ON COMMIT DROP
@ 2018-01-29 06:02 Rob Sargent <robjsargent@gmail.com>
parent: David G. Johnston <david.g.johnston@gmail.com>
0 siblings, 0 replies; 7+ messages in thread
From: Rob Sargent @ 2018-01-29 06:02 UTC (permalink / raw)
To: David G. Johnston <david.g.johnston@gmail.com>; +Cc: Andy Colson <andy@squeakycode.net>; Ryan Murphy <ryanfmurphy@gmail.com>; pgsql-general
I think the table is dropped by end of function
> On Jan 28, 2018, at 2:45 PM, David G. Johnston <david.g.johnston@gmail.com> wrote:
>
>> On Sunday, January 28, 2018, Rob Sargent <robjsargent@gmail.com> wrote:
>> The commit in question is the function btw.
>
> That doesn't make sense - functions can't commit.
>
> David J.
^ permalink raw reply [nested|flat] 7+ messages in thread
end of thread, other threads:[~2018-01-29 06:02 UTC | newest]
Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-01-28 13:58 Downsides of liberally using CREATE TEMP TABLE ... ON COMMIT DROP Ryan Murphy <ryanfmurphy@gmail.com>
2018-01-28 14:40 ` David G. Johnston <david.g.johnston@gmail.com>
2018-01-28 14:46 ` Ryan Murphy <ryanfmurphy@gmail.com>
2018-01-28 15:53 ` Andy Colson <andy@squeakycode.net>
2018-01-28 19:22 ` Rob Sargent <robjsargent@gmail.com>
2018-01-28 21:45 ` David G. Johnston <david.g.johnston@gmail.com>
2018-01-29 06:02 ` Rob Sargent <robjsargent@gmail.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox