Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit ca64391

Browse files
committed
Updated the pg_get_constraintdef() to use conbin. Update pg_dump to use
pg_get_constraintdef() for >= 70400. Rod Taylor <rbt@rbt.ca>
1 parent be94f19 commit ca64391

File tree

4 files changed

+105
-15
lines changed

4 files changed

+105
-15
lines changed

src/backend/utils/adt/ruleutils.c

Lines changed: 32 additions & 3 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.141 2003/05/28 16:03:59 tgl Exp $
6+
* $Header: /cvsroot/pgsql/src/backend/utils/adt/ruleutils.c,v 1.142 2003/06/25 03:56:30 momjian Exp $
77
*
88
* This software is copyrighted by Jan Wieck - Hamburg.
99
*
@@ -894,21 +894,50 @@ pg_get_constraintdef(PG_FUNCTION_ARGS)
894894
{
895895
Datum val;
896896
bool isnull;
897+
char *conbin;
898+
char *consrc;
899+
Node *expr;
900+
List *context;
897901

898902
/* Start off the constraint definition */
899903
/* The consrc for CHECK constraints always seems to be
900904
bracketed, so we don't add extra brackets here. */
901905
appendStringInfo(&buf, "CHECK ");
902906

903907
/* Fetch constraint source */
904-
val = heap_getattr(tup, Anum_pg_constraint_consrc,
908+
val = heap_getattr(tup, Anum_pg_constraint_conbin,
905909
RelationGetDescr(conDesc), &isnull);
906910
if (isnull)
907911
elog(ERROR, "pg_get_constraintdef: Null consrc for constraint %u",
908912
constraintId);
909913

914+
conbin = DatumGetCString(DirectFunctionCall1(textout, val));
915+
expr = stringToNode(conbin);
916+
917+
/*
918+
* If top level is a List, assume it is an implicit-AND structure, and
919+
* convert to explicit AND. This is needed for partial index
920+
* predicates.
921+
*/
922+
if (expr && IsA(expr, List))
923+
expr = (Node *) make_ands_explicit((List *) expr);
924+
925+
if (conForm->conrelid != InvalidOid)
926+
/* It's a Relation */
927+
context = deparse_context_for(get_rel_name(conForm->conrelid),
928+
conForm->conrelid);
929+
else
930+
/*
931+
* Since VARNOs aren't allowed in domain constraints, relation context
932+
* isn't required as anything other than a shell.
933+
*/
934+
context = deparse_context_for(get_typname(conForm->contypid),
935+
InvalidOid);
936+
937+
consrc = deparse_expression(expr, context, false, false);
938+
910939
/* Append the constraint source */
911-
appendStringInfoString(&buf, DatumGetCString(DirectFunctionCall1(textout, val)));
940+
appendStringInfoString(&buf, consrc);
912941

913942
break;
914943
}

src/backend/utils/cache/lsyscache.c

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/utils/cache/lsyscache.c,v 1.97 2003/06/24 23:14:46 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/utils/cache/lsyscache.c,v 1.98 2003/06/25 03:56:31 momjian Exp $
1111
*
1212
* NOTES
1313
* Eventually, the index information should go through here, too.
@@ -1443,6 +1443,37 @@ get_typtype(Oid typid)
14431443
return '\0';
14441444
}
14451445

1446+
/*
1447+
* get_typname
1448+
* Returns the name of a given type.
1449+
*
1450+
* Returns a palloc'd copy of the string, or NULL if no such relation.
1451+
*
1452+
* NOTE: since type name is not unique, be wary of code that uses this
1453+
* for anything except preparing error messages.
1454+
*/
1455+
char *
1456+
get_typname(Oid typid)
1457+
{
1458+
HeapTuple tp;
1459+
1460+
tp = SearchSysCache(TYPEOID,
1461+
ObjectIdGetDatum(typid),
1462+
0, 0, 0);
1463+
if (HeapTupleIsValid(tp))
1464+
{
1465+
Form_pg_type typtup = (Form_pg_type) GETSTRUCT(tp);
1466+
char *result;
1467+
1468+
result = pstrdup(NameStr(typtup->typname));
1469+
ReleaseSysCache(tp);
1470+
return result;
1471+
}
1472+
else
1473+
return NULL;
1474+
}
1475+
1476+
14461477
/*
14471478
* get_typ_typrelid
14481479
*

src/bin/pg_dump/pg_dump.c

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* by PostgreSQL
1313
*
1414
* IDENTIFICATION
15-
* $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.333 2003/06/11 16:29:42 tgl Exp $
15+
* $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.334 2003/06/25 03:56:31 momjian Exp $
1616
*
1717
*-------------------------------------------------------------------------
1818
*/
@@ -3303,10 +3303,17 @@ dumpOneDomain(Archive *fout, TypeInfo *tinfo)
33033303
/*
33043304
* Fetch and process CHECK constraints for the domain
33053305
*/
3306-
appendPQExpBuffer(chkquery, "SELECT conname, consrc "
3307-
"FROM pg_catalog.pg_constraint "
3308-
"WHERE contypid = '%s'::pg_catalog.oid",
3309-
tinfo->oid);
3306+
if (g_fout->remoteVersion >= 70400)
3307+
appendPQExpBuffer(chkquery, "SELECT conname,"
3308+
"pg_catalog.pg_get_constraintdef(oid) AS consrc "
3309+
"FROM pg_catalog.pg_constraint "
3310+
"WHERE contypid = '%s'::pg_catalog.oid",
3311+
tinfo->oid);
3312+
else
3313+
appendPQExpBuffer(chkquery, "SELECT conname, 'CHECK (' || consrc || ')'"
3314+
"FROM pg_catalog.pg_constraint "
3315+
"WHERE contypid = '%s'::pg_catalog.oid",
3316+
tinfo->oid);
33103317

33113318
res = PQexec(g_conn, chkquery->data);
33123319
if (!res ||
@@ -3326,7 +3333,7 @@ dumpOneDomain(Archive *fout, TypeInfo *tinfo)
33263333
conname = PQgetvalue(res, i, PQfnumber(res, "conname"));
33273334
consrc = PQgetvalue(res, i, PQfnumber(res, "consrc"));
33283335

3329-
appendPQExpBuffer(q, "\n\tCONSTRAINT %s CHECK %s",
3336+
appendPQExpBuffer(q, "\n\tCONSTRAINT %s %s",
33303337
fmtId(conname), consrc);
33313338
}
33323339

@@ -5257,8 +5264,29 @@ dumpOneTable(Archive *fout, TableInfo *tbinfo, TableInfo *g_tblinfo)
52575264
tbinfo->relname);
52585265

52595266
resetPQExpBuffer(query);
5260-
if (g_fout->remoteVersion >= 70300)
5261-
appendPQExpBuffer(query, "SELECT conname, consrc"
5267+
if (g_fout->remoteVersion >= 70400)
5268+
appendPQExpBuffer(query, "SELECT conname, "
5269+
" pg_catalog.pg_get_constraintdef(c1.oid) AS consrc "
5270+
" from pg_catalog.pg_constraint c1"
5271+
" where conrelid = '%s'::pg_catalog.oid "
5272+
" and contype = 'c' "
5273+
" and not exists "
5274+
" (select 1 from "
5275+
" pg_catalog.pg_constraint c2, "
5276+
" pg_catalog.pg_inherits i "
5277+
" where i.inhrelid = c1.conrelid "
5278+
" and (c2.conname = c1.conname "
5279+
" or (c2.conname[0] = '$' "
5280+
" and c1.conname[0] = '$')"
5281+
" )"
5282+
" and pg_catalog.pg_get_constraintdef(c2.oid) "
5283+
" = pg_catalog.pg_get_constraintdef(c1.oid) "
5284+
" and c2.conrelid = i.inhparent) "
5285+
" order by conname ",
5286+
tbinfo->oid);
5287+
else if (g_fout->remoteVersion >= 70300)
5288+
appendPQExpBuffer(query, "SELECT conname, "
5289+
" 'CHECK (' || consrc || ')'"
52625290
" from pg_catalog.pg_constraint c1"
52635291
" where conrelid = '%s'::pg_catalog.oid "
52645292
" and contype = 'c' "
@@ -5276,7 +5304,8 @@ dumpOneTable(Archive *fout, TableInfo *tbinfo, TableInfo *g_tblinfo)
52765304
" order by conname ",
52775305
tbinfo->oid);
52785306
else
5279-
appendPQExpBuffer(query, "SELECT rcname as conname, rcsrc as consrc"
5307+
appendPQExpBuffer(query, "SELECT rcname as conname,"
5308+
" 'CHECK (' || rcsrc || ')' as consrc"
52805309
" from pg_relcheck c1"
52815310
" where rcrelid = '%s'::oid "
52825311
" and not exists "
@@ -5321,7 +5350,7 @@ dumpOneTable(Archive *fout, TableInfo *tbinfo, TableInfo *g_tblinfo)
53215350
if (name[0] != '$')
53225351
appendPQExpBuffer(q, "CONSTRAINT %s ",
53235352
fmtId(name));
5324-
appendPQExpBuffer(q, "CHECK (%s)", expr);
5353+
appendPQExpBuffer(q, "%s", expr);
53255354
}
53265355
PQclear(res2);
53275356
}

src/include/utils/lsyscache.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
9-
* $Id: lsyscache.h,v 1.72 2003/06/24 23:14:49 momjian Exp $
9+
* $Id: lsyscache.h,v 1.73 2003/06/25 03:56:31 momjian Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -81,6 +81,7 @@ extern char get_typtype(Oid typid);
8181
extern Oid get_typ_typrelid(Oid typid);
8282
extern Oid get_element_type(Oid typid);
8383
extern Oid get_array_type(Oid typid);
84+
extern char *get_typname(Oid relid);
8485
extern void getTypeInputInfo(Oid type, Oid *typInput, Oid *typElem);
8586
extern void getTypeOutputInfo(Oid type, Oid *typOutput, Oid *typElem,
8687
bool *typIsVarlena);

0 commit comments

Comments
 (0)