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

Commit218527d

Browse files
committed
Don't bother checking the result of SPI_connect[_ext] anymore.
SPI_connect/SPI_connect_ext have not returned any value other thanSPI_OK_CONNECT since commit1833f1a in v10; any errors are thrownvia ereport. (The most likely failure is out-of-memory, which hasalways been thrown that way, so callers had better be prepared forsuch errors.) This makes it somewhat pointless to check thesefunctions' result, and some callers within our code haven't beenbothering; indeed, the only usage example within spi.sgml doesn'tbother. So it's likely that the omission has propagated intoextensions too.Hence, let's standardize on not checking, and document the returnvalue as historical, while not actually changing these functions'behavior. (The original proposal was to change their return typeto "void", but that would needlessly break extensions that areconforming to the old practice.) This saves a small amount ofboilerplate code in a lot of places.Stepan NeretinDiscussion:https://postgr.es/m/CAMaYL5Z9Uk8cD9qGz9QaZ2UBJFOu7jFx5Mwbznz-1tBbPDQZow@mail.gmail.com
1 parentcdb6b0f commit218527d

File tree

16 files changed

+44
-88
lines changed

16 files changed

+44
-88
lines changed

‎contrib/dblink/dblink.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2377,9 +2377,7 @@ get_tuple_of_interest(Relation rel, int *pkattnums, int pknumatts, char **src_pk
23772377
/*
23782378
* Connect to SPI manager
23792379
*/
2380-
if ((ret=SPI_connect())<0)
2381-
/* internal error */
2382-
elog(ERROR,"SPI connect failure - returned %d",ret);
2380+
SPI_connect();
23832381

23842382
initStringInfo(&buf);
23852383

‎contrib/spi/refint.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,7 @@ check_primary_key(PG_FUNCTION_ARGS)
108108
tupdesc=rel->rd_att;
109109

110110
/* Connect to SPI manager */
111-
if ((ret=SPI_connect())<0)
112-
/* internal error */
113-
elog(ERROR,"check_primary_key: SPI_connect returned %d",ret);
111+
SPI_connect();
114112

115113
/*
116114
* We use SPI plan preparation feature, so allocate space to place key
@@ -328,9 +326,7 @@ check_foreign_key(PG_FUNCTION_ARGS)
328326
tupdesc=rel->rd_att;
329327

330328
/* Connect to SPI manager */
331-
if ((ret=SPI_connect())<0)
332-
/* internal error */
333-
elog(ERROR,"check_foreign_key: SPI_connect returned %d",ret);
329+
SPI_connect();
334330

335331
/*
336332
* We use SPI plan preparation feature, so allocate space to place key

‎contrib/tablefunc/tablefunc.c

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -385,9 +385,7 @@ crosstab(PG_FUNCTION_ARGS)
385385
per_query_ctx=rsinfo->econtext->ecxt_per_query_memory;
386386

387387
/* Connect to SPI manager */
388-
if ((ret=SPI_connect())<0)
389-
/* internal error */
390-
elog(ERROR,"crosstab: SPI_connect returned %d",ret);
388+
SPI_connect();
391389

392390
/* Retrieve the desired rows */
393391
ret=SPI_execute(sql, true,0);
@@ -724,9 +722,7 @@ load_categories_hash(char *cats_sql, MemoryContext per_query_ctx)
724722
HASH_ELEM |HASH_STRINGS |HASH_CONTEXT);
725723

726724
/* Connect to SPI manager */
727-
if ((ret=SPI_connect())<0)
728-
/* internal error */
729-
elog(ERROR,"load_categories_hash: SPI_connect returned %d",ret);
725+
SPI_connect();
730726

