Skip to content

Commit d45843e

Browse files
committed
COPY FROM deparse more complete, PG_SHARDMAN macro.
Now column names, FORCE NULL and FORCE NOT NULL are deparsed too. PG_SHARDMAN macro ensures this PG contains patches for Postgres.
1 parent 09aaf41 commit d45843e

File tree

5 files changed

+91
-16
lines changed

5 files changed

+91
-16
lines changed

contrib/postgres_fdw/deparse.c

Lines changed: 64 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3176,33 +3176,89 @@ get_relation_column_alias_ids(Var *node, RelOptInfo *foreignrel,
31763176
}
31773177

31783178
/*
3179-
* Deparse COPY FROM
3179+
* Deparse COPY FROM into given buf.
31803180
*/
31813181
void
31823182
deparseCopyFromSql(StringInfo buf, Relation rel, CopyState cstate,
31833183
const char *dest_relname)
31843184
{
3185+
ListCell *cur;
3186+
31853187
appendStringInfoString(buf, "COPY ");
31863188
if (dest_relname == NULL)
31873189
deparseRelation(buf, rel);
31883190
else
31893191
appendStringInfoString(buf, dest_relname);
3190-
appendStringInfoString(buf, " FROM STDIN WITH (");
31913192

3192-
/* TODO: deparse column names */
31933193
if (cstate->binary)
31943194
{
3195-
ereport(ERROR,
3196-
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
3197-
errmsg("cannot copy to postgres_fdw table \"%s\" in binary format ",
3198-
RelationGetRelationName(rel))));
3195+
ereport(ERROR,
3196+
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
3197+
errmsg("cannot copy to postgres_fdw table \"%s\" in binary format ",
3198+
RelationGetRelationName(rel))));
3199+
}
3200+
3201+
/* deparse column names */
3202+
if (cstate->attnumlist != NIL)
3203+
{
3204+
bool first = true;
3205+
3206+
appendStringInfoString(buf, " (");
3207+
foreach(cur, cstate->attnumlist)
3208+
{
3209+
int attnum = lfirst_int(cur);
3210+
char *attname;
3211+
3212+
if (!first)
3213+
appendStringInfoString(buf, ", ");
3214+
first = false;
3215+
3216+
attname = get_relid_attribute_name(rel->rd_id, attnum);
3217+
appendStringInfoString(buf, quote_identifier(attname));
3218+
}
3219+
appendStringInfoString(buf, " )");
31993220
}
3221+
3222+
appendStringInfoString(buf, " FROM STDIN WITH (");
32003223
if (cstate->csv_mode)
32013224
{
32023225
appendStringInfoString(buf, " FORMAT csv ");
32033226
appendStringInfo(buf, ", QUOTE '%c'", *(cstate->quote));
32043227
appendStringInfo(buf, ", ESCAPE '%c'", *(cstate->escape));
3205-
/* TODO: force quote, force not null, force null */
3228+
if (cstate->force_notnull != NIL)
3229+
{
3230+
bool first = true;
3231+
3232+
appendStringInfoString(buf, " FORCE NOT NULL (");
3233+
foreach(cur, cstate->force_notnull)
3234+
{
3235+
char *attname = strVal(lfirst(cur));
3236+
3237+
if (!first)
3238+
appendStringInfoString(buf, ", ");
3239+
first = false;
3240+
3241+
appendStringInfoString(buf, quote_identifier(attname));
3242+
}
3243+
appendStringInfoString(buf, " )");
3244+
}
3245+
if (cstate->force_null != NIL)
3246+
{
3247+
bool first = true;
3248+
3249+
appendStringInfoString(buf, " FORCE NULL (");
3250+
foreach(cur, cstate->force_null)
3251+
{
3252+
char *attname = strVal(lfirst(cur));
3253+
3254+
if (!first)
3255+
appendStringInfoString(buf, ", ");
3256+
first = false;
3257+
3258+
appendStringInfoString(buf, quote_identifier(attname));
3259+
}
3260+
appendStringInfoString(buf, " )");
3261+
}
32063262
}
32073263
else
32083264
{

contrib/postgres_fdw/postgres_fdw.c

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ static void postgresGetForeignUpperPaths(PlannerInfo *root,
360360
static void postgresBeginForeignCopyFrom(EState *estate,
361361
ResultRelInfo *rinfo,
362362
CopyState cstate,
363-
const char *dest_relname);
363+
ResultRelInfo *parent_rinfo);
364364
static void postgresForeignNextCopyFrom(EState *estate,
365365
ResultRelInfo *rinfo,
366366
CopyState cstate);
@@ -5219,20 +5219,28 @@ postgres_fdw_exec(PG_FUNCTION_ARGS)
52195219
PG_RETURN_VOID();
52205220
}
52215221

5222-
/* Begin COPY FROM to foreign table */
5222+
/*
5223+
* Begin COPY FROM to foreign table. Currently we do it in a bit perverted
5224+
* way: we redirect COPY FROM to parent table on foreign server, assuming it
5225+
* exists in public schema (as in shardman), and let it direct tuples to
5226+
* proper partitions. Otherwise we would have to modify logic of managing
5227+
* connections and keep many connections open to one server from one backend.
5228+
* This probably should not be used outside pg_shardman.
5229+
*/
52235230
static void
52245231
postgresBeginForeignCopyFrom(EState *estate, ResultRelInfo *rinfo,
5225-
CopyState cstate, const char *dest_relname)
5232+
CopyState cstate, ResultRelInfo *parent_rinfo)
52265233
{
52275234
Relation rel = rinfo->ri_RelationDesc;
52285235
RangeTblEntry *rte;
52295236
Oid userid;
52305237
ForeignTable *table;
52315238
UserMapping *user;
52325239
StringInfoData sql;
5233-
PGconn *conn;
5234-
PGresult *res;
5235-
bool *copy_from_started;
5240+
PGconn *conn;
5241+
PGresult *res;
5242+
bool *copy_from_started;
5243+
char *dest_relname;
52365244

52375245
/*
52385246
* Identify which user to do the remote access as. This should match what
@@ -5253,6 +5261,11 @@ postgresBeginForeignCopyFrom(EState *estate, ResultRelInfo *rinfo,
52535261
return;
52545262

52555263
/* deparse COPY stmt */
5264+
dest_relname = psprintf(
5265+
"public.%s", quote_identifier(RelationGetRelationName(
5266+
parent_rinfo == NULL ?
5267+
rinfo->ri_RelationDesc :
5268+
parent_rinfo->ri_RelationDesc)));
52565269
initStringInfo(&sql);
52575270
deparseCopyFromSql(&sql, rel, cstate, dest_relname);
52585271

@@ -5327,4 +5340,4 @@ _PG_init(void)
53275340
"Use repeatable read isilation error for remote transactions", NULL,
53285341
&UseRepeatableRead, true, PGC_USERSET, 0, NULL,
53295342
NULL, NULL);
5330-
}
5343+
}

src/include/foreign/fdwapi.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ typedef bool (*IsForeignScanParallelSafe_function) (PlannerInfo *root,
163163
typedef void (*BeginForeignCopyFrom_function) (EState *estate,
164164
ResultRelInfo *rinfo,
165165
CopyState cstate,
166-
const char *dest_relname);
166+
ResultRelInfo *parent_rinfo);
167167
/*
168168
* Currently we support only text and csv format and pass each row in
169169
* cstate->line_buf. We should also pass binary data and/or deformed tuple.

src/include/pg_config.h.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,9 @@
753753
/* PostgreSQL version as a number */
754754
#undef PG_VERSION_NUM
755755

756+
/* This version contains patches for pg_shardman */
757+
#define PG_SHARDMAN
758+
756759
/* A string containing the version number, platform, and C compiler */
757760
#undef PG_VERSION_STR
758761

src/include/pg_config.h.win32

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,9 @@
565565
/* PostgreSQL version as a number */
566566
#define PG_VERSION_NUM 100000
567567

568+
/* This version contains patches for pg_shardman */
569+
#define PG_SHARDMAN
570+
568571
/* Define to the one symbol short name of this package. */
569572
#define PACKAGE_TARNAME "postgresql"
570573

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy