99 *
1010 *
1111 * IDENTIFICATION
12- * $PostgreSQL: pgsql/src/backend/utils/adt/ruleutils.c,v 1.297 2009/04/05 19:59:40 tgl Exp $
12+ * $PostgreSQL: pgsql/src/backend/utils/adt/ruleutils.c,v 1.298 2009/05/26 17:36:05 tgl Exp $
1313 *
1414 *-------------------------------------------------------------------------
1515 */
@@ -146,7 +146,7 @@ static char *pg_get_indexdef_worker(Oid indexrelid, int colno, bool showTblSpc,
146146 int prettyFlags );
147147static char * pg_get_constraintdef_worker (Oid constraintId , bool fullCommand ,
148148 int prettyFlags );
149- static char * pg_get_expr_worker (text * expr , Oid relid , char * relname ,
149+ static text * pg_get_expr_worker (text * expr , Oid relid , const char * relname ,
150150 int prettyFlags );
151151static int print_function_arguments (StringInfo buf , HeapTuple proctup ,
152152 bool print_table_args , bool print_defaults );
@@ -1198,7 +1198,8 @@ decompile_column_index_array(Datum column_index_array, Oid relId,
11981198 *
11991199 * Currently, the expression can only refer to a single relation, namely
12001200 * the one specified by the second parameter. This is sufficient for
1201- * partial indexes, column default expressions, etc.
1201+ * partial indexes, column default expressions, etc. We also support
1202+ * Var-free expressions, for which the OID can be InvalidOid.
12021203 * ----------
12031204 */
12041205Datum
@@ -1208,12 +1209,24 @@ pg_get_expr(PG_FUNCTION_ARGS)
12081209 Oid relid = PG_GETARG_OID (1 );
12091210 char * relname ;
12101211
1211- /* Get the name for the relation */
1212- relname = get_rel_name ( relid );
1213- if ( relname == NULL )
1214- PG_RETURN_NULL (); /* should we raise an error? */
1212+ if ( OidIsValid ( relid ))
1213+ {
1214+ /* Get the name for the relation */
1215+ relname = get_rel_name ( relid );
12151216
1216- PG_RETURN_TEXT_P (string_to_text (pg_get_expr_worker (expr , relid , relname , 0 )));
1217+ /*
1218+ * If the OID isn't actually valid, don't throw an error, just return
1219+ * NULL. This is a bit questionable, but it's what we've done
1220+ * historically, and it can help avoid unwanted failures when
1221+ * examining catalog entries for just-deleted relations.
1222+ */
1223+ if (relname == NULL )
1224+ PG_RETURN_NULL ();
1225+ }
1226+ else
1227+ relname = NULL ;
1228+
1229+ PG_RETURN_TEXT_P (pg_get_expr_worker (expr , relid , relname , 0 ));
12171230}
12181231
12191232Datum
@@ -1227,16 +1240,22 @@ pg_get_expr_ext(PG_FUNCTION_ARGS)
12271240
12281241 prettyFlags = pretty ? PRETTYFLAG_PAREN | PRETTYFLAG_INDENT : 0 ;
12291242
1230- /* Get the name for the relation */
1231- relname = get_rel_name (relid );
1232- if (relname == NULL )
1233- PG_RETURN_NULL (); /* should we raise an error? */
1243+ if (OidIsValid (relid ))
1244+ {
1245+ /* Get the name for the relation */
1246+ relname = get_rel_name (relid );
1247+ /* See notes above */
1248+ if (relname == NULL )
1249+ PG_RETURN_NULL ();
1250+ }
1251+ else
1252+ relname = NULL ;
12341253
1235- PG_RETURN_TEXT_P (string_to_text ( pg_get_expr_worker (expr , relid , relname , prettyFlags ) ));
1254+ PG_RETURN_TEXT_P (pg_get_expr_worker (expr , relid , relname , prettyFlags ));
12361255}
12371256
1238- static char *
1239- pg_get_expr_worker (text * expr , Oid relid , char * relname , int prettyFlags )
1257+ static text *
1258+ pg_get_expr_worker (text * expr , Oid relid , const char * relname , int prettyFlags )
12401259{
12411260 Node * node ;
12421261 List * context ;
@@ -1249,14 +1268,19 @@ pg_get_expr_worker(text *expr, Oid relid, char *relname, int prettyFlags)
12491268 /* Convert expression to node tree */
12501269 node = (Node * ) stringToNode (exprstr );
12511270
1271+ pfree (exprstr );
1272+
1273+ /* Prepare deparse context if needed */
1274+ if (OidIsValid (relid ))
1275+ context = deparse_context_for (relname , relid );
1276+ else
1277+ context = NIL ;
1278+
12521279 /* Deparse */
1253- context = deparse_context_for (relname , relid );
12541280 str = deparse_expression_pretty (node , context , false, false,
12551281 prettyFlags , 0 );
12561282
1257- pfree (exprstr );
1258-
1259- return str ;
1283+ return string_to_text (str );
12601284}
12611285
12621286
0 commit comments