pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedTemp Tables
24+ messages / 18 participants
[nested] [flat]
* Temp Tables
@ 2002-03-04 20:48 mdb <mdb002@yahoo.com>
2002-03-07 17:56 ` Re: Temp Tables Tom Lane <tgl@sss.pgh.pa.us>
2002-03-07 17:58 ` Re: Temp Tables Bruce Momjian <pgman@candle.pha.pa.us>
2002-03-07 22:16 ` Re: Temp Tables Scott Marlowe <smarlowe@ihs.com>
0 siblings, 3 replies; 24+ messages in thread
From: mdb002@yahoo.com @ 2002-03-04 20:48 UTC (permalink / raw)
To: pgsql-general
Hello,
I am running a client server app and am using temp tables to do some
data manipulation. However, Postgresql seems to prevent multiple temp
tables from being created with the same name. The temp tables are
created by seperate connections to the database, but this does not
seem to matter. Is this correct? Do I need to make the names unique
by using a user id? Am I doing something wrong? I am using
Postgresql 7.1.3 and odbc 7.1.9.
Thank you
Marc
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: Temp Tables
2002-03-04 20:48 Temp Tables mdb <mdb002@yahoo.com>
@ 2002-03-07 17:56 ` Tom Lane <tgl@sss.pgh.pa.us>
2 siblings, 0 replies; 24+ messages in thread
From: Tom Lane @ 2002-03-07 17:56 UTC (permalink / raw)
To: mdb002@yahoo.com; +Cc: pgsql-general
mdb002@yahoo.com (mdb) writes:
> I am running a client server app and am using temp tables to do some
> data manipulation. However, Postgresql seems to prevent multiple temp
> tables from being created with the same name. The temp tables are
> created by seperate connections to the database, but this does not
> seem to matter.
Surely not.
In window 1:
regression=# create temp table foo (f1 int);
CREATE
regression=#
In window 2:
regression=# create temp table foo (f1 float, f2 float);
CREATE
regression=#
So I don't see a problem. Would you give us an example of exactly what
you are doing?
regards, tom lane
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: Temp Tables
2002-03-04 20:48 Temp Tables mdb <mdb002@yahoo.com>
@ 2002-03-07 17:58 ` Bruce Momjian <pgman@candle.pha.pa.us>
2002-03-07 18:45 ` Re: Temp Tables Len Morgan <len-morgan@kttk.net>
2 siblings, 1 reply; 24+ messages in thread
From: Bruce Momjian @ 2002-03-07 17:58 UTC (permalink / raw)
To: mdb <mdb002@yahoo.com>; +Cc: pgsql-general
mdb wrote:
> Hello,
>
> I am running a client server app and am using temp tables to do some
> data manipulation. However, Postgresql seems to prevent multiple temp
> tables from being created with the same name. The temp tables are
> created by seperate connections to the database, but this does not
> seem to matter. Is this correct? Do I need to make the names unique
> by using a user id? Am I doing something wrong? I am using
> Postgresql 7.1.3 and odbc 7.1.9.
Temp tables can be created with identical names in different
connections. I have no idea why it is failing. Are you trying to
create the same temp table in the same session? Could you show us the
failure?
--
Bruce Momjian | http://candle.pha.pa.us
pgman@candle.pha.pa.us | (610) 853-3000
+ If your life is a hard drive, | 830 Blythe Avenue
+ Christ can be your backup. | Drexel Hill, Pennsylvania 19026
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: Temp Tables
2002-03-04 20:48 Temp Tables mdb <mdb002@yahoo.com>
2002-03-07 17:58 ` Re: Temp Tables Bruce Momjian <pgman@candle.pha.pa.us>
@ 2002-03-07 18:45 ` Len Morgan <len-morgan@kttk.net>
0 siblings, 0 replies; 24+ messages in thread
From: Len Morgan @ 2002-03-07 18:45 UTC (permalink / raw)
To: pgsql-general
> mdb wrote:
> > Hello,
> >
> > I am running a client server app and am using temp tables to do some
> > data manipulation. However, Postgresql seems to prevent multiple temp
> > tables from being created with the same name. The temp tables are
> > created by seperate connections to the database, but this does not
> > seem to matter. Is this correct? Do I need to make the names unique
> > by using a user id? Am I doing something wrong? I am using
> > Postgresql 7.1.3 and odbc 7.1.9.
>
> Temp tables can be created with identical names in different
> connections. I have no idea why it is failing. Are you trying to
> create the same temp table in the same session? Could you show us the
> failure?
>
I believe (and I'm no expert) that the table names must be unique by user so
if your client/server app uses the same generic user name (like your web
server's user for example), I don't think the particular session matters.
It's a user_name+temp_table_name thing.
But I could be wrong. :-)
Len Morgan
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: Temp Tables
2002-03-04 20:48 Temp Tables mdb <mdb002@yahoo.com>
@ 2002-03-07 22:16 ` Scott Marlowe <smarlowe@ihs.com>
2002-03-08 14:45 ` Re: Temp Tables mdb <mdb002@yahoo.com>
2 siblings, 1 reply; 24+ messages in thread
From: smarlowe@ihs.com @ 2002-03-07 22:16 UTC (permalink / raw)
To: pgsql-general
mdb002@yahoo.com (mdb) wrote in message news:<9e6ea67a.0203041248.24859ff2@posting.google.com>...
> Hello,
>
> I am running a client server app and am using temp tables to do some
> data manipulation. However, Postgresql seems to prevent multiple temp
> tables from being created with the same name. The temp tables are
> created by seperate connections to the database, but this does not
> seem to matter. Is this correct? Do I need to make the names unique
> by using a user id? Am I doing something wrong? I am using
> Postgresql 7.1.3 and odbc 7.1.9.
Common mistake. Basically, you should be doing this inside of a
transaction block so that that the temp table is hidden from all the
other users, like so:
begin;
create temp table test (name text, id int);
(do mysterious and wonderful things in SQL here)
end;
the temp table will be built, seen only by your transaction, then
automatically dropped on exit.
note that SEQUENCES should not be created, i.e. don't do 'create temp
table yada (name text, id serial)' because that requires the building
of a sequence, which isn't a temporary kind of thing. you can
reference a sequence within a transacation block in a temp table
though, like so
create temp table test (name text, id int not null default
nextval('"test_id_seq"'::text));
Good luck
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: Temp Tables
2002-03-04 20:48 Temp Tables mdb <mdb002@yahoo.com>
2002-03-07 22:16 ` Re: Temp Tables Scott Marlowe <smarlowe@ihs.com>
@ 2002-03-08 14:45 ` mdb <mdb002@yahoo.com>
2002-03-08 19:13 ` Re: Temp Tables mdb <mdb002@yahoo.com>
0 siblings, 1 reply; 24+ messages in thread
From: mdb002@yahoo.com @ 2002-03-08 14:45 UTC (permalink / raw)
To: pgsql-general
Scott,
Thanks for the response.
However, I have further narrowed down the problem. The temp tables
were not the problem. I was creating views of the temp tables which
was the problem. Now why would I create a view of temp tables you
ask? I am using VB, before I get blasted for this heresy it was
originally an access and vb application that we are upgrading to
PostgreSQL. The code is substantial and to recode everthing would be
a substantial undertaking. Back to the problem the VB connection does
not see the temp table if I do not use a view. A little bit about the
temp table it is used to emulate an access crosstab query and columns
are added and removed (table deleted and columns added) multiple times
as the users enter data. I display the data in DBGrid using a
recordset and setting the dbgrid datasource property (no
dataenvironment or datacontrol used). Has anybody used temp tables to
display data with vb and how do you do it?
Marc
smarlowe@ihs.com (Scott Marlowe) wrote in message news:<a03059a3.0203071416.7bf79d27@posting.google.com>...
> mdb002@yahoo.com (mdb) wrote in message news:<9e6ea67a.0203041248.24859ff2@posting.google.com>...
> > Hello,
> >
> > I am running a client server app and am using temp tables to do some
> > data manipulation. However, Postgresql seems to prevent multiple temp
> > tables from being created with the same name. The temp tables are
> > created by seperate connections to the database, but this does not
> > seem to matter. Is this correct? Do I need to make the names unique
> > by using a user id? Am I doing something wrong? I am using
> > Postgresql 7.1.3 and odbc 7.1.9.
>
> Common mistake. Basically, you should be doing this inside of a
> transaction block so that that the temp table is hidden from all the
> other users, like so:
>
> begin;
> create temp table test (name text, id int);
> (do mysterious and wonderful things in SQL here)
> end;
>
> the temp table will be built, seen only by your transaction, then
> automatically dropped on exit.
>
> note that SEQUENCES should not be created, i.e. don't do 'create temp
> table yada (name text, id serial)' because that requires the building
> of a sequence, which isn't a temporary kind of thing. you can
> reference a sequence within a transacation block in a temp table
> though, like so
>
> create temp table test (name text, id int not null default
> nextval('"test_id_seq"'::text));
>
> Good luck
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: Temp Tables
2002-03-04 20:48 Temp Tables mdb <mdb002@yahoo.com>
2002-03-07 22:16 ` Re: Temp Tables Scott Marlowe <smarlowe@ihs.com>
2002-03-08 14:45 ` Re: Temp Tables mdb <mdb002@yahoo.com>
@ 2002-03-08 19:13 ` mdb <mdb002@yahoo.com>
0 siblings, 0 replies; 24+ messages in thread
From: mdb002@yahoo.com @ 2002-03-08 19:13 UTC (permalink / raw)
To: pgsql-general
The problem was the name of the temp table. When I created the view
inside a stored procedure the view was created "normally" and could be
referenced by the name I gave it. The temp tables can not be
referenced by the name I gave them. VB/ODBC did not produce an error
when I tried to open a recordset with the temp table name I used (ex.
table does not exist) it just did not open the recordset and continued
on. A little misleading.
I did find a work around posted by Richard Huxton/Bruce Momjian -
CREATE FUNCTION getpid () returns int4 as '/lib/libc.so.6' LANGUAGE
'C';
This returns the process id. The temp table named uses the process id
(pg_temp.PROCESS_ID.TEMP_TABLE_NUMBER) using the returned process id
and a counter in the VB program (the temp table is destroyed and
created multiple times) I was able to select using PostgreSQL
generated name.
Thank You to all that responded.
Bruce, Tom and everyone else involved with PostgreSQL,
This is my first experience with your database, despite a learning
curve I am going through right now I am throughly impressed with
Postgre. It is an awesome database.
Marc
^ permalink raw reply [nested|flat] 24+ messages in thread
* Temp Tables
@ 2002-03-07 17:00 Peter Darley <pdarley@kinesis-cem.com>
0 siblings, 0 replies; 24+ messages in thread
From: Peter Darley @ 2002-03-07 17:00 UTC (permalink / raw)
To: pgsql-general
Friends,
I'm hoping someone can give me some advice on a problem I'm having.
I have a system that produces 3060 reports each night. Each report uses a
temp table to limit the data set to the appropriate records, then joins that
temp table to other tables to produce its output. I'm a bit concerned that
this gets slower as the process goes on and the database gets more inserts
into those tables, etc. It looks like there are several ways I could handle
this, and I'm hoping someone can tell me which one is faster, or give me
some advice for testing which one is faster without actually running all the
options (which could easily take 50 hours, which I can't spare the db server
for).
1) I could reuse the same temp table and delete from it at the beginning of
every report.
2) I could drop the temp table and rebuild it at the beginning of every
report.
3) I could not drop the temp table, and create a new one for every report,
leaving cleanup to PostgreSQL when I disconnect from the db.
4) I could not drop the temp table, and for every report disconnect from
the db forcing cleanup of it, reconnect and recreate the table
5) I could create a real (non temp) table and reuse it for every report.
If anyone has some insight to the overhead of using temp tables that they
can pass on to me I would be very grateful.
Thanks,
Peter Darley
^ permalink raw reply [nested|flat] 24+ messages in thread
* Temp tables
@ 2002-08-20 00:20 Andrew Bartley <abartley@evolvosystems.com>
2002-08-20 02:43 ` Re: Temp tables Bruce Momjian <pgman@candle.pha.pa.us>
0 siblings, 1 reply; 24+ messages in thread
From: Andrew Bartley @ 2002-08-20 00:20 UTC (permalink / raw)
To: pgsql-general
Hi all,
Can someone tell me what the difference is in a plpgsql function between dropping a temp table at the end of a function and not dropping the temp tables.
What other tables does it have an effect on?
It appears there is some overhead when the drop is being executed.
Is there some simple doco I can read to clue me up on the process?
Thanks
Andrew
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: Temp tables
2002-08-20 00:20 Temp tables Andrew Bartley <abartley@evolvosystems.com>
@ 2002-08-20 02:43 ` Bruce Momjian <pgman@candle.pha.pa.us>
0 siblings, 0 replies; 24+ messages in thread
From: Bruce Momjian @ 2002-08-20 02:43 UTC (permalink / raw)
To: Andrew Bartley <abartley@evolvosystems.com>; +Cc: pgsql-general
Andrew Bartley wrote:
> Hi all,
>
> Can someone tell me what the difference is in a plpgsql function
> between dropping a temp table at the end of a function and not
> dropping the temp tables.
>
> What other tables does it have an effect on?
>
> It appears there is some overhead when the drop is being executed.
>
> Is there some simple doco I can read to clue me up on the process?
You can drop it whenever you want. If not, it drops on session exit.
On issue with plpgsql is that when you create a temp table in a function
and drop it, then call the function again, it still think the original
temp table exists. You need to use EXECUTE in plpgsql if you want to
drop/create temp tables multiple times in the same session.
--
Bruce Momjian | http://candle.pha.pa.us
pgman@candle.pha.pa.us | (610) 359-1001
+ If your life is a hard drive, | 13 Roberts Road
+ Christ can be your backup. | Newtown Square, Pennsylvania 19073
^ permalink raw reply [nested|flat] 24+ messages in thread
* Temp tables...
@ 2005-07-12 17:52 Greg Patnude <gpatnude@hotmail.com>
2005-07-13 00:57 ` Re: Temp tables... Michael Fuhr <mike@fuhr.org>
0 siblings, 1 reply; 24+ messages in thread
From: Greg Patnude @ 2005-07-12 17:52 UTC (permalink / raw)
To: pgsql-general
Performing an update to an inherited table system from inside of a stored
procedure (PLPGSQL) seems to be unusually sluggish... Does anyone have a
faster solution ? I am updating 50 records and it takes approximately 4.375
seconds + or -....
The inherited table has an ON INSERT DO INSTEAD and there are approximately
2 million rows in the inherited table structure...
Any ideas ? Suggestions ?
Regards,
Greg Patnude - Manager, Dynamic Applications Group
Data Mosaics, Inc.
2406 South Dishman-Mica Road / Suite # 6
Spokane Valley, WA 99206-6429
VOICE: (866) 904-DMSF
FAX: (509) 928-4236
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: Temp tables...
2005-07-12 17:52 Temp tables... Greg Patnude <gpatnude@hotmail.com>
@ 2005-07-13 00:57 ` Michael Fuhr <mike@fuhr.org>
2005-07-13 15:48 ` Re: Temp tables... Greg Patnude <gpatnude@hotmail.com>
0 siblings, 1 reply; 24+ messages in thread
From: Michael Fuhr @ 2005-07-13 00:57 UTC (permalink / raw)
To: Greg Patnude <gpatnude@hotmail.com>; +Cc: pgsql-general
On Tue, Jul 12, 2005 at 10:52:24AM -0700, Greg Patnude wrote:
>
> Performing an update to an inherited table system from inside of a stored
> procedure (PLPGSQL) seems to be unusually sluggish...
Is the update slower when done inside a function than when doing
it directly (e.g., from psql)? That is, is the use of a function
relevant, or is the update equally slow in any case? Could you
post the EXPLAIN ANALYZE output for the update?
The message subject is "Temp tables." Are you using temporary
tables, and if so, are you seeing different behavior with temporary
tables than with "real" tables? Again, is that relevant to the
problem?
> Does anyone have a faster solution ? I am updating 50 records and it
> takes approximately 4.375 seconds + or -....
>
> The inherited table has an ON INSERT DO INSTEAD and there are approximately
> 2 million rows in the inherited table structure...
Could you post the table definitions, including all indexes, rules,
etc.? Do all the child tables have indexes on the column(s) used
to restrict the update? As the documentation states, indexes aren't
inherited, so you might need to create additional indexes on the
children, indexes that you'd think would be redundant. Example:
CREATE TABLE parent (id serial PRIMARY KEY);
CREATE TABLE child (x integer) INHERITS (parent);
INSERT INTO child (x) SELECT * FROM generate_series(1, 100000);
ANALYZE parent;
ANALYZE child;
EXPLAIN ANALYZE UPDATE child SET id = id WHERE id BETWEEN 1 AND 50;
QUERY PLAN
-------------------------------------------------------------------------------------------------------
Seq Scan on child (cost=0.00..1991.00 rows=41 width=14) (actual time=0.059..307.234 rows=50 loops=1)
Filter: ((id >= 1) AND (id <= 50))
Total runtime: 309.350 ms
(3 rows)
EXPLAIN ANALYZE UPDATE parent SET id = id WHERE id BETWEEN 1 AND 50;
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------
Append (cost=0.00..2006.37 rows=52 width=14) (actual time=304.838..306.252 rows=50 loops=1)
-> Index Scan using parent_pkey on parent (cost=0.00..15.37 rows=11 width=10) (actual time=0.110..0.110 rows=0 loops=1)
Index Cond: ((id >= 1) AND (id <= 50))
-> Seq Scan on child parent (cost=0.00..1991.00 rows=41 width=14) (actual time=304.705..305.619 rows=50 loops=1)
Filter: ((id >= 1) AND (id <= 50))
Total runtime: 307.935 ms
(6 rows)
Notice the sequential scans on child, even though we have an index
on parent.id, a column that child inherits. We need to create an
index on child.id as well:
CREATE INDEX child_id_idx ON child (id);
EXPLAIN ANALYZE UPDATE child SET id = id WHERE id BETWEEN 1 AND 50;
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------
Index Scan using child_id_idx on child (cost=0.00..3.65 rows=41 width=14) (actual time=0.369..1.371 rows=50 loops=1)
Index Cond: ((id >= 1) AND (id <= 50))
Total runtime: 6.100 ms
(3 rows)
EXPLAIN ANALYZE UPDATE parent SET id = id WHERE id BETWEEN 1 AND 50;
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------------
Append (cost=0.00..19.02 rows=52 width=14) (actual time=0.119..1.895 rows=50 loops=1)
-> Index Scan using parent_pkey on parent (cost=0.00..15.37 rows=11 width=10) (actual time=0.037..0.037 rows=0 loops=1)
Index Cond: ((id >= 1) AND (id <= 50))
-> Index Scan using child_id_idx on child parent (cost=0.00..3.65 rows=41 width=14) (actual time=0.066..1.320 rows=50 loops=1)
Index Cond: ((id >= 1) AND (id <= 50))
Total runtime: 7.820 ms
(6 rows)
If that's not the problem, then do other tables have foreign key
references to the table(s) you're updating? If so, then you might
need indexes on the foreign key columns in the referring tables.
--
Michael Fuhr
http://www.fuhr.org/~mfuhr/
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: Temp tables...
2005-07-12 17:52 Temp tables... Greg Patnude <gpatnude@hotmail.com>
2005-07-13 00:57 ` Re: Temp tables... Michael Fuhr <mike@fuhr.org>
@ 2005-07-13 15:48 ` Greg Patnude <gpatnude@hotmail.com>
0 siblings, 0 replies; 24+ messages in thread
From: Greg Patnude @ 2005-07-13 15:48 UTC (permalink / raw)
To: pgsql-general
I am "TOP POSTING" intentionally --
Thanks Mike... Very informative -- I never realized that child (INHERITED)
tables do NOT inherit the indexes from their parent... that might be part of
the solution.... I duplicated the parents index on the child table -- the
function still takes 4672 ms to complete... Based on your evaluation -- I
now believe that the issue is in the UPDATE clause --
-- THIS IS AN EXAMPLE OF THE ACTUAL UPDATE... WHICH GETS CALLED 50 TIMES...
EXPLAIN ANALYZE UPDATE ONLY l_store_hours SET amount = amount * 1.00 WHERE
l_store_hours.id = 14511;
-- OUTPUT...
"Nested Loop (cost=0.00..6.05 rows=1 width=52) (actual time=67.487..67.493
rows=1 loops=1)"
" -> Index Scan using l_store_hours_pkey on l_store_hours lh
(cost=0.00..3.01 rows=1 width=52) (actual time=54.674..54.675 rows=1
loops=1)"
" Index Cond: (14511 = id)"
" -> Index Scan using l_store_hours_pkey on l_store_hours
(cost=0.00..3.01 rows=1 width=4) (actual time=0.006..0.009 rows=1 loops=1)"
" Index Cond: (id = 14511)"
"Total runtime: 87.803 ms"
""
"Index Scan using l_store_hours_pkey on l_store_hours (cost=0.00..3.02
rows=1 width=58) (actual time=0.032..0.034 rows=1 loops=1)"
" Index Cond: (id = 14511)"
"Total runtime: 0.500 ms"
-- THE TABLES, CHILD TABLE, VIEW, AND FUNCTION...
CREATE TABLE l_store_hours (
id serial PRIMARY KEY,
l_store_id int4 NOT NULL DEFAULT 0,
l_activity_type int4 NOT NULL REFERENCES l_activity_type(id),
week_code int4 NOT NULL DEFAULT 0,
year_code int4 NOT NULL DEFAULT 0,
amount numeric(10,2) DEFAULT 0.00,
create_dt timestamp NOT NULL DEFAULT now(),
change_dt timestamp NOT NULL DEFAULT now(),
change_id int4 DEFAULT 0,
active_flag bool DEFAULT true
) WITH OIDS;
CREATE INDEX idx_store_hours ON l_store_hours USING btree (id, l_store_id,
year_code, week_code);
ALTER TABLE l_store_hours CLUSTER ON idx_store_hours;
-- THE CHILD (INHERITED) TABLE...
CREATE TABLE l_store_hours_history (
hist_id serial PRIMARY KEY,
hist_dt timestamp NOT NULL DEFAULT now()
) INHERITS (l_store_hours) WITH OIDS;
CREATE INDEX idx_store_hours_history ON l_store_hours_history USING btree
(id, l_store_id, year_code, week_code);
ALTER TABLE l_store_hours_history CLUSTER ON idx_store_hours_history;
-- THE UPDATE RULE ON THE PARENT TABLE...
CREATE OR REPLACE RULE l_store_hours_history_upd AS ON UPDATE TO
l_store_hours DO INSERT INTO l_store_hours_history (SELECT * FROM ONLY
l_store_hours LH WHERE LH.id = old.id);
-- THE VIEW...
CREATE OR REPLACE VIEW v_storehours AS
SELECT ls.id, ls.l_activity_type, ls.l_store_id, ls.week_code, ls.year_code,
ls.amount, ls.create_dt, ls.change_dt, ls.change_id, ls.active_flag,
COALESCE(lsh.amount, ls.amount) AS previous,
COALESCE(lsh.hist_id, 0) AS history, lsh.hist_dt
FROM ONLY l_store_hours ls
FULL JOIN l_store_hours_history lsh ON ls.id = lsh.id
ORDER BY ls.year_code, ls.week_code;
/*
This function updates the block of expected employee hours (l_store_hours)
based on the current projected sales figures
*/
CREATE OR REPLACE FUNCTION l_updatehoursonsales(int4, int4, int4)
RETURNS bool AS
$BODY$
DECLARE ROW v_storesales%ROWTYPE;
DECLARE F1 real;
DECLARE CUR t_updhours%ROWTYPE;
DECLARE STORE ALIAS FOR $1;
DECLARE WEEK ALIAS FOR $2;
DECLARE YEAR ALIAS FOR $3;
DECLARE C RECORD;
BEGIN
RAISE LOG 'STARTING: l_updatehoursonsales for store: %', $1;
RAISE LOG 'STARTING: l_updatehoursonsales for week: %', $2;
RAISE LOG 'STARTING: l_updatehoursonsales for year: %', $3;
-- RAISE LOG 'CREATING TEMP TABLE AS SELECT...';
CREATE TEMPORARY TABLE tmphours AS
SELECT LT.type_desc, 0 AS hist_id, LSH.* FROM ONLY l_store_hours LSH
FULL OUTER JOIN l_activity_type LT ON LSH.l_activity_type = LT.id
WHERE LSH.l_store_id = $1 AND LSH.week_code = $2
AND LSH.year_code = $3 ORDER BY l_activity_type;
-- UPDATE THE TEMP TABLE WITH THE LEAST HISTORY ID FROM THE HISTORY
TABLE...
-- RAISE LOG 'SETTING HISTORY IDs...';
UPDATE tmphours SET hist_id = (
SELECT A.hist_id FROM (
SELECT MIN(hist_id) AS hist_id, id FROM ONLY l_store_hours_history LSH
WHERE tmphours.l_store_id = LSH.l_store_id
AND tmphours.year_code = LSH.year_code
AND tmphours.week_code = LSH.week_code
AND tmphours.l_activity_type = LSH.l_activity_type
AND tmphours.id = LSH.id
GROUP BY 2
)
AS A);
-- UPDATE THE AMOUNTS (HOURS) WITH THE PRESERVED VALUES...
-- RAISE LOG 'UPDATING tmphours --> Setting amount...';
UPDATE tmphours SET amount = LSH.amount FROM ONLY l_store_hours_history LSH
WHERE tmphours.hist_id = LSH.hist_id;
-- GET THE CURRENT SALES FIGURES FROM THE VIEW...
FOR ROW IN SELECT VSS.* FROM v_storesales VSS
WHERE VSS.l_store_id = $1 AND VSS.week_code = $2 AND VSS.year_code = $3
ORDER BY history LIMIT 1
LOOP
-- RAISE LOG 'LOOPING FOR UPDATE... %', ROW.id;
IF (ROW.amount > ROW.previous) THEN
SELECT (1 + (((ROW.amount::float / ROW.previous::float)::float - 1) *
0.8))::NUMERIC(10, 2) INTO F1;
RAISE LOG 'USING FORMULA (a > b): %', F1;
END IF;
IF (ROW.amount < ROW.previous) THEN
SELECT (ROW.amount::float / ROW.previous::float)::NUMERIC(10, 2) INTO F1;
RAISE LOG 'USING FORMULA: (a < b) %', F1;
END IF;
IF (ROW.amount = ROW.previous) THEN
SELECT 1.00::NUMERIC(10, 2) INTO F1;
RAISE LOG 'USING FORMULA: (a = b)%', F1;
END IF;
-- LOOP THROUGH THE TEMP TABLE AND UPDATE l_store_hours...
FOR C IN SELECT * FROM tmphours LOOP
UPDATE ONLY l_store_hours SET amount = C.amount * F1 WHERE
l_store_hours.id = C.id;
END LOOP;
END LOOP;
-- CLEAN UP AFTER YOURSELF...
RAISE LOG 'LOOP COMPLETE! Dropping TEMPORARY TABLE tmphours...';
DROP TABLE tmphours;
-- AND RETURN...
RETURN TRUE;
END;
$BODY$
LANGUAGE 'plpgsql' VOLATILE;
Regards,
Greg Patnude - Manager, Dynamic Applications Group
Data Mosaics, Inc.
2406 South Dishman-Mica Road / Suite # 6
Spokane Valley, WA 99206-6429
VOICE: (866) 904-DMSF
FAX: (509) 928-4236
"Michael Fuhr" <mike@fuhr.org> wrote in message
news:20050713005700.GA87233@winnie.fuhr.org...
> On Tue, Jul 12, 2005 at 10:52:24AM -0700, Greg Patnude wrote:
>>
>> Performing an update to an inherited table system from inside of a stored
>> procedure (PLPGSQL) seems to be unusually sluggish...
>
> Is the update slower when done inside a function than when doing
> it directly (e.g., from psql)? That is, is the use of a function
> relevant, or is the update equally slow in any case? Could you
> post the EXPLAIN ANALYZE output for the update?
>
> The message subject is "Temp tables." Are you using temporary
> tables, and if so, are you seeing different behavior with temporary
> tables than with "real" tables? Again, is that relevant to the
> problem?
>
>> Does anyone have a faster solution ? I am updating 50 records and it
>> takes approximately 4.375 seconds + or -....
>>
>> The inherited table has an ON INSERT DO INSTEAD and there are
>> approximately
>> 2 million rows in the inherited table structure...
>
> Could you post the table definitions, including all indexes, rules,
> etc.? Do all the child tables have indexes on the column(s) used
> to restrict the update? As the documentation states, indexes aren't
> inherited, so you might need to create additional indexes on the
> children, indexes that you'd think would be redundant. Example:
>
> CREATE TABLE parent (id serial PRIMARY KEY);
> CREATE TABLE child (x integer) INHERITS (parent);
>
> INSERT INTO child (x) SELECT * FROM generate_series(1, 100000);
>
> ANALYZE parent;
> ANALYZE child;
>
> EXPLAIN ANALYZE UPDATE child SET id = id WHERE id BETWEEN 1 AND 50;
> QUERY PLAN
> -------------------------------------------------------------------------------------------------------
> Seq Scan on child (cost=0.00..1991.00 rows=41 width=14) (actual
> time=0.059..307.234 rows=50 loops=1)
> Filter: ((id >= 1) AND (id <= 50))
> Total runtime: 309.350 ms
> (3 rows)
>
> EXPLAIN ANALYZE UPDATE parent SET id = id WHERE id BETWEEN 1 AND 50;
> QUERY PLAN
> -----------------------------------------------------------------------------------------------------------------------------
> Append (cost=0.00..2006.37 rows=52 width=14) (actual
> time=304.838..306.252 rows=50 loops=1)
> -> Index Scan using parent_pkey on parent (cost=0.00..15.37 rows=11
> width=10) (actual time=0.110..0.110 rows=0 loops=1)
> Index Cond: ((id >= 1) AND (id <= 50))
> -> Seq Scan on child parent (cost=0.00..1991.00 rows=41 width=14)
> (actual time=304.705..305.619 rows=50 loops=1)
> Filter: ((id >= 1) AND (id <= 50))
> Total runtime: 307.935 ms
> (6 rows)
>
> Notice the sequential scans on child, even though we have an index
> on parent.id, a column that child inherits. We need to create an
> index on child.id as well:
>
> CREATE INDEX child_id_idx ON child (id);
>
> EXPLAIN ANALYZE UPDATE child SET id = id WHERE id BETWEEN 1 AND 50;
> QUERY PLAN
> -----------------------------------------------------------------------------------------------------------------------
> Index Scan using child_id_idx on child (cost=0.00..3.65 rows=41 width=14)
> (actual time=0.369..1.371 rows=50 loops=1)
> Index Cond: ((id >= 1) AND (id <= 50))
> Total runtime: 6.100 ms
> (3 rows)
>
> EXPLAIN ANALYZE UPDATE parent SET id = id WHERE id BETWEEN 1 AND 50;
> QUERY PLAN
> ------------------------------------------------------------------------------------------------------------------------------------
> Append (cost=0.00..19.02 rows=52 width=14) (actual time=0.119..1.895
> rows=50 loops=1)
> -> Index Scan using parent_pkey on parent (cost=0.00..15.37 rows=11
> width=10) (actual time=0.037..0.037 rows=0 loops=1)
> Index Cond: ((id >= 1) AND (id <= 50))
> -> Index Scan using child_id_idx on child parent (cost=0.00..3.65
> rows=41 width=14) (actual time=0.066..1.320 rows=50 loops=1)
> Index Cond: ((id >= 1) AND (id <= 50))
> Total runtime: 7.820 ms
> (6 rows)
>
> If that's not the problem, then do other tables have foreign key
> references to the table(s) you're updating? If so, then you might
> need indexes on the foreign key columns in the referring tables.
>
> --
> Michael Fuhr
> http://www.fuhr.org/~mfuhr/
>
> ---------------------------(end of broadcast)---------------------------
> TIP 9: In versions below 8.0, the planner will ignore your desire to
> choose an index scan if your joining column's datatypes do not
> match
>
^ permalink raw reply [nested|flat] 24+ messages in thread
* Temp Tables
@ 2006-05-25 12:42 Brandon E Hofmann <Brandon_E_Hofmann@notes.ntrs.com>
2006-05-29 17:07 ` Re: Temp Tables William Leite Araújo <william.bh@gmail.com>
2006-05-29 17:14 ` Re: Temp Tables William Leite Araújo <william.bh@gmail.com>
0 siblings, 2 replies; 24+ messages in thread
From: Brandon E Hofmann @ 2006-05-25 12:42 UTC (permalink / raw)
To: pgsql-general
When using a temp table in plpgsql functions that has columns comprised
from many tables populated by joins, how do you specify a temp table return
type? I get an error when I specify returns setof temp_table. Also when I
specify a permanent table as the return type, I get extra blank columns in
the result set which are the additional columns of the permanent table I
don't need and the data is displayed in the wrong columns.
I tried defining composite types, but get a runtime error that it isn't
available.
Any help would be greatly appreciated.
Thanks,
Brandon
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: Temp Tables
2006-05-25 12:42 Temp Tables Brandon E Hofmann <Brandon_E_Hofmann@notes.ntrs.com>
@ 2006-05-29 17:07 ` William Leite Araújo <william.bh@gmail.com>
1 sibling, 0 replies; 24+ messages in thread
From: William Leite Araújo @ 2006-05-29 17:07 UTC (permalink / raw)
To: Brandon E Hofmann <Brandon_E_Hofmann@notes.ntrs.com>; +Cc: pgsql-general
2006/5/25, Brandon E Hofmann <Brandon_E_Hofmann@notes.ntrs.com>:
>
>
> (..)
> I tried defining composite types, but get a runtime error that it isn't
> available.
>
That is you postgres
--
William Leite Araújo
Especialista em Geoprocessamento - UFMG
Bacharel em Ciêncida da Computação - UFMG
MSN: ufmgwil@yahoo.com.br
ICQ: 222159351
GTalk: william.bh@gmail.com
Yahoo: ufmgwil@yahoo.com.br
Skype: william.bh
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: Temp Tables
2006-05-25 12:42 Temp Tables Brandon E Hofmann <Brandon_E_Hofmann@notes.ntrs.com>
@ 2006-05-29 17:14 ` William Leite Araújo <william.bh@gmail.com>
1 sibling, 0 replies; 24+ messages in thread
From: William Leite Araújo @ 2006-05-29 17:14 UTC (permalink / raw)
To: Brandon E Hofmann <Brandon_E_Hofmann@notes.ntrs.com>; +Cc: pgsql-general
2006/5/25, Brandon E Hofmann <Brandon_E_Hofmann@notes.ntrs.com>:
>
>
> (..)
> I tried defining composite types, but get a runtime error that it isn't
> available.
>
That is you postgresql version?
Why you need return the temporary table type? Why create a temporary
table if you use a function return type "setof"?
--
William Leite Araújo
^ permalink raw reply [nested|flat] 24+ messages in thread
* temp tables
@ 2010-04-30 16:14 Geoffrey <lists@serioustechnology.com>
2010-04-30 17:29 ` Re: temp tables A. Kretschmer <andreas.kretschmer@schollglas.com>
0 siblings, 1 reply; 24+ messages in thread
From: Geoffrey @ 2010-04-30 16:14 UTC (permalink / raw)
To: pgsql-general
Do temp tables need to be explicitly dropped, or do the go away when the
process that created them leaves?
--
Until later, Geoffrey
"I predict future happiness for America if they can prevent
the government from wasting the labors of the people under
the pretense of taking care of them."
- Thomas Jefferson
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: temp tables
2010-04-30 16:14 temp tables Geoffrey <lists@serioustechnology.com>
@ 2010-04-30 17:29 ` A. Kretschmer <andreas.kretschmer@schollglas.com>
0 siblings, 0 replies; 24+ messages in thread
From: A. Kretschmer @ 2010-04-30 17:29 UTC (permalink / raw)
To: pgsql-general
In response to Geoffrey :
> Do temp tables need to be explicitly dropped, or do the go away when the
> process that created them leaves?
The latter one.
But explicitely delete them isn't an error.
Andreas
--
Andreas Kretschmer
Kontakt: Heynitz: 035242/47150, D1: 0160/7141639 (mehr: -> Header)
GnuPG: 0x31720C99, 1006 CCB4 A326 1D42 6431 2EB0 389D 1DC2 3172 0C99
^ permalink raw reply [nested|flat] 24+ messages in thread
* Temp tables
@ 2013-05-23 07:24 Pascal Tufenkji <ptufenkji@usj.edu.lb>
2013-05-23 19:42 ` Re: Temp tables Steve Crawford <scrawford@pinpointresearch.com>
0 siblings, 1 reply; 24+ messages in thread
From: Pascal Tufenkji @ 2013-05-23 07:24 UTC (permalink / raw)
To: pgsql-general
Hi,
To enhance the performance of the queries, I use temp tables in my website
reports.
It seems that a temp table has not been dropped automatically in a certain
session, and now the report is giving an error since the temp table already
exists ?!
When I type the following, the database gives me an error:
dragon=# CREATE TEMP TABLE _parcours (id int);
ERROR: type "_parcours" already exists
1. How can I identify the session in which the temp table is still
locked, so I can drop it manually
2. Why does those cases happen and the temp tables don’t drop
automatically
I’d appreciate a quick reply
Thanks in advance
Pascal TUFENKJI
Service de Technologie de l'Information
Université Saint-Joseph - Rectorat
Tel: +961 1 421 132
Email: <mailto:ptufenkji@usj.edu.lb> ptufenkji@usj.edu.lb
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: Temp tables
2013-05-23 07:24 Temp tables Pascal Tufenkji <ptufenkji@usj.edu.lb>
@ 2013-05-23 19:42 ` Steve Crawford <scrawford@pinpointresearch.com>
2013-05-23 20:32 ` Re: Temp tables Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 24+ messages in thread
From: Steve Crawford @ 2013-05-23 19:42 UTC (permalink / raw)
To: Pascal Tufenkji <ptufenkji@usj.edu.lb>; +Cc: pgsql-general
On 05/23/2013 12:24 AM, Pascal Tufenkji wrote:
>
> Hi,
>
> To enhance the performance of the queries, I use temp tables in my
> website reports.
>
> It seems that a temp table has not been dropped automatically in a
> certain session, and now the report is giving an error since the temp
> table already exists ?!
>
> When I type the following, the database gives me an error:
>
> dragon=# CREATE TEMP TABLE _parcours (id int);
>
> ERROR: type "_parcours" already exists
>
> 1.How can I identify the session in which the temp table is still
> locked, so I can drop it manually
>
> 2.Why does those cases happen and the temp tables don't drop automatically
>
Two questions:
1. Is that the exact message you are getting. I would expect *relation*
"_parcours" already exists not *type*.
2. Are you using persistent connections or connection pooling?
It feels a bit like there is something leftover from a previous process
that was using the same connection.
Cheers,
Steve
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: Temp tables
2013-05-23 07:24 Temp tables Pascal Tufenkji <ptufenkji@usj.edu.lb>
2013-05-23 19:42 ` Re: Temp tables Steve Crawford <scrawford@pinpointresearch.com>
@ 2013-05-23 20:32 ` Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 0 replies; 24+ messages in thread
From: Tom Lane @ 2013-05-23 20:32 UTC (permalink / raw)
To: Steve Crawford <scrawford@pinpointresearch.com>; +Cc: Pascal Tufenkji <ptufenkji@usj.edu.lb>; pgsql-general
Steve Crawford <scrawford@pinpointresearch.com> writes:
> On 05/23/2013 12:24 AM, Pascal Tufenkji wrote:
>> When I type the following, the database gives me an error:
>> dragon=# CREATE TEMP TABLE _parcours (id int);
>> ERROR: type "_parcours" already exists
Perhaps you have a type or temp table named "parcours"? If so,
"_parcours" is the internal name of the associated array type.
You'll need to use a different name.
> It feels a bit like there is something leftover from a previous process
> that was using the same connection.
We have seen a small number of reports where it seemed that some catalog
entry(s) associated with temp tables didn't get dropped when they should
have been. No one's been able to create a reproducible case though.
regards, tom lane
--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general
^ permalink raw reply [nested|flat] 24+ messages in thread
* Temp tables
@ 2018-12-09 19:56 bhargav kamineni <kbn98406@gmail.com>
2018-12-09 20:06 ` Re: Temp tables David G. Johnston <david.g.johnston@gmail.com>
0 siblings, 1 reply; 24+ messages in thread
From: bhargav kamineni @ 2018-12-09 19:56 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
Hi,
What happens if we create and insert/update the data in TEMP tables , Does
that data really gets inserted at disk level or at buffer level and what
happens to this data after completion of the transaction ?
Thanks
Banu
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: Temp tables
2018-12-09 19:56 Temp tables bhargav kamineni <kbn98406@gmail.com>
@ 2018-12-09 20:06 ` David G. Johnston <david.g.johnston@gmail.com>
2018-12-10 11:31 ` Re: Temp tables Laurenz Albe <laurenz.albe@cybertec.at>
0 siblings, 1 reply; 24+ messages in thread
From: David G. Johnston @ 2018-12-09 20:06 UTC (permalink / raw)
To: bhargav kamineni <kbn98406@gmail.com>; +Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
On Sunday, December 9, 2018, bhargav kamineni <kbn98406@gmail.com> wrote:
>
> What happens if we create and insert/update the data in TEMP tables , Does
> that data really gets inserted at disk level or at buffer level
>
Disk
> and what happens to this data after completion of the transaction ?
>
Your choice. See “on conflict” clause of create temp table command.
David J.
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: Temp tables
2018-12-09 19:56 Temp tables bhargav kamineni <kbn98406@gmail.com>
2018-12-09 20:06 ` Re: Temp tables David G. Johnston <david.g.johnston@gmail.com>
@ 2018-12-10 11:31 ` Laurenz Albe <laurenz.albe@cybertec.at>
0 siblings, 0 replies; 24+ messages in thread
From: Laurenz Albe @ 2018-12-10 11:31 UTC (permalink / raw)
To: David G. Johnston <david.g.johnston@gmail.com>; bhargav kamineni <kbn98406@gmail.com>; +Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
David G. Johnston wrote:
> > and what happens to this data after completion of the transaction ?
> >
>
> Your choice. See “on conflict” clause of create temp table command.
You mean the ON COMMIT clause.
Yours,
Laurenz Albe
--
Cybertec | https://www.cybertec-postgresql.com
^ permalink raw reply [nested|flat] 24+ messages in thread
end of thread, other threads:[~2018-12-10 11:31 UTC | newest]
Thread overview: 24+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2002-03-04 20:48 Temp Tables mdb <mdb002@yahoo.com>
2002-03-07 17:56 ` Tom Lane <tgl@sss.pgh.pa.us>
2002-03-07 17:58 ` Bruce Momjian <pgman@candle.pha.pa.us>
2002-03-07 18:45 ` Len Morgan <len-morgan@kttk.net>
2002-03-07 22:16 ` Scott Marlowe <smarlowe@ihs.com>
2002-03-08 14:45 ` mdb <mdb002@yahoo.com>
2002-03-08 19:13 ` mdb <mdb002@yahoo.com>
2002-03-07 17:00 Temp Tables Peter Darley <pdarley@kinesis-cem.com>
2002-08-20 00:20 Temp tables Andrew Bartley <abartley@evolvosystems.com>
2002-08-20 02:43 ` Re: Temp tables Bruce Momjian <pgman@candle.pha.pa.us>
2005-07-12 17:52 Temp tables... Greg Patnude <gpatnude@hotmail.com>
2005-07-13 00:57 ` Re: Temp tables... Michael Fuhr <mike@fuhr.org>
2005-07-13 15:48 ` Re: Temp tables... Greg Patnude <gpatnude@hotmail.com>
2006-05-25 12:42 Temp Tables Brandon E Hofmann <Brandon_E_Hofmann@notes.ntrs.com>
2006-05-29 17:07 ` William Leite Araújo <william.bh@gmail.com>
2006-05-29 17:14 ` William Leite Araújo <william.bh@gmail.com>
2010-04-30 16:14 temp tables Geoffrey <lists@serioustechnology.com>
2010-04-30 17:29 ` Re: temp tables A. Kretschmer <andreas.kretschmer@schollglas.com>
2013-05-23 07:24 Temp tables Pascal Tufenkji <ptufenkji@usj.edu.lb>
2013-05-23 19:42 ` Re: Temp tables Steve Crawford <scrawford@pinpointresearch.com>
2013-05-23 20:32 ` Re: Temp tables Tom Lane <tgl@sss.pgh.pa.us>
2018-12-09 19:56 Temp tables bhargav kamineni <kbn98406@gmail.com>
2018-12-09 20:06 ` Re: Temp tables David G. Johnston <david.g.johnston@gmail.com>
2018-12-10 11:31 ` Re: Temp tables Laurenz Albe <laurenz.albe@cybertec.at>
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