Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit604bd36

Browse files
committed
PG_FINALLY
This gives an alternative way of catching exceptions, for the commoncase where the cleanup code is the same in the error and non-errorcases. So instead of PG_TRY(); { ... code that might throw ereport(ERROR) ... } PG_CATCH(); { cleanup();PG_RE_THROW(); } PG_END_TRY(); cleanup();one can write PG_TRY(); { ... code that might throw ereport(ERROR) ... } PG_FINALLY(); { cleanup(); } PG_END_TRY();Discussion:https://www.postgresql.org/message-id/flat/95a822c3-728b-af0e-d7e5-71890507ae0c%402ndquadrant.com
1 parent7302514 commit604bd36

File tree

32 files changed

+91
-245
lines changed

32 files changed

+91
-245
lines changed

‎contrib/auto_explain/auto_explain.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -320,12 +320,10 @@ explain_ExecutorRun(QueryDesc *queryDesc, ScanDirection direction,
320320
prev_ExecutorRun(queryDesc,direction,count,execute_once);
321321
else
322322
standard_ExecutorRun(queryDesc,direction,count,execute_once);
323-
nesting_level--;
324323
}
325-
PG_CATCH();
324+
PG_FINALLY();
326325
{
327326
nesting_level--;
328-
PG_RE_THROW();
329327
}
330328
PG_END_TRY();
331329
}
@@ -343,12 +341,10 @@ explain_ExecutorFinish(QueryDesc *queryDesc)
343341
prev_ExecutorFinish(queryDesc);
344342
else
345343
standard_ExecutorFinish(queryDesc);
346-
nesting_level--;
347344
}
348-
PG_CATCH();
345+
PG_FINALLY();
349346
{
350347
nesting_level--;
351-
PG_RE_THROW();
352348
}
353349
PG_END_TRY();
354350
}

‎contrib/dblink/dblink.c

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -776,19 +776,14 @@ dblink_record_internal(FunctionCallInfo fcinfo, bool is_async)
776776
}
777777
}
778778
}
779-
PG_CATCH();
779+
PG_FINALLY();
780780
{
781781
/* if needed, close the connection to the database */
782782
if (freeconn)
783783
PQfinish(conn);
784-
PG_RE_THROW();
785784
}
786785
PG_END_TRY();
787786

788-
/* if needed, close the connection to the database */
789-
if (freeconn)
790-
PQfinish(conn);
791-
792787
return (Datum)0;
793788
}
794789

@@ -952,14 +947,11 @@ materializeResult(FunctionCallInfo fcinfo, PGconn *conn, PGresult *res)
952947
/* clean up and return the tuplestore */
953948
tuplestore_donestoring(tupstore);
954949
}
955-
956-
PQclear(res);
957950
}
958-
PG_CATCH();
951+
PG_FINALLY();
959952
{
960953
/* be sure to release the libpq result */
961954
PQclear(res);
962-
PG_RE_THROW();
963955
}
964956
PG_END_TRY();
965957
}
@@ -1464,19 +1456,14 @@ dblink_exec(PG_FUNCTION_ARGS)
14641456
errmsg("statement returning results not allowed")));
14651457
}
14661458
}
1467-
PG_CATCH();
1459+
PG_FINALLY();
14681460
{
14691461
/* if needed, close the connection to the database */
14701462
if (freeconn)
14711463
PQfinish(conn);
1472-
PG_RE_THROW();
14731464
}
14741465
PG_END_TRY();
14751466

1476-
/* if needed, close the connection to the database */
1477-
if (freeconn)
1478-
PQfinish(conn);
1479-
14801467
PG_RETURN_TEXT_P(sql_cmd_status);
14811468
}
14821469

‎contrib/hstore_plpython/hstore_plpython.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,14 +180,11 @@ plpython_to_hstore(PG_FUNCTION_ARGS)
180180
pcount=hstoreUniquePairs(pairs,pcount,&buflen);
181181
out=hstorePairs(pairs,pcount,buflen);
182182
}
183-
PG_CATCH();
183+
PG_FINALLY();
184184
{
185185
Py_DECREF(items);
186-
PG_RE_THROW();
187186
}
188187
PG_END_TRY();
189188

190-
Py_DECREF(items);
191-
192189
PG_RETURN_POINTER(out);
193190
}

‎contrib/jsonb_plpython/jsonb_plpython.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -307,15 +307,12 @@ PLyMapping_ToJsonbValue(PyObject *obj, JsonbParseState **jsonb_state)
307307

308308
out=pushJsonbValue(jsonb_state,WJB_END_OBJECT,NULL);
309309
}
310-
PG_CATCH();
310+
PG_FINALLY();
311311
{
312312
Py_DECREF(items);
313-
PG_RE_THROW();
314313
}
315314
PG_END_TRY();
316315

