Skip to content

Commit 1b5cffa

Browse files
committed
Make ALTER TABLE RENAME on a view rename the view's on-select rule too.
Needed to keep pg_dump from getting confused.
1 parent a0c449a commit 1b5cffa

File tree

8 files changed

+117
-45
lines changed

8 files changed

+117
-45
lines changed

src/backend/commands/rename.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/commands/Attic/rename.c,v 1.56 2001/03/22 03:59:23 momjian Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/commands/Attic/rename.c,v 1.57 2001/08/12 21:35:18 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -26,6 +26,8 @@
2626
#include "miscadmin.h"
2727
#include "storage/smgr.h"
2828
#include "optimizer/prep.h"
29+
#include "rewrite/rewriteDefine.h"
30+
#include "rewrite/rewriteSupport.h"
2931
#include "utils/acl.h"
3032
#include "utils/relcache.h"
3133
#include "utils/syscache.h"
@@ -265,4 +267,17 @@ renamerel(const char *oldrelname, const char *newrelname)
265267
*/
266268
if (relkind != RELKIND_INDEX)
267269
TypeRename(oldrelname, newrelname);
270+
271+
/*
272+
* If it's a view, must also rename the associated ON SELECT rule.
273+
*/
274+
if (relkind == RELKIND_VIEW)
275+
{
276+
char *oldrulename,
277+
*newrulename;
278+
279+
oldrulename = MakeRetrieveViewRuleName(oldrelname);
280+
newrulename = MakeRetrieveViewRuleName(newrelname);
281+
RenameRewriteRule(oldrulename, newrulename);
282+
}
268283
}

src/backend/commands/view.c

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@
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.55 2001/08/10 18:57:35 tgl Exp $
9+
* $Id: view.c,v 1.56 2001/08/12 21:35:18 tgl Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
13-
1413
#include "postgres.h"
1514

1615
#include "access/xact.h"
@@ -24,10 +23,8 @@
2423
#include "rewrite/rewriteDefine.h"
2524
#include "rewrite/rewriteManip.h"
2625
#include "rewrite/rewriteRemove.h"
26+
#include "rewrite/rewriteSupport.h"
2727

28-
#ifdef MULTIBYTE
29-
#include "mb/pg_wchar.h"
30-
#endif
3128

3229
/*---------------------------------------------------------------------
3330
* DefineVirtualRelation
@@ -100,35 +97,6 @@ DefineVirtualRelation(char *relname, List *tlist)
10097
DefineRelation(createStmt, RELKIND_VIEW);
10198
}
10299

103-
/*------------------------------------------------------------------
104-
* makeViewRetrieveRuleName
105-
*
106-
* Given a view name, returns the name for the 'on retrieve to "view"'
107-
* rule.
108-
*------------------------------------------------------------------
109-
*/
110-
char *
111-
MakeRetrieveViewRuleName(char *viewName)
112-
{
113-
char *buf;
114-
int buflen,
115-
maxlen;
116-
117-
buflen = strlen(viewName) + 5;
118-
buf = palloc(buflen);
119-
snprintf(buf, buflen, "_RET%s", viewName);
120-
/* clip to less than NAMEDATALEN bytes, if necessary */
121-
#ifdef MULTIBYTE
122-
maxlen = pg_mbcliplen(buf, strlen(buf), NAMEDATALEN - 1);
123-
#else
124-
maxlen = NAMEDATALEN - 1;
125-
#endif
126-
if (maxlen < buflen)
127-
buf[maxlen] = '\0';
128-
129-
return buf;
130-
}
131-
132100
static RuleStmt *
133101
FormViewRetrieveRule(char *viewName, Query *viewParse)
134102
{

src/backend/rewrite/rewriteDefine.c

Lines changed: 50 additions & 1 deletion
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.62 2001/05/03 21:16:48 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteDefine.c,v 1.63 2001/08/12 21:35:18 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -28,6 +28,7 @@
2828
#include "rewrite/rewriteSupport.h"
2929
#include "storage/smgr.h"
3030
#include "utils/builtins.h"
31+
#include "utils/syscache.h"
3132

3233

3334
static void setRuleCheckAsUser(Query *qry, Oid userid);
@@ -478,3 +479,51 @@ setRuleCheckAsUser_walker(Node *node, Oid *context)
478479
return expression_tree_walker(node, setRuleCheckAsUser_walker,
479480
(void *) context);
480481
}
482+
483+
484+
/*
485+
* Rename an existing rewrite rule.
486+
*
487+
* There is not currently a user command to invoke this directly
488+
* (perhaps there should be). But we need it anyway to rename the
489+
* ON SELECT rule associated with a view, when the view is renamed.
490+
*/
491+
void
492+
RenameRewriteRule(char *oldname, char *newname)
493+
{
494+
Relation pg_rewrite_desc;
495+
HeapTuple ruletup;
496+
497+
pg_rewrite_desc = heap_openr(RewriteRelationName, RowExclusiveLock);
498+
499+
ruletup = SearchSysCacheCopy(RULENAME,
500+
PointerGetDatum(oldname),
501+
0, 0, 0);
502+
if (!HeapTupleIsValid(ruletup))
503+
elog(ERROR, "RenameRewriteRule: rule \"%s\" does not exist", oldname);
504+
505+
/* should not already exist */
506+
if (IsDefinedRewriteRule(newname))
507+
elog(ERROR, "Attempt to rename rule \"%s\" failed: \"%s\" already exists",
508+
oldname, newname);
509+
510+
StrNCpy(NameStr(((Form_pg_rewrite) GETSTRUCT(ruletup))->rulename),
511+
newname, NAMEDATALEN);
512+
513+
simple_heap_update(pg_rewrite_desc, &ruletup->t_self, ruletup);
514+
515+
/* keep system catalog indices current */
516+
if (RelationGetForm(pg_rewrite_desc)->relhasindex)
517+
{
518+
Relation idescs[Num_pg_rewrite_indices];
519+
520+
CatalogOpenIndices(Num_pg_rewrite_indices, Name_pg_rewrite_indices,
521+
idescs);
522+
CatalogIndexInsert(idescs, Num_pg_rewrite_indices, pg_rewrite_desc,
523+
ruletup);
524+
CatalogCloseIndices(Num_pg_rewrite_indices, idescs);
525+
}
526+
527+
heap_freetuple(ruletup);
528+
heap_close(pg_rewrite_desc, RowExclusiveLock);
529+
}

src/backend/rewrite/rewriteSupport.c

Lines changed: 39 additions & 2 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.48 2001/03/22 03:59:44 momjian Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteSupport.c,v 1.49 2001/08/12 21:35:19 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -20,15 +20,52 @@
2020
#include "rewrite/rewriteSupport.h"
2121
#include "utils/syscache.h"
2222

23+
#ifdef MULTIBYTE
24+
#include "mb/pg_wchar.h"
25+
#endif
2326

27+
28+
/*
29+
* Is there a rule by the given name?
30+
*/
2431
bool
25-
IsDefinedRewriteRule(char *ruleName)
32+
IsDefinedRewriteRule(const char *ruleName)
2633
{
2734
return SearchSysCacheExists(RULENAME,
2835
PointerGetDatum(ruleName),
2936
0, 0, 0);
3037
}
3138

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

src/backend/utils/adt/ruleutils.c

Lines changed: 2 additions & 2 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.81 2001/07/31 17:56:31 tgl Exp $
6+
* $Header: /cvsroot/pgsql/src/backend/utils/adt/ruleutils.c,v 1.82 2001/08/12 21:35:19 tgl Exp $
77
*
88
* This software is copyrighted by Jan Wieck - Hamburg.
99
*
@@ -44,7 +44,6 @@
4444
#include "catalog/pg_index.h"
4545
#include "catalog/pg_operator.h"
4646
#include "catalog/pg_shadow.h"
47-
#include "commands/view.h"
4847
#include "executor/spi.h"
4948
#include "lib/stringinfo.h"
5049
#include "optimizer/clauses.h"
@@ -53,6 +52,7 @@
5352
#include "parser/parse_expr.h"
5453
#include "parser/parsetree.h"
5554
#include "rewrite/rewriteManip.h"
55+
#include "rewrite/rewriteSupport.h"
5656
#include "utils/lsyscache.h"
5757

5858

src/include/commands/view.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $Id: view.h,v 1.9 2001/01/24 19:43:23 momjian Exp $
10+
* $Id: view.h,v 1.10 2001/08/12 21:35:19 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -16,7 +16,6 @@
1616

1717
#include "nodes/parsenodes.h"
1818

19-
extern char *MakeRetrieveViewRuleName(char *view_name);
2019
extern void DefineView(char *view_name, Query *view_parse);
2120
extern void RemoveView(char *view_name);
2221

src/include/rewrite/rewriteDefine.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $Id: rewriteDefine.h,v 1.9 2001/01/24 19:43:27 momjian Exp $
10+
* $Id: rewriteDefine.h,v 1.10 2001/08/12 21:35:19 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -18,4 +18,6 @@
1818

1919
extern void DefineQueryRewrite(RuleStmt *args);
2020

21+
extern void RenameRewriteRule(char *oldname, char *newname);
22+
2123
#endif /* REWRITEDEFINE_H */

src/include/rewrite/rewriteSupport.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@
77
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $Id: rewriteSupport.h,v 1.16 2001/03/22 04:01:04 momjian Exp $
10+
* $Id: rewriteSupport.h,v 1.17 2001/08/12 21:35:19 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
1414
#ifndef REWRITESUPPORT_H
1515
#define REWRITESUPPORT_H
1616

17-
extern bool IsDefinedRewriteRule(char *ruleName);
17+
extern bool IsDefinedRewriteRule(const char *ruleName);
18+
19+
extern char *MakeRetrieveViewRuleName(const char *view_name);
1820

1921
extern void SetRelationRuleStatus(Oid relationId, bool relHasRules,
2022
bool relIsBecomingView);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy