pg_dump and inherited attributes
Hi,
I'm looking at pg_dump/common.c:flagInhAttrs() and suspect that it can
be more or less rewritten completely, and probably should to get rigth
all the cases mentioned in the past attisinherited discussion. Is this
desirable for 7.3? It can probably be hacked around and the rewrite
kept for 7.4, but I think it will be much simpler after the rewrite.
What do people think about this?
--
Alvaro Herrera (<alvherre[a]atentus.com>)
"Siempre hay que alimentar a los dioses, aunque la tierra este seca" (Orual)
Alvaro Herrera <alvherre@atentus.com> writes:
I'm looking at pg_dump/common.c:flagInhAttrs() and suspect that it can
be more or less rewritten completely, and probably should to get rigth
all the cases mentioned in the past attisinherited discussion. Is this
desirable for 7.3? It can probably be hacked around and the rewrite
kept for 7.4, but I think it will be much simpler after the rewrite.
If it's a bug then it's fair game to fix in 7.3. But keep in mind that
pg_dump has to behave at least somewhat sanely when called against older
servers ... will your rewrite behave reasonably if the server does not
offer attinhcount values?
regards, tom lane
En Wed, 25 Sep 2002 00:01:24 -0400
Tom Lane <tgl@sss.pgh.pa.us> escribi�:
Alvaro Herrera <alvherre@atentus.com> writes:
I'm looking at pg_dump/common.c:flagInhAttrs() and suspect that it can
be more or less rewritten completely, and probably should to get rigth
all the cases mentioned in the past attisinherited discussion. Is this
desirable for 7.3? It can probably be hacked around and the rewrite
kept for 7.4, but I think it will be much simpler after the rewrite.If it's a bug then it's fair game to fix in 7.3. But keep in mind that
pg_dump has to behave at least somewhat sanely when called against older
servers ... will your rewrite behave reasonably if the server does not
offer attinhcount values?
Nah. I don't think it's worth it: I had forgotten that older versions
should be supported. I just left the code as is and added a
version-specific test.
This patch allows pg_dump to dump correctly local definition of columns.
In particular,
CREATE TABLE p1 (f1 int, f2 int);
CREATE TABLE p2 (f1 int);
CREATE TABLE c () INHERITS (p1, p2);
ALTER TABLE ONLY p1 DROP COLUMN f1;
CREATE TABLE p3 (f1 int);
CREATE TABLE c2 (f1 int) INHERITS (p3);
Will be dumped as
CREATE TABLE p1 (f2 int);
CREATE TABLE p2 (f1 int);
CREATE TABLE c (f1 int) INHERITS (p1, p2);
CREATE TABLE c2 (f1 int) INHERITS (p3);
(Previous version will dump
CREATE TABLE c () INHERITS (p1, p2)
CREATE TABLE c2 () INHERITS (p3) )
--
Alvaro Herrera (<alvherre[a]atentus.com>)
A male gynecologist is like an auto mechanic who never owned a car.
- Carrie Snow
Attachments:
pg_dump-attislocal.patchapplication/octet-stream; name=pg_dump-attislocal.patchDownload
Index: src/bin/pg_dump//common.c
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/bin/pg_dump/common.c,v
retrieving revision 1.70
diff -c -r1.70 common.c
*** src/bin/pg_dump//common.c 2002/09/04 20:31:34 1.70
--- src/bin/pg_dump//common.c 2002/09/28 17:43:47
***************
*** 284,299 ****
if (numParents == 0)
continue; /* nothing to see here, move along */
! /*
* For each attr, check the parent info: if no parent has an attr
* with the same name, then it's not inherited. If there *is* an
* attr with the same name, then only dump it if:
*
! * - it is NOT NULL and zero parents are NOT NULL OR - it has a
! * default value AND the default value does not match all parent
! * default values, or no parents specify a default.
*
* See discussion on -hackers around 2-Apr-2001.
*/
for (j = 0; j < tblinfo[i].numatts; j++)
{
--- 284,301 ----
if (numParents == 0)
continue; /* nothing to see here, move along */
! /*----------------------------------------------------------------
* For each attr, check the parent info: if no parent has an attr
* with the same name, then it's not inherited. If there *is* an
* attr with the same name, then only dump it if:
*
! * - it is NOT NULL and zero parents are NOT NULL
! * OR
! * - it has a default value AND the default value does not match
! * all parent default values, or no parents specify a default.
*
* See discussion on -hackers around 2-Apr-2001.
+ *----------------------------------------------------------------
*/
for (j = 0; j < tblinfo[i].numatts; j++)
{
***************
*** 358,363 ****
--- 360,371 ----
{
tblinfo[i].inhAttrs[j] = false;
tblinfo[i].inhNotNull[j] = false;
+ }
+
+ /* Clear it if attr has local definition */
+ if (g_fout->remoteVersion >= 70300 && tblinfo[i].attislocal[j])
+ {
+ tblinfo[i].inhAttrs[j] = false;
}
}
}
Index: src/bin/pg_dump//pg_dump.c
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/bin/pg_dump/pg_dump.c,v
retrieving revision 1.301
diff -c -r1.301 pg_dump.c
*** src/bin/pg_dump//pg_dump.c 2002/09/24 23:14:25 1.301
--- src/bin/pg_dump//pg_dump.c 2002/09/28 17:44:03
***************
*** 2356,2361 ****
--- 2356,2362 ----
int i_attnotnull;
int i_atthasdef;
int i_attisdropped;
+ int i_attislocal;
PGresult *res;
int ntups;
bool hasdefaults;
***************
*** 2397,2403 ****
if (g_fout->remoteVersion >= 70300)
{
appendPQExpBuffer(q, "SELECT attnum, attname, atttypmod, attstattarget, "
! "attnotnull, atthasdef, attisdropped, "
"pg_catalog.format_type(atttypid,atttypmod) as atttypname "
"from pg_catalog.pg_attribute a "
"where attrelid = '%s'::pg_catalog.oid "
--- 2398,2404 ----
if (g_fout->remoteVersion >= 70300)
{
appendPQExpBuffer(q, "SELECT attnum, attname, atttypmod, attstattarget, "
! "attnotnull, atthasdef, attisdropped, attislocal, "
"pg_catalog.format_type(atttypid,atttypmod) as atttypname "
"from pg_catalog.pg_attribute a "
"where attrelid = '%s'::pg_catalog.oid "
***************
*** 2413,2419 ****
* been explicitly set or was just a default.
*/
appendPQExpBuffer(q, "SELECT attnum, attname, atttypmod, -1 as attstattarget, "
! "attnotnull, atthasdef, false as attisdropped, "
"format_type(atttypid,atttypmod) as atttypname "
"from pg_attribute a "
"where attrelid = '%s'::oid "
--- 2414,2420 ----
* been explicitly set or was just a default.
*/
appendPQExpBuffer(q, "SELECT attnum, attname, atttypmod, -1 as attstattarget, "
! "attnotnull, atthasdef, false as attisdropped, null as attislocal, "
"format_type(atttypid,atttypmod) as atttypname "
"from pg_attribute a "
"where attrelid = '%s'::oid "
***************
*** 2425,2431 ****
{
/* format_type not available before 7.1 */
appendPQExpBuffer(q, "SELECT attnum, attname, atttypmod, -1 as attstattarget, "
! "attnotnull, atthasdef, false as attisdropped, "
"(select typname from pg_type where oid = atttypid) as atttypname "
"from pg_attribute a "
"where attrelid = '%s'::oid "
--- 2426,2432 ----
{
/* format_type not available before 7.1 */
appendPQExpBuffer(q, "SELECT attnum, attname, atttypmod, -1 as attstattarget, "
! "attnotnull, atthasdef, false as attisdropped, null as attislocal, "
"(select typname from pg_type where oid = atttypid) as atttypname "
"from pg_attribute a "
"where attrelid = '%s'::oid "
***************
*** 2451,2456 ****
--- 2452,2458 ----
i_attnotnull = PQfnumber(res, "attnotnull");
i_atthasdef = PQfnumber(res, "atthasdef");
i_attisdropped = PQfnumber(res, "attisdropped");
+ i_attislocal = PQfnumber(res, "attislocal");
tbinfo->numatts = ntups;
tbinfo->attnames = (char **) malloc(ntups * sizeof(char *));
***************
*** 2458,2463 ****
--- 2460,2466 ----
tbinfo->atttypmod = (int *) malloc(ntups * sizeof(int));
tbinfo->attstattarget = (int *) malloc(ntups * sizeof(int));
tbinfo->attisdropped = (bool *) malloc(ntups * sizeof(bool));
+ tbinfo->attislocal = (bool *) malloc(ntups * sizeof(bool));
tbinfo->attisserial = (bool *) malloc(ntups * sizeof(bool));
tbinfo->notnull = (bool *) malloc(ntups * sizeof(bool));
tbinfo->adef_expr = (char **) malloc(ntups * sizeof(char *));
***************
*** 2473,2478 ****
--- 2476,2482 ----
tbinfo->atttypmod[j] = atoi(PQgetvalue(res, j, i_atttypmod));
tbinfo->attstattarget[j] = atoi(PQgetvalue(res, j, i_attstattarget));
tbinfo->attisdropped[j] = (PQgetvalue(res, j, i_attisdropped)[0] == 't');
+ tbinfo->attislocal[j] = (PQgetvalue(res, j, i_attislocal)[0] == 't');
tbinfo->attisserial[j] = false; /* fix below */
tbinfo->notnull[j] = (PQgetvalue(res, j, i_attnotnull)[0] == 't');
tbinfo->adef_expr[j] = NULL; /* fix below */
Index: src/bin/pg_dump//pg_dump.h
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/bin/pg_dump/pg_dump.h,v
retrieving revision 1.99
diff -c -r1.99 pg_dump.h
*** src/bin/pg_dump//pg_dump.h 2002/09/04 20:31:35 1.99
--- src/bin/pg_dump//pg_dump.h 2002/09/28 17:44:03
***************
*** 129,134 ****
--- 129,135 ----
int *atttypmod; /* type-specific type modifiers */
int *attstattarget; /* attribute statistics targets */
bool *attisdropped; /* true if attr is dropped; don't dump it */
+ bool *attislocal; /* true if attr has local definition */
bool *attisserial; /* true if attr is serial or bigserial */
/*
Your patch has been added to the PostgreSQL unapplied patches list at:
http://candle.pha.pa.us/cgi-bin/pgpatches
I will try to apply it within the next 48 hours.
---------------------------------------------------------------------------
Alvaro Herrera wrote:
En Wed, 25 Sep 2002 00:01:24 -0400
Tom Lane <tgl@sss.pgh.pa.us> escribi?:Alvaro Herrera <alvherre@atentus.com> writes:
I'm looking at pg_dump/common.c:flagInhAttrs() and suspect that it can
be more or less rewritten completely, and probably should to get rigth
all the cases mentioned in the past attisinherited discussion. Is this
desirable for 7.3? It can probably be hacked around and the rewrite
kept for 7.4, but I think it will be much simpler after the rewrite.If it's a bug then it's fair game to fix in 7.3. But keep in mind that
pg_dump has to behave at least somewhat sanely when called against older
servers ... will your rewrite behave reasonably if the server does not
offer attinhcount values?Nah. I don't think it's worth it: I had forgotten that older versions
should be supported. I just left the code as is and added a
version-specific test.This patch allows pg_dump to dump correctly local definition of columns.
In particular,CREATE TABLE p1 (f1 int, f2 int);
CREATE TABLE p2 (f1 int);
CREATE TABLE c () INHERITS (p1, p2);
ALTER TABLE ONLY p1 DROP COLUMN f1;
CREATE TABLE p3 (f1 int);
CREATE TABLE c2 (f1 int) INHERITS (p3);Will be dumped as
CREATE TABLE p1 (f2 int);
CREATE TABLE p2 (f1 int);
CREATE TABLE c (f1 int) INHERITS (p1, p2);
CREATE TABLE c2 (f1 int) INHERITS (p3);(Previous version will dump
CREATE TABLE c () INHERITS (p1, p2)
CREATE TABLE c2 () INHERITS (p3) )--
Alvaro Herrera (<alvherre[a]atentus.com>)
A male gynecologist is like an auto mechanic who never owned a car.
- Carrie Snow
[ Attachment, skipping... ]
---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?
--
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.
---------------------------------------------------------------------------
Alvaro Herrera wrote:
En Wed, 25 Sep 2002 00:01:24 -0400
Tom Lane <tgl@sss.pgh.pa.us> escribi?:Alvaro Herrera <alvherre@atentus.com> writes:
I'm looking at pg_dump/common.c:flagInhAttrs() and suspect that it can
be more or less rewritten completely, and probably should to get rigth
all the cases mentioned in the past attisinherited discussion. Is this
desirable for 7.3? It can probably be hacked around and the rewrite
kept for 7.4, but I think it will be much simpler after the rewrite.If it's a bug then it's fair game to fix in 7.3. But keep in mind that
pg_dump has to behave at least somewhat sanely when called against older
servers ... will your rewrite behave reasonably if the server does not
offer attinhcount values?Nah. I don't think it's worth it: I had forgotten that older versions
should be supported. I just left the code as is and added a
version-specific test.This patch allows pg_dump to dump correctly local definition of columns.
In particular,CREATE TABLE p1 (f1 int, f2 int);
CREATE TABLE p2 (f1 int);
CREATE TABLE c () INHERITS (p1, p2);
ALTER TABLE ONLY p1 DROP COLUMN f1;
CREATE TABLE p3 (f1 int);
CREATE TABLE c2 (f1 int) INHERITS (p3);Will be dumped as
CREATE TABLE p1 (f2 int);
CREATE TABLE p2 (f1 int);
CREATE TABLE c (f1 int) INHERITS (p1, p2);
CREATE TABLE c2 (f1 int) INHERITS (p3);(Previous version will dump
CREATE TABLE c () INHERITS (p1, p2)
CREATE TABLE c2 () INHERITS (p3) )--
Alvaro Herrera (<alvherre[a]atentus.com>)
A male gynecologist is like an auto mechanic who never owned a car.
- Carrie Snow
[ Attachment, skipping... ]
---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?
--
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