Firebird Gfix
Firebird Gfix
Firebird Gfix
Norman Dunbar
09 April 2013 Document version 1.4
Table of Contents
Introduction ........................................................................................................................................... 3
Command Line Options ......................................................................................................................... 3
Gfix Commands ..................................................................................................................................... 4
Shadow Files ......................................................................................................................................... 6
Activating Shadows ........................................................................................................................ 6
Killing Shadows ............................................................................................................................. 7
Set Database Page Buffers ...................................................................................................................... 8
Limbo Transaction Management ............................................................................................................. 9
Listing Limbo Transactions ............................................................................................................ 9
Committing Or Rolling Back ........................................................................................................ 10
Automatic Two-phase Recovery .................................................................................................... 11
Cache Manager .................................................................................................................................... 11
Changing The Database Mode .............................................................................................................. 12
Setting The Database Dialect ................................................................................................................ 13
Database Housekeeping And Garbage Collection ................................................................................... 15
Garbage ....................................................................................................................................... 15
Setting Sweep Interval .................................................................................................................. 16
Manual Garbage Collection .......................................................................................................... 17
Disabling Automatic Sweeping ..................................................................................................... 17
Database Startup and Shutdown ............................................................................................................ 18
Database Shutdown ...................................................................................................................... 18
Starting a Database ...................................................................................................................... 19
New Startup and Shutdown States in Firebird 2.0 .......................................................................... 20
Database Page Space Utilisation ........................................................................................................... 21
Database Validation and Recovery ........................................................................................................ 22
Database Validation ..................................................................................................................... 22
Database Recovery ....................................................................................................................... 23
Database Write Mode ........................................................................................................................... 24
Version Number ................................................................................................................................... 25
Caveats ................................................................................................................................................ 25
Shadows ...................................................................................................................................... 25
Response Codes Are Usually Zero ................................................................................................ 25
Force Closing a Database ............................................................................................................. 26
Limbo Transactions ...................................................................................................................... 26
Appendix A: Document history ............................................................................................................ 28
Appendix B: License notice .................................................................................................................. 29
Introduction
Gfix allows attempts to fix corrupted databases, starting and stopping of databases, resolving 'in limbo' transactions between multiple databases, changing the number of page buffers and so on. Gfix is a general purpose tool
for system administrators (and database owners) to use to make various 'system level' changes to their databases.
Almost all the gfix commands have the same format when typed on the command line:
gfix [commands and parameters] database_name
The commands and their options are described in the following sections. The database name is the name of the
primary database file which for a single file database is simply the database name and for multi-file databases,
it is the first data file added.
Coming up in the remainder of this manual, we will discuss the following:
Gfix Commands
Note
In the following discussion, I use the full parameter names in all examples. This is not necessary as each
command can be abbreviated. When the command is shown with '[' and ']' in the name then these are the
optional characters.
For example, the command -validate is shown as -v[alidate] and so can be specified as -v, -va, val and so on up to the full -validate version.
For almost all of the options in the following sections, two of the above command line options will be required.
These are -u[ser] and -pa[ssword]. These can be supplied for every command as parameters on the command line, or can be configured once in a pair of environment variables.
-?
This switch displays the command line options and switches. It replaces the old method in which you had to
supply an invalid switch (such as -help) in order to see the list of valid ones.
Note
Firebird 2.5 onwards.
-u[ser] username
Allows the username of the SYSDBA user, or the owner of the database to be specified This need not be
supplied if the ISC_USER environment variable has been defined and has the correct value.
-pa[ssword] password
Supplies the password for the username specified above. This need not be supplied if the ISC_PASSWORD
environment variable has been defined and have the correct value.
Note
Up until Firebird 2, any utility which was executed with a password on the command line could result in
other users of the server seeing that password using a command like ps -efx | grep -i pass. From Firebird
2 onwards, this is no longer the case as the password on the command line can no longer be seen by the
ps (or other) commands.
Alternatively, on Windows:
C:\> set ISC_USER=sysdba
C:\> set ISC_PASSWORD=masterkey
Warning
This is very insecure as it allows anyone who can access your session the ability to perform DBA functions
that you might not want to allow.
Note
The line that starts with 'Unable to perform' above, has had to be split to fit on the page of the PDF file. In
reality, it is a single line.
You will notice, hopefully, that some commands do not give any printed output at all. gfix, in the main, only
reports when problems are encountered. Always check the response code returned by gfix to be sure that it
5
Shadow Files
A shadow file is an additional copy of the primary database file(s). More than one shadow file may exist for any
given database and these may be activated and de-activated at will using the gfix utility.
The following descriptions of activating and de-activating shadow files assume that a shadow file already exists
for the database. To this end, a shadow was created as follows:
It can be seen that the database now has two separate shadow files created, but as they are manual, they have
not been activated. We can see that shadows are in use if we use gstat as follows:
linux> gstat -header my_employee | grep -i shadow
Shadow count 2
Note
Sometimes, it takes gstat a while to figure out that there are shadow files for the database.
Note
Shadow file details can be found in the RDB$FILES table within the database.
Activating Shadows
The command to activate a database shadow is:
gfix -ac[tivate] <shadow_file_name>
6
Once a shadow file has been activated, you can see the fact that there are active shadows in the output from gstat:
linux> gstat -header my_employee | grep -i shadow
Shadow count 2
Attributes
active shadow, multi-user maintenance
Note
The DBA can set up the database to automatically create a new shadow file in the event of a current shadow
being activated. This allows a continuous supply of shadow files and prevents the database ever running without
one.
Killing Shadows
The command to kill all unavailable database shadows, for a specific database, is:
gfix -k[ill] database_name
In the event that a database running with shadow files loses a shadow, or a shadow becomes unusable for some
reason, the database will stop accepting new connections until such time as the DBA kills the faulty shadow
and, ideally, creates a new shadow to replace the broken one.
The following (contrived) example, shows what happens when the database loses a shadow file and an attempt
is made to connect to that database. There are two sessions in the following example, one is connected to the
database while the second deletes a shadow file and then tries to connect to the database. The command line
prompts shows which of the two sessions we are using at the time.
First, the initial session is connected to the database and can see that there are two shadow files attached:
linux_1>isql my_employee
Database: my_employee
SQL> show database;
Database: my_employee
Owner: SYSDBA
Shadow 1: "/home/norman/firebird/shadow/my_employee.shd1" manual
Shadow 2: "/home/norman/firebird/shadow/my_employee.shd2" manual
...
In the second session, we delete one of the shadow files, and then try to connect to the database
linux_2> rm /home/norman/firebird/shadow/my_employee.shd2
The second session cannot connect to the database until the problem is fixed. The DBA would use the gfix k[ill] command to remove details of the problem shadow file from the database and once completed, the second
(and subsequent) sessions would be able to connect.
linux_2> gfix -kill my_employee
linux_2> isql my_employee
Database: my_employee
SQL> show database;
Database: my_employee
Owner: SYSDBA
Shadow 1: "/home/norman/firebird/shadow/my_employee.shd1" manual
...
The database now has a single shadow file where before it had two. It is noted, however, that gstat still shows
the database as having two shadows, even when one has been removed.
linux> gstat -header my_employee | grep -i shadow
Shadow count 2
Attributes
active shadow, multi-user maintenance
Note
In addition to the above strange result, if I subsequently DROP SHADOW 1 and COMMIT, to remove the remaining shadow file, gstat now shows that the shadow count has gone up to three when it should have gone
down to zero!
If the command is run against the remote database then nothing will be listed because that database does not
have any limbo transactions - the transaction that went into limbo, when the network failed, for example, was
initiated on the local database.
You may also supply the -p[rompt] option to the command and you will be prompted to COMMIT or ROLLBACK each detected limbo transaction. In this case, the command would be:
gfix -l[ist] -p[rompt] database_name
An example of this is shown below.
linux> gfix -list -prompt my_employee
Transaction 67 is in limbo.
Multidatabase transaction:
Host Site: linux
Transaction 67
has been prepared.
Remote Site: remote
Database path: /opt/firebird/examples/testlimbo.fdb
Commit, rollback or neither (c, r, or n)?
When committing or rolling back all limbo transactions, the -p[rompt] option can be specified. It is, however,
not permitted when processing a single transaction. An example of using the -p[rompt] option has been
shown above under listing limbo transactions.
Cache Manager
When the help page for gfix is displayed there is a message in the output for the -ca[che] option which states:
...
11
However, when called this option simply displays the help page again.
The question that immediately springs to my mind is, if we can shutdown the cache manager with this option,
how do we start it back up again?
Note
Only databases in dialect 3 can be changed to read only mode.
12
If there are any connections to the database in read/write mode when you attempt to convert the database to read
only, the attempt will fail as shown below with Firebird 1.5.
linux> gfix -mode read_only my_employee
lock time-out on wait transaction
-lock time-out on wait transaction
-object my_employee is in use
linux> echo $?
0
Warning
As with many failures of gfix, the response code returned to the operating system is zero.
Because you cannot use gstat remotely, you may also use the isql command SHOW SQL DIALECT from a remote
location to see which dialect your client and database are using, as follows:
remote> isql my_employee -user norman -password whatever
Database: my_employee
SQL> show sql dialect;
Client SQL dialect is set to: 3 and database SQL dialect is: 3
Although dialect 2 is possible on the client, trying to set a dialect of 2 will fail on the server as the following
example shows.
linux> gfix -sql_dialect 2 my_employee
Database dialect 2 is not a valid dialect.
-Valid database dialects are 1 and 3.
-Database dialect not changed.
To set dialect 2 for your client connection, you use isql as follows:
linux> isql my_employee
Database: my_employee
SQL> set sql dialect 2;
WARNING: Client SQL dialect has been set to 2 when connecting to Database SQL dialect 3 database.
SQL> show sql dialect;
Client SQL dialect is set to: 2 and database SQL dialect is: 3
14
Note
The WARNING line above has had to be split to fit on the page of the PDF version of this manual. In reality,
it is a single line of text.
In the Super Server version of Firebird 2.0, garbage collection has been vastly improved. There are now three
different ways of operation and these are configurable by setting the GCPOLICY parameter in the firebird.
15
Note
Classic Server ignores the setting and always uses cooperative garbage collection.
An interesting transaction is one which has not yet committed. It may be still active, in limbo or may have been
rolled back.
The sweep facility runs through the database and gets rid of old rows in tables that are out of date. This prevents
the database from growing too big and helps reduce the time it takes to start a new transaction on the database.
16
Note
If you find that starting a new transaction takes a long time, it may be a good idea to run a manual sweep of
the database in case the need for a sweep is causing the hold-up.
You can check if a manual sweep may be required by running the gstat utility to check the database header page
and extract the oldest and next transaction numbers from the output. If the gap is small (less than the sweep
interval) then a manual sweep may be in order. Alternatively, the SHOW DATABASE command in isql will also
show the details you need.
A manual sweep can be run by using the -s[weep] command. (See below).
To alter the database's automatic sweep interval, use the following command:
gfix -h[ousekeeping] INTERVAL database_name
The INTERVAL parameter is the new value for the sweep interval. The database name parameter is the database
upon which you wish to alter the setting for automatic sweeping. The following example shows the setting being
changed from the default to a new value of 1,000.
linux> gfix -h 1000 my_employee
linux> gstat -header my_employee | grep Sweep
Sweep interval:
1000
Database Shutdown
If there is maintenance work required on a database, you may wish to close down that database under certain circumstances. This is different from stopping the Firebird server as the server may well be running other databases
which you do not wish to affect.
The command to close a database is:
gfix -shut OPTION TIMEOUT database_name
The TIMEOUT parameter is the time, in seconds, that the shutdown must complete in. If the command cannot
complete in the specified time, the shutdown is aborted. There are various reasons why the shutdown may not
complete in the given time and these vary with the mode of the shutdown and are described below.
The OPTION parameter is one of the following:
-at[tach] - prevents new connections.
-tr[an] - prevents new transactions.
-f[orce] - simply aborts all connections and transactions.
When a database is closed, the SYSDBA or the database owner can still connect to perform maintenance operations or even query and update the database tables.
Note
If you specify a long time for the shutdown command to complete in, you can abort the shutdown by using the
-online command (see below) if the timeout period has not completed.
18
Connections in the database will still be able to start new transactions or complete old ones.
If any user connected to the database being shutdown with the -tr[an] tries to start a new transaction during
the shutdown timeout period, the following will result:
SQL> select * from test;
Statement failed, SQLCODE = -902
database /home/norman/firebird/my_employee.fdb shutdown in progress
Statement failed, SQLCODE = -902
database /home/norman/firebird/my_employee.fdb shutdown in progress
Statement failed, SQLCODE = -901
Dynamic SQL Error
-SQL error code = -901
-invalid transaction handle (expecting explicit transaction start)
Force Closure
-f[orce] : shuts down with no regard for the connection or transaction status of the database. No new connections or transactions are permitted and any active sessions are terminated along with any active transactions.
Anyone other than SYSDBA or the database owner trying to connect to the database during the timeout period
will not be able to connect successfully or start any (new) transactions.
Be nice to your users, use the -f[orce] option with great care.
Warning
There is a bug in Classic Server which still exists at version 2.0. The bug is such that the -f[orce] option
behaves in exactly the same way as the -at[tach] option.
Starting a Database
Once all maintenance work required on a database has been carried out, you need to restart the database to allow
normal use again. (See shutdown option above for details of closing a database.)
19
There is a hierarchy of states for a database. The above list shows them in order with normal at the top and
full at the bottom.
This hierarchy is important, you cannot shutdown a database to a higher or equal level that it currently is, nor
can you startup a database to a lower or equal level.
If you need to identify which level a database is currently running at, gstat will supply the answers. The following
example puts a database fully online then progressively shuts it down to fully offline. At each stage, gstat is run
to extract the Attributes of the database.
linux> gfix -online normal my_employee
linux> gstat -header my_employee | grep Attributes
Attributes
linux> gfix -shut multi -attach 0 my_employee
20
multi-user maintenance
single-user maintenance
full shutdown
linux>
The following example sets the page usage back to the default:
linux> gfix -use reserve my_employee
linux> gstat -header my_employee | grep Attributes
Attributes
If you are using full page utilisation then the Attributes show up with 'no reserve' in the text. This doesn't appear
for normal 80% utilisation mode.
21
When a database is validated the following checks are made and corrected by default:
Orphan pages are returned to free space. This updates the database.
Pages that have been misallocated are reported.
Corrupt data structures are reported.
There are options to perform further, more intensive, validation and these are discussed below.
Default Validation
The command to carry out default database validation is:
gfix -v[alidate] database_name
This command validates the database and makes updates to it when any orphan pages are found. An orphan page
is one which was allocated for use by a transaction that subsequently failed, for example, when the application
aborted. In this case, committed data is safe but uncommitted data will have been rolled back. The page appears
to have been allocated for use, but is unused.
This option updates the database and fixes any corrupted structures.
Full Validation
By default, validation works at page level. If no need to go deeper and validate at the record level as well, the
command to do this is:
gfix -v[alidate] -full database_name
22
Read-only Validation
As explained above, a validation of a database will actually validate and update the database structures to,
hopefully, return the database to a working state. However, you may not want this to happen and in this case, you
would perform a read only validation which simply reports any problem areas and does not make any changes
to the database.
To carry out a read only validation, simply supply the -n[o_update] option to whichever command line you
are using for the validation. To perform a full validation, at record and page level, but in reporting mode only,
use the following command:
gfix -v[alidate] -full -n[o_update] database_name
On the other hand, to stay at page level validation only, the command would be:
gfix -v[alidate] -n[o_update] database_name
Database Recovery
If the database validation described above produces no output then the database structures can be assumed to
be valid. However, in the event that errors are reported, you may have to repair the database before it can be
used again.
Warning
Cache flushing on Windows servers (up to but not including Vista - which has not been confirmed yet) is
unreliable. If you set the database to async mode (forced writes disabled) then it is possible that the cache
will never be flushed and data could be lost if the server is never shutdown tidily.
Warning
If your database was originally created with Interbase 6 or an early beta version of Firebird then the database
will be running in asynchronous mode - which is not ideal.
Version Number
The -z option to gfix simply prints out the version of the Firebird utility software that you are running. It takes
no parameters as the following example (running on Linux) shows.
linux> gfix -z
gfix version LI-V2.0.0.12748 Firebird 2.0
Caveats
This section summarises the various problems that you may encounter from time to time when using gfix. They
have already been discussed above, or mentioned in passing, but are explained in more details here.
Shadows
The gstat seems to take some time to respond to the addition of shadow files to a database. After adding two
shadows to a test database, gstat still showed that there was a Shadow count of zero.
Even worse, after killing the second shadow file and running the DROP SHADOW command in isql to remove
the one remaining shadow file, gstat decided that there were now three shadow files in use.
25
Note
As mentioned above, this is no longer a problem from release 2.1 RC1 onwards. The second attempt to close
the database will correctly return 1 to the shell.
Limbo Transactions
There are a couple of problems with limbo transactions as discovered by Paul in his testing.
26
In the above example, the original database my_employee.fdb was first of all copied using the operating system
command cp to my_new_employee.fdb and then renamed to my_old_employee.fdb.
Gfix was then run on the copy named my_new_employee.fdb and it noted the limbo transaction. However, it could
not find the original database file as it had been renamed, so gfix prompted for the path to the original database
file. When this was entered, gfix happily listed the details.
Warning
This implies that if you have a database with limbo transactions and you copy it using the operating system
utilities and subsequently run gfix against the new database, it is possible to have gfix fix limbo transactions
in the original database file and not in the one you think it is updating - the copy.
It is also a good warning about making copies of databases without using the correct tools for the job.
27
Appendix A:
Document history
The exact file history is recorded in the manual module in our CVS tree; see http://sourceforge.net/cvs/?group_
id=9028. The full URL of the CVS log for this file can be found at http://firebird.cvs.sourceforge.net/viewvc/
firebird/manual/src/docs/firebirddocs/fbutil_gfix.xml?view=log
Revision History
1.0
19 June 2007
ND
1.1
20 October
2009
ND
1.2
25 June 2010
ND
1.3
11 October
2011
ND
1.4
09 April 1013
ND
Updated to note that gfix returns correct error codes to the shell from
release 2.1 RC1 onwards.
28
Appendix B:
License notice
The contents of this Documentation are subject to the Public Documentation License Version 1.0 (the License); you may only use this Documentation if you comply with the terms of this License. Copies of the License are available at http://www.firebirdsql.org/pdfmanual/pdl.pdf (PDF) and http://www.firebirdsql.org/manual/pdl.html (HTML).
The Original Documentation is titled Firebird Database Housekeeping Utility.
The Initial Writer of the Original Documentation is: Norman Dunbar.
Copyright (C) 20072009. All Rights Reserved. Initial Writer contact: NormanDunbar at users dot sourceforge
dot net.
29