Re: plpgsql: variables of domain of composite types are not correctly initialized - Mailing list pgsql-hackers

From Tom Lane
Subject Re: plpgsql: variables of domain of composite types are not correctly initialized
Date
Msg-id 13789.1770844226@sss.pgh.pa.us
Whole thread Raw
In response to plpgsql: variables of domain of composite types are not correctly initialized  (Pavel Stehule <pavel.stehule@gmail.com>)
Responses Re: plpgsql: variables of domain of composite types are not correctly initialized
List pgsql-hackers
Pavel Stehule <pavel.stehule@gmail.com> writes:
> I got a bug report for plpgsql_check related to domains of composite types.
> While I played with code, maybe I found a bug:

Indeed.  Looks like exec_stmt_return's special case for a simple
variable reference forgets to fill estate->rettype when the variable
is a null record.  This is an old bug, but I think we'd managed not
to notice because that value isn't consulted unless we have to cast
to a domain.

The behavior we want is what exec_eval_datum does, and after looking
at it for a minute I wondered why we don't just use exec_eval_datum
instead of duplicating logic.  The ROW case already does that, so
we can fix the bug with strictly less code.

            regards, tom lane

diff --git a/src/pl/plpgsql/src/pl_exec.c b/src/pl/plpgsql/src/pl_exec.c
index f80264e184e..723048ab833 100644
--- a/src/pl/plpgsql/src/pl_exec.c
+++ b/src/pl/plpgsql/src/pl_exec.c
@@ -3255,28 +3255,14 @@ exec_stmt_return(PLpgSQL_execstate *estate, PLpgSQL_stmt_return *stmt)
                 }
                 break;

-            case PLPGSQL_DTYPE_REC:
-                {
-                    PLpgSQL_rec *rec = (PLpgSQL_rec *) retvar;
-
-                    /* If record is empty, we return NULL not a row of nulls */
-                    if (rec->erh && !ExpandedRecordIsEmpty(rec->erh))
-                    {
-                        estate->retval = ExpandedRecordGetDatum(rec->erh);
-                        estate->retisnull = false;
-                        estate->rettype = rec->rectypeid;
-                    }
-                }
-                break;
-
             case PLPGSQL_DTYPE_ROW:
+            case PLPGSQL_DTYPE_REC:
                 {
-                    PLpgSQL_row *row = (PLpgSQL_row *) retvar;
+                    /* exec_eval_datum can handle these cases */
                     int32        rettypmod;

-                    /* We get here if there are multiple OUT parameters */
                     exec_eval_datum(estate,
-                                    (PLpgSQL_datum *) row,
+                                    retvar,
                                     &estate->rettype,
                                     &rettypmod,
                                     &estate->retval,

pgsql-hackers by date:

Previous
From: "Matheus Alcantara"
Date:
Subject: Re: Add CREATE SCHEMA ... LIKE support
Next
From: Paul A Jungwirth
Date:
Subject: Re: SQL:2011 Application Time Update & Delete