Skip to content

Commit f35e1c8

Browse files
committed
Revise init_sequence so that it doesn't leak memory if the requested
sequence doesn't exist.
1 parent 8a40400 commit f35e1c8

File tree

1 file changed

+31
-31
lines changed

1 file changed

+31
-31
lines changed

src/backend/commands/sequence.c

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -388,57 +388,57 @@ static SeqTable
388388
init_sequence(char *caller, char *name)
389389
{
390390
SeqTable elm,
391-
priv = (SeqTable) NULL;
392-
SeqTable temp;
391+
prev = (SeqTable) NULL;
392+
Relation seqrel;
393393

394-
for (elm = seqtab; elm != (SeqTable) NULL;)
394+
/* Look to see if we already have a seqtable entry for name */
395+
for (elm = seqtab; elm != (SeqTable) NULL; elm = elm->next)
395396
{
396397
if (strcmp(elm->name, name) == 0)
397398
break;
398-
priv = elm;
399-
elm = elm->next;
399+
prev = elm;
400400
}
401401

402-
if (elm == (SeqTable) NULL) /* not found */
403-
{
404-
temp = (SeqTable) malloc(sizeof(SeqTableData));
405-
temp->name = malloc(strlen(name) + 1);
406-
strcpy(temp->name, name);
407-
temp->rel = (Relation) NULL;
408-
temp->cached = temp->last = temp->increment = 0;
409-
temp->next = (SeqTable) NULL;
410-
}
411-
else
412-
/* found */
413-
{
414-
if (elm->rel != (Relation) NULL) /* already opened */
415-
return elm;
416-
temp = elm;
417-
}
402+
/* If so, and if it's already been opened in this xact, just return it */
403+
if (elm != (SeqTable) NULL && elm->rel != (Relation) NULL)
404+
return elm;
418405

419-
temp->rel = heap_openr(name, AccessShareLock);
420-
421-
if (temp->rel->rd_rel->relkind != RELKIND_SEQUENCE)
406+
/* Else open and check it */
407+
seqrel = heap_openr(name, AccessShareLock);
408+
if (seqrel->rd_rel->relkind != RELKIND_SEQUENCE)
422409
elog(ERROR, "%s.%s: %s is not sequence !", name, caller, name);
423410

424-
if (elm != (SeqTable) NULL) /* we opened sequence from our */
425-
{ /* SeqTable - check relid ! */
426-
if (RelationGetRelid(elm->rel) != elm->relid)
411+
if (elm != (SeqTable) NULL)
412+
{
413+
/* We are using a seqtable entry left over from a previous xact;
414+
* must check for relid change.
415+
*/
416+
elm->rel = seqrel;
417+
if (RelationGetRelid(seqrel) != elm->relid)
427418
{
428419
elog(NOTICE, "%s.%s: sequence was re-created",
429420
name, caller, name);
421+
elm->relid = RelationGetRelid(seqrel);
430422
elm->cached = elm->last = elm->increment = 0;
431-
elm->relid = RelationGetRelid(elm->rel);
432423
}
433424
}
434425
else
435426
{
436-
elm = temp;
437-
elm->relid = RelationGetRelid(elm->rel);
427+
/* Time to make a new seqtable entry. These entries live as long
428+
* as the backend does, so we use plain malloc for them.
429+
*/
430+
elm = (SeqTable) malloc(sizeof(SeqTableData));
431+
elm->name = malloc(strlen(name) + 1);
432+
strcpy(elm->name, name);
433+
elm->rel = seqrel;
434+
elm->relid = RelationGetRelid(seqrel);
435+
elm->cached = elm->last = elm->increment = 0;
436+
elm->next = (SeqTable) NULL;
437+
438438
if (seqtab == (SeqTable) NULL)
439439
seqtab = elm;
440440
else
441-
priv->next = elm;
441+
prev->next = elm;
442442
}
443443

444444
return elm;

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