Skip to content

Commit b325dab

Browse files
committed
new_relation_targetlist used to cause about 8 separate (and
redundant) SearchSysCache searches per table column in an INSERT, which accounted for a good percentage of the CPU time for INSERT ... VALUES(). Now it only does two searches in the typical case.
1 parent ce2586d commit b325dab

File tree

1 file changed

+74
-58
lines changed

1 file changed

+74
-58
lines changed

src/backend/optimizer/prep/preptlist.c

Lines changed: 74 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/optimizer/prep/preptlist.c,v 1.21 1999/05/25 16:09:46 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/optimizer/prep/preptlist.c,v 1.22 1999/05/29 01:48:06 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -23,6 +23,7 @@
2323
#include "nodes/makefuncs.h"
2424

2525
#include "utils/builtins.h"
26+
#include "utils/syscache.h"
2627
#include "utils/lsyscache.h"
2728
#include "utils/palloc.h"
2829
#include "parser/parse_type.h"
@@ -286,80 +287,95 @@ replace_matching_resname(List *new_tlist, List *old_tlist)
286287
static List *
287288
new_relation_targetlist(Oid relid, Index rt_index, NodeTag node_type)
288289
{
289-
AttrNumber attno;
290290
List *t_list = NIL;
291-
char *attname;
292-
int typlen;
293-
Oid atttype = 0;
294-
bool attisset = false;
291+
int natts = get_relnatts(relid);
292+
AttrNumber attno;
295293

296-
for (attno = 1; attno <= get_relnatts(relid); attno++)
294+
for (attno = 1; attno <= natts; attno++)
297295
{
298-
attname = get_attname(relid, attno);
299-
atttype = get_atttype(relid, attno);
300-
301-
/*
302-
* Since this is an append or replace, the size of any set
303-
* attribute is the size of the OID used to represent it.
304-
*/
305-
attisset = get_attisset(relid, attname);
306-
if (attisset)
307-
typlen = typeLen(typeidType(OIDOID));
308-
else
309-
typlen = get_typlen(atttype);
296+
HeapTuple tp;
297+
Form_pg_attribute att_tup;
298+
char *attname;
299+
Oid atttype;
300+
int32 atttypmod;
301+
bool attisset;
302+
303+
tp = SearchSysCacheTuple(ATTNUM,
304+
ObjectIdGetDatum(relid),
305+
UInt16GetDatum(attno),
306+
0, 0);
307+
if (! HeapTupleIsValid(tp))
308+
{
309+
elog(ERROR, "new_relation_targetlist: no attribute tuple %u %d",
310+
relid, attno);
311+
}
312+
att_tup = (Form_pg_attribute) GETSTRUCT(tp);
313+
attname = pstrdup(att_tup->attname.data);
314+
atttype = att_tup->atttypid;
315+
atttypmod = att_tup->atttypmod;
316+
attisset = att_tup->attisset;
310317

311318
switch (node_type)
312319
{
313-
case T_Const:
320+
case T_Const: /* INSERT command */
314321
{
315322
struct varlena *typedefault = get_typdefault(atttype);
316-
int temp = 0;
317-
Const *temp2 = (Const *) NULL;
318-
TargetEntry *temp3 = (TargetEntry *) NULL;
323+
int typlen;
324+
Const *temp_const;
325+
TargetEntry *temp_tle;
319326

320327
if (typedefault == NULL)
321-
temp = 0;
328+
typlen = 0;
322329
else
323-
temp = typlen;
324-
325-
temp2 = makeConst(atttype,
326-
temp,
327-
(Datum) typedefault,
328-
(typedefault == (struct varlena *) NULL),
329-
/* XXX ? */
330-
false,
331-
false, /* not a set */
332-
false);
333-
334-
temp3 = makeTargetEntry(makeResdom(attno,
335-
atttype,
336-
-1,
337-
attname,
338-
0,
339-
(Oid) 0,
340-
false),
341-
(Node *) temp2);
342-
t_list = lappend(t_list, temp3);
330+
{
331+
/*
332+
* Since this is an append or replace, the size of
333+
* any set attribute is the size of the OID used to
334+
* represent it.
335+
*/
336+
if (attisset)
337+
typlen = get_typlen(OIDOID);
338+
else
339+
typlen = get_typlen(atttype);
340+
}
341+
342+
temp_const = makeConst(atttype,
343+
typlen,
344+
(Datum) typedefault,
345+
(typedefault == NULL),
346+
/* XXX ? */
347+
false,
348+
false, /* not a set */
349+
false);
350+
351+
temp_tle = makeTargetEntry(makeResdom(attno,
352+
atttype,
353+
-1,
354+
attname,
355+
0,
356+
(Oid) 0,
357+
false),
358+
(Node *) temp_const);
359+
t_list = lappend(t_list, temp_tle);
343360
break;
344361
}
345-
case T_Var:
362+
case T_Var: /* UPDATE command */
346363
{
347-
Var *temp_var = (Var *) NULL;
348-
TargetEntry *temp_list = NULL;
364+
Var *temp_var;
365+
TargetEntry *temp_tle;
349366

350-
temp_var = makeVar(rt_index, attno, atttype,
351-
get_atttypmod(relid, attno),
367+
temp_var = makeVar(rt_index, attno, atttype, atttypmod,
352368
0, rt_index, attno);
353369

354-
temp_list = makeTargetEntry(makeResdom(attno,
355-
atttype,
356-
get_atttypmod(relid, attno),
357-
attname,
358-
0,
359-
(Oid) 0,
360-
false),
361-
(Node *) temp_var);
362-
t_list = lappend(t_list, temp_list);
370+
temp_tle = makeTargetEntry(makeResdom(attno,
371+
atttype,
372+
atttypmod,
373+
attname,
374+
0,
375+
(Oid) 0,
376+
false),
377+
(Node *) temp_var);
378+
t_list = lappend(t_list, temp_tle);
363379
break;
364380
}
365381
default: /* do nothing */

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy