_RETURN now, since there's no need to keep 'em unique anymore.
report an error because the query cycled too many times:
-CREATE RULE "_RETemp" AS
+CREATE RULE "_RETURN" AS
ON SELECT TO emp
DO INSTEAD
SELECT * FROM toyemp;
-CREATE RULE "_RETtoyemp" AS
+CREATE RULE "_RETURN" AS
ON SELECT TO toyemp
DO INSTEAD
SELECT * FROM emp;
-
+
The Rule System
CREATE TABLE myview (same attribute list as for mytab);
-CREATE RULE "_RETmyview" AS ON SELECT TO myview DO INSTEAD
+CREATE RULE "_RETURN" AS ON SELECT TO myview DO INSTEAD
SELECT * FROM mytab;
range table and checks if there are rules in pg_rewrite
for any relation. When processing the range table entry for
shoelace (the only one up to now) it finds the
- rule _RETshoelace with the parse tree
+ _RETURN rule with the parse tree
SELECT s.sl_name, s.sl_avail,
Again it's an INSTEAD rule and the previous parse tree is trashed.
Note that this query still uses the view shoelace.
But the rule system isn't finished with this loop so it continues
- and applies the rule _RETshoelace on it and we get
+ and applies the _RETURN rule on it and we get
UPDATE shoelace_data SET
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/commands/tablecmds.c,v 1.4 2002/04/19 16:36:08 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/commands/tablecmds.c,v 1.5 2002/04/19 23:13:54 tgl Exp $
*
*-------------------------------------------------------------------------
*/
#include "parser/parse_expr.h"
#include "parser/parse_relation.h"
#include "parser/parse_type.h"
-#include "rewrite/rewriteDefine.h"
-#include "rewrite/rewriteSupport.h"
#include "utils/acl.h"
#include "utils/builtins.h"
#include "utils/fmgroids.h"
if (relkind != RELKIND_INDEX)
TypeRename(oldrelname, namespaceId, newrelname);
- /*
- * If it's a view, must also rename the associated ON SELECT rule.
- */
- if (relkind == RELKIND_VIEW)
- {
- char *oldrulename,
- *newrulename;
-
- oldrulename = MakeRetrieveViewRuleName(oldrelname);
- newrulename = MakeRetrieveViewRuleName(newrelname);
- RenameRewriteRule(relid, oldrulename, newrulename);
- }
-
/*
* Update rel name in any RI triggers associated with the relation.
*/
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
- * $Id: view.c,v 1.62 2002/04/15 05:22:03 tgl Exp $
+ * $Id: view.c,v 1.63 2002/04/19 23:13:54 tgl Exp $
*
*-------------------------------------------------------------------------
*/
FormViewRetrieveRule(const RangeVar *view, Query *viewParse)
{
RuleStmt *rule;
- char *rname;
/*
* Create a RuleStmt that corresponds to the suitable rewrite rule
* args for DefineQueryRewrite();
*/
- rname = MakeRetrieveViewRuleName(view->relname);
-
rule = makeNode(RuleStmt);
rule->relation = copyObject((RangeVar *) view);
- rule->rulename = pstrdup(rname);
+ rule->rulename = pstrdup(ViewSelectRuleName);
rule->whereClause = NULL;
rule->event = CMD_SELECT;
rule->instead = true;
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteDefine.c,v 1.67 2002/04/18 20:01:09 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteDefine.c,v 1.68 2002/04/19 23:13:54 tgl Exp $
*
*-------------------------------------------------------------------------
*/
{
List *tllist;
int i;
- char *expected_name;
/*
* So there cannot be INSTEAD NOTHING, ...
}
/*
- * ... and finally the rule must be named _RETviewname.
+ * ... and finally the rule must be named _RETURN.
*/
- expected_name = MakeRetrieveViewRuleName(event_obj->relname);
- if (strcmp(expected_name, stmt->rulename) != 0)
+ if (strcmp(stmt->rulename, ViewSelectRuleName) != 0)
{
- elog(ERROR, "view rule for \"%s\" must be named \"%s\"",
- event_obj->relname, expected_name);
+ /*
+ * In versions before 7.3, the expected name was _RETviewname.
+ * For backwards compatibility with old pg_dump output, accept
+ * that and silently change it to _RETURN. Since this is just
+ * a quick backwards-compatibility hack, limit the number of
+ * characters checked to a few less than NAMEDATALEN; this
+ * saves having to worry about where a multibyte character might
+ * have gotten truncated.
+ */
+ if (strncmp(stmt->rulename, "_RET", 4) != 0 ||
+ strncmp(stmt->rulename + 4, event_obj->relname,
+ NAMEDATALEN - 4 - 4) != 0)
+ elog(ERROR, "view rule for \"%s\" must be named \"%s\"",
+ event_obj->relname, ViewSelectRuleName);
+ stmt->rulename = pstrdup(ViewSelectRuleName);
}
- pfree(expected_name);
/*
* Are we converting a relation to a view?
/*
* Rename an existing rewrite rule.
*
- * There is not currently a user command to invoke this directly
- * (perhaps there should be). But we need it anyway to rename the
- * ON SELECT rule associated with a view, when the view is renamed.
+ * This is unused code at the moment.
*/
void
RenameRewriteRule(Oid owningRel, const char *oldName,
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteSupport.c,v 1.50 2002/04/18 20:01:09 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteSupport.c,v 1.51 2002/04/19 23:13:54 tgl Exp $
*
*-------------------------------------------------------------------------
*/
#include "rewrite/rewriteSupport.h"
#include "utils/syscache.h"
-#ifdef MULTIBYTE
-#include "mb/pg_wchar.h"
-#endif
-
/*
* Is there a rule by the given name?
0, 0);
}
-/*
- * makeViewRetrieveRuleName
- *
- * Given a view name, returns the name for the associated ON SELECT rule.
- *
- * XXX this is not the only place in the backend that knows about the _RET
- * name-forming convention.
- */
-char *
-MakeRetrieveViewRuleName(const char *viewName)
-{
- char *buf;
- int buflen,
- maxlen;
-
- buflen = strlen(viewName) + 5;
- buf = palloc(buflen);
- snprintf(buf, buflen, "_RET%s", viewName);
- /* clip to less than NAMEDATALEN bytes, if necessary */
-#ifdef MULTIBYTE
- maxlen = pg_mbcliplen(buf, strlen(buf), NAMEDATALEN - 1);
-#else
- maxlen = NAMEDATALEN - 1;
-#endif
- if (maxlen < buflen)
- buf[maxlen] = '\0';
-
- return buf;
-}
/*
* SetRelationRuleStatus
* back to source text
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/utils/adt/ruleutils.c,v 1.97 2002/04/18 20:01:09 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/utils/adt/ruleutils.c,v 1.98 2002/04/19 23:13:54 tgl Exp $
*
* This software is copyrighted by Jan Wieck - Hamburg.
*
StringInfoData buf;
int len;
char *viewname;
- char *name;
/*
* Connect to SPI manager
* Get the pg_rewrite tuple for the view's SELECT rule
*/
viewname = get_rel_name(viewoid);
- name = MakeRetrieveViewRuleName(viewname);
args[0] = ObjectIdGetDatum(viewoid);
- args[1] = PointerGetDatum(name);
+ args[1] = PointerGetDatum(ViewSelectRuleName);
nulls[0] = ' ';
nulls[1] = ' ';
spirc = SPI_execp(plan_getviewrule, args, nulls, 2);
VARATT_SIZEP(ruledef) = len;
memcpy(VARDATA(ruledef), buf.data, buf.len);
pfree(buf.data);
- pfree(name);
/*
* Disconnect from SPI manager
# Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
# Portions Copyright (c) 1994, Regents of the University of California
#
-# $Header: /cvsroot/pgsql/src/bin/initdb/Attic/initdb.sh,v 1.149 2002/04/18 20:01:10 tgl Exp $
+# $Header: /cvsroot/pgsql/src/bin/initdb/Attic/initdb.sh,v 1.150 2002/04/19 23:13:54 tgl Exp $
#
#-------------------------------------------------------------------------
pg_get_ruledef(R.oid) AS definition \
FROM (pg_rewrite R JOIN pg_class C ON (C.oid = R.ev_class)) \
LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace) \
- WHERE R.rulename !~ '^_RET';
+ WHERE R.rulename != '_RETURN';
CREATE VIEW pg_views AS \
SELECT \
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.249 2002/04/18 20:01:10 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.250 2002/04/19 23:13:54 tgl Exp $
*
*-------------------------------------------------------------------------
*/
"oid as view_oid"
" from pg_rewrite where"
" ev_class = '%s'::oid and"
- " rulename = ('_RET' || ",
+ " rulename = '_RETURN';",
tblinfo[i].oid);
- formatStringLiteral(query, tblinfo[i].relname, CONV_ALL);
- appendPQExpBuffer(query, ")::name;");
}
res2 = PQexec(g_conn, query->data);
continue;
/*
- * Get all rules defined for this table
+ * Get all rules defined for this table, except view select rules
*/
resetPQExpBuffer(query);
"FROM pg_rewrite, pg_class "
"WHERE pg_class.oid = '%s'::oid "
" AND pg_rewrite.ev_class = pg_class.oid "
- " AND pg_rewrite.rulename !~ '^_RET' "
+ " AND pg_rewrite.rulename != '_RETURN' "
"ORDER BY pg_rewrite.oid",
tblinfo[t].oid);
}
*
* Copyright 2000 by PostgreSQL Global Development Group
*
- * $Header: /cvsroot/pgsql/src/bin/psql/describe.c,v 1.49 2002/04/11 20:00:08 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/bin/psql/describe.c,v 1.50 2002/04/19 23:13:54 tgl Exp $
*/
#include "postgres_fe.h"
#include "describe.h"
/* Rule description (ignore rules for views) */
"UNION ALL\n"
" SELECT r.oid as oid, r.tableoid as tableoid,\n"
- " CAST(r.rulename AS text) as name, CAST('%s' AS text) as object\n"
+ " CAST(r.rulename AS text) as name, CAST('%s' AS text) as object\n"
" FROM pg_rewrite r\n"
- " WHERE r.rulename !~ '^_RET'\n"
+ " WHERE r.rulename != '_RETURN'\n"
/* Trigger description */
"UNION ALL\n"
sprintf(buf,
"SELECT r.rulename\n"
"FROM pg_rewrite r, pg_class c\n"
- "WHERE c.relname='%s' AND c.oid = r.ev_class\n"
- "AND r.rulename NOT LIKE '_RET%%'",
+ "WHERE c.relname = '%s' AND c.oid = r.ev_class\n"
+ "AND r.rulename != '_RETURN'",
name);
result = PSQLexec(buf);
if (!result)
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
- * $Id: catversion.h,v 1.119 2002/04/19 16:36:08 tgl Exp $
+ * $Id: catversion.h,v 1.120 2002/04/19 23:13:54 tgl Exp $
*
*-------------------------------------------------------------------------
*/
*/
/* yyyymmddN */
-#define CATALOG_VERSION_NO 200204182
+#define CATALOG_VERSION_NO 200204191
#endif
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
- * $Id: rewriteSupport.h,v 1.21 2002/04/18 20:01:11 tgl Exp $
+ * $Id: rewriteSupport.h,v 1.22 2002/04/19 23:13:54 tgl Exp $
*
*-------------------------------------------------------------------------
*/
#ifndef REWRITESUPPORT_H
#define REWRITESUPPORT_H
-extern bool IsDefinedRewriteRule(Oid owningRel, const char *ruleName);
+/* The ON SELECT rule of a view is always named this: */
+#define ViewSelectRuleName "_RETURN"
-extern char *MakeRetrieveViewRuleName(const char *view_name);
+extern bool IsDefinedRewriteRule(Oid owningRel, const char *ruleName);
extern void SetRelationRuleStatus(Oid relationId, bool relHasRules,
bool relIsBecomingView);
--------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
iexit | SELECT ih.name, ih.thepath, interpt_pp(ih.thepath, r.thepath) AS exit FROM ihighway ih, ramp r WHERE (ih.thepath ## r.thepath);
pg_indexes | SELECT c.relname AS tablename, i.relname AS indexname, pg_get_indexdef(x.indexrelid) AS indexdef FROM pg_index x, pg_class c, pg_class i WHERE ((((c.relkind = 'r'::"char") AND (i.relkind = 'i'::"char")) AND (c.oid = x.indrelid)) AND (i.oid = x.indexrelid));
- pg_rules | SELECT n.nspname AS schemaname, c.relname AS tablename, r.rulename, pg_get_ruledef(r.oid) AS definition FROM ((pg_rewrite r JOIN pg_class c ON ((c.oid = r.ev_class))) LEFT JOIN pg_namespace n ON ((n.oid = c.relnamespace))) WHERE (r.rulename !~ '^_RET'::text);
+ pg_rules | SELECT n.nspname AS schemaname, c.relname AS tablename, r.rulename, pg_get_ruledef(r.oid) AS definition FROM ((pg_rewrite r JOIN pg_class c ON ((c.oid = r.ev_class))) LEFT JOIN pg_namespace n ON ((n.oid = c.relnamespace))) WHERE (r.rulename <> '_RETURN'::name);
pg_stat_activity | SELECT d.oid AS datid, d.datname, pg_stat_get_backend_pid(s.backendid) AS procpid, pg_stat_get_backend_userid(s.backendid) AS usesysid, u.usename, pg_stat_get_backend_activity(s.backendid) AS current_query FROM pg_database d, (SELECT pg_stat_get_backend_idset() AS backendid) s, pg_shadow u WHERE ((pg_stat_get_backend_dbid(s.backendid) = d.oid) AND (pg_stat_get_backend_userid(s.backendid) = u.usesysid));
pg_stat_all_indexes | SELECT c.oid AS relid, i.oid AS indexrelid, c.relname, i.relname AS indexrelname, pg_stat_get_numscans(i.oid) AS idx_scan, pg_stat_get_tuples_returned(i.oid) AS idx_tup_read, pg_stat_get_tuples_fetched(i.oid) AS idx_tup_fetch FROM pg_class c, pg_class i, pg_index x WHERE (((c.relkind = 'r'::"char") AND (x.indrelid = c.oid)) AND (x.indexrelid = i.oid));
pg_stat_all_tables | SELECT c.oid AS relid, c.relname, pg_stat_get_numscans(c.oid) AS seq_scan, pg_stat_get_tuples_returned(c.oid) AS seq_tup_read, sum(pg_stat_get_numscans(i.indexrelid)) AS idx_scan, sum(pg_stat_get_tuples_fetched(i.indexrelid)) AS idx_tup_fetch, pg_stat_get_tuples_inserted(c.oid) AS n_tup_ins, pg_stat_get_tuples_updated(c.oid) AS n_tup_upd, pg_stat_get_tuples_deleted(c.oid) AS n_tup_del FROM (pg_class c LEFT JOIN pg_index i ON ((c.oid = i.indrelid))) WHERE (c.relkind = 'r'::"char") GROUP BY c.oid, c.relname;