Remove useless pfree()s at the ends of various ValuePerCall SRFs.
authorTom Lane <tgl@sss.pgh.pa.us>
Tue, 17 Mar 2020 01:36:53 +0000 (21:36 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Tue, 17 Mar 2020 01:36:53 +0000 (21:36 -0400)
We don't need to manually clean up allocations in a SRF's
multi_call_memory_ctx, because the SRF_RETURN_DONE infrastructure
takes care of that (and also ensures that it will happen even if the
function never gets a final call, which simple manual cleanup cannot
do).

Hence, the code removed by this patch is a waste of code and cycles.
Worse, it gives the impression that cleaning up manually is a thing,
which can lead to more serious errors such as those fixed in
commits 085b6b667 and b4570d33a.  So we should get rid of it.

These are not quite actual bugs though, so I couldn't muster the
enthusiasm to back-patch.  Fix in HEAD only.

Justin Pryzby

Discussion: https://postgr.es/m/20200308173103.GC1357@telsasoft.com

contrib/pageinspect/btreefuncs.c
contrib/pageinspect/ginfuncs.c
contrib/pageinspect/hashfuncs.c
src/backend/access/transam/multixact.c
src/backend/tsearch/wparser.c
src/backend/utils/adt/jsonfuncs.c
src/backend/utils/adt/tsvector_op.c

index e6a2fc1e155186a5e2cd753bcebcf7697d9a93fe..e7a323044bf9ad081fc2287ba52bc6a83ad5b0ba 100644 (file)
@@ -502,12 +502,8 @@ bt_page_items(PG_FUNCTION_ARGS)
                uargs->offset++;
                SRF_RETURN_NEXT(fctx, result);
        }
-       else
-       {
-               pfree(uargs->page);
-               pfree(uargs);
-               SRF_RETURN_DONE(fctx);
-       }
+
+       SRF_RETURN_DONE(fctx);
 }
 
 /*-------------------------------------------------------
@@ -590,11 +586,8 @@ bt_page_items_bytea(PG_FUNCTION_ARGS)
                uargs->offset++;
                SRF_RETURN_NEXT(fctx, result);
        }
-       else
-       {
-               pfree(uargs);
-               SRF_RETURN_DONE(fctx);
-       }
+
+       SRF_RETURN_DONE(fctx);
 }
 
 /* Number of output arguments (columns) for bt_metap() */
index 7e2cafab74de8fc7892c2a40a1bb3e7bdd8f6aa9..711473579a86fc999dd5c8f936af4894c5ae4577 100644 (file)
@@ -260,6 +260,6 @@ gin_leafpage_items(PG_FUNCTION_ARGS)
 
                SRF_RETURN_NEXT(fctx, result);
        }
-       else
-               SRF_RETURN_DONE(fctx);
+
+       SRF_RETURN_DONE(fctx);
 }
index 984ac33186040da6de0a4cbea9493e1e1c0d58b3..3b2f0339cfe09edea2e175af3dafe7895622b045 100644 (file)
@@ -374,11 +374,8 @@ hash_page_items(PG_FUNCTION_ARGS)
 
                SRF_RETURN_NEXT(fctx, result);
        }
-       else
-       {
-               pfree(uargs);
-               SRF_RETURN_DONE(fctx);
-       }
+
+       SRF_RETURN_DONE(fctx);
 }
 
 /* ------------------------------------------------
index 50e98caaeb03e22bfb0072286c6d3f6f2a1505fb..fdd0394ffae53fbfbcbec40266a05087984b8109 100644 (file)
@@ -3388,9 +3388,5 @@ pg_get_multixact_members(PG_FUNCTION_ARGS)
                SRF_RETURN_NEXT(funccxt, HeapTupleGetDatum(tuple));
        }
 
-       if (multi->nmembers > 0)
-               pfree(multi->members);
-       pfree(multi);
-
        SRF_RETURN_DONE(funccxt);
 }
index 88005c0519a0bd15a9780d6b1ca6e7fdf26ea9d6..9c1fc7b10142befdc6d732eb38d39a13d2f851e9 100644 (file)
@@ -104,9 +104,6 @@ tt_process_call(FuncCallContext *funcctx)
                st->cur++;
                return result;
        }
-       if (st->list)
-               pfree(st->list);
-       pfree(st);
        return (Datum) 0;
 }
 
@@ -245,12 +242,6 @@ prs_process_call(FuncCallContext *funcctx)
                st->cur++;
                return result;
        }
-       else
-       {
-               if (st->list)
-                       pfree(st->list);
-               pfree(st);
-       }
        return (Datum) 0;
 }
 
index f92861d8d2967c451f2593612387003ba154569e..4b5007e0d6fdd8c9c7411fb804190075b1d95ff9 100644 (file)
@@ -535,7 +535,6 @@ jsonb_object_keys(PG_FUNCTION_ARGS)
 {
        FuncCallContext *funcctx;
        OkeysState *state;
-       int                     i;
 
        if (SRF_IS_FIRSTCALL())
        {
@@ -598,12 +597,6 @@ jsonb_object_keys(PG_FUNCTION_ARGS)
                SRF_RETURN_NEXT(funcctx, CStringGetTextDatum(nxt));
        }
 
-       /* cleanup to reduce or eliminate memory leaks */
-       for (i = 0; i < state->result_count; i++)
-               pfree(state->result[i]);
-       pfree(state->result);
-       pfree(state);
-
        SRF_RETURN_DONE(funcctx);
 }
 
@@ -706,7 +699,6 @@ json_object_keys(PG_FUNCTION_ARGS)
 {
        FuncCallContext *funcctx;
        OkeysState *state;
-       int                     i;
 
        if (SRF_IS_FIRSTCALL())
        {
@@ -755,12 +747,6 @@ json_object_keys(PG_FUNCTION_ARGS)
                SRF_RETURN_NEXT(funcctx, CStringGetTextDatum(nxt));
        }
 
-       /* cleanup to reduce or eliminate memory leaks */
-       for (i = 0; i < state->result_count; i++)
-               pfree(state->result[i]);
-       pfree(state->result);
-       pfree(state);
-
        SRF_RETURN_DONE(funcctx);
 }
 
index 108dd998c7a5bd4c853c1e585d96f341d7f26aa2..24525879b78322c05d59c2b9f038b220138222e0 100644 (file)
@@ -706,7 +706,6 @@ tsvector_unnest(PG_FUNCTION_ARGS)
        }
        else
        {
-               pfree(tsin);
                SRF_RETURN_DONE(funcctx);
        }
 }