behavior, and is so little used that no one has been interested in fixing it.
To ensure that possible uses are covered, remove the ALIAS declaration's
arbitrary restriction that only $n identifiers can be aliased.
(We could alternatively make RENAME act just like ALIAS, but per discussion
having two different ways to do the same thing is probably more confusing than
helpful.)
-<!-- $PostgreSQL: pgsql/doc/src/sgml/plpgsql.sgml,v 1.143 2009/09/29 20:05:29 tgl Exp $ -->
+<!-- $PostgreSQL: pgsql/doc/src/sgml/plpgsql.sgml,v 1.144 2009/11/05 16:58:36 tgl Exp $ -->
<chapter id="plpgsql">
<title><application>PL/pgSQL</application> - <acronym>SQL</acronym> Procedural Language</title>
and <type>anyenum</>. The actual
data types handled by a polymorphic function can vary from call to
call, as discussed in <xref linkend="extend-types-polymorphic">.
- An example is shown in <xref linkend="plpgsql-declaration-aliases">.
+ An example is shown in <xref linkend="plpgsql-declaration-parameters">.
</para>
<para>
<para>
Specific examples appear in
- <xref linkend="plpgsql-declaration-aliases"> and
+ <xref linkend="plpgsql-declaration-parameters"> and
<xref linkend="plpgsql-statements-returning">.
</para>
</sect2>
</programlisting>
</para>
- <sect2 id="plpgsql-declaration-aliases">
- <title>Aliases for Function Parameters</title>
+ <sect2 id="plpgsql-declaration-parameters">
+ <title>Declaring Function Parameters</title>
<para>
Parameters passed to functions are named with the identifiers
These two examples are not perfectly equivalent. In the first case,
<literal>subtotal</> could be referenced as
<literal>sales_tax.subtotal</>, but in the second case it could not.
- (Had we attached a label to the block, <literal>subtotal</> could
+ (Had we attached a label to the inner block, <literal>subtotal</> could
be qualified with that label, instead.)
</para>
</note>
</para>
</sect2>
+ <sect2 id="plpgsql-declaration-alias">
+ <title><literal>ALIAS</></title>
+
+<synopsis>
+<replaceable>newname</> ALIAS FOR <replaceable>oldname</>;
+</synopsis>
+
+ <para>
+ The <literal>ALIAS</> syntax is more general than is suggested in the
+ previous section: you can declare an alias for any variable, not just
+ function parameters. The main practical use for this is to assign
+ a different name for variables with predetermined names, such as
+ <varname>NEW</varname> or <varname>OLD</varname> within
+ a trigger procedure.
+ </para>
+
+ <para>
+ Examples:
+<programlisting>
+DECLARE
+ prior ALIAS FOR old;
+ updated ALIAS FOR new;
+</programlisting>
+ </para>
+
+ <para>
+ Since <literal>ALIAS</> creates two different ways to name the same
+ object, unrestricted use can be confusing. It's best to use it only
+ for the purpose of overriding predetermined names.
+ </para>
+ </sect2>
+
<sect2 id="plpgsql-declaration-type">
<title>Copying Types</title>
structure on-the-fly.
</para>
</sect2>
-
- <sect2 id="plpgsql-declaration-renaming-vars">
- <title><literal>RENAME</></title>
-
-<synopsis>
-RENAME <replaceable>oldname</replaceable> TO <replaceable>newname</replaceable>;
-</synopsis>
-
- <para>
- Using the <literal>RENAME</literal> declaration you can change the
- name of a variable, record or row. This is primarily useful if
- <varname>NEW</varname> or <varname>OLD</varname> should be
- referenced by another name inside a trigger procedure. See also
- <literal>ALIAS</literal>.
- </para>
-
- <para>
- Examples:
-<programlisting>
-RENAME id TO user_id;
-RENAME this_var TO that_var;
-</programlisting>
- </para>
-
- <note>
- <para>
- <literal>RENAME</literal> appears to be broken as of
- <productname>PostgreSQL</> 7.3. Fixing this is of low priority,
- since <literal>ALIAS</literal> covers most of the practical uses
- of <literal>RENAME</literal>.
- </para>
- </note>
- </sect2>
</sect1>
<sect1 id="plpgsql-expressions">
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/pl/plpgsql/src/gram.y,v 1.129 2009/11/04 22:26:07 tgl Exp $
+ * $PostgreSQL: pgsql/src/pl/plpgsql/src/gram.y,v 1.130 2009/11/05 16:58:36 tgl Exp $
*
*-------------------------------------------------------------------------
*/
%type <declhdr> decl_sect
%type <varname> decl_varname
-%type <str> decl_renname
%type <boolean> decl_const decl_notnull exit_type
%type <expr> decl_defval decl_cursor_query
%type <dtype> decl_datatype
%token K_PERFORM
%token K_ROW_COUNT
%token K_RAISE
-%token K_RENAME
%token K_RESULT_OID
%token K_RETURN
%token K_REVERSE
plpgsql_ns_additem($4->itemtype,
$4->itemno, $1.name);
}
- | K_RENAME decl_renname K_TO decl_renname ';'
- {
- plpgsql_ns_rename($2, $4);
- }
| decl_varname opt_scrollable K_CURSOR
{ plpgsql_ns_push($1.name); }
decl_cursor_args decl_is_for decl_cursor_query
char *name;
PLpgSQL_nsitem *nsi;
+ /* XXX should allow block-label-qualified names */
plpgsql_convert_ident($1, &name, 1);
- if (name[0] != '$')
- yyerror("only positional parameters can be aliased");
plpgsql_ns_setlocal(false);
{
plpgsql_error_lineno = plpgsql_scanner_lineno();
ereport(ERROR,
- (errcode(ERRCODE_UNDEFINED_PARAMETER),
- errmsg("function has no parameter \"%s\"",
+ (errcode(ERRCODE_UNDEFINED_OBJECT),
+ errmsg("variable \"%s\" does not exist",
name)));
}
}
;
-/* XXX this is broken because it doesn't allow for T_SCALAR,T_ROW,T_RECORD */
-decl_renname : T_WORD
- {
- char *name;
-
- plpgsql_convert_ident(yytext, &name, 1);
- /* the result must be palloc'd, see plpgsql_ns_rename */
- $$ = name;
- }
- ;
-
decl_const :
{ $$ = false; }
| K_CONSTANT
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/pl/plpgsql/src/pl_funcs.c,v 1.82 2009/11/04 22:26:07 tgl Exp $
+ * $PostgreSQL: pgsql/src/pl/plpgsql/src/pl_funcs.c,v 1.83 2009/11/05 16:58:36 tgl Exp $
*
*-------------------------------------------------------------------------
*/
}
-/* ----------
- * plpgsql_ns_rename Rename a namespace entry
- * ----------
- */
-void
-plpgsql_ns_rename(char *oldname, char *newname)
-{
- PLpgSQL_ns *ns;
- PLpgSQL_nsitem *newitem;
- int i;
-
- /*
- * Lookup name in the namestack
- */
- for (ns = ns_current; ns != NULL; ns = ns->upper)
- {
- for (i = 1; i < ns->items_used; i++)
- {
- if (strcmp(ns->items[i]->name, oldname) == 0)
- {
- newitem = palloc(sizeof(PLpgSQL_nsitem) + strlen(newname));
- newitem->itemtype = ns->items[i]->itemtype;
- newitem->itemno = ns->items[i]->itemno;
- strcpy(newitem->name, newname);
-
- pfree(oldname);
- pfree(newname);
-
- pfree(ns->items[i]);
- ns->items[i] = newitem;
- return;
- }
- }
- }
-
- ereport(ERROR,
- (errcode(ERRCODE_UNDEFINED_OBJECT),
- errmsg("variable \"%s\" does not exist in the current block",
- oldname)));
-}
-
-
/* ----------
* plpgsql_convert_ident
*
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/pl/plpgsql/src/plpgsql.h,v 1.118 2009/11/04 22:26:07 tgl Exp $
+ * $PostgreSQL: pgsql/src/pl/plpgsql/src/plpgsql.h,v 1.119 2009/11/05 16:58:36 tgl Exp $
*
*-------------------------------------------------------------------------
*/
{ /* Item in the compilers namestack */
int itemtype;
int itemno;
- char name[1];
+ char name[1]; /* actually, as long as needed */
} PLpgSQL_nsitem;
extern PLpgSQL_nsitem *plpgsql_ns_lookup(const char *name1, const char *name2,
const char *name3, int *names_used);
extern PLpgSQL_nsitem *plpgsql_ns_lookup_label(const char *name);
-extern void plpgsql_ns_rename(char *oldname, char *newname);
/* ----------
* Other functions in pl_funcs.c
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/pl/plpgsql/src/scan.l,v 1.72 2009/09/29 20:05:29 tgl Exp $
+ * $PostgreSQL: pgsql/src/pl/plpgsql/src/scan.l,v 1.73 2009/11/05 16:58:36 tgl Exp $
*
*-------------------------------------------------------------------------
*/
or { return K_OR; }
perform { return K_PERFORM; }
raise { return K_RAISE; }
-rename { return K_RENAME; }
result_oid { return K_RESULT_OID; }
return { return K_RETURN; }
reverse { return K_REVERSE; }
create function tg_pslot_biu() returns trigger as $proc$
declare
pfrec record;
- rename new to ps;
+ ps alias for new;
begin
select into pfrec * from PField where name = ps.pfname;
if not found then
create function tg_pslot_biu() returns trigger as $proc$
declare
pfrec record;
- rename new to ps;
+ ps alias for new;
begin
select into pfrec * from PField where name = ps.pfname;
if not found then