where are indexes created in AGE extension on Postgres since normally indexes are created on every column where we create index. But in case of AGE since its graph-based so there are nodes and edges so how does indexes work in this scenario ??
Since nodes are different from columns so how does indexes work on them ??
I'm trying to understand this query as how does indexes work in apache-age !
With postgres running, you can see a list of relations of your current database by typing the command \d.
ag_catalog.ag_graph and ag_catalog.ag_label are both tables which contain the data of your grpahs and labels. With the commands \d ag_graph and \d ag_label, it will show the column names, their type, if they are nullable and their indexes. Note that the indexes are based on the column names.
Choose one of the tables that appear with the command SELECT * FROM ag_catalog.ag_label;. After this, do the \d command followed by the label's table name (e.g. \d schema_name."label_name"). You'll see that there are only two columns: id and properties
You could create an index on one of these columns, but I don't think you can do this with one of the properties of the node.
In AGE the initial schema is 'ag_catalog'. For every new graph a new schema is formed. To see the general indexes we can do:
SELECT schemaname, indexname, tablename
FROM pg_indexes
WHERE schemaname = 'ag_catalog'
ORDER BY indexname;
For a particular graph you can also see the indexes by just changing
WHERE schemaname = '<graph_name>'
Related
In some of the migrations, I have indexes created without explicit name set. Which usually provoke the automatic (by PostgreSQL engine) index name generation.
I'm looking a way to drop indexes by specific field names instead of index name.
There seems to be a way to get the list of indexes:
select *
from pg_indexes
where tablename = 'myTable'
Any recommendations for elegant way to "programmatically" DROP INDEX having only specific field or multiple field names?
I have create a redshift cluster and created a db inside.
My schema is new_schema
I have created 2 tables inside two tables inside table1, table2
My Question.
I want to list the datatypes of table1
I need to change the datatype of description which is inside the table1 which is of VARCHAR to TEXT
I have tried to list the datatypes of table1 with below query but nothing listing
SELECT * FROM PG_TABLE_DEF WHERE schemaname = 'new_schema';
A few possibilities as to why you are not seeing the expected results. Most likely is that new_schema isn't in your search_path. Pg_table_info only return info for tables in your search_path - see: https://docs.aws.amazon.com/redshift/latest/dg/r_PG_TABLE_DEF.html
Another possibility is that the tables have no data rows (no blocks assigned) and this can lead to incomplete info from some system tables.
Another possibility is that the tables were not committed by the creating session and being checked by a different session. Since you say that you are creating a new db this comes to mind.
Are the tables visible in svv_table_info?
Also the premise of changing varchar to text is a bit off. From https://docs.aws.amazon.com/redshift/latest/dg/r_Character_types.html#r_Character_types-text-and-bpchar-types
You can create an Amazon Redshift table with a TEXT column, but it is
converted to a VARCHAR(256) column that accepts variable-length values
with a maximum of 256 characters.
So it seems like the objective you are trying to achieve is a bit off.
In a PostgreSQL db I'm working on, half of the tables have one particular column, always named the same, that is of type varchar(5). The size became a bit too restricting and I want to change it to varchar(10).
The number of tables in my particular case is actually very manageable to do it by hand. But I was wondering how one could script this with a query for larger dbs. It generally should be possible in just a few steps.
Identify all the tables in the schema, then (?) filter by condition if column present.
Create ALTER TABLE statements for each table found
I have some idea about how to write a query that identifies all tables in the schema. But I wouldn't know how to filter them. And if I didn't filter them, I assume the generated alter table statements would break.
Would be great if someone could share their knowledge on this.
Thanks to Abelisto for providing some guidance. Eventually, this is how I did it.
First, I created a query that in turn creates the ALTER TABLE statements. MyDB and MyColumn need to reflect actual values.
SELECT
'ALTER TABLE '||columns.table_name||' ALTER COLUMN '||MyColumn||' TYPE varchar(20);'
FROM
information_schema.columns
WHERE
columns.table_catalog = 'MyDB' AND
columns.table_schema = 'public' AND
columns.column_name = 'MyColumn';
Then it was just a matter of executing the output as a new query. All done.
In Microsoft SQL Server and MySQL, index names need to unique within the table, but not within the database. This doesn't seem to be the case for PostgreSQL.
Here's what I'm doing: I made a copy of a table using CREATE TABLE new_table AS SELECT * FROM old_table etc and need to re-create the indexes.
Running a query like CREATE INDEX idx_column_name ON new_table USING GIST(column_name) causes ERROR: relation "idx_column_name" already exists
What's going on here?
Indexes and tables (and views, and sequences, and...) are stored in the pg_class catalog, and they're unique per schema due to a unique key on it:
# \d pg_class
Table "pg_catalog.pg_class"
Column | Type | Modifiers
----------------+-----------+-----------
relname | name | not null
relnamespace | oid | not null
...
Indexes:
"pg_class_oid_index" UNIQUE, btree (oid)
"pg_class_relname_nsp_index" UNIQUE, btree (relname, relnamespace)
Per #wildplasser's comment, you can omit the name when creating the index, and PG will assign a unique name automatically.
Names are unique within the schema. A schema is basically a namespace for {tables,constraints}, (and indexes, functions,etc).
cross-schema-constraints are allowed
Indexes share their namespace ( :=schema) with tables. (for Postgres: an index is a table).
(IIRC) the SQL standard does not define indexes; use constraints whenever you can (The GIST index in the question is probably an exception)
Ergo You'll need to invent another name.
or omit it: the system can invent a name if you dont supply one.
The downside of this: you can create multipe indices with the same definition (their names will be suffixed with _1, _2, IIRC)
I tried getting the number of tables of a particular tablespace and database from SYSIBM.SYSTABLES using the select query. This number is more than the number of tables for the same tablespace and database stored in the SYSIBM.SYSTABLESPACE table under the NTABLES column. Why is this so?
It could be the fact that systables stores entries for each table, view or alias, in fact a large number of objects that may not necessarily be included within a tablespace.
You could confirm this by only listing those where type = 'T' (or some other combination of the allowed values).
If you select count(*) from systables (for a given tablespace) and group it by type, you may find it reasonably easy to assign some of those types to the tablespace.