Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oAgsg-0005Wr-RP for pgsql-general@arkaria.postgresql.org; Mon, 11 Jul 2022 00:02:31 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oAgsf-0007n5-Gm for pgsql-general@arkaria.postgresql.org; Mon, 11 Jul 2022 00:02:29 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oAgse-0007mw-S1 for pgsql-general@lists.postgresql.org; Mon, 11 Jul 2022 00:02:29 +0000 Received: from mail-qk1-x729.google.com ([2607:f8b0:4864:20::729]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1oAgsc-0000kN-2z for pgsql-general@lists.postgresql.org; Mon, 11 Jul 2022 00:02:27 +0000 Received: by mail-qk1-x729.google.com with SMTP id z11so1362262qkz.13 for ; Sun, 10 Jul 2022 17:02:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:date:mime-version:user-agent:subject:content-language:to :cc:references:from:in-reply-to; bh=B1yLKHldtD7jPXtQp4Onon87W9jO78QmaaMHOU3CHtI=; b=Yqos3GkBaT8aZ5DzAwvuZ4v5qs0VWZ133stYtK018PVGS/Aos6TafhPhR3kajpzSAY AuqBNveG6BNRkBf6sMQQW3CMRmOgyIeTxEpIfYiie0e58YCj7GXH4G5KUyQH3NTERo8X dpjjVzPj5SRSStQIrhGLlDkAorxTvDzZHmBdOIPhJTP7QXphEKXooqKVq2jZehlbluFK 5pMYGbQCJMlHX3kBf1DRaY+svIAHQ4CQjx7n1g5RimsZr98L8+FWVrENHX6eJCHrYC6B pqOL/8Nb2ovZVcQeIYjHdc5NJKddwq6EtLT7DH23bEes4fUwN8cdpNtrZdk3TnSjIX3U k5cg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:cc:references:from:in-reply-to; bh=B1yLKHldtD7jPXtQp4Onon87W9jO78QmaaMHOU3CHtI=; b=dWSG+wtkxKV9VPRtGVfcAQSmXa4a35nvnofiBhiZcpprkri+nbY52mo7Dw9UmmhUj6 oG1e1VUsfEUSEkc1AO3ufnGtYYoFVsGUez4t4g1eH9SQLrQ1qLg7MKSA2vfePcGfY1wD 3y90sG9RgsURtFoy6+s5S03joP3KUNH6vaXIFyvFP8Hkoq9c8qBy/r/RAJC0KSczBwkF Pg/6qZzuAarlU73LS0Q8/EZkyiWVVKqPSIHu989MF+0cFKL/WPn6cqPRnFJ7JIdxPCy3 kP0BBg6MtywLjPsh1nZveo8p89N+N3TZprZ9MBEypJGNyN+he+HFLDcT20HiFMrOeDzr 5l6g== X-Gm-Message-State: AJIora8I7xHR3R0I/Mc2w/og6msqszBu126Wl4hUWsEbtBcEh9p+tcuv ogO/wF+Oi6Kywdv3v57xGKU= X-Google-Smtp-Source: AGRyM1uBkDELjKphAhzpQZ33P/MMeQQkpmhtsEZRh92WFww/x8JFiOWpS6Zji3NjaE53QCOCQkc3ew== X-Received: by 2002:a05:620a:3708:b0:6af:6c72:10ec with SMTP id de8-20020a05620a370800b006af6c7210ecmr9389363qkb.644.1657497744093; Sun, 10 Jul 2022 17:02:24 -0700 (PDT) Received: from [192.168.2.100] (pool-72-79-19-93.nwrknj.east.verizon.net. [72.79.19.93]) by smtp.gmail.com with ESMTPSA id bq11-20020a05620a468b00b006af6f0893c6sm4678767qkb.91.2022.07.10.17.02.23 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 10 Jul 2022 17:02:23 -0700 (PDT) Content-Type: multipart/alternative; boundary="------------6RoPu8rEIccx49K6OnTnZzTl" Message-ID: <95625425-a4b1-ee75-0053-389d51b31458@gmail.com> Date: Sun, 10 Jul 2022 20:02:22 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.11.0 Subject: Re: Get the table creation DDL Content-Language: en-US To: Igor Korot Cc: "pgsql-generallists.postgresql.org" References: From: Mladen Gogala In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------6RoPu8rEIccx49K6OnTnZzTl Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 7/10/22 17:00, Igor Korot wrote: > I understand. > The Problem is that I need to put this inside the C/ODBC interface for > my project. > > I'm sure it is not a problem when people are working out of psql or > writing some scripts, > but for me it is painful to go and try to recreate it. > > Now, I'm not sure if this extension can be freely re-used (query > extracted and placed > inside someone else's project). > > Thank you. Igor, https://github.com/MichaelDBA/pg_get_tabledef provides "pg_get_tabledef" function which can be called from SQL and therefore used from ODBC/C. This "extension" is nothing PL/PGSQL source code of the function that returns DDL. That's about it. This is how it works: mgogala@umajor Downloads]$ psql -h postgres -f pg_get_tabledef-main/pg_get_tabledef.sql Password for user mgogala: DO CREATE FUNCTION [mgogala@umajor Downloads]$ psql -h postgres Password for user mgogala: psql (13.6, server 14.4) WARNING: psql major version 13, server major version 14.          Some psql features might not work. Type "help" for help. mgogala=# select pg_get_tabledef('mgogala','emp'); pg_get_tabledef ---------------------------------------------------------------------  CREATE  TABLE mgogala.emp (                                        +    empno smallint NOT NULL,                                         +    ename character varying(10) NULL,                                +    job character varying(9) NULL,                                   +    mgr smallint NULL,                                               +    hiredate timestamp without time zone NULL,                       +    sal double precision NULL,                                       +    comm double precision NULL,                                      +    deptno smallint NULL,                                            +    CONSTRAINT emp_pkey PRIMARY KEY (empno),                         +    CONSTRAINT fk_deptno FOREIGN KEY (deptno) REFERENCES dept(deptno)+  ) TABLESPACE pg_default;                                           +                                                                     + (1 row) So, you clone the Git repository, run the "CREATE FUNCTION" script and, voila, you can get the DDL for the desired table. Here is the same stuff produced by the psql utility: mgogala=# \d emp                            Table "mgogala.emp"   Column  |            Type             | Collation | Nullable | Default ----------+-----------------------------+-----------+----------+---------  empno    | smallint                    |           | not null |  ename    | character varying(10)       |           |          |  job      | character varying(9)        |           |          |  mgr      | smallint                    |           |          |  hiredate | timestamp without time zone |           |          |  sal      | double precision            |           |          |  comm     | double precision            |           |          |  deptno   | smallint                    |           |          | Indexes:     "emp_pkey" PRIMARY KEY, btree (empno) Foreign-key constraints:     "fk_deptno" FOREIGN KEY (deptno) REFERENCES dept(deptno) And here is using the function from an ODBC connection: |[mgogala@umajor Downloads]$ isql mgogala-pg +---------------------------------------+ | Connected! | | | | sql-statement | | help [tablename] | | quit | | | +---------------------------------------+ SQL> select pg_get_tabledef('mgogala','emp'); +-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | pg_get_tabledef | +-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | CREATE TABLE mgogala.emp ( empno smallint NOT NULL, ename character varying(10) NULL, job character varying(9) NULL, mgr smallint NULL, hiredate timestamp without time zone NULL, sal double precision NULL, comm double precision NULL, deptno smallint NULL, CONSTRAINT emp_pkey PR...| +-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ SQLRowCount returns 1 1 rows fetched SQL> ||| |The function description looks like this:| |mgogala=# \df pg_get_tabledef                                 List of functions  Schema |      Name       | Result data type |                                                    Argument data types | Typ e --------+-----------------+------------------+---------------------------------- -------------------------------------------------------------------------------- ---------------------------------------------------------------------------+---- --  public | pg_get_tabledef | text             | in_schema character varying, in_t able character varying, in_fktype tabledef_fkeys DEFAULT 'FKEYS_INTERNAL'::table def_fkeys, in_trigger tabledef_trigs DEFAULT 'NO_TRIGGERS'::tabledef_trigs | fun c (1 row) | |As expected, the function returns the "text" data type. | |Regards | || || -- Mladen Gogala Database Consultant Tel: (347) 321-1217 https://dbwhisperer.wordpress.com --------------6RoPu8rEIccx49K6OnTnZzTl Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit
On 7/10/22 17:00, Igor Korot wrote:
I understand.
The Problem is that I need to put this inside the C/ODBC interface for
my project.

I'm sure it is not a problem when people are working out of psql or
writing some scripts,
but for me it is painful to go and try to recreate it.

Now, I'm not sure if this extension can be freely re-used (query
extracted and placed
inside someone else's project).

Thank you.

Igor, https://github.com/MichaelDBA/pg_get_tabledef provides "pg_get_tabledef" function which can be called from SQL and therefore used from ODBC/C. This "extension" is nothing PL/PGSQL source code of the function that returns DDL. That's about it. This is how it works:

mgogala@umajor Downloads]$ psql -h postgres -f pg_get_tabledef-main/pg_get_tabledef.sql
Password for user mgogala:
DO
CREATE FUNCTION
[mgogala@umajor Downloads]$ psql -h postgres
Password for user mgogala:
psql (13.6, server 14.4)
WARNING: psql major version 13, server major version 14.
         Some psql features might not work.
Type "help" for help.

mgogala=# select pg_get_tabledef('mgogala','emp');
                           pg_get_tabledef                           
---------------------------------------------------------------------
 CREATE  TABLE mgogala.emp (                                        +
   empno smallint NOT NULL,                                         +
   ename character varying(10) NULL,                                +
   job character varying(9) NULL,                                   +
   mgr smallint NULL,                                               +
   hiredate timestamp without time zone NULL,                       +
   sal double precision NULL,                                       +
   comm double precision NULL,                                      +
   deptno smallint NULL,                                            +
   CONSTRAINT emp_pkey PRIMARY KEY (empno),                         +
   CONSTRAINT fk_deptno FOREIGN KEY (deptno) REFERENCES dept(deptno)+
 ) TABLESPACE pg_default;                                           +
                                                                    +
 
(1 row)

So, you clone the Git repository, run the "CREATE FUNCTION" script and, voila, you can get the DDL for the desired table. Here is the same stuff produced by the psql utility:

mgogala=# \d emp
                           Table "mgogala.emp"
  Column  |            Type             | Collation | Nullable | Default
----------+-----------------------------+-----------+----------+---------
 empno    | smallint                    |           | not null |
 ename    | character varying(10)       |           |          |
 job      | character varying(9)        |           |          |
 mgr      | smallint                    |           |          |
 hiredate | timestamp without time zone |           |          |
 sal      | double precision            |           |          |
 comm     | double precision            |           |          |
 deptno   | smallint                    |           |          |
Indexes:
    "emp_pkey" PRIMARY KEY, btree (empno)
Foreign-key constraints:
    "fk_deptno" FOREIGN KEY (deptno) REFERENCES dept(deptno)

And here is using the function from an ODBC connection:

[mgogala@umajor Downloads]$ isql mgogala-pg
+---------------------------------------+
| Connected!                            |
|                                       |
| sql-statement                         |
| help [tablename]                      |
| quit                                  |
|                                       |
+---------------------------------------+
SQL> select pg_get_tabledef('mgogala','emp');
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| pg_get_tabledef                                                                                                                                                                                                                                                                                             |
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| CREATE  TABLE mgogala.emp (
  empno smallint NOT NULL,
  ename character varying(10) NULL,
  job character varying(9) NULL,
  mgr smallint NULL,
  hiredate timestamp without time zone NULL,
  sal double precision NULL,
  comm double precision NULL,
  deptno smallint NULL,
  CONSTRAINT emp_pkey PR...|
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
SQLRowCount returns 1
1 rows fetched
SQL> 


The function description looks like this:

mgogala=# \df pg_get_tabledef
                                                                                
                                List of functions
 Schema |      Name       | Result data type |                                  
                                                   Argument data types          
                                                                           | Typ
e
--------+-----------------+------------------+----------------------------------
--------------------------------------------------------------------------------
---------------------------------------------------------------------------+----
--
 public | pg_get_tabledef | text             | in_schema character varying, in_t
able character varying, in_fktype tabledef_fkeys DEFAULT 'FKEYS_INTERNAL'::table
def_fkeys, in_trigger tabledef_trigs DEFAULT 'NO_TRIGGERS'::tabledef_trigs | fun
c
(1 row)

As expected, the function returns the "text" data type.

Regards

-- 
Mladen Gogala
Database Consultant
Tel: (347) 321-1217
https://dbwhisperer.wordpress.com
--------------6RoPu8rEIccx49K6OnTnZzTl--