Skip to content

Commit 6d6ca21

Browse files
committed
Change naming rule for ON SELECT rules of views: they're all just
_RETURN now, since there's no need to keep 'em unique anymore.
1 parent 2017371 commit 6d6ca21

File tree

13 files changed

+50
-97
lines changed

13 files changed

+50
-97
lines changed

doc/src/sgml/ref/create_rule.sgml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$Header: /cvsroot/pgsql/doc/src/sgml/ref/create_rule.sgml,v 1.34 2002/04/19 16:36:08 tgl Exp $
2+
$Header: /cvsroot/pgsql/doc/src/sgml/ref/create_rule.sgml,v 1.35 2002/04/19 23:13:53 tgl Exp $
33
PostgreSQL documentation
44
-->
55

@@ -268,12 +268,12 @@ CREATE
268268
report an error because the query cycled too many times:
269269

270270
<programlisting>
271-
CREATE RULE "_RETemp" AS
271+
CREATE RULE "_RETURN" AS
272272
ON SELECT TO emp
273273
DO INSTEAD
274274
SELECT * FROM toyemp;
275275

276-
CREATE RULE "_RETtoyemp" AS
276+
CREATE RULE "_RETURN" AS
277277
ON SELECT TO toyemp
278278
DO INSTEAD
279279
SELECT * FROM emp;

doc/src/sgml/rules.sgml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $Header: /cvsroot/pgsql/doc/src/sgml/rules.sgml,v 1.22 2002/03/22 19:20:26 petere Exp $ -->
1+
<!-- $Header: /cvsroot/pgsql/doc/src/sgml/rules.sgml,v 1.23 2002/04/19 23:13:53 tgl Exp $ -->
22

33
<Chapter Id="rules">
44
<Title>The Rule System</Title>
@@ -289,7 +289,7 @@ CREATE VIEW myview AS SELECT * FROM mytab;
289289

290290
<ProgramListing>
291291
CREATE TABLE myview (<Replaceable>same attribute list as for mytab</Replaceable>);
292-
CREATE RULE "_RETmyview" AS ON SELECT TO myview DO INSTEAD
292+
CREATE RULE "_RETURN" AS ON SELECT TO myview DO INSTEAD
293293
SELECT * FROM mytab;
294294
</ProgramListing>
295295

@@ -517,7 +517,7 @@ SELECT shoelace.sl_name, shoelace.sl_avail,
517517
range table and checks if there are rules in <Filename>pg_rewrite</Filename>
518518
for any relation. When processing the range table entry for
519519
<Filename>shoelace</Filename> (the only one up to now) it finds the
520-
rule <literal>_RETshoelace</literal> with the parse tree
520+
<literal>_RETURN</literal> rule with the parse tree
521521

522522
<ProgramListing>
523523
<emphasis>SELECT s.sl_name, s.sl_avail,
@@ -1494,7 +1494,7 @@ UPDATE shoelace_data SET
14941494
Again it's an INSTEAD rule and the previous parse tree is trashed.
14951495
Note that this query still uses the view <Filename>shoelace</Filename>.
14961496
But the rule system isn't finished with this loop so it continues
1497-
and applies the rule <literal>_RETshoelace</literal> on it and we get
1497+
and applies the <literal>_RETURN</literal> rule on it and we get
14981498

14991499
<ProgramListing>
15001500
UPDATE shoelace_data SET

src/backend/commands/tablecmds.c

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/commands/tablecmds.c,v 1.4 2002/04/19 16:36:08 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/commands/tablecmds.c,v 1.5 2002/04/19 23:13:54 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -40,8 +40,6 @@
4040
#include "parser/parse_expr.h"
4141
#include "parser/parse_relation.h"
4242
#include "parser/parse_type.h"
43-
#include "rewrite/rewriteDefine.h"
44-
#include "rewrite/rewriteSupport.h"
4543
#include "utils/acl.h"
4644
#include "utils/builtins.h"
4745
#include "utils/fmgroids.h"
@@ -2814,19 +2812,6 @@ renamerel(Oid relid, const char *newrelname)
28142812
if (relkind != RELKIND_INDEX)
28152813
TypeRename(oldrelname, namespaceId, newrelname);
28162814

2817-
/*
2818-
* If it's a view, must also rename the associated ON SELECT rule.
2819-
*/
2820-
if (relkind == RELKIND_VIEW)
2821-
{
2822-
char *oldrulename,
2823-
*newrulename;
2824-
2825-
oldrulename = MakeRetrieveViewRuleName(oldrelname);
2826-
newrulename = MakeRetrieveViewRuleName(newrelname);
2827-
RenameRewriteRule(relid, oldrulename, newrulename);
2828-
}
2829-
28302815
/*
28312816
* Update rel name in any RI triggers associated with the relation.
28322817
*/

src/backend/commands/view.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
9-
* $Id: view.c,v 1.62 2002/04/15 05:22:03 tgl Exp $
9+
* $Id: view.c,v 1.63 2002/04/19 23:13:54 tgl Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -99,17 +99,14 @@ static RuleStmt *
9999
FormViewRetrieveRule(const RangeVar *view, Query *viewParse)
100100
{
101101
RuleStmt *rule;
102-
char *rname;
103102

104103
/*
105104
* Create a RuleStmt that corresponds to the suitable rewrite rule
106105
* args for DefineQueryRewrite();
107106
*/
108-
rname = MakeRetrieveViewRuleName(view->relname);
109-
110107
rule = makeNode(RuleStmt);
111108
rule->relation = copyObject((RangeVar *) view);
112-
rule->rulename = pstrdup(rname);
109+
rule->rulename = pstrdup(ViewSelectRuleName);
113110
rule->whereClause = NULL;
114111
rule->event = CMD_SELECT;
115112
rule->instead = true;

src/backend/rewrite/rewriteDefine.c

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteDefine.c,v 1.67 2002/04/18 20:01:09 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteDefine.c,v 1.68 2002/04/19 23:13:54 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -176,7 +176,6 @@ DefineQueryRewrite(RuleStmt *stmt)
176176
{
177177
List *tllist;
178178
int i;
179-
char *expected_name;
180179

181180
/*
182181
* So there cannot be INSTEAD NOTHING, ...
@@ -265,15 +264,26 @@ DefineQueryRewrite(RuleStmt *stmt)
265264
}
266265

267266
/*
268-
* ... and finally the rule must be named _RETviewname.
267+
* ... and finally the rule must be named _RETURN.
269268
*/
270-
expected_name = MakeRetrieveViewRuleName(event_obj->relname);
271-
if (strcmp(expected_name, stmt->rulename) != 0)
269+
if (strcmp(stmt->rulename, ViewSelectRuleName) != 0)
272270
{
273-
elog(ERROR, "view rule for \"%s\" must be named \"%s\"",
274-
event_obj->relname, expected_name);
271+
/*
272+
* In versions before 7.3, the expected name was _RETviewname.
273+
* For backwards compatibility with old pg_dump output, accept
274+
* that and silently change it to _RETURN. Since this is just
275+
* a quick backwards-compatibility hack, limit the number of
276+
* characters checked to a few less than NAMEDATALEN; this
277+
* saves having to worry about where a multibyte character might
278+
* have gotten truncated.
279+
*/
280+
if (strncmp(stmt->rulename, "_RET", 4) != 0 ||
281+
strncmp(stmt->rulename + 4, event_obj->relname,
282+
NAMEDATALEN - 4 - 4) != 0)
283+
elog(ERROR, "view rule for \"%s\" must be named \"%s\"",
284+
event_obj->relname, ViewSelectRuleName);
285+
stmt->rulename = pstrdup(ViewSelectRuleName);
275286
}
276-
pfree(expected_name);
277287

278288
/*
279289
* Are we converting a relation to a view?
@@ -418,9 +428,7 @@ setRuleCheckAsUser_walker(Node *node, Oid *context)
418428
/*
419429
* Rename an existing rewrite rule.
420430
*
421-
* There is not currently a user command to invoke this directly
422-
* (perhaps there should be). But we need it anyway to rename the
423-
* ON SELECT rule associated with a view, when the view is renamed.
431+
* This is unused code at the moment.
424432
*/
425433
void
426434
RenameRewriteRule(Oid owningRel, const char *oldName,

src/backend/rewrite/rewriteSupport.c

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteSupport.c,v 1.50 2002/04/18 20:01:09 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteSupport.c,v 1.51 2002/04/19 23:13:54 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -20,10 +20,6 @@
2020
#include "rewrite/rewriteSupport.h"
2121
#include "utils/syscache.h"
2222

23-
#ifdef MULTIBYTE
24-
#include "mb/pg_wchar.h"
25-
#endif
26-
2723

2824
/*
2925
* Is there a rule by the given name?
@@ -37,35 +33,6 @@ IsDefinedRewriteRule(Oid owningRel, const char *ruleName)
3733
0, 0);
3834
}
3935

40-
/*
41-
* makeViewRetrieveRuleName
42-
*
43-
* Given a view name, returns the name for the associated ON SELECT rule.
44-
*
45-
* XXX this is not the only place in the backend that knows about the _RET
46-
* name-forming convention.
47-
*/
48-
char *
49-
MakeRetrieveViewRuleName(const char *viewName)
50-
{
51-
char *buf;
52-
int buflen,
53-
maxlen;
54-
55-
buflen = strlen(viewName) + 5;
56-
buf = palloc(buflen);
57-
snprintf(buf, buflen, "_RET%s", viewName);
58-
/* clip to less than NAMEDATALEN bytes, if necessary */
59-
#ifdef MULTIBYTE
60-
maxlen = pg_mbcliplen(buf, strlen(buf), NAMEDATALEN - 1);
61-
#else
62-
maxlen = NAMEDATALEN - 1;
63-
#endif
64-
if (maxlen < buflen)
65-
buf[maxlen] = '\0';
66-
67-
return buf;
68-
}
6936

7037
/*
7138
* SetRelationRuleStatus

src/backend/utils/adt/ruleutils.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* back to source text
44
*
55
* IDENTIFICATION
6-
* $Header: /cvsroot/pgsql/src/backend/utils/adt/ruleutils.c,v 1.97 2002/04/18 20:01:09 tgl Exp $
6+
* $Header: /cvsroot/pgsql/src/backend/utils/adt/ruleutils.c,v 1.98 2002/04/19 23:13:54 tgl Exp $
77
*
88
* This software is copyrighted by Jan Wieck - Hamburg.
99
*
@@ -283,7 +283,6 @@ pg_do_getviewdef(Oid viewoid)
283283
StringInfoData buf;
284284
int len;
285285
char *viewname;
286-
char *name;
287286

288287
/*
289288
* Connect to SPI manager
@@ -313,9 +312,8 @@ pg_do_getviewdef(Oid viewoid)
313312
* Get the pg_rewrite tuple for the view's SELECT rule
314313
*/
315314
viewname = get_rel_name(viewoid);
316-
name = MakeRetrieveViewRuleName(viewname);
317315
args[0] = ObjectIdGetDatum(viewoid);
318-
args[1] = PointerGetDatum(name);
316+
args[1] = PointerGetDatum(ViewSelectRuleName);
319317
nulls[0] = ' ';
320318
nulls[1] = ' ';
321319
spirc = SPI_execp(plan_getviewrule, args, nulls, 2);
@@ -338,7 +336,6 @@ pg_do_getviewdef(Oid viewoid)
338336
VARATT_SIZEP(ruledef) = len;
339337
memcpy(VARDATA(ruledef), buf.data, buf.len);
340338
pfree(buf.data);
341-
pfree(name);
342339

343340
/*
344341
* Disconnect from SPI manager

src/bin/initdb/initdb.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
# Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
2828
# Portions Copyright (c) 1994, Regents of the University of California
2929
#
30-
# $Header: /cvsroot/pgsql/src/bin/initdb/Attic/initdb.sh,v 1.149 2002/04/18 20:01:10 tgl Exp $
30+
# $Header: /cvsroot/pgsql/src/bin/initdb/Attic/initdb.sh,v 1.150 2002/04/19 23:13:54 tgl Exp $
3131
#
3232
#-------------------------------------------------------------------------
3333

@@ -695,7 +695,7 @@ CREATE VIEW pg_rules AS \
695695
pg_get_ruledef(R.oid) AS definition \
696696
FROM (pg_rewrite R JOIN pg_class C ON (C.oid = R.ev_class)) \
697697
LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace) \
698-
WHERE R.rulename !~ '^_RET';
698+
WHERE R.rulename != '_RETURN';
699699
700700
CREATE VIEW pg_views AS \
701701
SELECT \

src/bin/pg_dump/pg_dump.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
*
2323
*
2424
* IDENTIFICATION
25-
* $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.249 2002/04/18 20:01:10 tgl Exp $
25+
* $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.250 2002/04/19 23:13:54 tgl Exp $
2626
*
2727
*-------------------------------------------------------------------------
2828
*/
@@ -2291,10 +2291,8 @@ getTables(int *numTables, FuncInfo *finfo, int numFuncs, const char *tablename)
22912291
"oid as view_oid"
22922292
" from pg_rewrite where"
22932293
" ev_class = '%s'::oid and"
2294-
" rulename = ('_RET' || ",
2294+
" rulename = '_RETURN';",
22952295
tblinfo[i].oid);
2296-
formatStringLiteral(query, tblinfo[i].relname, CONV_ALL);
2297-
appendPQExpBuffer(query, ")::name;");
22982296
}
22992297

23002298
res2 = PQexec(g_conn, query->data);
@@ -5006,7 +5004,7 @@ dumpRules(Archive *fout, const char *tablename,
50065004
continue;
50075005

50085006
/*
5009-
* Get all rules defined for this table
5007+
* Get all rules defined for this table, except view select rules
50105008
*/
50115009
resetPQExpBuffer(query);
50125010

@@ -5036,7 +5034,7 @@ dumpRules(Archive *fout, const char *tablename,
50365034
"FROM pg_rewrite, pg_class "
50375035
"WHERE pg_class.oid = '%s'::oid "
50385036
" AND pg_rewrite.ev_class = pg_class.oid "
5039-
" AND pg_rewrite.rulename !~ '^_RET' "
5037+
" AND pg_rewrite.rulename != '_RETURN' "
50405038
"ORDER BY pg_rewrite.oid",
50415039
tblinfo[t].oid);
50425040
}

src/bin/psql/describe.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright 2000 by PostgreSQL Global Development Group
55
*
6-
* $Header: /cvsroot/pgsql/src/bin/psql/describe.c,v 1.49 2002/04/11 20:00:08 tgl Exp $
6+
* $Header: /cvsroot/pgsql/src/bin/psql/describe.c,v 1.50 2002/04/19 23:13:54 tgl Exp $
77
*/
88
#include "postgres_fe.h"
99
#include "describe.h"
@@ -385,9 +385,9 @@ objectDescription(const char *object)
385385
/* Rule description (ignore rules for views) */
386386
"UNION ALL\n"
387387
" SELECT r.oid as oid, r.tableoid as tableoid,\n"
388-
" CAST(r.rulename AS text) as name, CAST('%s' AS text) as object\n"
388+
" CAST(r.rulename AS text) as name, CAST('%s' AS text) as object\n"
389389
" FROM pg_rewrite r\n"
390-
" WHERE r.rulename !~ '^_RET'\n"
390+
" WHERE r.rulename != '_RETURN'\n"
391391

392392
/* Trigger description */
393393
"UNION ALL\n"
@@ -704,8 +704,8 @@ describeTableDetails(const char *name, bool desc)
704704
sprintf(buf,
705705
"SELECT r.rulename\n"
706706
"FROM pg_rewrite r, pg_class c\n"
707-
"WHERE c.relname='%s' AND c.oid = r.ev_class\n"
708-
"AND r.rulename NOT LIKE '_RET%%'",
707+
"WHERE c.relname = '%s' AND c.oid = r.ev_class\n"
708+
"AND r.rulename != '_RETURN'",
709709
name);
710710
result = PSQLexec(buf);
711711
if (!result)

src/include/catalog/catversion.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
3838
* Portions Copyright (c) 1994, Regents of the University of California
3939
*
40-
* $Id: catversion.h,v 1.119 2002/04/19 16:36:08 tgl Exp $
40+
* $Id: catversion.h,v 1.120 2002/04/19 23:13:54 tgl Exp $
4141
*
4242
*-------------------------------------------------------------------------
4343
*/
@@ -53,6 +53,6 @@
5353
*/
5454

5555
/* yyyymmddN */
56-
#define CATALOG_VERSION_NO 200204182
56+
#define CATALOG_VERSION_NO 200204191
5757

5858
#endif

0 commit comments

Comments
 (0)