317-
Py_DECREF(items);
318-
319316
returnout;
320317
}
321318

‎contrib/pg_stat_statements/pg_stat_statements.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -892,12 +892,10 @@ pgss_ExecutorRun(QueryDesc *queryDesc, ScanDirection direction, uint64 count,
892892
prev_ExecutorRun(queryDesc,direction,count,execute_once);
893893
else
894894
standard_ExecutorRun(queryDesc,direction,count,execute_once);
895-
nested_level--;
896895
}
897-
PG_CATCH();
896+
PG_FINALLY();
898897
{
899898
nested_level--;
900-
PG_RE_THROW();
901899
}
902900
PG_END_TRY();
903901
}
@@ -915,12 +913,10 @@ pgss_ExecutorFinish(QueryDesc *queryDesc)
915913
prev_ExecutorFinish(queryDesc);
916914
else
917915
standard_ExecutorFinish(queryDesc);
918-
nested_level--;
919916
}
920-
PG_CATCH();
917+
PG_FINALLY();
921918
{
922919
nested_level--;
923-
PG_RE_THROW();
924920
}
925921
PG_END_TRY();
926922
}
@@ -1007,12 +1003,10 @@ pgss_ProcessUtility(PlannedStmt *pstmt, const char *queryString,
10071003
standard_ProcessUtility(pstmt,queryString,
10081004
context,params,queryEnv,
10091005
dest,completionTag);
1010-
nested_level--;
10111006
}
1012-
PG_CATCH();
1007+
PG_FINALLY();
10131008
{
10141009
nested_level--;
1015-
PG_RE_THROW();
10161010
}
10171011
PG_END_TRY();
10181012

‎contrib/pg_trgm/trgm_regexp.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -555,15 +555,12 @@ createTrgmNFA(text *text_re, Oid collation,
555555
{
556556
trg=createTrgmNFAInternal(&regex,graph,rcontext);
557557
}
558-
PG_CATCH();
558+
PG_FINALLY();
559559
{
560560
pg_regfree(&regex);
561-
PG_RE_THROW();
562561
}
563562
PG_END_TRY();
564563

565-
pg_regfree(&regex);
566-
567564
/* Clean up all the cruft we created */
568565
MemoryContextSwitchTo(oldcontext);
569566
MemoryContextDelete(tmpcontext);

‎contrib/postgres_fdw/connection.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -631,15 +631,12 @@ pgfdw_report_error(int elevel, PGresult *res, PGconn *conn,
631631
message_context ?errcontext("%s",message_context) :0,
632632
sql ?errcontext("remote SQL command: %s",sql) :0));
633633
}
634-
PG_CATCH();
634+
PG_FINALLY();
635635
{
636636
if (clear)
637637
PQclear(res);
638-
PG_RE_THROW();
639638
}
640639
PG_END_TRY();
641-
if (clear)
642-
PQclear(res);
643640
}
644641

645642
/*

‎contrib/postgres_fdw/postgres_fdw.c

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3155,15 +3155,11 @@ get_remote_estimate(const char *sql, PGconn *conn,
31553155
startup_cost,total_cost,rows,width);
31563156
if (n!=4)
31573157
elog(ERROR,"could not interpret EXPLAIN output: \"%s\"",line);
3158-
3159-
PQclear(res);
3160-
res=NULL;
31613158
}
3162-
PG_CATCH();
3159+
PG_FINALLY();
31633160
{
31643161
if (res)
31653162
PQclear(res);
3166-
PG_RE_THROW();
31673163
}
31683164
PG_END_TRY();
31693165
}
@@ -3383,15 +3379,11 @@ fetch_more_data(ForeignScanState *node)
33833379

33843380
/* Must be EOF if we didn't get as many tuples as we asked for. */
33853381
fsstate->eof_reached= (numrows<fsstate->fetch_size);
3386-
3387-
PQclear(res);
3388-
res=NULL;
33893382
}
3390-
PG_CATCH();
3383+
PG_FINALLY();
33913384
{
33923385
if (res)
33933386
PQclear(res);
3394-
PG_RE_THROW();
33953387
}
33963388
PG_END_TRY();
33973389

@@ -4404,15 +4396,11 @@ postgresAnalyzeForeignTable(Relation relation,
44044396
if (PQntuples(res)!=1||PQnfields(res)!=1)
44054397
elog(ERROR,"unexpected result from deparseAnalyzeSizeSql query");
44064398
*totalpages=strtoul(PQgetvalue(res,0,0),NULL,10);
4407-
4408-
PQclear(res);
4409-
res=NULL;
44104399
}
4411-
PG_CATCH();
4400+
PG_FINALLY();
44124401
{
44134402
if (res)
44144403
PQclear(res);
4415-
PG_RE_THROW();
44164404
}
44174405
PG_END_TRY();
44184406

@@ -4925,16 +4913,11 @@ postgresImportForeignSchema(ImportForeignSchemaStmt *stmt, Oid serverOid)
49254913

49264914
commands=lappend(commands,pstrdup(buf.data));
49274915
}
4928-
4929-
/* Clean up */
4930-
PQclear(res);
4931-
res=NULL;
49324916
}
4933-
PG_CATCH();
4917+
PG_FINALLY();
49344918
{
49354919
if (res)
49364920
PQclear(res);
4937-
PG_RE_THROW();
49384921
}
49394922
PG_END_TRY();
49404923

‎contrib/sepgsql/hooks.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -372,13 +372,11 @@ sepgsql_utility_command(PlannedStmt *pstmt,
372372
context,params,queryEnv,
373373
dest,completionTag);
374374
}
375-
PG_CATCH();
375+
PG_FINALLY();
376376
{
377377
sepgsql_context_info=saved_context_info;
378-
PG_RE_THROW();
379378
}
380379
PG_END_TRY();
381-
sepgsql_context_info=saved_context_info;
382380
}
383381

384382
/*

‎contrib/sepgsql/label.c

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -465,14 +465,11 @@ sepgsql_get_label(Oid classId, Oid objectId, int32 subId)
465465
{
466466
label=pstrdup(unlabeled);
467467
}
468-
PG_CATCH();
468+
PG_FINALLY();
469469
{
470470
freecon(unlabeled);
471-
PG_RE_THROW();
472471
}
473472
PG_END_TRY();
474-
475-
freecon(unlabeled);
476473
}
477474
returnlabel;
478475
}
@@ -600,13 +597,11 @@ sepgsql_mcstrans_in(PG_FUNCTION_ARGS)
600597
{
601598
result=pstrdup(raw_label);
602599
}
603-
PG_CATCH();
600+
PG_FINALLY();
604601
{
605602
freecon(raw_label);
606-
PG_RE_THROW();
607603
}
608604
PG_END_TRY();
609-
freecon(raw_label);
610605

611606
PG_RETURN_TEXT_P(cstring_to_text(result));
612607
}
@@ -640,13 +635,11 @@ sepgsql_mcstrans_out(PG_FUNCTION_ARGS)
640635
{
641636
result=pstrdup(qual_label);
642637
}
643-
PG_CATCH();
638+
PG_FINALLY();
644639
{
645640
freecon(qual_label);
646-
PG_RE_THROW();
647641
}
648642
PG_END_TRY();
649-
freecon(qual_label);
650643

651644
PG_RETURN_TEXT_P(cstring_to_text(result));
652645
}
@@ -851,13 +844,11 @@ exec_object_restorecon(struct selabel_handle *sehnd, Oid catalogId)
851844

852845
SetSecurityLabel(&object,SEPGSQL_LABEL_TAG,context);
853846
}
854-
PG_CATCH();
847+
PG_FINALLY();
855848
{
856849
freecon(context);
857-
PG_RE_THROW();
858850
}
859851
PG_END_TRY();
860-
freecon(context);
861852
}
862853
elseif (errno==ENOENT)
863854
ereport(WARNING,
@@ -937,14 +928,11 @@ sepgsql_restorecon(PG_FUNCTION_ARGS)
937928
exec_object_restorecon(sehnd,AttributeRelationId);
938929
exec_object_restorecon(sehnd,ProcedureRelationId);
939930
}
940-
PG_CATCH();
931+
PG_FINALLY();
941932
{
942933
selabel_close(sehnd);
943-
PG_RE_THROW();
944934
}
945935
PG_END_TRY();
946936

947-
selabel_close(sehnd);
948-
949937
PG_RETURN_BOOL(true);
950938
}

‎contrib/sepgsql/selinux.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -871,13 +871,11 @@ sepgsql_compute_create(const char *scontext,
871871
{
872872
result=pstrdup(ncontext);
873873
}
874-
PG_CATCH();
874+
PG_FINALLY();
875875
{
876876
freecon(ncontext);
877-
PG_RE_THROW();
878877
}
879878
PG_END_TRY();
880-
freecon(ncontext);
881879

882880
returnresult;
883881
}

‎contrib/sepgsql/uavc.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,14 +181,11 @@ sepgsql_avc_unlabeled(void)
181181
{
182182
avc_unlabeled=MemoryContextStrdup(avc_mem_cxt,unlabeled);
183183
}
184-
PG_CATCH();
184+
PG_FINALLY();
185185
{
186186
freecon(unlabeled);
187-
PG_RE_THROW();
188187
}
189188
PG_END_TRY();
190-
191-
freecon(unlabeled);
192189
}
193190
returnavc_unlabeled;
194191
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp