Postgres sequence problem after DB restore - Help please
Moderators: gto, cipto_kh, EgonHugeist
Postgres sequence problem after DB restore - Help please
Hi all,
Using Delphi6 & Postgres with zeos 6.1.5 - and have not had any problems like this before.
My app is very dependant of schemas in the DB and also use serials columns for PK's:
CREATE TABLE sch1.tab1( tabid SERIAL PRIMARY KEY, tabdesc TEXT );
which implies:
CREATE SEQUENCE sch1.tab1_tabid_seq;
My app makes use of TZQuery's APPEND, EDIT, POST methods to insert/update data on the tables - this have worked without any issues in the past.
However, recently I'm encountering errors on the POST method (after APPEND) to the extent of:
1) SQL Error: ERROR: syntax error at or near "sch1" at character 18....
2) SQL Error: ERROR: relation "tab1_tabid_seq" does not exist.
I traced the error: it seems to occur when trying to get the next value in the sequence - seems that the schema is not recognized or taken into account.
This is confirmed: when dropping the SERIAL column and adding it, the error does not re-occur.
When doing the INSERT from other Postgres clients, the data is inserted without problems.
Something else I can add: seems that this is occurring at sites where the database has been restored recently.
Some variables which I can think to contribute this behaviour to:
-recent updates to Postgres 8.3,
-recent updates to Windows 7 (??)
-recent changes in the way Postgres's backup/restore tools handle linking between SERIAL column and a SEQUENCE
Hope somebody can help me out.
P.S. I understand some work with Postgres serials (and sequences) have been revamped in zeos 6.5.1+ and some may suggest I upgrade. The way in which sequences is implemented in newer versions of ZEOS, however, makes it unfeasible for me to redo all my code.
Using Delphi6 & Postgres with zeos 6.1.5 - and have not had any problems like this before.
My app is very dependant of schemas in the DB and also use serials columns for PK's:
CREATE TABLE sch1.tab1( tabid SERIAL PRIMARY KEY, tabdesc TEXT );
which implies:
CREATE SEQUENCE sch1.tab1_tabid_seq;
My app makes use of TZQuery's APPEND, EDIT, POST methods to insert/update data on the tables - this have worked without any issues in the past.
However, recently I'm encountering errors on the POST method (after APPEND) to the extent of:
1) SQL Error: ERROR: syntax error at or near "sch1" at character 18....
2) SQL Error: ERROR: relation "tab1_tabid_seq" does not exist.
I traced the error: it seems to occur when trying to get the next value in the sequence - seems that the schema is not recognized or taken into account.
This is confirmed: when dropping the SERIAL column and adding it, the error does not re-occur.
When doing the INSERT from other Postgres clients, the data is inserted without problems.
Something else I can add: seems that this is occurring at sites where the database has been restored recently.
Some variables which I can think to contribute this behaviour to:
-recent updates to Postgres 8.3,
-recent updates to Windows 7 (??)
-recent changes in the way Postgres's backup/restore tools handle linking between SERIAL column and a SEQUENCE
Hope somebody can help me out.
P.S. I understand some work with Postgres serials (and sequences) have been revamped in zeos 6.5.1+ and some may suggest I upgrade. The way in which sequences is implemented in newer versions of ZEOS, however, makes it unfeasible for me to redo all my code.
- mdaems
- Zeos Project Manager
- Posts: 2766
- Joined: 20.09.2005, 15:28
- Location: Brussels, Belgium
- Contact:
Seems like you are not having a zeoslib 6.1.5 problem, but you're trying to do some important steps in 1 go without knowing what's changing.
If you just want to restore a database : just restore it on exactly the same database as the original one. Then your zeoslib 6.1.5 program shoud work as expected. Next step can be upgrading the db or zeoslib. But just one step at a time.
Mark
If you just want to restore a database : just restore it on exactly the same database as the original one. Then your zeoslib 6.1.5 program shoud work as expected. Next step can be upgrading the db or zeoslib. But just one step at a time.
Mark
-
- Expert Boarder
- Posts: 164
- Joined: 18.03.2008, 13:03
- Contact:
Hello, gmb,
If I remember correctly, there was a problem with serial collumns once.. In ZDbcPostgreSqlMetadata the expresion pg_get_expr(def.adbin, def.adrelid) should be used instead of adsrc when finding default values for fields. The problem is pg_restore does not restore adsrc correctly. It loses the schema. When I wrote about the problem to postgresql mailing list I was told that I should not use adsrc. I doubt they fixed the backup/restore mechanism. Instead the patch was applied to ZDbcPostgreSqlMetadata. I'm not sure what vertion that was in, but it has been a while...
Good luck!
If I remember correctly, there was a problem with serial collumns once.. In ZDbcPostgreSqlMetadata the expresion pg_get_expr(def.adbin, def.adrelid) should be used instead of adsrc when finding default values for fields. The problem is pg_restore does not restore adsrc correctly. It loses the schema. When I wrote about the problem to postgresql mailing list I was told that I should not use adsrc. I doubt they fixed the backup/restore mechanism. Instead the patch was applied to ZDbcPostgreSqlMetadata. I'm not sure what vertion that was in, but it has been a while...
Good luck!
THanks for the replies.
Will changing the ZConnection.Protocol have any impact?
Currently I'm using postgresql-7.3 (for no specific reason - never had any problems so did not think necessary to change it)
although I'm running Postgres 8.3 at most sites (the rest should be PG 8.2).
Hi Wild_Pointer, what you are saying here sounds exactly like my problem. Strange that is only occurring now. Maybe related to changes in pg_dump/pg_restore?It loses the schema
Will changing the ZConnection.Protocol have any impact?
Currently I'm using postgresql-7.3 (for no specific reason - never had any problems so did not think necessary to change it)
although I'm running Postgres 8.3 at most sites (the rest should be PG 8.2).
-
- Expert Boarder
- Posts: 164
- Joined: 18.03.2008, 13:03
- Contact:
gmb,
You have 2 options:
1) update zeos components. (that would be the right thing to do). We are now using zeos 7 in our system without problems.
2) have a sql scripts to update pg_attribute.adsrc after restoring the DB (not acceptable in a long run as you would have to have different scripts for different versions ).
Good luck!
You are truly right.. Have you been doing backup/restore successfully in past? I can't say since when backup/restore cuts the schema off, but I faced the problem more than 2 years ago.Strange that is only occurring now.
Changing the protocol will not help.Will changing the ZConnection.Protocol have any impact?
You have 2 options:
1) update zeos components. (that would be the right thing to do). We are now using zeos 7 in our system without problems.
2) have a sql scripts to update pg_attribute.adsrc after restoring the DB (not acceptable in a long run as you would have to have different scripts for different versions ).
Good luck!
As you say this will be the most "correct" way of solving the problem and probably something I'll have to take a look at in the long run.1) update zeos components. (that would be the right thing to do). We are now using zeos 7 in our system without problems.
The introduction of TZSequence in versions after 6.1.5, however, implies a major overhaul of my code - something I just do not have the time for now.
Will have to happen sooner than later, though.
Any ideas on how this script will look?2) have a sql scripts to update pg_attribute.adsrc after restoring the DB (not acceptable in a long run as you would have to have different scripts for different versions ).
Something else:
I did take a look at ZDbcPostgreSqlMetadata.pas: found that pg_get_expr was only introduced in Zeos 6.6.6;
I do however not get the same type of error with my applications running on zeos 6.5.1. Can this be because of the different way 6.5.1 handles serials (TZSequence)?
BTW After making the necessary changes to the ZDbcPostgreSqlMetadata source (6.1.5) and recompiling it seems to have made the necessary difference.
If all else fails this will have to be my short-term solution
Thanks again for your help
-
- Expert Boarder
- Posts: 164
- Joined: 18.03.2008, 13:03
- Contact:
gmb,
Well, the script can be very simple update pg_attrdef set adsrc = pg_get_expr(def.adbin, def.adrelid); Anyway - I have never done so and because only God knows who uses adsrc and for what reasons I would discourage you from updating it (without serious testing at least).
Well, the script can be very simple update pg_attrdef set adsrc = pg_get_expr(def.adbin, def.adrelid); Anyway - I have never done so and because only God knows who uses adsrc and for what reasons I would discourage you from updating it (without serious testing at least).
I use sequences in every table for pseudo key (id) as this saves me alot of time when updating records. I have never used TZSequence yet, so I guess you have nothing to worry about...The introduction of TZSequence in versions after 6.1.5, however, implies a major overhaul of my code - something I just do not have the time for now
Yes. This is the third solution to your problem I guess...BTW After making the necessary changes to the ZDbcPostgreSqlMetadata source (6.1.5) and recompiling it seems to have made the necessary difference
Wild_Pointer
Thanks , I'll probably stick with the 3rd option for now.
Using zeos 6.5.1, when I run the following without adding TZSequence and setting ZQuery1's Sequence and Sequencefield properties accordingly, I'll get error: Field 'tabid' must have a value.
Is there another way of doing this where TZSequence is not necessary? That would make upgrading a whole lot easier
Thanks , I'll probably stick with the 3rd option for now.
Sorry if this is a stupid question, but how do you manage this?The introduction of TZSequence in versions after 6.1.5, however, implies a major overhaul of my code - something I just do not have the time for nowI have never used TZSequence yet
Using zeos 6.5.1, when I run the following without adding TZSequence and setting ZQuery1's Sequence and Sequencefield properties accordingly, I'll get error: Field 'tabid' must have a value.
Code: Select all
zquery1.sql.text := 'SELECT * FROM sch1.tab1';
zquery1.append;
zquery1.FieldByName('tabdesc').ASString:='foo';
zquery1.post;
-
- Expert Boarder
- Posts: 164
- Joined: 18.03.2008, 13:03
- Contact: