Fix hints on CREATE PROCEDURE errors

Started by Jeremy Evansover 7 years ago7 messageshackers
Jump to latest
#1Jeremy Evans
code@jeremyevans.net

When testing out CREATE PROCEDURE with 11 beta 2, I noticed, the hints
in the errors reference DROP FUNCTION, which doesn't work for
procedures. DROP ROUTINE works for both functions and procedures, so
this patch should work for both.

Please CC me when responding as I don't currently subscribe to the
list.

Thanks,
Jeremy

From 1e4880e77b2b11917021e9d46756264013bd87d5 Mon Sep 17 00:00:00 2001
From: Jeremy Evans <code@jeremyevans.net>
Date: Mon, 6 Aug 2018 11:01:00 -0700
Subject: [PATCH] Have hint use DROP ROUTINE instead of DROP FUNCTION

The current code's hint is misleading for procedures:

CREATE OR REPLACE PROCEDURE a(in int)
LANGUAGE SQL
AS $$
SELECT NULL;
$$;
# CREATE PROCEDURE

CREATE OR REPLACE PROCEDURE a(inout int)
LANGUAGE SQL
AS $$
SELECT NULL;
$$;
# ERROR: cannot change return type of existing function
# HINT: Use DROP FUNCTION a(integer) first.

DROP FUNCTION a(integer);
# ERROR: a(integer) is not a function

DROP ROUTINE a(integer);
# DROP ROUTINE

DROP ROUTINE should work for both functions and procedures.
---
src/backend/catalog/pg_proc.c | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/src/backend/catalog/pg_proc.c b/src/backend/catalog/pg_proc.c
index 9b4015d0d4..84ee8d686d 100644
--- a/src/backend/catalog/pg_proc.c
+++ b/src/backend/catalog/pg_proc.c
@@ -409,7 +409,7 @@ ProcedureCreate(const char *procedureName,
 			ereport(ERROR,
 					(errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
 					 errmsg("cannot change return type of existing function"),
-					 errhint("Use DROP FUNCTION %s first.",
+					 errhint("Use DROP ROUTINE %s first.",
 							 format_procedure(HeapTupleGetOid(oldtup)))));
 		/*
@@ -434,7 +434,7 @@ ProcedureCreate(const char *procedureName,
 						(errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
 						 errmsg("cannot change return type of existing function"),
 						 errdetail("Row type defined by OUT parameters is different."),
-						 errhint("Use DROP FUNCTION %s first.",
+						 errhint("Use DROP ROUTINE %s first.",
 								 format_procedure(HeapTupleGetOid(oldtup)))));
 		}
@@ -477,7 +477,7 @@ ProcedureCreate(const char *procedureName,
 							(errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
 							 errmsg("cannot change name of input parameter \"%s\"",
 									old_arg_names[j]),
-							 errhint("Use DROP FUNCTION %s first.",
+							 errhint("Use DROP ROUTINE %s first.",
 									 format_procedure(HeapTupleGetOid(oldtup)))));
 			}
 		}
@@ -501,7 +501,7 @@ ProcedureCreate(const char *procedureName,
 				ereport(ERROR,
 						(errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
 						 errmsg("cannot remove parameter defaults from existing function"),
-						 errhint("Use DROP FUNCTION %s first.",
+						 errhint("Use DROP ROUTINE %s first.",
 								 format_procedure(HeapTupleGetOid(oldtup)))));
 			proargdefaults = SysCacheGetAttr(PROCNAMEARGSNSP, oldtup,
@@ -527,7 +527,7 @@ ProcedureCreate(const char *procedureName,
 					ereport(ERROR,
 							(errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
 							 errmsg("cannot change data type of existing parameter default value"),
-							 errhint("Use DROP FUNCTION %s first.",
+							 errhint("Use DROP ROUTINE %s first.",
 									 format_procedure(HeapTupleGetOid(oldtup)))));
 				newlc = lnext(newlc);
 			}
-- 
2.17.1
#2Amit Langote
Langote_Amit_f8@lab.ntt.co.jp
In reply to: Jeremy Evans (#1)
Re: Fix hints on CREATE PROCEDURE errors

Hi.

On 2018/08/07 3:32, Jeremy Evans wrote:

When testing out CREATE PROCEDURE with 11 beta 2, I noticed, the hints
in the errors reference DROP FUNCTION, which doesn't work for
procedures.

Good catch.

DROP ROUTINE works for both functions and procedures, so
this patch should work for both.

Not sure if we should fix it the way your patch does, but it seems you
forgot to include changes to the expected output of some of the regression
tests affected by this patch. Attached updated patch includes those.

Thanks,
Amit

Attachments:

drop-routine-in-HINT.patchtext/plain; charset=UTF-8; name=drop-routine-in-HINT.patchDownload+10-10
#3Peter Eisentraut
peter_e@gmx.net
In reply to: Jeremy Evans (#1)
Re: Fix hints on CREATE PROCEDURE errors

On 06/08/2018 20:32, Jeremy Evans wrote:

The current code's hint is misleading for procedures:

CREATE OR REPLACE PROCEDURE a(in int)
LANGUAGE SQL
AS $$
SELECT NULL;
$$;
# CREATE PROCEDURE

CREATE OR REPLACE PROCEDURE a(inout int)
LANGUAGE SQL
AS $$
SELECT NULL;
$$;
# ERROR: cannot change return type of existing function
# HINT: Use DROP FUNCTION a(integer) first.

Yes, the hint should be changed. But I also think the error message
should be changed to be more appropriate to the procedure situation
(where is the return type?). Attached patch does both. Unlike your
patch, I kept the "DROP FUNCTION" message for the function case. It
might be too confusing otherwise. Thoughts?

--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

Attachments:

0001-Improve-error-messages-for-CREATE-OR-REPLACE-PROCEDU.patchtext/plain; charset=UTF-8; name=0001-Improve-error-messages-for-CREATE-OR-REPLACE-PROCEDU.patch; x-mac-creator=0; x-mac-type=0Download+20-7
#4Tom Lane
tgl@sss.pgh.pa.us
In reply to: Peter Eisentraut (#3)
Re: Fix hints on CREATE PROCEDURE errors

Peter Eisentraut <peter.eisentraut@2ndquadrant.com> writes:

Yes, the hint should be changed. But I also think the error message
should be changed to be more appropriate to the procedure situation
(where is the return type?). Attached patch does both. Unlike your
patch, I kept the "DROP FUNCTION" message for the function case. It
might be too confusing otherwise. Thoughts?

I'm not a translator, but if I were, stuff like "Use DROP %s %s first."
would probably confuse me. IMO it's too close to assembling a message
out of parts, even if it's true that neither %s needs translation.
I think you'd be better off with

isprocedure ? errhint("Use DROP PROCEDURE %s first.", ...)
: errhint("Use DROP FUNCTION %s first.", ...)

Or if that seems too carpal-tunnel-inducing, maybe a workable compromise
is

dropcmd = (prokind == PROKIND_PROCEDURE ? "DROP PROCEDURE" : "DROP FUNCTION");

/* translator: first %s is DROP FUNCTION or DROP PROCEDURE */
errhint("Use %s %s first.", dropcmd, ...)

Looks reasonable other than that quibble.

regards, tom lane

#5Jonathan S. Katz
jonathan.katz@excoventures.com
In reply to: Tom Lane (#4)
Re: Fix hints on CREATE PROCEDURE errors

On Aug 8, 2018, at 3:18 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:

Peter Eisentraut <peter.eisentraut@2ndquadrant.com> writes:

Yes, the hint should be changed. But I also think the error message
should be changed to be more appropriate to the procedure situation
(where is the return type?). Attached patch does both. Unlike your
patch, I kept the "DROP FUNCTION" message for the function case. It
might be too confusing otherwise. Thoughts?

I'm not a translator, but if I were, stuff like "Use DROP %s %s first."
would probably confuse me. IMO it's too close to assembling a message
out of parts, even if it's true that neither %s needs translation.
I think you'd be better off with

isprocedure ? errhint("Use DROP PROCEDURE %s first.", ...)
: errhint("Use DROP FUNCTION %s first.", ...)

Or if that seems too carpal-tunnel-inducing, maybe a workable compromise
is

dropcmd = (prokind == PROKIND_PROCEDURE ? "DROP PROCEDURE" : "DROP FUNCTION");

/* translator: first %s is DROP FUNCTION or DROP PROCEDURE */
errhint("Use %s %s first.", dropcmd, ...)

Looks reasonable other than that quibble.

To help move this along, I went ahead and applied Tom’s first suggestion
to the patch. I tested the various scenarios and it seemed to work.

Jonathan

Attachments:

0001-Improve-error-messages-for-CREATE-OR-REPLACE-PROCEDU-v2.patchapplication/octet-stream; name=0001-Improve-error-messages-for-CREATE-OR-REPLACE-PROCEDU-v2.patch; x-unix-mode=0644Download+31-20
#6Peter Eisentraut
peter_e@gmx.net
In reply to: Jonathan S. Katz (#5)
Re: Fix hints on CREATE PROCEDURE errors

This has been committed.

--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

#7Jonathan S. Katz
jonathan.katz@excoventures.com
In reply to: Peter Eisentraut (#6)
Re: Fix hints on CREATE PROCEDURE errors

On Aug 18, 2018, at 4:22 PM, Peter Eisentraut <peter.eisentraut@2ndquadrant.com> wrote:

This has been committed.

Thanks - I’ve moved it off of the open items list.

Jonathan