731727
/* Retrieve the category name rows */
732728
ret=SPI_execute(cats_sql, true,0);
@@ -806,9 +802,7 @@ get_crosstab_tuplestore(char *sql,
806802
tupstore=tuplestore_begin_heap(randomAccess, false,work_mem);
807803

808804
/* Connect to SPI manager */
809-
if ((ret=SPI_connect())<0)
810-
/* internal error */
811-
elog(ERROR,"get_crosstab_tuplestore: SPI_connect returned %d",ret);
805+
SPI_connect();
812806

813807
/* Now retrieve the crosstab source rows */
814808
ret=SPI_execute(sql, true,0);
@@ -1151,15 +1145,11 @@ connectby(char *relname,
11511145
AttInMetadata*attinmeta)
11521146
{
11531147
Tuplestorestate*tupstore=NULL;
1154-
intret;
11551148
MemoryContextoldcontext;
1156-
11571149
intserial=1;
11581150

11591151
/* Connect to SPI manager */
1160-
if ((ret=SPI_connect())<0)
1161-
/* internal error */
1162-
elog(ERROR,"connectby: SPI_connect returned %d",ret);
1152+
SPI_connect();
11631153

11641154
/* switch to longer term context to create the tuple store */
11651155
oldcontext=MemoryContextSwitchTo(per_query_ctx);

‎contrib/xml2/xpath.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -560,8 +560,7 @@ xpath_table(PG_FUNCTION_ARGS)
560560
relname,
561561
condition);
562562

563-
if ((ret=SPI_connect())<0)
564-
elog(ERROR,"xpath_table: SPI_connect returned %d",ret);
563+
SPI_connect();
565564

566565
if ((ret=SPI_exec(query_buf.data,0))!=SPI_OK_SELECT)
567566
elog(ERROR,"xpath_table: SPI execution failed for query %s",

‎doc/src/sgml/spi.sgml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -126,16 +126,16 @@ int SPI_connect_ext(int <parameter>options</parameter>)
126126
</para>
127127
</listitem>
128128
</varlistentry>
129-
130-
<varlistentry>
131-
<term><symbol>SPI_ERROR_CONNECT</symbol></term>
132-
<listitem>
133-
<para>
134-
on error
135-
</para>
136-
</listitem>
137-
</varlistentry>
138129
</variablelist>
130+
131+
<para>
132+
The fact that these functions return <type>int</type>
133+
not <type>void</type> is historical. All failure cases are reported
134+
via <function>ereport</function> or <function>elog</function>.
135+
(In versions before <productname>PostgreSQL</productname> v10,
136+
some but not all failures would be reported with a result value
137+
of <symbol>SPI_ERROR_CONNECT</symbol>.)
138+
</para>
139139
</refsect1>
140140
</refentry>
141141

‎doc/src/sgml/trigger.sgml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -915,8 +915,7 @@ trigf(PG_FUNCTION_ARGS)
915915
tupdesc = trigdata->tg_relation->rd_att;
916916

917917
/* connect to SPI manager */
918-
if ((ret = SPI_connect()) < 0)
919-
elog(ERROR, "trigf (fired %s): SPI_connect returned %d", when, ret);
918+
SPI_connect();
920919

921920
/* get number of rows in table */
922921
ret = SPI_exec("SELECT count(*) FROM ttest", 0);

‎src/backend/commands/matview.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -639,8 +639,7 @@ refresh_by_match_merge(Oid matviewOid, Oid tempOid, Oid relowner,
639639
relnatts=RelationGetNumberOfAttributes(matviewRel);
640640

641641
/* Open SPI context. */
642-
if (SPI_connect()!=SPI_OK_CONNECT)
643-
elog(ERROR,"SPI_connect failed");
642+
SPI_connect();
644643

645644
/* Analyze the temp table with the new contents. */
646645
appendStringInfo(&querybuf,"ANALYZE %s",tempname);

‎src/backend/utils/adt/ri_triggers.c

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -340,8 +340,7 @@ RI_FKey_check(TriggerData *trigdata)
340340
break;
341341
}
342342

343-
if (SPI_connect()!=SPI_OK_CONNECT)
344-
elog(ERROR,"SPI_connect failed");
343+
SPI_connect();
345344

346345
/* Fetch or prepare a saved plan for the real check */
347346
ri_BuildQueryKey(&qkey,riinfo,RI_PLAN_CHECK_LOOKUPPK);
@@ -469,8 +468,7 @@ ri_Check_Pk_Match(Relation pk_rel, Relation fk_rel,
469468
/* Only called for non-null rows */
470469
Assert(ri_NullCheck(RelationGetDescr(pk_rel),oldslot,riinfo, true)==RI_KEYS_NONE_NULL);
471470

472-
if (SPI_connect()!=SPI_OK_CONNECT)
473-
elog(ERROR,"SPI_connect failed");
471+
SPI_connect();
474472

475473
/*
476474
* Fetch or prepare a saved plan for checking PK table with values coming
@@ -656,8 +654,7 @@ ri_restrict(TriggerData *trigdata, bool is_no_action)
656654
returnPointerGetDatum(NULL);
657655
}
658656

659-
if (SPI_connect()!=SPI_OK_CONNECT)
660-
elog(ERROR,"SPI_connect failed");
657+
SPI_connect();
661658

662659
/*
663660
* Fetch or prepare a saved plan for the restrict lookup (it's the same
@@ -766,8 +763,7 @@ RI_FKey_cascade_del(PG_FUNCTION_ARGS)
766763
pk_rel=trigdata->tg_relation;
767764
oldslot=trigdata->tg_trigslot;
768765

769-
if (SPI_connect()!=SPI_OK_CONNECT)
770-
elog(ERROR,"SPI_connect failed");
766+
SPI_connect();
771767

772768
/* Fetch or prepare a saved plan for the cascaded delete */
773769
ri_BuildQueryKey(&qkey,riinfo,RI_PLAN_CASCADE_ONDELETE);
@@ -875,8 +871,7 @@ RI_FKey_cascade_upd(PG_FUNCTION_ARGS)
875871
newslot=trigdata->tg_newslot;
876872
oldslot=trigdata->tg_trigslot;
877873

878-
if (SPI_connect()!=SPI_OK_CONNECT)
879-
elog(ERROR,"SPI_connect failed");
874+
SPI_connect();
880875

881876
/* Fetch or prepare a saved plan for the cascaded update */
882877
ri_BuildQueryKey(&qkey,riinfo,RI_PLAN_CASCADE_ONUPDATE);
@@ -1051,8 +1046,7 @@ ri_set(TriggerData *trigdata, bool is_set_null, int tgkind)
10511046
pk_rel=trigdata->tg_relation;
10521047
oldslot=trigdata->tg_trigslot;
10531048

1054-
if (SPI_connect()!=SPI_OK_CONNECT)
1055-
elog(ERROR,"SPI_connect failed");
1049+
SPI_connect();
10561050

10571051
/*
10581052
* Fetch or prepare a saved plan for the trigger.
@@ -1547,8 +1541,7 @@ RI_Initial_Check(Trigger *trigger, Relation fk_rel, Relation pk_rel)
15471541
PGC_USERSET,PGC_S_SESSION,
15481542
GUC_ACTION_SAVE, true,0, false);
15491543

1550-
if (SPI_connect()!=SPI_OK_CONNECT)
1551-
elog(ERROR,"SPI_connect failed");
1544+
SPI_connect();
15521545

15531546
/*
15541547
* Generate the plan. We don't need to cache it, and there are no
@@ -1787,8 +1780,7 @@ RI_PartitionRemove_Check(Trigger *trigger, Relation fk_rel, Relation pk_rel)
17871780
PGC_USERSET,PGC_S_SESSION,
17881781
GUC_ACTION_SAVE, true,0, false);
17891782

1790-
if (SPI_connect()!=SPI_OK_CONNECT)
1791-
elog(ERROR,"SPI_connect failed");
1783+
SPI_connect();
17921784

17931785
/*
17941786
* Generate the plan. We don't need to cache it, and there are no

‎src/backend/utils/adt/ruleutils.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -589,8 +589,7 @@ pg_get_ruledef_worker(Oid ruleoid, int prettyFlags)
589589
/*
590590
* Connect to SPI manager
591591
*/
592-
if (SPI_connect()!=SPI_OK_CONNECT)
593-
elog(ERROR,"SPI_connect failed");
592+
SPI_connect();
594593

595594
/*
596595
* On the first call prepare the plan to lookup pg_rewrite. We read
@@ -782,8 +781,7 @@ pg_get_viewdef_worker(Oid viewoid, int prettyFlags, int wrapColumn)
782781
/*
783782
* Connect to SPI manager
784783
*/
785-
if (SPI_connect()!=SPI_OK_CONNECT)
786-
elog(ERROR,"SPI_connect failed");
784+
SPI_connect();
787785

788786
/*
789787
* On the first call prepare the plan to lookup pg_rewrite. We read

‎src/pl/plperl/plperl.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1947,8 +1947,7 @@ plperl_inline_handler(PG_FUNCTION_ARGS)
19471947

19481948
current_call_data=&this_call_data;
19491949

1950-
if (SPI_connect_ext(codeblock->atomic ?0 :SPI_OPT_NONATOMIC)!=SPI_OK_CONNECT)
1951-
elog(ERROR,"could not connect to SPI manager");
1950+
SPI_connect_ext(codeblock->atomic ?0 :SPI_OPT_NONATOMIC);
19521951

19531952
select_perl_context(desc.lanpltrusted);
19541953

@@ -2412,8 +2411,7 @@ plperl_func_handler(PG_FUNCTION_ARGS)
24122411
IsA(fcinfo->context,CallContext)&&
24132412
!castNode(CallContext,fcinfo->context)->atomic;
24142413

2415-
if (SPI_connect_ext(nonatomic ?SPI_OPT_NONATOMIC :0)!=SPI_OK_CONNECT)
2416-
elog(ERROR,"could not connect to SPI manager");
2414+
SPI_connect_ext(nonatomic ?SPI_OPT_NONATOMIC :0);
24172415

24182416
prodesc=compile_plperl_function(fcinfo->flinfo->fn_oid, false, false);
24192417
current_call_data->prodesc=prodesc;
@@ -2530,8 +2528,7 @@ plperl_trigger_handler(PG_FUNCTION_ARGS)
25302528
intrcPG_USED_FOR_ASSERTS_ONLY;
25312529

25322530
/* Connect to SPI manager */
2533-
if (SPI_connect()!=SPI_OK_CONNECT)
2534-
elog(ERROR,"could not connect to SPI manager");
2531+
SPI_connect();
25352532

25362533
/* Make transition tables visible to this SPI connection */
25372534
tdata= (TriggerData*)fcinfo->context;
@@ -2638,8 +2635,7 @@ plperl_event_trigger_handler(PG_FUNCTION_ARGS)
26382635
ErrorContextCallbackpl_error_context;
26392636

26402637
/* Connect to SPI manager */
2641-
if (SPI_connect()!=SPI_OK_CONNECT)
2642-
elog(ERROR,"could not connect to SPI manager");
2638+
SPI_connect();
26432639

26442640
/* Find or compile the function */
26452641
prodesc=compile_plperl_function(fcinfo->flinfo->fn_oid, false, true);

‎src/pl/plpgsql/src/pl_handler.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,7 @@ plpgsql_call_handler(PG_FUNCTION_ARGS)
235235
/*
236236
* Connect to SPI manager
237237
*/
238-
if ((rc=SPI_connect_ext(nonatomic ?SPI_OPT_NONATOMIC :0))!=SPI_OK_CONNECT)
239-
elog(ERROR,"SPI_connect failed: %s",SPI_result_code_string(rc));
238+
SPI_connect_ext(nonatomic ?SPI_OPT_NONATOMIC :0);
240239

241240
/* Find or compile the function */
242241
func=plpgsql_compile(fcinfo, false);
@@ -326,8 +325,7 @@ plpgsql_inline_handler(PG_FUNCTION_ARGS)
326325
/*
327326
* Connect to SPI manager
328327
*/
329-
if ((rc=SPI_connect_ext(codeblock->atomic ?0 :SPI_OPT_NONATOMIC))!=SPI_OK_CONNECT)
330-
elog(ERROR,"SPI_connect failed: %s",SPI_result_code_string(rc));
328+
SPI_connect_ext(codeblock->atomic ?0 :SPI_OPT_NONATOMIC);
331329

332330
/* Compile the anonymous code block */
333331
func=plpgsql_compile_inline(codeblock->source_text);
@@ -510,8 +508,7 @@ plpgsql_validator(PG_FUNCTION_ARGS)
510508
/*
511509
* Connect to SPI manager (is this needed for compilation?)
512510
*/
513-
if ((rc=SPI_connect())!=SPI_OK_CONNECT)
514-
elog(ERROR,"SPI_connect failed: %s",SPI_result_code_string(rc));
511+
SPI_connect();
515512

516513
/*
517514
* Set up a fake fcinfo with just enough info to satisfy

‎src/pl/plpython/plpy_main.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,7 @@ plpython3_call_handler(PG_FUNCTION_ARGS)
202202
!castNode(CallContext,fcinfo->context)->atomic;
203203

204204
/* Note: SPI_finish() happens in plpy_exec.c, which is dubious design */
205-
if (SPI_connect_ext(nonatomic ?SPI_OPT_NONATOMIC :0)!=SPI_OK_CONNECT)
206-
elog(ERROR,"SPI_connect failed");
205+
SPI_connect_ext(nonatomic ?SPI_OPT_NONATOMIC :0);
207206

208207
/*
209208
* Push execution context onto stack. It is important that this get
@@ -272,8 +271,7 @@ plpython3_inline_handler(PG_FUNCTION_ARGS)
272271
PLy_initialize();
273272

274273
/* Note: SPI_finish() happens in plpy_exec.c, which is dubious design */
275-
if (SPI_connect_ext(codeblock->atomic ?0 :SPI_OPT_NONATOMIC)!=SPI_OK_CONNECT)
276-
elog(ERROR,"SPI_connect failed");
274+
SPI_connect_ext(codeblock->atomic ?0 :SPI_OPT_NONATOMIC);
277275

278276
MemSet(fcinfo,0,SizeForFunctionCallInfo(0));
279277
MemSet(&flinfo,0,sizeof(flinfo));

‎src/pl/tcl/pltcl.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -808,8 +808,7 @@ pltcl_func_handler(PG_FUNCTION_ARGS, pltcl_call_state *call_state,
808808
!castNode(CallContext,fcinfo->context)->atomic;
809809

810810
/* Connect to SPI manager */
811-
if (SPI_connect_ext(nonatomic ?SPI_OPT_NONATOMIC :0)!=SPI_OK_CONNECT)
812-
elog(ERROR,"could not connect to SPI manager");
811+
SPI_connect_ext(nonatomic ?SPI_OPT_NONATOMIC :0);
813812

814813
/* Find or compile the function */
815814
prodesc=compile_pltcl_function(fcinfo->flinfo->fn_oid,InvalidOid,
@@ -1072,8 +1071,7 @@ pltcl_trigger_handler(PG_FUNCTION_ARGS, pltcl_call_state *call_state,
10721071
call_state->trigdata=trigdata;
10731072

10741073
/* Connect to SPI manager */
1075-
if (SPI_connect()!=SPI_OK_CONNECT)
1076-
elog(ERROR,"could not connect to SPI manager");
1074+
SPI_connect();
10771075

10781076
/* Make transition tables visible to this SPI connection */
10791077
rc=SPI_register_trigger_data(trigdata);
@@ -1321,8 +1319,7 @@ pltcl_event_trigger_handler(PG_FUNCTION_ARGS, pltcl_call_state *call_state,
13211319
inttcl_rc;
13221320

13231321
/* Connect to SPI manager */
1324-
if (SPI_connect()!=SPI_OK_CONNECT)
1325-
elog(ERROR,"could not connect to SPI manager");
1322+
SPI_connect();
13261323

13271324
/* Find or compile the function */
13281325
prodesc=compile_pltcl_function(fcinfo->flinfo->fn_oid,

‎src/test/modules/plsample/plsample.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,7 @@ plsample_trigger_handler(PG_FUNCTION_ARGS)
220220
elog(ERROR,"not called by trigger manager");
221221

222222
/* Connect to the SPI manager */
223-
if (SPI_connect()!=SPI_OK_CONNECT)
224-
elog(ERROR,"could not connect to SPI manager");
223+
SPI_connect();
225224

226225
rc=SPI_register_trigger_data(trigdata);
227226
Assert(rc >=0);

‎src/test/modules/test_predtest/test_predtest.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ test_predtest(PG_FUNCTION_ARGS)
5454
inti;
5555

5656
/* We use SPI to parse, plan, and execute the test query */
57-
if (SPI_connect()!=SPI_OK_CONNECT)
58-
elog(ERROR,"SPI_connect failed");
57+
SPI_connect();
5958

6059
/*
6160
* First, plan and execute the query, and inspect the results. To the

‎src/test/regress/regress.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,8 +379,7 @@ ttdummy(PG_FUNCTION_ARGS)
379379
newoff=Int32GetDatum((int32)DatumGetInt64(newoff));
380380

381381
/* Connect to SPI manager */
382-
if ((ret=SPI_connect())<0)
383-
elog(ERROR,"ttdummy (%s): SPI_connect returned %d",relname,ret);
382+
SPI_connect();
384383

385384
/* Fetch tuple values and nulls */
386385
cvals= (Datum*)palloc(natts*sizeof(Datum));

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp