Replace run-time error check with assertion

Started by Peter Eisentrautover 4 years ago2 messages
#1Peter Eisentraut
peter.eisentraut@enterprisedb.com
1 attachment(s)

In the attached patch, the error message was checking that the
structures returned from the parser matched expectations. That's
something we usually use assertions for, not a full user-facing error
message. So I replaced that with an assertion (hidden inside
lfirst_node()).

Attachments:

0001-Replace-run-time-error-check-with-assertion.patchtext/plain; charset=UTF-8; name=0001-Replace-run-time-error-check-with-assertion.patch; x-mac-creator=0; x-mac-type=0Download
From a30697758c475db2f4333cedbe6aad1e1acd0ee8 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter@eisentraut.org>
Date: Tue, 25 May 2021 11:27:04 +0200
Subject: [PATCH] Replace run-time error check with assertion

The error message was checking that the structures returned from the
parser matched expectations.  That's something we usually use
assertions for, not a full user-facing error message.
---
 src/backend/commands/statscmds.c | 21 +++++----------------
 1 file changed, 5 insertions(+), 16 deletions(-)

diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c
index acae19176c..b244a0fbd7 100644
--- a/src/backend/commands/statscmds.c
+++ b/src/backend/commands/statscmds.c
@@ -220,26 +220,14 @@ CreateStatistics(CreateStatsStmt *stmt)
 	 */
 	foreach(cell, stmt->exprs)
 	{
-		Node	   *expr = (Node *) lfirst(cell);
-		StatsElem  *selem;
-		HeapTuple	atttuple;
-		Form_pg_attribute attForm;
-		TypeCacheEntry *type;
-
-		/*
-		 * We should not get anything else than StatsElem, given the grammar.
-		 * But let's keep it as a safety.
-		 */
-		if (!IsA(expr, StatsElem))
-			ereport(ERROR,
-					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
-					 errmsg("only simple column references and expressions are allowed in CREATE STATISTICS")));
-
-		selem = (StatsElem *) expr;
+		StatsElem  *selem = lfirst_node(StatsElem, cell);
 
 		if (selem->name)		/* column reference */
 		{
 			char	   *attname;
+			HeapTuple	atttuple;
+			Form_pg_attribute attForm;
+			TypeCacheEntry *type;
 
 			attname = selem->name;
 
@@ -273,6 +261,7 @@ CreateStatistics(CreateStatsStmt *stmt)
 		{
 			Node	   *expr = selem->expr;
 			Oid			atttype;
+			TypeCacheEntry *type;
 
 			Assert(expr != NULL);
 
-- 
2.31.1

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Peter Eisentraut (#1)
Re: Replace run-time error check with assertion

Peter Eisentraut <peter.eisentraut@enterprisedb.com> writes:

In the attached patch, the error message was checking that the
structures returned from the parser matched expectations. That's
something we usually use assertions for, not a full user-facing error
message. So I replaced that with an assertion (hidden inside
lfirst_node()).

Works for me. It's certainly silly to use a translatable ereport
rather than elog for this.

Localizing those variables some more looks sane too.

regards, tom lane