Thread: [Fwd: Make psql use all pretty print options]
Resubmission of patch (for 7.4). I fixed the problems I was having what I chatted to you Bruce, I've tested it well and it shouldn't be a problem to apply for 7.4. It looks really nice with the pretty print stuff! Chris -------- Original Message -------- Subject: [PATCHES] Make psql use all pretty print options Date: Mon, 29 Sep 2003 12:31:18 +0800 (WST) From: Christopher Kings-Lynne <chriskl@familyhealth.com.au> To: pgsql-patches@postgresql.org Hi, This patch finishes off the work that I did with making view definitions use pretty printing. It does: * Pretty check constraints * Pretty index predicates * Pretty rule definitions * Uppercases PRIMARY KEY and UNIQUE to be consistent with CHECK and FOREIGN KEY * View rules are improved to match table rules: View "public.v" Column | Type | Modifiers ----------+---------+----------- ?column? | integer | View definition: SELECT 1; Rules: r1 AS ON INSERT TO v DO INSTEAD NOTHING r2 AS ON INSERT TO v DO INSTEAD NOTHING Chris Index: describe.c =================================================================== RCS file: /projects/cvsroot/pgsql-server/src/bin/psql/describe.c,v retrieving revision 1.85 diff -c -r1.85 describe.c *** describe.c 7 Sep 2003 03:43:53 -0000 1.85 --- describe.c 29 Sep 2003 04:24:56 -0000 *************** *** 857,863 **** printfPQExpBuffer(&buf, "SELECT i.indisunique, i.indisprimary, a.amname, c2.relname,\n" ! " pg_catalog.pg_get_expr(i.indpred, i.indrelid)\n" "FROM pg_catalog.pg_index i, pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_ama\n" "WHERE i.indexrelid = c.oid AND c.oid = '%s' AND c.relam = a.oid\n" "AND i.indrelid = c2.oid", --- 857,863 ---- printfPQExpBuffer(&buf, "SELECT i.indisunique, i.indisprimary, a.amname, c2.relname,\n" ! " pg_catalog.pg_get_expr(i.indpred, i.indrelid, true)\n" "FROM pg_catalog.pg_index i, pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_ama\n" "WHERE i.indexrelid = c.oid AND c.oid = '%s' AND c.relam = a.oid\n" "AND i.indrelid = c2.oid", *************** *** 880,888 **** char *indpred = PQgetvalue(result, 0, 4); if (strcmp(indisprimary, "t") == 0) ! printfPQExpBuffer(&tmpbuf, _("primary key, ")); else if (strcmp(indisunique, "t") == 0) ! printfPQExpBuffer(&tmpbuf, _("unique, ")); else resetPQExpBuffer(&tmpbuf); appendPQExpBuffer(&tmpbuf, "%s, ", indamname); --- 880,888 ---- char *indpred = PQgetvalue(result, 0, 4); if (strcmp(indisprimary, "t") == 0) ! printfPQExpBuffer(&tmpbuf, _("PRIMARY KEY, ")); else if (strcmp(indisunique, "t") == 0) ! printfPQExpBuffer(&tmpbuf, _("UNIQUE, ")); else resetPQExpBuffer(&tmpbuf); appendPQExpBuffer(&tmpbuf, "%s, ", indamname); *************** *** 892,898 **** schemaname, indtable); if (strlen(indpred)) ! appendPQExpBuffer(&tmpbuf, ", predicate %s", indpred); footers = xmalloczero(2 * sizeof(*footers)); footers[0] = xstrdup(tmpbuf.data); --- 892,898 ---- schemaname, indtable); if (strlen(indpred)) ! appendPQExpBuffer(&tmpbuf, ", predicate (%s)", indpred); footers = xmalloczero(2 * sizeof(*footers)); footers[0] = xstrdup(tmpbuf.data); *************** *** 911,917 **** if (tableinfo.hasrules) { printfPQExpBuffer(&buf, ! "SELECT r.rulename\n" "FROM pg_catalog.pg_rewrite r\n" "WHERE r.ev_class = '%s' AND r.rulename != '_RETURN'", oid); --- 911,917 ---- if (tableinfo.hasrules) { printfPQExpBuffer(&buf, ! "SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid, true))\n" "FROM pg_catalog.pg_rewrite r\n" "WHERE r.ev_class = '%s' AND r.rulename != '_RETURN'", oid); *************** *** 923,949 **** } /* Footer information about a view */ ! footers = xmalloczero((rule_count + 2) * sizeof(*footers)); footers[count_footers] = xmalloc(64 + strlen(view_def)); snprintf(footers[count_footers], 64 + strlen(view_def), _("View definition:\n%s"), view_def); count_footers++; /* print rules */ ! for (i = 0; i < rule_count; i++) { ! char *s = _("Rules"); ! if (i == 0) ! printfPQExpBuffer(&buf, "%s: %s", s, PQgetvalue(result, i, 0)); ! else ! printfPQExpBuffer(&buf, "%*s %s", (int) strlen(s), "", PQgetvalue(result, i, 0)); ! if (i < rule_count - 1) ! appendPQExpBuffer(&buf, ","); ! footers[count_footers++] = xstrdup(buf.data); } - PQclear(result); footers[count_footers] = NULL; --- 923,953 ---- } /* Footer information about a view */ ! footers = xmalloczero((rule_count + 3) * sizeof(*footers)); footers[count_footers] = xmalloc(64 + strlen(view_def)); snprintf(footers[count_footers], 64 + strlen(view_def), _("View definition:\n%s"), view_def); count_footers++; /* print rules */ ! if (rule_count > 0) { ! printfPQExpBuffer(&buf, _("Rules:")); ! footers[count_footers++] = xstrdup(buf.data); ! for (i = 0; i < rule_count; i++) ! { ! const char *ruledef; ! /* Everything after "CREATE RULE" is echoed verbatim */ ! ruledef = PQgetvalue(result, i, 1); ! ruledef += 12; ! printfPQExpBuffer(&buf, " %s", ruledef); ! ! footers[count_footers++] = xstrdup(buf.data); ! } ! PQclear(result); } footers[count_footers] = NULL; *************** *** 970,976 **** { printfPQExpBuffer(&buf, "SELECT c2.relname, i.indisprimary, i.indisunique, " ! "pg_catalog.pg_get_indexdef(i.indexrelid)\n" "FROM pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_index i\n" "WHERE c.oid = '%s' AND c.oid = i.indrelid AND i.indexrelid = c2.oid\n" "ORDER BY i.indisprimary DESC, i.indisunique DESC, c2.relname", --- 974,980 ---- { printfPQExpBuffer(&buf, "SELECT c2.relname, i.indisprimary, i.indisunique, " ! "pg_catalog.pg_get_indexdef(i.indexrelid, 0, true)\n" "FROM pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_index i\n" "WHERE c.oid = '%s' AND c.oid = i.indrelid AND i.indexrelid = c2.oid\n" "ORDER BY i.indisprimary DESC, i.indisunique DESC, c2.relname", *************** *** 986,992 **** if (tableinfo.checks) { printfPQExpBuffer(&buf, ! "SELECT consrc, conname\n" "FROM pg_catalog.pg_constraint r\n" "WHERE r.conrelid = '%s' AND r.contype = 'c'", oid); --- 990,996 ---- if (tableinfo.checks) { printfPQExpBuffer(&buf, ! "SELECT pg_catalog.pg_get_constraintdef(oid, true) AS consrc, conname\n" "FROM pg_catalog.pg_constraint r\n" "WHERE r.conrelid = '%s' AND r.contype = 'c'", oid); *************** *** 1004,1010 **** if (tableinfo.hasrules) { printfPQExpBuffer(&buf, ! "SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid))\n" "FROM pg_catalog.pg_rewrite r\n" "WHERE r.ev_class = '%s'", oid); --- 1008,1014 ---- if (tableinfo.hasrules) { printfPQExpBuffer(&buf, ! "SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid, true))\n" "FROM pg_catalog.pg_rewrite r\n" "WHERE r.ev_class = '%s'", oid); *************** *** 1049,1055 **** { printfPQExpBuffer(&buf, "SELECT conname,\n" ! " pg_catalog.pg_get_constraintdef(oid) as condef\n" "FROM pg_catalog.pg_constraint r\n" "WHERE r.conrelid = '%s' AND r.contype = 'f'", oid); --- 1053,1059 ---- { printfPQExpBuffer(&buf, "SELECT conname,\n" ! " pg_catalog.pg_get_constraintdef(oid, true) as condef\n" "FROM pg_catalog.pg_constraint r\n" "WHERE r.conrelid = '%s' AND r.contype = 'f'", oid); *************** *** 1095,1103 **** /* Label as primary key or unique (but not both) */ appendPQExpBuffer(&buf, strcmp(PQgetvalue(result1, i, 1), "t") == 0 ! ? _(" primary key,") : (strcmp(PQgetvalue(result1, i, 2), "t") == 0 ! ? _(" unique,") : "")); /* Everything after "USING" is echoed verbatim */ --- 1099,1107 ---- /* Label as primary key or unique (but not both) */ appendPQExpBuffer(&buf, strcmp(PQgetvalue(result1, i, 1), "t") == 0 ! ? _(" PRIMARY KEY,") : (strcmp(PQgetvalue(result1, i, 2), "t") == 0 ! ? _(" UNIQUE,") : "")); /* Everything after "USING" is echoed verbatim */ *************** *** 1119,1125 **** footers[count_footers++] = xstrdup(buf.data); for (i = 0; i < check_count; i++) { ! printfPQExpBuffer(&buf, _(" \"%s\" CHECK %s"), PQgetvalue(result2, i, 1), PQgetvalue(result2, i, 0)); --- 1123,1129 ---- footers[count_footers++] = xstrdup(buf.data); for (i = 0; i < check_count; i++) { ! printfPQExpBuffer(&buf, _(" \"%s\" %s"), PQgetvalue(result2, i, 1), PQgetvalue(result2, i, 0));
Uh, I can't apply this to 7.4 unless you can get others to agree it should be added. Sorry. I am including the patch in this email in case people want to review it. --------------------------------------------------------------------------- Christopher Kings-Lynne wrote: > Resubmission of patch (for 7.4). > > I fixed the problems I was having what I chatted to you Bruce, I've > tested it well and it shouldn't be a problem to apply for 7.4. It looks > really nice with the pretty print stuff! > > Chris > > -------- Original Message -------- > Subject: [PATCHES] Make psql use all pretty print options > Date: Mon, 29 Sep 2003 12:31:18 +0800 (WST) > From: Christopher Kings-Lynne <chriskl@familyhealth.com.au> > To: pgsql-patches@postgresql.org > > Hi, > > This patch finishes off the work that I did with making view > definitions use pretty printing. > > It does: > > * Pretty check constraints > * Pretty index predicates > * Pretty rule definitions > * Uppercases PRIMARY KEY and UNIQUE to be consistent with CHECK and > FOREIGN KEY > * View rules are improved to match table rules: > > View "public.v" > Column | Type | Modifiers > ----------+---------+----------- > ?column? | integer | > View definition: > SELECT 1; > Rules: > r1 AS > ON INSERT TO v DO INSTEAD NOTHING > r2 AS > ON INSERT TO v DO INSTEAD NOTHING > > Chris > > > > Index: describe.c > =================================================================== > RCS file: /projects/cvsroot/pgsql-server/src/bin/psql/describe.c,v > retrieving revision 1.85 > diff -c -r1.85 describe.c > *** describe.c 7 Sep 2003 03:43:53 -0000 1.85 > --- describe.c 29 Sep 2003 04:24:56 -0000 > *************** > *** 857,863 **** > > printfPQExpBuffer(&buf, > "SELECT i.indisunique, i.indisprimary, a.amname, c2.relname,\n" > ! " pg_catalog.pg_get_expr(i.indpred, i.indrelid)\n" > "FROM pg_catalog.pg_index i, pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_ama\n" > "WHERE i.indexrelid = c.oid AND c.oid = '%s' AND c.relam = a.oid\n" > "AND i.indrelid = c2.oid", > --- 857,863 ---- > > printfPQExpBuffer(&buf, > "SELECT i.indisunique, i.indisprimary, a.amname, c2.relname,\n" > ! " pg_catalog.pg_get_expr(i.indpred, i.indrelid, true)\n" > "FROM pg_catalog.pg_index i, pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_ama\n" > "WHERE i.indexrelid = c.oid AND c.oid = '%s' AND c.relam = a.oid\n" > "AND i.indrelid = c2.oid", > *************** > *** 880,888 **** > char *indpred = PQgetvalue(result, 0, 4); > > if (strcmp(indisprimary, "t") == 0) > ! printfPQExpBuffer(&tmpbuf, _("primary key, ")); > else if (strcmp(indisunique, "t") == 0) > ! printfPQExpBuffer(&tmpbuf, _("unique, ")); > else > resetPQExpBuffer(&tmpbuf); > appendPQExpBuffer(&tmpbuf, "%s, ", indamname); > --- 880,888 ---- > char *indpred = PQgetvalue(result, 0, 4); > > if (strcmp(indisprimary, "t") == 0) > ! printfPQExpBuffer(&tmpbuf, _("PRIMARY KEY, ")); > else if (strcmp(indisunique, "t") == 0) > ! printfPQExpBuffer(&tmpbuf, _("UNIQUE, ")); > else > resetPQExpBuffer(&tmpbuf); > appendPQExpBuffer(&tmpbuf, "%s, ", indamname); > *************** > *** 892,898 **** > schemaname, indtable); > > if (strlen(indpred)) > ! appendPQExpBuffer(&tmpbuf, ", predicate %s", indpred); > > footers = xmalloczero(2 * sizeof(*footers)); > footers[0] = xstrdup(tmpbuf.data); > --- 892,898 ---- > schemaname, indtable); > > if (strlen(indpred)) > ! appendPQExpBuffer(&tmpbuf, ", predicate (%s)", indpred); > > footers = xmalloczero(2 * sizeof(*footers)); > footers[0] = xstrdup(tmpbuf.data); > *************** > *** 911,917 **** > if (tableinfo.hasrules) > { > printfPQExpBuffer(&buf, > ! "SELECT r.rulename\n" > "FROM pg_catalog.pg_rewrite r\n" > "WHERE r.ev_class = '%s' AND r.rulename != '_RETURN'", > oid); > --- 911,917 ---- > if (tableinfo.hasrules) > { > printfPQExpBuffer(&buf, > ! "SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid, true))\n" > "FROM pg_catalog.pg_rewrite r\n" > "WHERE r.ev_class = '%s' AND r.rulename != '_RETURN'", > oid); > *************** > *** 923,949 **** > } > > /* Footer information about a view */ > ! footers = xmalloczero((rule_count + 2) * sizeof(*footers)); > footers[count_footers] = xmalloc(64 + strlen(view_def)); > snprintf(footers[count_footers], 64 + strlen(view_def), > _("View definition:\n%s"), view_def); > count_footers++; > > /* print rules */ > ! for (i = 0; i < rule_count; i++) > { > ! char *s = _("Rules"); > > ! if (i == 0) > ! printfPQExpBuffer(&buf, "%s: %s", s, PQgetvalue(result, i, 0)); > ! else > ! printfPQExpBuffer(&buf, "%*s %s", (int) strlen(s), "", PQgetvalue(result, i, 0)); > ! if (i < rule_count - 1) > ! appendPQExpBuffer(&buf, ","); > > ! footers[count_footers++] = xstrdup(buf.data); > } > - PQclear(result); > > footers[count_footers] = NULL; > > --- 923,953 ---- > } > > /* Footer information about a view */ > ! footers = xmalloczero((rule_count + 3) * sizeof(*footers)); > footers[count_footers] = xmalloc(64 + strlen(view_def)); > snprintf(footers[count_footers], 64 + strlen(view_def), > _("View definition:\n%s"), view_def); > count_footers++; > > /* print rules */ > ! if (rule_count > 0) > { > ! printfPQExpBuffer(&buf, _("Rules:")); > ! footers[count_footers++] = xstrdup(buf.data); > ! for (i = 0; i < rule_count; i++) > ! { > ! const char *ruledef; > > ! /* Everything after "CREATE RULE" is echoed verbatim */ > ! ruledef = PQgetvalue(result, i, 1); > ! ruledef += 12; > > ! printfPQExpBuffer(&buf, " %s", ruledef); > ! > ! footers[count_footers++] = xstrdup(buf.data); > ! } > ! PQclear(result); > } > > footers[count_footers] = NULL; > > *************** > *** 970,976 **** > { > printfPQExpBuffer(&buf, > "SELECT c2.relname, i.indisprimary, i.indisunique, " > ! "pg_catalog.pg_get_indexdef(i.indexrelid)\n" > "FROM pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_index i\n" > "WHERE c.oid = '%s' AND c.oid = i.indrelid AND i.indexrelid = c2.oid\n" > "ORDER BY i.indisprimary DESC, i.indisunique DESC, c2.relname", > --- 974,980 ---- > { > printfPQExpBuffer(&buf, > "SELECT c2.relname, i.indisprimary, i.indisunique, " > ! "pg_catalog.pg_get_indexdef(i.indexrelid, 0, true)\n" > "FROM pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_index i\n" > "WHERE c.oid = '%s' AND c.oid = i.indrelid AND i.indexrelid = c2.oid\n" > "ORDER BY i.indisprimary DESC, i.indisunique DESC, c2.relname", > *************** > *** 986,992 **** > if (tableinfo.checks) > { > printfPQExpBuffer(&buf, > ! "SELECT consrc, conname\n" > "FROM pg_catalog.pg_constraint r\n" > "WHERE r.conrelid = '%s' AND r.contype = 'c'", > oid); > --- 990,996 ---- > if (tableinfo.checks) > { > printfPQExpBuffer(&buf, > ! "SELECT pg_catalog.pg_get_constraintdef(oid, true) AS consrc, conname\n" > "FROM pg_catalog.pg_constraint r\n" > "WHERE r.conrelid = '%s' AND r.contype = 'c'", > oid); > *************** > *** 1004,1010 **** > if (tableinfo.hasrules) > { > printfPQExpBuffer(&buf, > ! "SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid))\n" > "FROM pg_catalog.pg_rewrite r\n" > "WHERE r.ev_class = '%s'", > oid); > --- 1008,1014 ---- > if (tableinfo.hasrules) > { > printfPQExpBuffer(&buf, > ! "SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid, true))\n" > "FROM pg_catalog.pg_rewrite r\n" > "WHERE r.ev_class = '%s'", > oid); > *************** > *** 1049,1055 **** > { > printfPQExpBuffer(&buf, > "SELECT conname,\n" > ! " pg_catalog.pg_get_constraintdef(oid) as condef\n" > "FROM pg_catalog.pg_constraint r\n" > "WHERE r.conrelid = '%s' AND r.contype = 'f'", > oid); > --- 1053,1059 ---- > { > printfPQExpBuffer(&buf, > "SELECT conname,\n" > ! " pg_catalog.pg_get_constraintdef(oid, true) as condef\n" > "FROM pg_catalog.pg_constraint r\n" > "WHERE r.conrelid = '%s' AND r.contype = 'f'", > oid); > *************** > *** 1095,1103 **** > /* Label as primary key or unique (but not both) */ > appendPQExpBuffer(&buf, > strcmp(PQgetvalue(result1, i, 1), "t") == 0 > ! ? _(" primary key,") : > (strcmp(PQgetvalue(result1, i, 2), "t") == 0 > ! ? _(" unique,") > : "")); > > /* Everything after "USING" is echoed verbatim */ > --- 1099,1107 ---- > /* Label as primary key or unique (but not both) */ > appendPQExpBuffer(&buf, > strcmp(PQgetvalue(result1, i, 1), "t") == 0 > ! ? _(" PRIMARY KEY,") : > (strcmp(PQgetvalue(result1, i, 2), "t") == 0 > ! ? _(" UNIQUE,") > : "")); > > /* Everything after "USING" is echoed verbatim */ > *************** > *** 1119,1125 **** > footers[count_footers++] = xstrdup(buf.data); > for (i = 0; i < check_count; i++) > { > ! printfPQExpBuffer(&buf, _(" \"%s\" CHECK %s"), > PQgetvalue(result2, i, 1), > PQgetvalue(result2, i, 0)); > > --- 1123,1129 ---- > footers[count_footers++] = xstrdup(buf.data); > for (i = 0; i < check_count; i++) > { > ! printfPQExpBuffer(&buf, _(" \"%s\" %s"), > PQgetvalue(result2, i, 1), > PQgetvalue(result2, i, 0)); > > > ---------------------------(end of broadcast)--------------------------- > TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org -- Bruce Momjian | http://candle.pha.pa.us pgman@candle.pha.pa.us | (610) 359-1001 + If your life is a hard drive, | 13 Roberts Road + Christ can be your backup. | Newtown Square, Pennsylvania 19073
On Thu, 9 Oct 2003, Bruce Momjian wrote: > Uh, I can't apply this to 7.4 unless you can get others to agree it > should be added. Sorry. It looks like the patch changes translated strings which isn't supposed to happen. It can of course still be accepted, that's not my call. ps. Unified diffs (-u) are much nicer then the default format. -- /Dennis
> Uh, I can't apply this to 7.4 unless you can get others to agree it > should be added. Sorry. OK, for reviewers. Note the index predicate and the check constraint in particular. With patch: test=# \d example Table "public.example" Column | Type | Modifiers --------+---------+-------------------------------------------------------- a | integer | not null default nextval('public.example_a_seq'::text) b | integer | c | integer | d | boolean | Indexes: "example_pkey" PRIMARY KEY, btree (a) "example_b_key" UNIQUE, btree (b) "example_idx" btree (abs(a)) WHERE c > 11 Check constraints: "example_d" CHECK (d) Foreign-key constraints: "$1" FOREIGN KEY (c) REFERENCES parent(a) Rules: example_rule AS ON INSERT TO example DO INSTEAD NOTHING Without patch: test=# \d example Table "public.example" Column | Type | Modifiers --------+---------+-------------------------------------------------------- a | integer | not null default nextval('public.example_a_seq'::text) b | integer | c | integer | d | boolean | Indexes: "example_pkey" primary key, btree (a) "example_b_key" unique, btree (b) "example_idx" btree (abs(a)) WHERE (c > 11) Check constraints: "example_d" CHECK d Foreign-key constraints: "$1" FOREIGN KEY (c) REFERENCES parent(a) Rules: example_rule AS ON INSERT TO example DO INSTEAD NOTHING
> It looks like the patch changes translated strings which isn't supposed to > happen. It can of course still be accepted, that's not my call. Yes, actually it does now that I look at it. However, I submitted this patch weeks ago. If it's not accepted for the time being due to freeze, then that's fine - but it should at least be added for 7.5. > ps. Unified diffs (-u) are much nicer then the default format. It was submitted in diff -c, the recommended pgsql patch format. Chris
On Fri, Oct 10, 2003 at 12:42:50PM +0800, Christopher Kings-Lynne wrote: > OK, for reviewers. Note the index predicate and the check constraint in > particular. It seems to have the nice property of allowing output to be copy-n-pasted without further modification. -- Alvaro Herrera (<alvherre[a]dcc.uchile.cl>) "The Gord often wonders why people threaten never to come back after they've been told never to return" (www.actsofgord.com)
Christopher Kings-Lynne wrote: > > It looks like the patch changes translated strings which isn't supposed to > > happen. It can of course still be accepted, that's not my call. > > Yes, actually it does now that I look at it. However, I submitted this > patch weeks ago. If it's not accepted for the time being due to freeze, > then that's fine - but it should at least be added for 7.5. Yes, it should be added for 7.5. Even when it was originally submitted, it was past the time for accepting new 7.4 features, so I always assumed it was for 7.5. -- Bruce Momjian | http://candle.pha.pa.us pgman@candle.pha.pa.us | (610) 359-1001 + If your life is a hard drive, | 13 Roberts Road + Christ can be your backup. | Newtown Square, Pennsylvania 19073
This has been saved for the 7.5 release: http:/momjian.postgresql.org/cgi-bin/pgpatches2 --------------------------------------------------------------------------- Christopher Kings-Lynne wrote: > Resubmission of patch (for 7.4). > > I fixed the problems I was having what I chatted to you Bruce, I've > tested it well and it shouldn't be a problem to apply for 7.4. It looks > really nice with the pretty print stuff! > > Chris > > -------- Original Message -------- > Subject: [PATCHES] Make psql use all pretty print options > Date: Mon, 29 Sep 2003 12:31:18 +0800 (WST) > From: Christopher Kings-Lynne <chriskl@familyhealth.com.au> > To: pgsql-patches@postgresql.org > > Hi, > > This patch finishes off the work that I did with making view > definitions use pretty printing. > > It does: > > * Pretty check constraints > * Pretty index predicates > * Pretty rule definitions > * Uppercases PRIMARY KEY and UNIQUE to be consistent with CHECK and > FOREIGN KEY > * View rules are improved to match table rules: > > View "public.v" > Column | Type | Modifiers > ----------+---------+----------- > ?column? | integer | > View definition: > SELECT 1; > Rules: > r1 AS > ON INSERT TO v DO INSTEAD NOTHING > r2 AS > ON INSERT TO v DO INSTEAD NOTHING > > Chris > > > > Index: describe.c > =================================================================== > RCS file: /projects/cvsroot/pgsql-server/src/bin/psql/describe.c,v > retrieving revision 1.85 > diff -c -r1.85 describe.c > *** describe.c 7 Sep 2003 03:43:53 -0000 1.85 > --- describe.c 29 Sep 2003 04:24:56 -0000 > *************** > *** 857,863 **** > > printfPQExpBuffer(&buf, > "SELECT i.indisunique, i.indisprimary, a.amname, c2.relname,\n" > ! " pg_catalog.pg_get_expr(i.indpred, i.indrelid)\n" > "FROM pg_catalog.pg_index i, pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_ama\n" > "WHERE i.indexrelid = c.oid AND c.oid = '%s' AND c.relam = a.oid\n" > "AND i.indrelid = c2.oid", > --- 857,863 ---- > > printfPQExpBuffer(&buf, > "SELECT i.indisunique, i.indisprimary, a.amname, c2.relname,\n" > ! " pg_catalog.pg_get_expr(i.indpred, i.indrelid, true)\n" > "FROM pg_catalog.pg_index i, pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_ama\n" > "WHERE i.indexrelid = c.oid AND c.oid = '%s' AND c.relam = a.oid\n" > "AND i.indrelid = c2.oid", > *************** > *** 880,888 **** > char *indpred = PQgetvalue(result, 0, 4); > > if (strcmp(indisprimary, "t") == 0) > ! printfPQExpBuffer(&tmpbuf, _("primary key, ")); > else if (strcmp(indisunique, "t") == 0) > ! printfPQExpBuffer(&tmpbuf, _("unique, ")); > else > resetPQExpBuffer(&tmpbuf); > appendPQExpBuffer(&tmpbuf, "%s, ", indamname); > --- 880,888 ---- > char *indpred = PQgetvalue(result, 0, 4); > > if (strcmp(indisprimary, "t") == 0) > ! printfPQExpBuffer(&tmpbuf, _("PRIMARY KEY, ")); > else if (strcmp(indisunique, "t") == 0) > ! printfPQExpBuffer(&tmpbuf, _("UNIQUE, ")); > else > resetPQExpBuffer(&tmpbuf); > appendPQExpBuffer(&tmpbuf, "%s, ", indamname); > *************** > *** 892,898 **** > schemaname, indtable); > > if (strlen(indpred)) > ! appendPQExpBuffer(&tmpbuf, ", predicate %s", indpred); > > footers = xmalloczero(2 * sizeof(*footers)); > footers[0] = xstrdup(tmpbuf.data); > --- 892,898 ---- > schemaname, indtable); > > if (strlen(indpred)) > ! appendPQExpBuffer(&tmpbuf, ", predicate (%s)", indpred); > > footers = xmalloczero(2 * sizeof(*footers)); > footers[0] = xstrdup(tmpbuf.data); > *************** > *** 911,917 **** > if (tableinfo.hasrules) > { > printfPQExpBuffer(&buf, > ! "SELECT r.rulename\n" > "FROM pg_catalog.pg_rewrite r\n" > "WHERE r.ev_class = '%s' AND r.rulename != '_RETURN'", > oid); > --- 911,917 ---- > if (tableinfo.hasrules) > { > printfPQExpBuffer(&buf, > ! "SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid, true))\n" > "FROM pg_catalog.pg_rewrite r\n" > "WHERE r.ev_class = '%s' AND r.rulename != '_RETURN'", > oid); > *************** > *** 923,949 **** > } > > /* Footer information about a view */ > ! footers = xmalloczero((rule_count + 2) * sizeof(*footers)); > footers[count_footers] = xmalloc(64 + strlen(view_def)); > snprintf(footers[count_footers], 64 + strlen(view_def), > _("View definition:\n%s"), view_def); > count_footers++; > > /* print rules */ > ! for (i = 0; i < rule_count; i++) > { > ! char *s = _("Rules"); > > ! if (i == 0) > ! printfPQExpBuffer(&buf, "%s: %s", s, PQgetvalue(result, i, 0)); > ! else > ! printfPQExpBuffer(&buf, "%*s %s", (int) strlen(s), "", PQgetvalue(result, i, 0)); > ! if (i < rule_count - 1) > ! appendPQExpBuffer(&buf, ","); > > ! footers[count_footers++] = xstrdup(buf.data); > } > - PQclear(result); > > footers[count_footers] = NULL; > > --- 923,953 ---- > } > > /* Footer information about a view */ > ! footers = xmalloczero((rule_count + 3) * sizeof(*footers)); > footers[count_footers] = xmalloc(64 + strlen(view_def)); > snprintf(footers[count_footers], 64 + strlen(view_def), > _("View definition:\n%s"), view_def); > count_footers++; > > /* print rules */ > ! if (rule_count > 0) > { > ! printfPQExpBuffer(&buf, _("Rules:")); > ! footers[count_footers++] = xstrdup(buf.data); > ! for (i = 0; i < rule_count; i++) > ! { > ! const char *ruledef; > > ! /* Everything after "CREATE RULE" is echoed verbatim */ > ! ruledef = PQgetvalue(result, i, 1); > ! ruledef += 12; > > ! printfPQExpBuffer(&buf, " %s", ruledef); > ! > ! footers[count_footers++] = xstrdup(buf.data); > ! } > ! PQclear(result); > } > > footers[count_footers] = NULL; > > *************** > *** 970,976 **** > { > printfPQExpBuffer(&buf, > "SELECT c2.relname, i.indisprimary, i.indisunique, " > ! "pg_catalog.pg_get_indexdef(i.indexrelid)\n" > "FROM pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_index i\n" > "WHERE c.oid = '%s' AND c.oid = i.indrelid AND i.indexrelid = c2.oid\n" > "ORDER BY i.indisprimary DESC, i.indisunique DESC, c2.relname", > --- 974,980 ---- > { > printfPQExpBuffer(&buf, > "SELECT c2.relname, i.indisprimary, i.indisunique, " > ! "pg_catalog.pg_get_indexdef(i.indexrelid, 0, true)\n" > "FROM pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_index i\n" > "WHERE c.oid = '%s' AND c.oid = i.indrelid AND i.indexrelid = c2.oid\n" > "ORDER BY i.indisprimary DESC, i.indisunique DESC, c2.relname", > *************** > *** 986,992 **** > if (tableinfo.checks) > { > printfPQExpBuffer(&buf, > ! "SELECT consrc, conname\n" > "FROM pg_catalog.pg_constraint r\n" > "WHERE r.conrelid = '%s' AND r.contype = 'c'", > oid); > --- 990,996 ---- > if (tableinfo.checks) > { > printfPQExpBuffer(&buf, > ! "SELECT pg_catalog.pg_get_constraintdef(oid, true) AS consrc, conname\n" > "FROM pg_catalog.pg_constraint r\n" > "WHERE r.conrelid = '%s' AND r.contype = 'c'", > oid); > *************** > *** 1004,1010 **** > if (tableinfo.hasrules) > { > printfPQExpBuffer(&buf, > ! "SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid))\n" > "FROM pg_catalog.pg_rewrite r\n" > "WHERE r.ev_class = '%s'", > oid); > --- 1008,1014 ---- > if (tableinfo.hasrules) > { > printfPQExpBuffer(&buf, > ! "SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid, true))\n" > "FROM pg_catalog.pg_rewrite r\n" > "WHERE r.ev_class = '%s'", > oid); > *************** > *** 1049,1055 **** > { > printfPQExpBuffer(&buf, > "SELECT conname,\n" > ! " pg_catalog.pg_get_constraintdef(oid) as condef\n" > "FROM pg_catalog.pg_constraint r\n" > "WHERE r.conrelid = '%s' AND r.contype = 'f'", > oid); > --- 1053,1059 ---- > { > printfPQExpBuffer(&buf, > "SELECT conname,\n" > ! " pg_catalog.pg_get_constraintdef(oid, true) as condef\n" > "FROM pg_catalog.pg_constraint r\n" > "WHERE r.conrelid = '%s' AND r.contype = 'f'", > oid); > *************** > *** 1095,1103 **** > /* Label as primary key or unique (but not both) */ > appendPQExpBuffer(&buf, > strcmp(PQgetvalue(result1, i, 1), "t") == 0 > ! ? _(" primary key,") : > (strcmp(PQgetvalue(result1, i, 2), "t") == 0 > ! ? _(" unique,") > : "")); > > /* Everything after "USING" is echoed verbatim */ > --- 1099,1107 ---- > /* Label as primary key or unique (but not both) */ > appendPQExpBuffer(&buf, > strcmp(PQgetvalue(result1, i, 1), "t") == 0 > ! ? _(" PRIMARY KEY,") : > (strcmp(PQgetvalue(result1, i, 2), "t") == 0 > ! ? _(" UNIQUE,") > : "")); > > /* Everything after "USING" is echoed verbatim */ > *************** > *** 1119,1125 **** > footers[count_footers++] = xstrdup(buf.data); > for (i = 0; i < check_count; i++) > { > ! printfPQExpBuffer(&buf, _(" \"%s\" CHECK %s"), > PQgetvalue(result2, i, 1), > PQgetvalue(result2, i, 0)); > > --- 1123,1129 ---- > footers[count_footers++] = xstrdup(buf.data); > for (i = 0; i < check_count; i++) > { > ! printfPQExpBuffer(&buf, _(" \"%s\" %s"), > PQgetvalue(result2, i, 1), > PQgetvalue(result2, i, 0)); > > > ---------------------------(end of broadcast)--------------------------- > TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org -- Bruce Momjian | http://candle.pha.pa.us pgman@candle.pha.pa.us | (610) 359-1001 + If your life is a hard drive, | 13 Roberts Road + Christ can be your backup. | Newtown Square, Pennsylvania 19073
Christopher Kings-Lynne <chriskl@familyhealth.com.au> writes: > Resubmission of patch (for 7.4). While I agree with the objection that we shouldn't break the strings freeze for this, I do think it might be a good idea to apply the change that prints CHECK constraints using pg_get_constraintdef() rather than simply looking at pg_constraint.consrc. This is arguably a bug fix, not a pretty printing improvement, because consrc doesn't track column renamings (and probably other changes). Doing that would require making this one string change: ! printfPQExpBuffer(&buf, _(" \"%s\" CHECK %s"), becomes ! printfPQExpBuffer(&buf, _(" \"%s\" %s"), but ISTM the latter string doesn't really need any translation and so it wouldn't be much of a problem. Comments? regards, tom lane
Tom Lane writes: > Doing that would require making this one string change: > > ! printfPQExpBuffer(&buf, _(" \"%s\" CHECK %s"), > becomes > ! printfPQExpBuffer(&buf, _(" \"%s\" %s"), > > but ISTM the latter string doesn't really need any translation and so > it wouldn't be much of a problem. The quotation marks need "translation". But indeed it wouldn't be "much" of a problem. I agree that this is a desirable bug fix. -- Peter Eisentraut peter_e@gmx.net
Your patch has been added to the PostgreSQL unapplied patches list at: http://momjian.postgresql.org/cgi-bin/pgpatches I will try to apply it within the next 48 hours. --------------------------------------------------------------------------- Christopher Kings-Lynne wrote: > Resubmission of patch (for 7.4). > > I fixed the problems I was having what I chatted to you Bruce, I've > tested it well and it shouldn't be a problem to apply for 7.4. It looks > really nice with the pretty print stuff! > > Chris > > -------- Original Message -------- > Subject: [PATCHES] Make psql use all pretty print options > Date: Mon, 29 Sep 2003 12:31:18 +0800 (WST) > From: Christopher Kings-Lynne <chriskl@familyhealth.com.au> > To: pgsql-patches@postgresql.org > > Hi, > > This patch finishes off the work that I did with making view > definitions use pretty printing. > > It does: > > * Pretty check constraints > * Pretty index predicates > * Pretty rule definitions > * Uppercases PRIMARY KEY and UNIQUE to be consistent with CHECK and > FOREIGN KEY > * View rules are improved to match table rules: > > View "public.v" > Column | Type | Modifiers > ----------+---------+----------- > ?column? | integer | > View definition: > SELECT 1; > Rules: > r1 AS > ON INSERT TO v DO INSTEAD NOTHING > r2 AS > ON INSERT TO v DO INSTEAD NOTHING > > Chris > > > > Index: describe.c > =================================================================== > RCS file: /projects/cvsroot/pgsql-server/src/bin/psql/describe.c,v > retrieving revision 1.85 > diff -c -r1.85 describe.c > *** describe.c 7 Sep 2003 03:43:53 -0000 1.85 > --- describe.c 29 Sep 2003 04:24:56 -0000 > *************** > *** 857,863 **** > > printfPQExpBuffer(&buf, > "SELECT i.indisunique, i.indisprimary, a.amname, c2.relname,\n" > ! " pg_catalog.pg_get_expr(i.indpred, i.indrelid)\n" > "FROM pg_catalog.pg_index i, pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_ama\n" > "WHERE i.indexrelid = c.oid AND c.oid = '%s' AND c.relam = a.oid\n" > "AND i.indrelid = c2.oid", > --- 857,863 ---- > > printfPQExpBuffer(&buf, > "SELECT i.indisunique, i.indisprimary, a.amname, c2.relname,\n" > ! " pg_catalog.pg_get_expr(i.indpred, i.indrelid, true)\n" > "FROM pg_catalog.pg_index i, pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_ama\n" > "WHERE i.indexrelid = c.oid AND c.oid = '%s' AND c.relam = a.oid\n" > "AND i.indrelid = c2.oid", > *************** > *** 880,888 **** > char *indpred = PQgetvalue(result, 0, 4); > > if (strcmp(indisprimary, "t") == 0) > ! printfPQExpBuffer(&tmpbuf, _("primary key, ")); > else if (strcmp(indisunique, "t") == 0) > ! printfPQExpBuffer(&tmpbuf, _("unique, ")); > else > resetPQExpBuffer(&tmpbuf); > appendPQExpBuffer(&tmpbuf, "%s, ", indamname); > --- 880,888 ---- > char *indpred = PQgetvalue(result, 0, 4); > > if (strcmp(indisprimary, "t") == 0) > ! printfPQExpBuffer(&tmpbuf, _("PRIMARY KEY, ")); > else if (strcmp(indisunique, "t") == 0) > ! printfPQExpBuffer(&tmpbuf, _("UNIQUE, ")); > else > resetPQExpBuffer(&tmpbuf); > appendPQExpBuffer(&tmpbuf, "%s, ", indamname); > *************** > *** 892,898 **** > schemaname, indtable); > > if (strlen(indpred)) > ! appendPQExpBuffer(&tmpbuf, ", predicate %s", indpred); > > footers = xmalloczero(2 * sizeof(*footers)); > footers[0] = xstrdup(tmpbuf.data); > --- 892,898 ---- > schemaname, indtable); > > if (strlen(indpred)) > ! appendPQExpBuffer(&tmpbuf, ", predicate (%s)", indpred); > > footers = xmalloczero(2 * sizeof(*footers)); > footers[0] = xstrdup(tmpbuf.data); > *************** > *** 911,917 **** > if (tableinfo.hasrules) > { > printfPQExpBuffer(&buf, > ! "SELECT r.rulename\n" > "FROM pg_catalog.pg_rewrite r\n" > "WHERE r.ev_class = '%s' AND r.rulename != '_RETURN'", > oid); > --- 911,917 ---- > if (tableinfo.hasrules) > { > printfPQExpBuffer(&buf, > ! "SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid, true))\n" > "FROM pg_catalog.pg_rewrite r\n" > "WHERE r.ev_class = '%s' AND r.rulename != '_RETURN'", > oid); > *************** > *** 923,949 **** > } > > /* Footer information about a view */ > ! footers = xmalloczero((rule_count + 2) * sizeof(*footers)); > footers[count_footers] = xmalloc(64 + strlen(view_def)); > snprintf(footers[count_footers], 64 + strlen(view_def), > _("View definition:\n%s"), view_def); > count_footers++; > > /* print rules */ > ! for (i = 0; i < rule_count; i++) > { > ! char *s = _("Rules"); > > ! if (i == 0) > ! printfPQExpBuffer(&buf, "%s: %s", s, PQgetvalue(result, i, 0)); > ! else > ! printfPQExpBuffer(&buf, "%*s %s", (int) strlen(s), "", PQgetvalue(result, i, 0)); > ! if (i < rule_count - 1) > ! appendPQExpBuffer(&buf, ","); > > ! footers[count_footers++] = xstrdup(buf.data); > } > - PQclear(result); > > footers[count_footers] = NULL; > > --- 923,953 ---- > } > > /* Footer information about a view */ > ! footers = xmalloczero((rule_count + 3) * sizeof(*footers)); > footers[count_footers] = xmalloc(64 + strlen(view_def)); > snprintf(footers[count_footers], 64 + strlen(view_def), > _("View definition:\n%s"), view_def); > count_footers++; > > /* print rules */ > ! if (rule_count > 0) > { > ! printfPQExpBuffer(&buf, _("Rules:")); > ! footers[count_footers++] = xstrdup(buf.data); > ! for (i = 0; i < rule_count; i++) > ! { > ! const char *ruledef; > > ! /* Everything after "CREATE RULE" is echoed verbatim */ > ! ruledef = PQgetvalue(result, i, 1); > ! ruledef += 12; > > ! printfPQExpBuffer(&buf, " %s", ruledef); > ! > ! footers[count_footers++] = xstrdup(buf.data); > ! } > ! PQclear(result); > } > > footers[count_footers] = NULL; > > *************** > *** 970,976 **** > { > printfPQExpBuffer(&buf, > "SELECT c2.relname, i.indisprimary, i.indisunique, " > ! "pg_catalog.pg_get_indexdef(i.indexrelid)\n" > "FROM pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_index i\n" > "WHERE c.oid = '%s' AND c.oid = i.indrelid AND i.indexrelid = c2.oid\n" > "ORDER BY i.indisprimary DESC, i.indisunique DESC, c2.relname", > --- 974,980 ---- > { > printfPQExpBuffer(&buf, > "SELECT c2.relname, i.indisprimary, i.indisunique, " > ! "pg_catalog.pg_get_indexdef(i.indexrelid, 0, true)\n" > "FROM pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_index i\n" > "WHERE c.oid = '%s' AND c.oid = i.indrelid AND i.indexrelid = c2.oid\n" > "ORDER BY i.indisprimary DESC, i.indisunique DESC, c2.relname", > *************** > *** 986,992 **** > if (tableinfo.checks) > { > printfPQExpBuffer(&buf, > ! "SELECT consrc, conname\n" > "FROM pg_catalog.pg_constraint r\n" > "WHERE r.conrelid = '%s' AND r.contype = 'c'", > oid); > --- 990,996 ---- > if (tableinfo.checks) > { > printfPQExpBuffer(&buf, > ! "SELECT pg_catalog.pg_get_constraintdef(oid, true) AS consrc, conname\n" > "FROM pg_catalog.pg_constraint r\n" > "WHERE r.conrelid = '%s' AND r.contype = 'c'", > oid); > *************** > *** 1004,1010 **** > if (tableinfo.hasrules) > { > printfPQExpBuffer(&buf, > ! "SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid))\n" > "FROM pg_catalog.pg_rewrite r\n" > "WHERE r.ev_class = '%s'", > oid); > --- 1008,1014 ---- > if (tableinfo.hasrules) > { > printfPQExpBuffer(&buf, > ! "SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid, true))\n" > "FROM pg_catalog.pg_rewrite r\n" > "WHERE r.ev_class = '%s'", > oid); > *************** > *** 1049,1055 **** > { > printfPQExpBuffer(&buf, > "SELECT conname,\n" > ! " pg_catalog.pg_get_constraintdef(oid) as condef\n" > "FROM pg_catalog.pg_constraint r\n" > "WHERE r.conrelid = '%s' AND r.contype = 'f'", > oid); > --- 1053,1059 ---- > { > printfPQExpBuffer(&buf, > "SELECT conname,\n" > ! " pg_catalog.pg_get_constraintdef(oid, true) as condef\n" > "FROM pg_catalog.pg_constraint r\n" > "WHERE r.conrelid = '%s' AND r.contype = 'f'", > oid); > *************** > *** 1095,1103 **** > /* Label as primary key or unique (but not both) */ > appendPQExpBuffer(&buf, > strcmp(PQgetvalue(result1, i, 1), "t") == 0 > ! ? _(" primary key,") : > (strcmp(PQgetvalue(result1, i, 2), "t") == 0 > ! ? _(" unique,") > : "")); > > /* Everything after "USING" is echoed verbatim */ > --- 1099,1107 ---- > /* Label as primary key or unique (but not both) */ > appendPQExpBuffer(&buf, > strcmp(PQgetvalue(result1, i, 1), "t") == 0 > ! ? _(" PRIMARY KEY,") : > (strcmp(PQgetvalue(result1, i, 2), "t") == 0 > ! ? _(" UNIQUE,") > : "")); > > /* Everything after "USING" is echoed verbatim */ > *************** > *** 1119,1125 **** > footers[count_footers++] = xstrdup(buf.data); > for (i = 0; i < check_count; i++) > { > ! printfPQExpBuffer(&buf, _(" \"%s\" CHECK %s"), > PQgetvalue(result2, i, 1), > PQgetvalue(result2, i, 0)); > > --- 1123,1129 ---- > footers[count_footers++] = xstrdup(buf.data); > for (i = 0; i < check_count; i++) > { > ! printfPQExpBuffer(&buf, _(" \"%s\" %s"), > PQgetvalue(result2, i, 1), > PQgetvalue(result2, i, 0)); > > > ---------------------------(end of broadcast)--------------------------- > TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org -- Bruce Momjian | http://candle.pha.pa.us pgman@candle.pha.pa.us | (610) 359-1001 + If your life is a hard drive, | 13 Roberts Road + Christ can be your backup. | Newtown Square, Pennsylvania 19073
Patch applied. Thanks. The CHECK part of this patch was already applied to 7.4. --------------------------------------------------------------------------- Christopher Kings-Lynne wrote: > Resubmission of patch (for 7.4). > > I fixed the problems I was having what I chatted to you Bruce, I've > tested it well and it shouldn't be a problem to apply for 7.4. It looks > really nice with the pretty print stuff! > > Chris > > -------- Original Message -------- > Subject: [PATCHES] Make psql use all pretty print options > Date: Mon, 29 Sep 2003 12:31:18 +0800 (WST) > From: Christopher Kings-Lynne <chriskl@familyhealth.com.au> > To: pgsql-patches@postgresql.org > > Hi, > > This patch finishes off the work that I did with making view > definitions use pretty printing. > > It does: > > * Pretty check constraints > * Pretty index predicates > * Pretty rule definitions > * Uppercases PRIMARY KEY and UNIQUE to be consistent with CHECK and > FOREIGN KEY > * View rules are improved to match table rules: > > View "public.v" > Column | Type | Modifiers > ----------+---------+----------- > ?column? | integer | > View definition: > SELECT 1; > Rules: > r1 AS > ON INSERT TO v DO INSTEAD NOTHING > r2 AS > ON INSERT TO v DO INSTEAD NOTHING > > Chris > > > > Index: describe.c > =================================================================== > RCS file: /projects/cvsroot/pgsql-server/src/bin/psql/describe.c,v > retrieving revision 1.85 > diff -c -r1.85 describe.c > *** describe.c 7 Sep 2003 03:43:53 -0000 1.85 > --- describe.c 29 Sep 2003 04:24:56 -0000 > *************** > *** 857,863 **** > > printfPQExpBuffer(&buf, > "SELECT i.indisunique, i.indisprimary, a.amname, c2.relname,\n" > ! " pg_catalog.pg_get_expr(i.indpred, i.indrelid)\n" > "FROM pg_catalog.pg_index i, pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_ama\n" > "WHERE i.indexrelid = c.oid AND c.oid = '%s' AND c.relam = a.oid\n" > "AND i.indrelid = c2.oid", > --- 857,863 ---- > > printfPQExpBuffer(&buf, > "SELECT i.indisunique, i.indisprimary, a.amname, c2.relname,\n" > ! " pg_catalog.pg_get_expr(i.indpred, i.indrelid, true)\n" > "FROM pg_catalog.pg_index i, pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_ama\n" > "WHERE i.indexrelid = c.oid AND c.oid = '%s' AND c.relam = a.oid\n" > "AND i.indrelid = c2.oid", > *************** > *** 880,888 **** > char *indpred = PQgetvalue(result, 0, 4); > > if (strcmp(indisprimary, "t") == 0) > ! printfPQExpBuffer(&tmpbuf, _("primary key, ")); > else if (strcmp(indisunique, "t") == 0) > ! printfPQExpBuffer(&tmpbuf, _("unique, ")); > else > resetPQExpBuffer(&tmpbuf); > appendPQExpBuffer(&tmpbuf, "%s, ", indamname); > --- 880,888 ---- > char *indpred = PQgetvalue(result, 0, 4); > > if (strcmp(indisprimary, "t") == 0) > ! printfPQExpBuffer(&tmpbuf, _("PRIMARY KEY, ")); > else if (strcmp(indisunique, "t") == 0) > ! printfPQExpBuffer(&tmpbuf, _("UNIQUE, ")); > else > resetPQExpBuffer(&tmpbuf); > appendPQExpBuffer(&tmpbuf, "%s, ", indamname); > *************** > *** 892,898 **** > schemaname, indtable); > > if (strlen(indpred)) > ! appendPQExpBuffer(&tmpbuf, ", predicate %s", indpred); > > footers = xmalloczero(2 * sizeof(*footers)); > footers[0] = xstrdup(tmpbuf.data); > --- 892,898 ---- > schemaname, indtable); > > if (strlen(indpred)) > ! appendPQExpBuffer(&tmpbuf, ", predicate (%s)", indpred); > > footers = xmalloczero(2 * sizeof(*footers)); > footers[0] = xstrdup(tmpbuf.data); > *************** > *** 911,917 **** > if (tableinfo.hasrules) > { > printfPQExpBuffer(&buf, > ! "SELECT r.rulename\n" > "FROM pg_catalog.pg_rewrite r\n" > "WHERE r.ev_class = '%s' AND r.rulename != '_RETURN'", > oid); > --- 911,917 ---- > if (tableinfo.hasrules) > { > printfPQExpBuffer(&buf, > ! "SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid, true))\n" > "FROM pg_catalog.pg_rewrite r\n" > "WHERE r.ev_class = '%s' AND r.rulename != '_RETURN'", > oid); > *************** > *** 923,949 **** > } > > /* Footer information about a view */ > ! footers = xmalloczero((rule_count + 2) * sizeof(*footers)); > footers[count_footers] = xmalloc(64 + strlen(view_def)); > snprintf(footers[count_footers], 64 + strlen(view_def), > _("View definition:\n%s"), view_def); > count_footers++; > > /* print rules */ > ! for (i = 0; i < rule_count; i++) > { > ! char *s = _("Rules"); > > ! if (i == 0) > ! printfPQExpBuffer(&buf, "%s: %s", s, PQgetvalue(result, i, 0)); > ! else > ! printfPQExpBuffer(&buf, "%*s %s", (int) strlen(s), "", PQgetvalue(result, i, 0)); > ! if (i < rule_count - 1) > ! appendPQExpBuffer(&buf, ","); > > ! footers[count_footers++] = xstrdup(buf.data); > } > - PQclear(result); > > footers[count_footers] = NULL; > > --- 923,953 ---- > } > > /* Footer information about a view */ > ! footers = xmalloczero((rule_count + 3) * sizeof(*footers)); > footers[count_footers] = xmalloc(64 + strlen(view_def)); > snprintf(footers[count_footers], 64 + strlen(view_def), > _("View definition:\n%s"), view_def); > count_footers++; > > /* print rules */ > ! if (rule_count > 0) > { > ! printfPQExpBuffer(&buf, _("Rules:")); > ! footers[count_footers++] = xstrdup(buf.data); > ! for (i = 0; i < rule_count; i++) > ! { > ! const char *ruledef; > > ! /* Everything after "CREATE RULE" is echoed verbatim */ > ! ruledef = PQgetvalue(result, i, 1); > ! ruledef += 12; > > ! printfPQExpBuffer(&buf, " %s", ruledef); > ! > ! footers[count_footers++] = xstrdup(buf.data); > ! } > ! PQclear(result); > } > > footers[count_footers] = NULL; > > *************** > *** 970,976 **** > { > printfPQExpBuffer(&buf, > "SELECT c2.relname, i.indisprimary, i.indisunique, " > ! "pg_catalog.pg_get_indexdef(i.indexrelid)\n" > "FROM pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_index i\n" > "WHERE c.oid = '%s' AND c.oid = i.indrelid AND i.indexrelid = c2.oid\n" > "ORDER BY i.indisprimary DESC, i.indisunique DESC, c2.relname", > --- 974,980 ---- > { > printfPQExpBuffer(&buf, > "SELECT c2.relname, i.indisprimary, i.indisunique, " > ! "pg_catalog.pg_get_indexdef(i.indexrelid, 0, true)\n" > "FROM pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_index i\n" > "WHERE c.oid = '%s' AND c.oid = i.indrelid AND i.indexrelid = c2.oid\n" > "ORDER BY i.indisprimary DESC, i.indisunique DESC, c2.relname", > *************** > *** 986,992 **** > if (tableinfo.checks) > { > printfPQExpBuffer(&buf, > ! "SELECT consrc, conname\n" > "FROM pg_catalog.pg_constraint r\n" > "WHERE r.conrelid = '%s' AND r.contype = 'c'", > oid); > --- 990,996 ---- > if (tableinfo.checks) > { > printfPQExpBuffer(&buf, > ! "SELECT pg_catalog.pg_get_constraintdef(oid, true) AS consrc, conname\n" > "FROM pg_catalog.pg_constraint r\n" > "WHERE r.conrelid = '%s' AND r.contype = 'c'", > oid); > *************** > *** 1004,1010 **** > if (tableinfo.hasrules) > { > printfPQExpBuffer(&buf, > ! "SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid))\n" > "FROM pg_catalog.pg_rewrite r\n" > "WHERE r.ev_class = '%s'", > oid); > --- 1008,1014 ---- > if (tableinfo.hasrules) > { > printfPQExpBuffer(&buf, > ! "SELECT r.rulename, trim(trailing ';' from pg_catalog.pg_get_ruledef(r.oid, true))\n" > "FROM pg_catalog.pg_rewrite r\n" > "WHERE r.ev_class = '%s'", > oid); > *************** > *** 1049,1055 **** > { > printfPQExpBuffer(&buf, > "SELECT conname,\n" > ! " pg_catalog.pg_get_constraintdef(oid) as condef\n" > "FROM pg_catalog.pg_constraint r\n" > "WHERE r.conrelid = '%s' AND r.contype = 'f'", > oid); > --- 1053,1059 ---- > { > printfPQExpBuffer(&buf, > "SELECT conname,\n" > ! " pg_catalog.pg_get_constraintdef(oid, true) as condef\n" > "FROM pg_catalog.pg_constraint r\n" > "WHERE r.conrelid = '%s' AND r.contype = 'f'", > oid); > *************** > *** 1095,1103 **** > /* Label as primary key or unique (but not both) */ > appendPQExpBuffer(&buf, > strcmp(PQgetvalue(result1, i, 1), "t") == 0 > ! ? _(" primary key,") : > (strcmp(PQgetvalue(result1, i, 2), "t") == 0 > ! ? _(" unique,") > : "")); > > /* Everything after "USING" is echoed verbatim */ > --- 1099,1107 ---- > /* Label as primary key or unique (but not both) */ > appendPQExpBuffer(&buf, > strcmp(PQgetvalue(result1, i, 1), "t") == 0 > ! ? _(" PRIMARY KEY,") : > (strcmp(PQgetvalue(result1, i, 2), "t") == 0 > ! ? _(" UNIQUE,") > : "")); > > /* Everything after "USING" is echoed verbatim */ > *************** > *** 1119,1125 **** > footers[count_footers++] = xstrdup(buf.data); > for (i = 0; i < check_count; i++) > { > ! printfPQExpBuffer(&buf, _(" \"%s\" CHECK %s"), > PQgetvalue(result2, i, 1), > PQgetvalue(result2, i, 0)); > > --- 1123,1129 ---- > footers[count_footers++] = xstrdup(buf.data); > for (i = 0; i < check_count; i++) > { > ! printfPQExpBuffer(&buf, _(" \"%s\" %s"), > PQgetvalue(result2, i, 1), > PQgetvalue(result2, i, 0)); > > > ---------------------------(end of broadcast)--------------------------- > TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org -- Bruce Momjian | http://candle.pha.pa.us pgman@candle.pha.pa.us | (610) 359-1001 + If your life is a hard drive, | 13 Roberts Road + Christ can be your backup. | Newtown Square, Pennsylvania 19073