diff options
author | Alvaro Herrera <alvherre@alvh.no-ip.org> | 2013-12-20 12:37:30 -0300 |
---|---|---|
committer | Alvaro Herrera <alvherre@alvh.no-ip.org> | 2013-12-20 12:37:30 -0300 |
commit | 6130208e75c98d76b526ac2ac08cedbd17b9f00d (patch) | |
tree | 2d4d53b490acf9339b38f14c06c1f1d6d0aa852b | |
parent | c32afe53c2e87a56e2ff930798a5588db0f7a516 (diff) | |
download | postgresql-6130208e75c98d76b526ac2ac08cedbd17b9f00d.tar.gz postgresql-6130208e75c98d76b526ac2ac08cedbd17b9f00d.zip |
Avoid useless palloc during transaction commit
We can allocate the initial relations-to-drop array when first needed,
instead of at function entry; this avoids allocating it when the
function is not going to do anything, which is most of the time.
Backpatch to 9.3, where this behavior was introduced by commit
279628a0a7cf5.
There's more that could be done here, such as possible reworking of the
code to avoid having to palloc anything, but that doesn't sound as
backpatchable as this relatively minor change.
Per complaint from Noah Misch in
20131031145234.GA621493@tornado.leadboat.com
-rw-r--r-- | src/backend/catalog/storage.c | 18 |
1 files changed, 11 insertions, 7 deletions
diff --git a/src/backend/catalog/storage.c b/src/backend/catalog/storage.c index 971a149d590..892f1636a90 100644 --- a/src/backend/catalog/storage.c +++ b/src/backend/catalog/storage.c @@ -314,8 +314,8 @@ smgrDoPendingDeletes(bool isCommit) PendingRelDelete *next; int nrels = 0, i = 0, - maxrels = 8; - SMgrRelation *srels = palloc(maxrels * sizeof(SMgrRelation)); + maxrels = 0; + SMgrRelation *srels = NULL; prev = NULL; for (pending = pendingDeletes; pending != NULL; pending = next) @@ -340,8 +340,13 @@ smgrDoPendingDeletes(bool isCommit) srel = smgropen(pending->relnode, pending->backend); - /* extend the array if needed (double the size) */ - if (maxrels <= nrels) + /* allocate the initial array, or extend it, if needed */ + if (maxrels == 0) + { + maxrels = 8; + srels = palloc(sizeof(SMgrRelation) * maxrels ); + } + else if (maxrels <= nrels) { maxrels *= 2; srels = repalloc(srels, sizeof(SMgrRelation) * maxrels); @@ -361,10 +366,9 @@ smgrDoPendingDeletes(bool isCommit) for (i = 0; i < nrels; i++) smgrclose(srels[i]); - } - - pfree(srels); + pfree(srels); + } } /* |