Skip to content

Commit 036166f

Browse files
committed
Document and use SPI_result_code_string()
A lot of semi-internal code just prints out numeric SPI error codes, which is not very helpful. We already have an API function to convert the codes to a string, so let's make more use of that. Reviewed-by: Michael Paquier <[email protected]>
1 parent 582bbcf commit 036166f

File tree

5 files changed

+63
-10
lines changed

5 files changed

+63
-10
lines changed

contrib/spi/refint.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ check_primary_key(PG_FUNCTION_ARGS)
182182
pplan = SPI_prepare(sql, nkeys, argtypes);
183183
if (pplan == NULL)
184184
/* internal error */
185-
elog(ERROR, "check_primary_key: SPI_prepare returned %d", SPI_result);
185+
elog(ERROR, "check_primary_key: SPI_prepare returned %s", SPI_result_code_string(SPI_result));
186186

187187
/*
188188
* Remember that SPI_prepare places plan in current memory context -
@@ -395,7 +395,7 @@ check_foreign_key(PG_FUNCTION_ARGS)
395395
/* this shouldn't happen! SPI_ERROR_NOOUTFUNC ? */
396396
if (oldval == NULL)
397397
/* internal error */
398-
elog(ERROR, "check_foreign_key: SPI_getvalue returned %d", SPI_result);
398+
elog(ERROR, "check_foreign_key: SPI_getvalue returned %s", SPI_result_code_string(SPI_result));
399399
newval = SPI_getvalue(newtuple, tupdesc, fnumber);
400400
if (newval == NULL || strcmp(oldval, newval) != 0)
401401
isequal = false;
@@ -529,7 +529,7 @@ check_foreign_key(PG_FUNCTION_ARGS)
529529
pplan = SPI_prepare(sql, nkeys, argtypes);
530530
if (pplan == NULL)
531531
/* internal error */
532-
elog(ERROR, "check_foreign_key: SPI_prepare returned %d", SPI_result);
532+
elog(ERROR, "check_foreign_key: SPI_prepare returned %s", SPI_result_code_string(SPI_result));
533533

534534
/*
535535
* Remember that SPI_prepare places plan in current memory context

contrib/spi/timetravel.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ timetravel(PG_FUNCTION_ARGS)
341341
/* Prepare plan for query */
342342
pplan = SPI_prepare(sql, natts, ctypes);
343343
if (pplan == NULL)
344-
elog(ERROR, "timetravel (%s): SPI_prepare returned %d", relname, SPI_result);
344+
elog(ERROR, "timetravel (%s): SPI_prepare returned %s", relname, SPI_result_code_string(SPI_result));
345345

346346
/*
347347
* Remember that SPI_prepare places plan in current memory context -

doc/src/sgml/spi.sgml

+53
Original file line numberDiff line numberDiff line change
@@ -3546,6 +3546,59 @@ char * SPI_getnspname(Relation <parameter>rel</parameter>)
35463546
</refsect1>
35473547
</refentry>
35483548

3549+
<refentry id="spi-spi-result-code-string">
3550+
<indexterm><primary>SPI_result_code_string</primary></indexterm>
3551+
3552+
<refmeta>
3553+
<refentrytitle>SPI_result_code_string</refentrytitle>
3554+
<manvolnum>3</manvolnum>
3555+
</refmeta>
3556+
3557+
<refnamediv>
3558+
<refname>SPI_result_code_string</refname>
3559+
<refpurpose>return error code as string</refpurpose>
3560+
</refnamediv>
3561+
3562+
<refsynopsisdiv>
3563+
<synopsis>
3564+
const char * SPI_result_code_string(int <parameter>code</parameter>);
3565+
</synopsis>
3566+
</refsynopsisdiv>
3567+
3568+
<refsect1>
3569+
<title>Description</title>
3570+
3571+
<para>
3572+
<function>SPI_result_code_string</function> returns a string representation
3573+
of the result code returned by various SPI functions or stored
3574+
in <varname>SPI_result</varname>.
3575+
</para>
3576+
</refsect1>
3577+
3578+
<refsect1>
3579+
<title>Arguments</title>
3580+
3581+
<variablelist>
3582+
<varlistentry>
3583+
<term><literal>int <parameter>code</parameter></literal></term>
3584+
<listitem>
3585+
<para>
3586+
result code
3587+
</para>
3588+
</listitem>
3589+
</varlistentry>
3590+
</variablelist>
3591+
</refsect1>
3592+
3593+
<refsect1>
3594+
<title>Return Value</title>
3595+
3596+
<para>
3597+
A string representation of the result code.
3598+
</para>
3599+
</refsect1>
3600+
</refentry>
3601+
35493602
</sect1>
35503603

35513604
<sect1 id="spi-memory">

src/backend/utils/adt/ri_triggers.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -2435,8 +2435,8 @@ RI_Initial_Check(Trigger *trigger, Relation fk_rel, Relation pk_rel)
24352435
qplan = SPI_prepare(querybuf.data, 0, NULL);
24362436

24372437
if (qplan == NULL)
2438-
elog(ERROR, "SPI_prepare returned %d for %s",
2439-
SPI_result, querybuf.data);
2438+
elog(ERROR, "SPI_prepare returned %s for %s",
2439+
SPI_result_code_string(SPI_result), querybuf.data);
24402440

24412441
/*
24422442
* Run the plan. For safety we force a current snapshot to be used. (In
@@ -2453,7 +2453,7 @@ RI_Initial_Check(Trigger *trigger, Relation fk_rel, Relation pk_rel)
24532453

24542454
/* Check result */
24552455
if (spi_result != SPI_OK_SELECT)
2456-
elog(ERROR, "SPI_execute_snapshot returned %d", spi_result);
2456+
elog(ERROR, "SPI_execute_snapshot returned %s", SPI_result_code_string(spi_result));
24572457

24582458
/* Did we find a tuple violating the constraint? */
24592459
if (SPI_processed > 0)
@@ -3016,7 +3016,7 @@ ri_PlanCheck(const char *querystr, int nargs, Oid *argtypes,
30163016
qplan = SPI_prepare(querystr, nargs, argtypes);
30173017

30183018
if (qplan == NULL)
3019-
elog(ERROR, "SPI_prepare returned %d for %s", SPI_result, querystr);
3019+
elog(ERROR, "SPI_prepare returned %s for %s", SPI_result_code_string(SPI_result), querystr);
30203020

30213021
/* Restore UID and security context */
30223022
SetUserIdAndSecContext(save_userid, save_sec_context);
@@ -3144,7 +3144,7 @@ ri_PerformCheck(const RI_ConstraintInfo *riinfo,
31443144

31453145
/* Check result */
31463146
if (spi_result < 0)
3147-
elog(ERROR, "SPI_execute_snapshot returned %d", spi_result);
3147+
elog(ERROR, "SPI_execute_snapshot returned %s", SPI_result_code_string(spi_result));
31483148

31493149
if (expect_OK >= 0 && spi_result != expect_OK)
31503150
ereport(ERROR,

src/test/regress/regress.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ ttdummy(PG_FUNCTION_ARGS)
612612
/* Prepare plan for query */
613613
pplan = SPI_prepare(query, natts, ctypes);
614614
if (pplan == NULL)
615-
elog(ERROR, "ttdummy (%s): SPI_prepare returned %d", relname, SPI_result);
615+
elog(ERROR, "ttdummy (%s): SPI_prepare returned %s", relname, SPI_result_code_string(SPI_result));
616616

617617
if (SPI_keepplan(pplan))
618618
elog(ERROR, "ttdummy (%s): SPI_keepplan failed", relname);

0 commit comments

Comments
 (0)