Can someone explain the functionality of ActiveRecord postgres pg_type? - postgresql

https://github.com/rails/rails/blob/master/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
Can anyone explain regarding pg_type in Postgres? As I cannot find types in other database connection adaptors like MySQL and SQLite, what is its functionality and features it provides?

PostgreSQL has a rich set of native data types available to users.
Users can add new types to PostgreSQL using the CREATE TYPE command or new domains using CREATE DOMAIN.
Also, when you create a table or a view, the corresponding composite type with the same name is automatically created.
Each database may have a different set of defined types. Information of all types and domains known in a database is stored in the system catalog pg_type.

The postgres catalog table pg_type contains information about all data types available in your database. That includes built-in datatypes like bool and text, extension datatypes like hstore, and custom datatypes that are the result of using CREATE TYPE.
There's more information available in the postgres documentation for that table, if you're interested. For most uses of the database, you don't need to access pg_type, but it can be useful. In this case, ActiveRecord is, among other things, querying pg_type to pull accurate information about the types of each column in a user-created table.

Related

Is it possible to build a custom tree structured warehouse system with LDAP?

My company is building a factory warehouse managing system with springboot and spring-data-ldap. The old system only supports warehouse with 3 tiers like this:
warehouse (contains) storagearea(s) (contains) storagelocation(s)
This kind of structure is way too strict for customers since their warehouses are uniquely different. We are thinking of building a system that users are able to config their own warehouse tiers without we hard coding the relations in java.
Now I am migrating mysql table schema of warehouse, storagearea and storagelocation, into LDAP. But it seems we have to define new objectClasses in ApacheDS since the attributes are not defined in OpenLdap.
Creating a new attribute type requires an OID, which is a unique number that has to be acquired by applying online from some website.
Is it possible to build a system like this?
Is there any way of obtaining OID or randomly create one?
That is possible. You need to aquire an OID from the IANA (Here is the link to the form: http://pen.iana.org/pen/PenApplication.page) and then you can create your own LDAP Schema entities.
And as long as the customer can create their tree with your schema entries there is nothing stopping you. And when you need to refactor your schema: do it.
You can find more on OIDs at https://ldapwiki.com/wiki/OID or o wikipedia.

db2look from SQL

Is it possible to get the table structure like db2look from SQL?
Or the only way is from command line? Thus, by wrapping a external stored procedure in C I could call the db2look, but that is not what I am looking for.
Clarification added later:
I want to know which tables have the non logged option from SQL.
It is possible to create the table structure from regular SQL and the public DB2 catalog - however, it is complex and requires some deeper skills.
The metadata is available in the DB2 catalog views in the SYSCAT schema. For a regular table you would first start off by looking into the values in SYSCAT.TABLES and SYSCAT.COLUMNS. From there you would need to branch off to other views depending on what table and column options you are after, whether time-travel tables, special partitioning rules, or many other options are involved.
Serge Rielau published an article on developerWorks called Backup and restore SQL schemas for DB2 Universal Database that provides a set of stored procedures that will do exactly what you're looking for.
The article is quite old (2006) so you may need to put some time in to update the procedures to be able to handle features that were added to DB2 since the date of publication, but the procedures may work for you now and are a nice jumping off point.

Query Tables for STRPDM columns

I'm new to DB2. Is there a way to query the tables to it will display the same data that STRPDM displays for a printed report (i.e.): Member, Type, Creation Date, Last Changed Date, Time, Records, Deleted Records, Text?
I'm not a DBA, but I have access to the SYSIBM schema (since I'm on the data team). I poked around the site and couldn't find a similar question.
Most of what you want is available in SYSPARTITIONSTAT. The way that source members are stored is unique to IBM i, and they aren't really a DB2 construct as such, although DB2 for i does keep limited information about source members modelled as partitions.
The IBM i way to get access to that information is through the RTVMBRD CL command or equivalent API. You can also see that via the DSPFD TYPE(*MBRLIST) command, which you can dump to a file via the OUTFILE() parameter and then query via SQL...

Migrating a schema from one database to other

As part of some requirement, I need to migrate a schema from some existing database to a new schema in a different database. Some part of it is already done and now I need to compare the 2 schema and make changes in the new schema as per gap finding.
I am not using a tool and was trying to understand some details using syscat command but could not get much success.
Any pointer on what is the best way to solve this?
Regards,
Ramakant
A tool really is the best way to solve this – IBM Data Studio is free and can compare schemas between databases.
Assuming you are using DB2 for Linux/UNIX/Windows, you can do a rudimentary compare by looking at selected columns in SYSCAT.TABLES and SYSCAT.COLUMNS (for table definitions), and SYSCAT.INDEXES (for indexes). Exporting this data to files and using diff may be the easiest method. However, doing this for more complex structures (tables with range or database partitioning, foreign keys, etc) will become very complex very quickly as this information is spread across a lot of different system catalog tables.
An alternative method would be to extract DDL using the db2look utility. However, you can't specify the order that db2look outputs objects (db2look extracts DDL based on the objects' CREATE_TIME), so you can't extract DDL for an entire schema into a file and expect to use diff to compare. You would need to extract DDL into a separate file for each table.
Use SchemaCrawler for IBM DB2, a free open-source tool that is designed to produce text output that is designed to be diffed. You can get very detailed information about your schema, including view and stored procedure definitions. All of the information that you need will be output in a single file, and can be compared very easily using a standard diff tool.
Sualeh Fatehi, SchemaCrawler
unfortunately as per company policy, cannot use these tools at this point of time. So am writing some program using JDBC to get the details and do some comparison kind of stuff.

DB2 external tables?

I just heard that Oracle has a feature called External Table that allows to access a flat file (for example a CSV file in the file system) from the database.
I just want to know if there is something similar in DB2 for LUW.
The closest thing I could see is to implement a Table function (written in Java, for example) that will read the file, and return a table with the data from the file. However, this procedure takes a long time (create the Java code, compile the Java and create the function in DB2 associating the Java class) and the implementation is not dynamic for different files with different quantity of columns (table function returns a predefined set of columns).
Here the documentation of Oracle External Tables: http://docs.oracle.com/cd/B28359_01/server.111/b28319/et_concepts.htm
Yes, IBM offers this as part of their InfoSphere Federation Server, which basically allows you to define nicknames inside a database to various data sources. Supported data sources
IBM Db2 11.5 has support for external tables that will allow you to do this.
This was formerly provided only by Netezza and this functionality has made its way to Db2.
See the manual page for CREATE EXTERNAL TABLE here https://www.ibm.com/support/knowledgecenter/en/SSEPGG_11.5.0/com.ibm.db2.luw.sql.ref.doc/doc/r_create_ext_table.html
As mentioned, InfoSphere Federation Server is a good choice. There are two alternatives for DB2 UDB (Universal Database), which may be helpful in specific use cases:
DataLinks: it is basically another data type
that keeps a reference to your external file. It also provides
several levels of control over external data such as referential
integrity, access control, coordinated backup and recovery, and
transaction consistency.
DB2 Extenders: they extend functionality of the DB2 to operate on specific file formats, e.g. XML Extender provide set of features to operate on XML files inside DB2
There is also:
(a) external table support in the warehousing engine products (Db2 Warehouse, Db2 Warehouse on Cloud) (b) Data virtualization (aka federation/fluid query) in all Db2 products which may achieve the same thing.