Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit48938ab

Browse files
committed
Allow the second argument of pg_get_expr() to be just zero when deparsing
an expression that's not supposed to contain variables. Per discussionwith Gevik Babakhani, this eliminates the need for an ugly kluge (namely,specifying some unrelated relation name). Remove one such kluge frompg_dump.
1 parent99bf328 commit48938ab

File tree

3 files changed

+50
-23
lines changed

3 files changed

+50
-23
lines changed

‎doc/src/sgml/func.sgml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.480 2009/05/18 08:59:29 petere Exp $ -->
1+
<!-- $PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.481 2009/05/26 17:36:05 tgl Exp $ -->
22

33
<chapter id="functions">
44
<title>Functions and Operators</title>
@@ -12367,7 +12367,9 @@ SELECT pg_type_is_visible('myschema.widget'::regtype);
1236712367
is a decompiled reconstruction, not the original text of the command.)
1236812368
<function>pg_get_expr</function> decompiles the internal form of an
1236912369
individual expression, such as the default value for a column. It can be
12370-
useful when examining the contents of system catalogs.
12370+
useful when examining the contents of system catalogs. If the expression
12371+
might contain Vars, specify the OID of the relation they refer to as the
12372+
second parameter; if no Vars are expected, zero is sufficient.
1237112373
<function>pg_get_viewdef</function> reconstructs the <command>SELECT</>
1237212374
query that defines a view. Most of these functions come in two variants,
1237312375
one of which can optionally <quote>pretty-print</> the result. The

‎src/backend/utils/adt/ruleutils.c

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
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
intprettyFlags);
147147
staticchar*pg_get_constraintdef_worker(OidconstraintId,boolfullCommand,
148148
intprettyFlags);
149-
staticchar*pg_get_expr_worker(text*expr,Oidrelid,char*relname,
149+
statictext*pg_get_expr_worker(text*expr,Oidrelid,constchar*relname,
150150
intprettyFlags);
151151
staticintprint_function_arguments(StringInfobuf,HeapTupleproctup,
152152
boolprint_table_args,boolprint_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
*/
12041205
Datum
@@ -1208,12 +1209,24 @@ pg_get_expr(PG_FUNCTION_ARGS)
12081209
Oidrelid=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

12191232
Datum
@@ -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-
staticchar*
1239-
pg_get_expr_worker(text*expr,Oidrelid,char*relname,intprettyFlags)
1257+
statictext*
1258+
pg_get_expr_worker(text*expr,Oidrelid,constchar*relname,intprettyFlags)
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-
returnstr;
1283+
returnstring_to_text(str);
12601284
}
12611285

12621286

‎src/bin/pg_dump/pg_dump.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*by PostgreSQL
1313
*
1414
* IDENTIFICATION
15-
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_dump.c,v 1.536 2009/05/21 01:08:43 petere Exp $
15+
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_dump.c,v 1.537 2009/05/26 17:36:05 tgl Exp $
1616
*
1717
*-------------------------------------------------------------------------
1818
*/
@@ -6186,13 +6186,14 @@ dumpBaseType(Archive *fout, TypeInfo *tinfo)
61866186
"typanalyze::pg_catalog.oid AS typanalyzeoid, "
61876187
"typcategory, typispreferred, "
61886188
"typdelim, typbyval, typalign, typstorage, "
6189-
"pg_catalog.pg_get_expr(typdefaultbin,'pg_catalog.pg_type'::pg_catalog.regclass) AS typdefaultbin, typdefault "
6189+
"pg_catalog.pg_get_expr(typdefaultbin,0) AS typdefaultbin, typdefault "
61906190
"FROM pg_catalog.pg_type "
61916191
"WHERE oid = '%u'::pg_catalog.oid",
61926192
tinfo->dobj.catId.oid);
61936193
}
61946194
elseif (fout->remoteVersion >=80300)
61956195
{
6196+
/* Before 8.4, pg_get_expr does not allow 0 for its second arg */
61966197
appendPQExpBuffer(query,"SELECT typlen, "
61976198
"typinput, typoutput, typreceive, typsend, "
61986199
"typmodin, typmodout, typanalyze, "

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp