Restore or some recreate a tablespace to another new tablespace on the side or empty db with only - db2

On db2 LUW LInux: Is there a possibility to recreate tablespace somewhere aside pointing to a different tablespace name created only for the purpose of unloading data from the table. I'd like to be able to extract data or restore data from a damaged table so that you don't have to recreate the entire database, which usually takes a lot of disk space and time

You can't do it back into the original db, but you could create a separate db that contains just the tablespace with the data you're interested in:
db2 restore db foo rebuild with tablespace ( syscatspace, mytbsp )
db2 rollforward db foo to end of logs and stop
db2 export to mytable.del of del select * from mytable

Related

Create temporary table in db2

DECLARE GLOBAL TEMPORARY TABLE SESSION.TEMP_EMP (EMPNO CHAR(6));
I get error :
A table space could not be found with a page size of at least "4096" that authorization ID "A" is authorized to use.. SQLCODE=-286, SQLSTATE=42727, DRIVER=4.19.56
I use CLP to configure :
db2 connect to sss
db2 create bufferpool bp8k pagesize 8K
db2 create tablespace data pagesize 8K bufferpool bp8K
db2 terminate
I create above temporary table again , but still get error. I create temporary table from IBM data studio. The database sss is created by command create database sss without any more parameters. Are there any problems if i change tablespace and bufferpool. Because i dont want to change default parameters of database. It harms my database
Temporary tables need a user temporary tablespace (not a regular one you created)
Check out this docs about temporary tables
Here is how you can create a temporary tablespace

Can we change data directory for single table or database in postgresql

Can we change data directory for single table or database in postgresql.
Actually my requirement is that I want to keep all tables data in C drive but customers table data in D drive. how to achieve this?
You should create a tablespace for the tables outside the data directory.
For example:
CREATE TABLESPACE tbsp LOCATION 'D:\customer_tables';
Then add TABLESPACE tbsp to all CREATE TABLE statements that should be on D.

How can I determine whether a file in a PostgreSQL data directory is used in the database?

I have a situation in which summing the size of the tables in a tablespace (using pg_class among others) reveals that there is 550G of datafiles in a particular tablespace in a particular database.
However, there is 670G of files in that directory on the server.
FWIW, I don't know how that can be. No files have been written to that directory via any mechanism other than Postgres. My best guess is perhaps the database crashed while an autovacuum was going on, leaving orphan files laying around...does that sound plausible?)
SO, I've worked out a way, by reading the contents of a ls command into the database, strip off the numeric extensions for tables > 1G in size, and compare them with the contents of pg_class, and have, in fact, found about 120G of files not reflected in pg_class.
My question is, is it safe for me to delete these files, or could they be in active use by the database but not reflected in pg_class?
Do not manually delete files in the PostgreSQL data directory.
This is not safe and will corrupt your database.
The safe way to purge any files that don't belong to the database is to perform a pg_dumpall, stop the server, remove the data directory and the contents of all tablespace directories, breate a new cluster with inindb and restore the dump.
If you want to investigate the issue, you could try to create a new tablespace and move everything from the old to the new tablespace. I will describe that in the rest of my answer.
Move all the tables and indexes in all databases to the new tablespace:
ALTER TABLE ALL IN TABLESPACE oldtblsp SET TABLESPACE newtblsp;
ALTER INDEX ALL IN TABLESPACE oldtblsp SET TABLESPACE newtblsp;
If oldtblsp is the default tablespace of a database:
ALTER DATABASE mydb SET TABLESPACE newtblsp;
Then run a checkpoint:
CHECKPOINT;
Make sure you forgot no database:
SELECT datname
FROM pg_database d
JOIN pg_tablespace s
ON d.dattablespace = s.oid
WHERE s.spcname = 'oldtblsp';
Make sure that there are no objects in the old tablespace by running this query in all databases:
SELECT t.relname, t.relnamespace::regnamespace, t.relkind
FROM pg_class t
JOIN pg_tablespace s
ON t.reltablespace = s.oid
WHERE s.spcname = 'oldtblsp';
This should return no results.
Now the old tablespace should be empty and you can
DROP TABLESPACE oldtblsp;
If you really get an error
ERROR: tablespace "tblsp" is not empty
there might be some files left behind.
Delete them at your own risk...

How a Table space for a query execution is chosen? - DB2

For one of my issue, I am trying to understand the functionalities of below DB2 entities,
System Temporary table space.
Page Size.
Table Space.
Buffer Pool.
And below is my observation,
There is a TableSpace linked to a table in the DB2 table syscat.tables
The TableSpaces are linked to BufferPool with the relation defined in syscat.tablespaces
System Temporary table space is the table space that the DB might use while executing the query.
Page Size is an unit that defines the limit of a TableSpace and says how much data can a TableSpace can hold.
Is there something wrong in my above understandings? And when I excute a query how does the DB chooses which TableSpace to choose?

Where will the tablespace be stored?

I am creating a table with tablespace:
CREATE TABLE SALARY.....
IN ACCOUNTING INDEX IN ACCOUNT_IDX
Where will the Accounting and Account_IDX be created?
The script that you have above will create SALARY in the ACCOUNTING tablespace, with indexes for that table in ACCOUNT_IDX tablespace.
The ACCOUNTING and ACCOUNT_IDX tablespaces need to be created in a separate script that has CREATE TABLESPACE statements.
If you look at the syntax for CREATE TABLESPACE, the USING part of the statement will tell DB2 where to put the files for the tablespace.
DB2 Create Tablespace Reference