Received: from localhost (maia-4.hub.org [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id 056289FA311 for ; Mon, 18 Dec 2006 19:06:13 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-new, port 10024) with ESMTP id 08649-09 for ; Mon, 18 Dec 2006 19:06:05 -0400 (AST) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.4 Received: from wx-out-0506.google.com (wx-out-0506.google.com [66.249.82.238]) by postgresql.org (Postfix) with ESMTP id E5EE09FA0EF for ; Mon, 18 Dec 2006 19:06:05 -0400 (AST) Received: by wx-out-0506.google.com with SMTP id i28so1530504wxd for ; Mon, 18 Dec 2006 15:06:04 -0800 (PST) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:references; b=ajDaXIp2SChoK52C8BgCGxmD3JGWCqyUoJQYoNxbzn0o8wmXmBNqh0I8kumen3xHBFu4pMK8L/ARW6yY0NUC7pBLw5EsvG+CDPGgM1C2lG4RqLihE4PFfkcBKOmyGxKdmkPZMYJgNZT+U6GGvYrV3wbyRVtyZ4X5QpoF1rK90Xo= Received: by 10.70.50.10 with SMTP id x10mr8408587wxx.1166483164610; Mon, 18 Dec 2006 15:06:04 -0800 (PST) Received: by 10.70.108.15 with HTTP; Mon, 18 Dec 2006 15:06:04 -0800 (PST) Message-ID: <357fa7590612181506r69a17e37x271623007f27c877@mail.gmail.com> Date: Mon, 18 Dec 2006 18:06:04 -0500 From: "Steven Flatt" To: "Tom Lane" Subject: Re: Insertion to temp table deteriorating over time Cc: pgsql-performance@postgresql.org In-Reply-To: <19298.1166464855@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_33150_2321674.1166483164556" References: <357fa7590612130844q58ef3ca5q31e164f9b006e0fc@mail.gmail.com> <11651.1166202586@sss.pgh.pa.us> <357fa7590612150955g1b9c733duaf000c23d28aa8af@mail.gmail.com> <12508.1166206006@sss.pgh.pa.us> <357fa7590612151026t4ae033f6w5367cac9d0b67064@mail.gmail.com> <13947.1166209788@sss.pgh.pa.us> <357fa7590612151121x5f95102fj21a96b1bdc7eb002@mail.gmail.com> <20792.1166211026@sss.pgh.pa.us> <357fa7590612180951q3da4c840l27caa82475444cfb@mail.gmail.com> <19298.1166464855@sss.pgh.pa.us> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200612/470 X-Sequence-Number: 22526 ------=_Part_33150_2321674.1166483164556 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline Please ignore my post from earlier today. As strange as it sounds, changing "CREATE TEMP TABLE ... AS" to "CREATE TEMP TABLE ... LIKE" appeared to fix my performance problem because things errored out so quickly (and silently in my test program). After checking the pgsql logs, it became clear to me that you can't use LIKE on a view. Duh. Moving forward, I have also discovered that our temp table did in fact have a varchar column (no specified limit on varchar). With this in mind, I could easily reproduce the problem on a temp table with one column. So... Issue #1: (I'm assuming there's a reasonable explanation for this.) If I create a temp table with a single varchar column (or text column), do 100 inserts to that table, copy to a permanent table, truncate the temp table and repeat, the time required for the 100 inserts grows almost linearly. Maybe the data is treated as large objects. Note that if I change the column type to varchar(SOME_LIMIT), integer, timestamptz, interval, etc., performance does not degrade. Also note that if I do not use a temp table (but do use a varchar column), inserts are slower (as expected) but do not degrade over time. So this seems to be specific to temp tables with varchar/text column(s). Issue #2: As I said earlier, the temp table is created via: CREATE TEMP TABLE tmp AS SELECT FROM perm LIMIT 0; where perm is a view defined as follows: View definition: SELECT FROM view2 JOIN tbl USING (col1, col2) WHERE UNION ALL SELECT FROM view3 JOIN tbl USING (col1, col2) WHERE ; Now the varchar columns that end up in the perm view come from the tbl table, but in tbl, they are defined as varchar(40). Somehow the 40 limit is lost when constructing the view. After a little more testing, I found that this problem only occurs when you are creating a view (i.e. CREATE TABLE ... AS does not observe this problem) and also that the UNION ALL clause must be present to observe this problem. This looks like a bug. I know this is Postgres 7.4.6 and I haven't been able to verify with a later version of Postgres, but does this look familiar to anyone? Steve ------=_Part_33150_2321674.1166483164556 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline
Please ignore my post from earlier today.  As strange as it sounds, changing "CREATE TEMP TABLE ... AS" to "CREATE TEMP TABLE ... LIKE" appeared to fix my performance problem because things errored out so quickly (and silently in my test program).  After checking the pgsql logs, it became clear to me that you can't use LIKE on a view.  Duh.

Moving forward, I have also discovered that our temp table did in fact have a varchar column (no specified limit on varchar).  With this in mind, I could easily reproduce the problem on a temp table with one column.  So...
 
Issue #1:
 
(I'm assuming there's a reasonable explanation for this.)  If I create a temp table with a single varchar column (or text column), do 100 inserts to that table, copy to a permanent table, truncate the temp table and repeat, the time required for the 100 inserts grows almost linearly.  Maybe the data is treated as large objects.
 
Note that if I change the column type to varchar(SOME_LIMIT), integer, timestamptz, interval, etc., performance does not degrade.  Also note that if I do not use a temp table (but do use a varchar column), inserts are slower (as expected) but do not degrade over time.  So this seems to be specific to temp tables with varchar/text column(s).
 
Issue #2:
 
As I said earlier, the temp table is created via:
 
CREATE TEMP TABLE tmp AS SELECT <column-list> FROM perm LIMIT 0;
 
where perm is a view defined as follows:
 
View definition:
 SELECT <column-list>
   FROM view2
   JOIN tbl USING (col1, col2)
  WHERE <some-conditions>
UNION ALL
 SELECT <column-list>
   FROM view3
   JOIN tbl USING (col1, col2)
  WHERE <some-conditions>;
 
Now the varchar columns that end up in the perm view come from the tbl table, but in tbl, they are defined as varchar(40).  Somehow the 40 limit is lost when constructing the view.  After a little more testing, I found that this problem only occurs when you are creating a view ( i.e. CREATE TABLE ... AS does not observe this problem) and also that the UNION ALL clause must be present to observe this problem.
 
This looks like a bug.  I know this is Postgres 7.4.6 and I haven't been able to verify with a later version of Postgres, but does this look familiar to anyone?
 
Steve
------=_Part_33150_2321674.1166483164556--